Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.

Slides:



Advertisements
Similar presentations
CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort.
Advertisements

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Chapter 9: Graphs Topological Sort
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Connected Components, Directed Graphs, Topological Sort COMP171.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
COMP171 Depth-First Search.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Intro to Graphs CSIT 402 Data Structures II. CSIT 402 Graph Introduction2 Graphs Graphs are composed of ›Nodes (vertices) Can be labeled ›Edges (arcs)
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 13 Graphs. Introduction to Graphs Examples of Graphs – Airline Route Map What is the fastest way to get from Pittsburgh to St Louis? What is the.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Data Structures Week 9 Introduction to Graphs Consider the following problem. A river with an island and bridges. The problem is to see if there is a way.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Great Theoretical Ideas in Computer Science for Some.
 2004 SDU 1 Lecture5-Strongly Connected Components.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
رياضيات متقطعة لعلوم الحاسب MATH 226. Chapter 10.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Breadth-First Search (BFS)
Graph & BFS.
Graphs Lecture 19 CS2110 – Spring 2013.
GRAPHS Lecture 16 CS2110 Fall 2017.
CSC317 Graph algorithms Why bother?
Depth-First Search.
Modeling and Simulation NETW 707
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Topological Sort CSE 373 Data Structures Lecture 19.
Elementary Graph Algorithms
Connected Components, Directed Graphs, Topological Sort
Richard Anderson Autumn 2016 Lecture 5
CSCI2100 Data Structures Tutorial
Richard Anderson Winter 2009 Lecture 6
GRAPHS Lecture 17 CS2110 Spring 2018.
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Richard Anderson Winter 2019 Lecture 5
Presentation transcript:

Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006

Graph / Slide 2 Graph Application: Connectivity D E A C F B G K H L N M O R Q P s How do we tell if two vertices are connected? A connected to F? A connected to L? G =

Graph / Slide 3 Connectivity * A graph is connected if and only if there exists a path between every pair of distinct vertices. * A graph is connected if and only if there exists a simple path between every pair of distinct vertices n since every non-simple path contains a cycle, which can be bypassed * How to check for connectivity? n Run BFS or DFS (using an arbitrary vertex as the source) n If all vertices have been visited, the graph is connected. n Running time? O(n + m)

Graph / Slide 4 Connected Components

Graph / Slide 5 Subgraphs

Graph / Slide 6 Connected Components * Formal definition n A connected component is a maximal connected subgraph of a graph * The set of connected components is unique for a given graph

Graph / Slide 7 Finding Connected Components For each vertex Call DFS This will find all vertices connected to “v” => one connected component Basic DFS algorithm If not visited

Graph / Slide 8 Time Complexity * Running time for each i connected component * Running time for the graph G * Reason: Can two connected components share n the same edge? n the same vertex?

Graph / Slide 9 Trees * Tree arises in many computer science applications * A graph G is a tree if and only if it is connected and acyclic (Acyclic means it does not contain any simple cycles) * The following statements are equivalent n G is a tree n G is acyclic and has exactly n-1 edges n G is connected and has exactly n-1 edges

Graph / Slide 10 Tree Example * Is it a graph? * Does it contain cycles? In other words, is it acyclic? * How many vertices? * How many edges?

Graph / Slide 11 Directed Graph * A graph is directed if direction is assigned to each edge. * Directed edges are denoted as arcs. n Arc is an ordered pair (u, v) * Recall: for an undirected graph n An edge is denoted {u,v}, which actually corresponds to two arcs (u,v) and (v,u)

Graph / Slide 12 Representations * The adjacency matrix and adjacency list can be used

Graph / Slide 13 Directed Acyclic Graph * A directed path is a sequence of vertices (v 0, v 1,..., v k ) n Such that (v i, v i+1 ) is an arc * A directed cycle is a directed path such that the first and last vertices are the same. * A directed graph is acyclic if it does not contain any directed cycles

Graph / Slide 14 Indegree and Outdegree * Since the edges are directed n We can’t simply talk about Deg(v) * Instead, we need to consider the arcs coming “in” and going “out” n Thus, we define terms Indegree(v), and Outdegree(v) * Each arc(u,v) contributes count 1 to the outdegree of u and the indegree of v

Graph / Slide 15 Calculate Indegree and Outdegree * Outdegree is simple to compute n Scan through list Adj[v] and count the arcs * Indegree caculation n First, initialize indegree[v]=0 for each vertex v n Scan through adj[v] list for each v  For each vertex w seen, indegree[w]++;  Running time: O(n+m)

