Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 The Graph Abstract Data Type CS 5050 Chapter 6.

Similar presentations


Presentation on theme: "1 The Graph Abstract Data Type CS 5050 Chapter 6."— Presentation transcript:

1 1 The Graph Abstract Data Type CS 5050 Chapter 6

2 2 Terminology Vertex (vertices) – position, holds an object Edge – also a position, holds an object Directed, undirected ((a)symmetric) Graph is a set of vertices and a bag of edges (can be duplicated) –Parallel and self-edges –Directed, undirected, mixed (both directed and undirected edges)– convert to digraph –End vertices/endpoints (of an edge), origin/destination, adjacent vertices (endpoints of same edge) –Incident edge, incoming, outgoing, in (out)-degree

3 3 Terminology continued Simple graphs have no parallel (multiple edges between same vertices) or self loop Path – sequence of alternating vertices and edges which match up Cycle - path with same first and last vertex Simple and directed paths and cycles (Spanning – contains all vertices) subgraph Connected graph, connected component Forest (acyclic), free (no root) trees (connected forest), spanning tree

4 4 Graph Facts Assume the graph G has n vertices (V) and m edges (E) –The sum over V of degrees is 2m –If G is directed, the sum over V of in-degrees = the sum over V of out-degrees = m –If G is simple and undirected, m <= n(n-1)/2 (worst case is every pair) –If G is simple and directed, m <= n(n-1) –So m is O( |V| 2 ) –If G is connected, m >= n-1, if a tree m = n-1 –If G is a forest, m <= n-1

5 5 Adjacency List -partitioned incidence container (in/out)

6 6 Adjacency Matrix

7 7 Traversals Visit each node – e.g., web crawler Have to restart traversal in each connected component, but this allows us to identify components Reachability in a digraph is an important issue – the transitive closure graph Book permits counter-direction motion in general traversals

8 8 Looking for a spanning Tree Many times algorithms depend on some ordering of the nodes of a graph Labeling the edges is some way is helpful in organizing thinking about a graph

9 9 Depth-First Search (undirected graph) Simple recursive backtracking algorithm calls recursive function at starting point –For each incident edge to a vertex If opposite (other) vertex is unvisited –Label edge as “discovery” –Recur on the opposite vertex Else label edge as “back” Discovery edges form a component spanning tree, back edges go to an ancestor with m edges, O(m) using an adjacency list, but not using an adjacency matrix

10 10 Depth-First Traversal

11 11 Biconnectivity SEAPVD MIA SNA ORD FCO

12 12 Outline and Reading Definitions (§6.3.2) –Separation vertices and edges –Biconnected graph –Biconnected components –Equivalence classes –Linked edges and link components Algorithms (§6.3.2) –Auxiliary graph –Proxy graph

13 13 Separation Edges and Vertices Definitions –Let G be a connected graph –A separation edge of G is an edge whose removal disconnects G –A separation vertex of G is a vertex whose removal disconnects G Applications –Separation edges and vertices represent single points of failure in a network and are critical to the operation of the network Example –DFW, LGA and LAX are separation vertices –(DFW,LAX) is a separation edge ORDPVD MIA DFW SFO LAX LGA HNL

14 14 Biconnected Graph Equivalent definitions of a biconnected graph G –Graph G has no separation edges and no separation vertices –For any two vertices u and v of G, there are two disjoint simple paths between u and v (i.e., two simple paths between u and v that share no other vertices or edges) –For any two vertices u and v of G, there is a simple cycle containing u and v Example ORD PVD MIA DFW SFO LAX LGA HNL

15 15 Biconnected Components Biconnected component of a graph G –A maximal biconnected subgraph of G, or –A subgraph consisting of a separation edge of G and its end vertices Interaction of biconnected components –An edge belongs to exactly one biconnected component –A nonseparation vertex belongs to exactly one biconnected component –A separation vertex belongs to two or more biconnected components Example of a graph with four biconnected components ORDPVD MIA DFW SFO LAX LGA HNL RDU

16 16 Equivalence Classes Given a set S, a relation R on S is a set of ordered pairs of elements of S, i.e., R is a subset of S  S An equivalence relation R on S satisfies the following properties Reflexive: (x,x)  R Symmetric: (x,y)  R  (y,x)  R Transitive: (x,y)  R  (y,z)  R  (x,z)  R An equivalence relation R on S induces a partition of the elements of S into equivalence classes Example (connectivity relation among the vertices of a graph): –Let V be the set of vertices of a graph G –Define the relation C = {(v,w)  V  V such that G has a path from v to w} –Relation C is an equivalence relation –The equivalence classes of relation C are the vertices in each connected component of graph G

