Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS138A 1999 1 Elementary Graph Algorithms Peter Schröder.

Similar presentations


Presentation on theme: "CS138A 1999 1 Elementary Graph Algorithms Peter Schröder."— Presentation transcript:

1 CS138A 1999 1 Elementary Graph Algorithms Peter Schröder

2 CS138A 1999 2 Representations For graphs G=(V,E) adjacency list for every vertex v list Adj[v] better for sparse graphs: size of O(max(E,V)) adjacency matrix A: a ij =1 if (v i,v j ) in E single bit entry for each edge in |V|x|V| matrix better for dense graphs: size is O(|V| 2 ) meaning of A 2 ?

3 CS138A 1999 3 Breadth First Search Given a graph G=(V,E) and a distinguished vertex s in V discover every vertex reachable from s find shortest path from s the name reflects the fact that all vertices at distance k-1 are discovered before any at distance k are discovered 3 colors white: undiscovered; gray: discovered; black: all neighbors have been discovered

4 CS138A 1999 4 Breadth First Tree Incremental buildup of a tree with a predecessor relationship 1. BFS(G,s) 2. for( u in V[G]-{s} ) 3. color[u] = white; d[u] = infinity; p[u] = nil; 4. color[s] = gray; d[s] = 0; p[s] = nil; Q = {s} 5. while( Q != {} ) 6. u = head[Q] 7. for( v in Adj[u] ) 8. if( color[v] == white ) 9. color[v] = gray; d[v] = d[u]+1; p[v] = u; 10. Enqueue(Q,v) 11. Dequeue(Q) 12. color[u] = black

5 CS138A 1999 5 Analysis Running time? Shortest paths Lemma 1: Let G=(V,E) be (un-)directed, s in V arbitrary. Then for any (u,v) in E: ∂(s,v)≤ ∂(s,u)+1 Lemma 2: Let G=(V,E) be (un-)directed, suppose BFS(G,s) is run. Then upon termination d[v]≥ ∂(s,v) for all v in V

6 CS138A 1999 6 Analysis Shortest paths Lemma 3: Suppose during execution of BFS(G,s) Q contains (v 1,v 2,…v r ). Then d[v r ] ≤d[v 1 ]+1 and d[v i ] ≤d[v i+1 ] for i=1,2,…r- 1 Theorem: Correctness of BFS. Suppose BFS(G,s) is run then every v in V reachable from s is discovered and d[v]= ∂(s,v) upon termination. One of the shortest paths from s to v is given by the shortest path from s to p[v] and the edge (p[v],v)

7 CS138A 1999 7 Breadth First Trees BFS builds a breadth first tree in the p[] field predecessor subgraph of G=(V,E) G p (V p,E p ) where V p contains all v in V with non-nil p[] field plus {s} and E p contains all predecessor edges for all v in V p this is a tree. Why? Lemma 5: BFS(G,s) produces a predecessor subgraph which is a breadth first tree

8 CS138A 1999 8 Depth First Search Different strategy always go as deep as possible before going on not just a single tree, but a forest results G p =(V,E p ) where E p consists of all edges (p[v],v) for any v in V with predecessor field not empty contrast with BFS tree: G p (V p,E p ), where E p depends on V p not V! there will be no distinguished start vertex

9 CS138A 1999 9 Depth First Search 1. DFS(G) 2. for( u in V[G] ) color[u] = white; p[u] = nil; 3. time = 0 4. for( u in V[G] ) if( color[u] == white ) DFS-visit(u) 5. DFS-visit(u) 6. color[u] = gray; d[u] = time = time+1; 7. for( v in adj[u] ) 8. if( color[v] == white ) 9. p[v] = u; DFS-visit(v); 10. color[u] = black; f[u] = time = time + 1;

10 CS138A 1999 10 Analysis Running time Correctness Theorem 6: (Parenthesis theorem) In any DFS of a (un-)directed graph G=(V,E) for any pair u,v one of the following holds [d[u],f[u]] and [d[v],f[v]] are entirely disjoint [d[u],f[u]] is contained in [d[v],f[v]] and u is a descencent of v vice versa Corollary 7: descendent intervals are nested

11 CS138A 1999 11 Analysis Correctness Theorem 8: (White path theorem) In a DFF of a (un-)directed G=(V,E) vertex v is a descendent of u iff at the time d[u] v can be reached from u along a path of white vertices

12 CS138A 1999 12 Classification DFS admits classification of edges Tree edges: members of DFF of G p. (u,v) is a tree edge if v was discovered exploring u Back edges: edges (u,v) connecting a vertex u to an ancestor v (self loops are back edges) Forward edges: non-tree edges (u,v) connecting u to a descendent v Cross edges: all others (c0uld be same tree, could be different trees)

13 CS138A 1999 13 Edge Types Undirected graphs Theorem 9: in a DFS of an undirected graph G=(V,E) every edge of G is either a tree edge or a back edge

14 CS138A 1999 14 Topological Sort For a directed acyclic graph (DAG) a topological sort is a linear ordering of all its vertices such that if G contains (u,v) then u appears before v in the ordering 1. Topological-Sort(G) 2. call DFS(G) to compute finish times 3. enter vertices onto front of list according to their finish times 1. return linked list of vertices

15 CS138A 1999 15 Analysis Running time Correctness Lemma 10: a directed graph is acyclic iff DFS(G) yields no back edges Theorem 11: Topological-Sort(G) produces a topological sort of a DAG G

16 CS138A 1999 16 A Classic Problem Strongly connected components a strongly connected component of a directed graph G=(V,E) is a maximal subset U of V such that for any u,v in U there is a path from u to v and a path from v to u form the transpose G T (runtime?) u is reachable from v in G iff v is reachable from u in G T

17 CS138A 1999 17 Algorithm 1. Strongly-Connected-Components(G) 2. call DFS(G) to compute finish times 3. compute G T 4. call DFS(G T ) and consider vertices 5. decreasing finish time order 6. output vertices of each tree in DFS(G T ) 7. as separate strongly connected component

18 CS138A 1999 18 Analysis Running time Correctness Lemma 12: if two vertices are in the same SCC then no path between them ever leaves the SCC Theorem 13: in any DFS all vertices in the same SCC are placed in the same DFT

19 CS138A 1999 19 Analysis Correctness Theorem 14: in a directed graph G=(V,E) the forefather phi(u) of any u in V in any DFS of G is an ancestor of u Corollary 15: in any DFS of a directed graph G=(V,E) vertices u and phi(u) for all u in V lie in the same SCC Theorem 16: in a directed graph G=(V,E) two vertices u,v in V lies in the same SCC iff they have the same forefather in DFS(G)


Download ppt "CS138A 1999 1 Elementary Graph Algorithms Peter Schröder."

Similar presentations


Ads by Google