480 likes | 744 Views
The Graph Abstract Data Type. CS 5050 Chapter 6. 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
E N D
The Graph Abstract Data Type CS 5050 Chapter 6
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
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
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
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
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
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
Biconnectivity SEA PVD ORD FCO SNA MIA
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
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 Separation Edges and Vertices ORD PVD SFO LGA HNL LAX DFW MIA
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 PVD ORD SFO LGA HNL LAX DFW MIA
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 ORD PVD SFO LGA RDU HNL LAX DFW MIA
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 SS • 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 relationC = {(v,w) VV 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
i g e b a d j f c 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 Equivalence classes of linked edges: {a} {b, c, d, e, f} {g, i, j} i g e b a d j f c
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 Link Components ORD PVD SFO LGA RDU HNL LAX DFW MIA
Auxiliary Graph h g i e b • 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,f1), (e,f2) , …, (e,fk),where f1, f2, …, fk are the discovery edges of G that form a simple cycle with e • Its connected components correspond to the the link components of G i j d c f a DFS on graph G g e i h b f j d c a Auxiliary graph B
Auxiliary Graph (cont.) • In the worst case, the number of edges of the auxiliary graph is proportional to nm DFS on graph G Auxiliary graph B
Proxy Graph h g AlgorithmproxyGraph(G) Inputconnectedgraph GOutputproxy graph F for GFempty 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 dEdgediscovery edge with dest. u F.insertEdge(bEdge,dEdge,) if getLabel(dEdge) =UNLINKED setLabel(dEdge, LINKED) uorigin of dEdge else uv{ ends the loop } until u=v returnF i e b i j d c f a DFS on graph G g e i h b f j d c a Proxy graph F
Proxy Graph (cont.) h g i e b • 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 i j d c f a DFS on graph G g e i h b f j d c a Proxy graph F
Breadth-First Search • By levels, typically using queues
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
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
E D C B A Digraphs • A digraph is a graph whose edges are all directed • Short for “directed graph” • Applications • one-way streets • flights • task scheduling
E D C B A 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.
Digraph Application • Scheduling: edge (a,b) means task a must be completed before b can be started ics21 ics22 ics23 ics51 ics53 ics52 ics161 ics131 ics141 ics121 ics171 The good life ics151
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
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 E D C B A
Reachability • DFS tree rooted at v: vertices reachable from v via directed paths E D E D C A C F E D A B C F A B
a g c d e b f Strong Connectivity • Each vertex can reach all other vertices
Strong Connectivity Algorithm a • 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). G: g c d e b f a g G’: c d e b f
a g c d e b f Strongly Connected Components • 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). { a , c , g } { f , d , e , b }
Transitive Closure D E • 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 G C A D E B C A G*
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. Computing the Transitive Closure • We can perform DFS starting at each vertex • O(n(n+m)) Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm
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: Uses only vertices numbered 1,…,k (add this edge if it’s not already in) i j Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 k
Floyd-Warshall’s Algorithm AlgorithmFloydWarshall(G) Inputdigraph G (vertices numbered) Outputtransitive closure G* of G G0G for k 1 to n do GkGk -1 for i 1 to n (i k)do for j 1 to n (j i, k)do if Gk -1.areAdjacent(vi vk) Gk -1.areAdjacent(vk vj) if Gk.areAdjacent(vi vj) Gk.insertDirectedEdge(vi vj) return Gn • Floyd-Warshall’s algorithm numbers the vertices of G as v1 , …, vn and computes a series of digraphs G0, …, Gn • G0=G • Gkhas directed edge (vi, vj) if G has directed path from vi to vjwith intermediate vertices in the set {v1 , …, vk} • We have that Gn = G* • In phase k, digraph Gk is computed from Gk -1 • Running time: O(n3), assuming areAdjacent is O(1) (e.g., adjacency matrix)
Floyd-Warshall Example BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
BOS Floyd-Warshall, Conclusion v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
DAGs and Topological Ordering D E • A directed acyclic graph (DAG) is a digraph that has no directed cycles • A topological ordering of a digraph is a numbering v1 , …, vn of the vertices such that for every edge (vi , vj), 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 C A DAG G v4 v5 D E v2 B v3 C v1 Topological ordering of G A
Topological Sorting • Number vertices, so that (u,v) in E implies u < v 1 A typical student day wake up 3 2 eat study computer sci. 5 4 nap more c.s. 7 play 8 write c.s. program 6 9 work out make cookies for professors 10 11 sleep dream about graphs
Algorithm for Topological Sorting • Note: This algorithm is different than the one in Goodrich-Tamassia • Running time: O(n + m). How…? MethodTopologicalSort(G) H G // Temporary copy of G n G.numVertices() whileH is not emptydo Let v be a vertex with no outgoing edges Label v n n n - 1 Remove v from H
Topological Sorting Algorithm using DFS AlgorithmtopologicalDFS(G, v) Inputgraph G and a start vertex v of G Outputlabeling of the vertices of G in the connected component of v setLabel(v, VISITED) for all edgeE G.incidentEdges(v) ifgetLabel(edgeE) = UNEXPLORED w opposite(v,edgeE) if getLabel(w) = UNEXPLORED setLabel(edgeE, DISCOVERY) topologicalDFS(G, w) else {e is a forward or cross edge} Label v with topological number n n n - 1 • Simulate the algorithm by using depth-first search • O(n+m) time. AlgorithmtopologicalDFS(G) Inputdag G Outputtopological ordering of Gn G.numVertices() for all u G.vertices() setLabel(u, UNEXPLORED) for all e G.edges() setLabel(e, UNEXPLORED) for all v G.vertices() ifgetLabel(v) = UNEXPLORED topologicalDFS(G, v)
Topological Sorting Example 2 3 4 6 5 7 8 9
Topological Sorting Example 2 1 3 4 6 5 7 8 9