17 17 Link Relation Edges e and f of connected graph G are linked if –e  f, or –G has a simple cycle containing e and f Theorem: The link relation on the edges of a graph is an equivalence relation Proof Sketch: –The reflexive and symmetric properties follow from the definition –For the transitive property, consider two simple cycles sharing an edge a b g c j d e f i Equivalence classes of linked edges: {a} {b, c, d, e, f} {g, i, j} a b g c j d e f i

18 18 Link Components The link components of a connected graph G are the equivalence classes of edges with respect to the link relation A biconnected component of G is the subgraph of G induced by an equivalence class of linked edges A separation edge is a single-element equivalence class of linked edges A separation vertex has incident edges in at least two distinct equivalence classes of linked edge ORDPVD MIA DFW SFO LAX LGA HNL RDU

19 19 Auxiliary Graph Auxiliary graph B for a connected graph G –Associated with a DFS traversal of G –The vertices of B are the edges of G –For each back edge e of G, B has edges (e,f 1 ), (e,f 2 ), …, (e,f k ), where f 1, f 2, …, f k are the discovery edges of G that form a simple cycle with e –Its connected components correspond to the the link components of G a b g c j d e f i Auxiliary graph B DFS on graph G a d b c e h i j f h g i

20 20 Auxiliary Graph (cont.) In the worst case, the number of edges of the auxiliary graph is proportional to nm Auxiliary graph B DFS on graph G

21 21 Proxy Graph Algorithm proxyGraph(G) Input connected graph G Output proxy graph F for G F  empty graph DFS(G, s) { s is any vertex of G} for all discovery edges dEdge of G F.insertVertex(dEdge) setLabel(dEdge, UNLINKED) for all vertices v of G in DFS visit order for all back edges bEdge  (u,v) F.insertVertex(bEdge) repeat dEdge  discovery edge with dest. u F.insertEdge(bEdge,dEdge,  ) if getLabel(dEdge)  UNLINKED setLabel(dEdge, LINKED) u  origin of dEdge else u  v { ends the loop } until u  v return F a b g c j d e f i Proxy graph F DFS on graph G a d b c e h i j f h g i

22 22 Proxy Graph (cont.) Proxy graph F for a connected graph G –Spanning forest of the auxiliary graph B –Has m vertices and O(m) edges –Can be constructed in O(n  m) time –Its connected components (trees) correspond to the the link components of G Given a graph G with n vertices and m edges, we can compute the following in O(n  m) time –The biconnected components of G –The separation vertices of G –The separation edges of G a b g c j d e f i Proxy graph F DFS on graph G a d b c e h i j f h g i

23 23 Breadth-First Search By levels, typically using queues

24 24 BFS Facts There are discovery and cross edges (why no back edges?) – Once marked, don’t follow again. Discovery edges form spanning tree Tree edges are paths, minimal in length Cross edges differ by at most one in level Try writing the code to do a BFS

25 25 Thm 6.19: Algorithms based on BFS Test for connectivity compute spanning forest compute connected components find shortest path between two points (in number of links) compute a cycle in graph, or report none (have cross edges) Good for shortest path information, while DFS better for complex connectivity questions

26 26 Digraphs A digraph is a graph whose edges are all directed –Short for “directed graph” Applications –one-way streets –flights –task scheduling A C E B D

27 27 Digraph Properties A graph G=(V,E) such that –Each edge goes in one direction: Edge (a,b) goes from a to b, but not b to a. If G is simple, m < n*(n-1). If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of in-edges and out-edges in time proportional to their size. A C E B D

28 28 Digraph Application Scheduling: edge (a,b) means task a must be completed before b can be started The good life ics141 ics131 ics121 ics53 ics52 ics51 ics23ics22ics21 ics161 ics151 ics171

29 29 Digraph Facts Directed DFS gives directed paths from root to each reachable vertex Used for O(n(n+m)) algorithm [dfs is O(n+m), these algorithms use n dfs searches] –Find all induced subgraphs (from each vertex, v, find subgraph reachable from v) –Test for strong connectivity –Compute the transitive closure Directed BFS has discovery, back, cross edges

30 30 Directed DFS We can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their direction In the directed DFS algorithm, we have four types of edges –discovery edges –back edges –forward edges –cross edges A directed DFS starting a a vertex s determines the vertices reachable from s A C E B D