Graph / Slide 16 Example Indeg(2)? Indeg(8)? Outdeg(0)? Num of Edges? Total OutDeg? Total Indeg?

Graph / Slide 17 Directed Graphs Usage * Directed graphs are often used to represent order- dependent tasks n That is we cannot start a task before another task finishes * We can model this task dependent constraint using arcs * An arc (i,j) means task j cannot start until task i is finished * Clearly, for the system not to hang, the graph must be acyclic i j Task j cannot start until task i is finished

Graph / Slide 18 University Example * CS departments course structure M132M How many indeg(171)? How many outdeg(171)? Any directed cycles?

Graph / Slide 19 Topological Sort * Topological sort is an algorithm for a directed acyclic graph * Linearly order the vertices so that the linear order respects the ordering relations implied by the arcs For example: 0, 1, 2, 5, 9 0, 4, 5, 9 0, 6, 3, 7 ?

Graph / Slide 20 Topological Sort Algorithm * Observations n Starting point must have zero indegree n If it doesn’t exist, the graph would not be acyclic * Algorithm 1. A vertex with zero indegree is a task that can start right away. So we can output it first in the linear order 2. If a vertex i is output, then its outgoing arcs (i, j) are no longer useful, since tasks j does not need to wait for i anymore- so remove all i’s outgoing arcs 3. With vertex i removed, the new graph is still a directed acyclic graph. So, repeat step 1-2 until no vertex is left.

Graph / Slide 21 Topological Sort Find all starting points Reduce indegree(w) Place new start vertices on the Q

Graph / Slide 22 Example Indegree start Q = { 0 } OUTPUT: 0

Graph / Slide Indegree Dequeue 0 Q = { } -> remove 0’s arcs – adjust indegrees of neighbors OUTPUT: 0 Decrement 0’s neighbors

Graph / Slide Indegree Q = { 6, 1, 4 } Enqueue all starting points OUTPUT: 0 Enqueue all new start points

Graph / Slide Indegree Dequeue 6 Q = { 1, 4 } Remove arcs.. Adjust indegrees of neighbors OUTPUT: 0 6 Adjust neighbors indegree

Graph / Slide Indegree Q = { 1, 4, 3 } Enqueue 3 OUTPUT: 0 6 Enqueue new start

Graph / Slide Indegree Dequeue 1 Q = { 4, 3 } Adjust indegrees of neighbors OUTPUT: Adjust neighbors of 1

Graph / Slide Indegree Dequeue 1 Q = { 4, 3, 2 } Enqueue 2 OUTPUT: Enqueue new starting points

Graph / Slide Indegree Dequeue 4 Q = { 3, 2 } Adjust indegrees of neighbors OUTPUT: Adjust 4’s neighbors

Graph / Slide Indegree Dequeue 4 Q = { 3, 2 } No new start points found OUTPUT: NO new start points

Graph / Slide Indegree Dequeue 3 Q = { 2 } Adjust 3’s neighbors OUTPUT:

Graph / Slide Indegree Dequeue 3 Q = { 2 } No new start points found OUTPUT:

Graph / Slide Indegree Dequeue 2 Q = { } Adjust 2’s neighbors OUTPUT:

Graph / Slide Indegree Dequeue 2 Q = { 5, 7 } Enqueue 5, 7 OUTPUT:

Graph / Slide Indegree Dequeue 5 Q = { 7 } Adjust neighbors OUTPUT:

Graph / Slide Indegree Dequeue 5 Q = { 7 } No new starts OUTPUT:

Graph / Slide Indegree Dequeue 7 Q = { } Adjust neighbors OUTPUT:

Graph / Slide Indegree Dequeue 7 Q = { 8 } Enqueue 8 OUTPUT:

Graph / Slide Indegree Dequeue 8 Q = { } Adjust indegrees of neighbors OUTPUT:

Graph / Slide Indegree Dequeue 8 Q = { 9 } Enqueue 9 Dequeue 9 Q = { } STOP – no neighbors OUTPUT:

Graph / Slide 41 OUTPUT: Is output topologically correct?

Graph / Slide 42 Topological Sort: Complexity * We never visited a vertex more than one time * For each vertex, we had to examine all outgoing edges n Σ outdegree(v) = m n This is summed over all vertices, not per vertex * So, our running time is exactly n O(n + m)