31 31 Reachability DFS tree rooted at v: vertices reachable from v via directed paths A C E B D F A C ED A C E B D F

32 32 Strong Connectivity Each vertex can reach all other vertices a d c b e f g

33 33 Pick a vertex v in G. Perform a DFS from v in G. –If there’s a w not visited, print “no”. Let G’ be G with edges reversed. Perform a DFS from v in G’. –If there’s a w not visited, print “no”. –Else, print “yes”. Running time: O(n+m). Strong Connectivity Algorithm G: G’: a d c b e f g a d c b e f g

34 34 Maximal subgraphs such that each vertex can reach all other vertices in the subgraph Can also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity). Strongly Connected Components { a, c, g } { f, d, e, b } a d c b e f g

35 35 Transitive Closure Given a digraph G, the transitive closure of G is the digraph G* such that –G* has the same vertices as G –if G has a directed path from u to v (u  v), G* has a directed edge from u to v The transitive closure provides reachability information about a digraph B A D C E B A D C E G G*

36 36 Computing the Transitive Closure We can perform DFS starting at each vertex –O(n(n+m)) If there's a way to get from A to B and from B to C, then there's a way to get from A to C. Alternatively... Use dynamic programming: The Floyd-Warshall Algorithm

37 37 Floyd-Warshall Transitive Closure Idea #1: Number the vertices 1, 2, …, n. Idea #2: Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices: k j i Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k (add this edge if it’s not already in)

38 38 Floyd-Warshall’s Algorithm Floyd-Warshall’s algorithm numbers the vertices of G as v 1, …, v n and computes a series of digraphs G 0, …, G n –G 0 =G –G k has directed edge (v i, v j ) if G has directed path from v i to v j with intermediate vertices in the set {v 1, …, v k } We have that G n = G* In phase k, digraph G k is computed from G k  1 Running time: O(n 3 ), assuming areAdjacent is O(1) (e.g., adjacency matrix) Algorithm FloydWarshall(G) Input digraph G (vertices numbered) Output transitive closure G* of G G 0  G for k  1 to n do G k  G k  1 for i  1 to n (i  k) do for j  1 to n (j  i, k) do if G k  1.areAdjacent(v i v k )  G k  1.areAdjacent(v k v j ) if  G k.areAdjacent(v i v j ) G k.insertDirectedEdge(v i v j ) return G n

39 39 Floyd-Warshall Example JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

40 40 1234567 1y 2 3yyy 4y 5yy 6yy 7yy

41 41 Floyd-Warshall, Conclusion JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS

42 42 DAGs and Topological Ordering A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering v 1, …, v n of the vertices such that for every edge (v i, v j ), we have i  j Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints Theorem A digraph admits a topological ordering if and only if it is a DAG B A D C E DAG G B A D C E Topological ordering of G v1v1 v2v2 v3v3 v4v4 v5v5

43 43 write c.s. program play Topological Sorting Number vertices, so that (u,v) in E implies u < v wake up eat nap study computer sci. more c.s. work out sleep dream about graphs A typical student day 1 2 3 4 5 6 7 8 9 10 11 make cookies for professors

44 44 Note: This algorithm is different than the one in Goodrich-Tamassia Running time: O(n + m). How…? Algorithm for Topological Sorting Method TopologicalSort(G) H  G// Temporary copy of G n  G.numVertices() while H is not empty do Let v be a vertex with no outgoing edges Label v  n n  n - 1 Remove v from H

45 45 Topological Sorting DFS Algorithm Simulate the algorithm by using depth-first search O(n+m) time. Algorithm topologicalDFS(G, v) Input graph G and a start vertex v of G (having no input arcs from unvisited nodes) Output labeling of the vertices of G in the connected component of v setLabel(v,VISITED) for all edges (v,w) if getLabel(w)  !VISITED topologicalDFS(G, w) Label v with topological number n n  n - 1 Algorithm topologicalDFS(G) Input dag G Output topological ordering of G n  G.numVertices() for all u  G.vertices() setLabel(u, !VISITED) for all v  G.vertices() which have no incoming edges. if getLabel(v)  VISITED topologicalDFS(G, v)

46 46 Topological Sorting Example a f c g e d b h

47 47 Topological Sorting Example 2 7 4 8 5 6 3 9

48 48 Topological Sorting Example 2 7 4 8 5 6 1 3 9


Download ppt "1 The Graph Abstract Data Type CS 5050 Chapter 6."

Similar presentations


Ads by Google