Chapter 22 Elementary Graph Algorithms
Graph Representations Graph G = (V,E) Directed Undirected Adjacency Lists Adjacency Matrix
Graph Representations Adjacency List: Undirected Memory: Adjacency:
Graph Representations Adjacency List: Undirected Memory: O(V+E) Adjacency:
Graph Representations Adjacency List: Undirected Memory: O(V+E) Adjacency: O(V)
Graph Representations Adjacency List: Directed Memory: O(V+E) Adjacency: O(V)
Graph Representations Adjacency Matrix: Undirected Memory: Adjacency:
Graph Representations Adjacency Matrix: Undirected Memory:!(V 2 ) Adjacency:
Graph Representations Adjacency Matrix: Undirected Memory:!(V 2 ) Adjacency:!(1)
Graph Representations Adjacency Matrix: Undirected Memory:!(V 2 ) Adjacency:!(1)
Breadth First Search Idea: Send a wave out from s First hits all vertices 1 away from s From there, hits all vertices 2 from s Etc. Use FIFO Queue Q to maintain wavefront v " Q if and only if the wave has hit v but has not come out of v yet d[v] is distance from s to u
Breadth First Search
BFS: Example r s t u 0 v w x y Q: s
BFS: Example r s t u 1 0 1 v w x y Q: w,r
BFS: Example r s t u 1 0 2 1 2 v w x y Q: r,t,x
BFS: Example r s t u 1 0 2 2 1 2 v w x y Q: t,x,v
BFS: Example r s t u 1 0 2 3 2 1 2 v w x y Q: x,v,u
BFS: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: v,u,y
BFS: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: u,y
BFS: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: y
BFS: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q:
Breadth First Search Analysis:
Breadth First Search Analysis: O(V) because every vertex is enqueued at most once. O(E) because every edge is dequeued at most once and we examine (u,v) only when u is dequeued. Therefore, every edge is examined at least once when directed, at least twice when undirected. Time = O(V+E)
Depth First Search Idea: Methodically explore every edge As soon as we discover a vertex, we explore it Use the stack to search the graph d[v] is the discovery time f [v] is the finishing time Every vertex has a color - white = undiscovered - grey = discovered, but not finished - black = finished (we have discovered all vertices reachable by it) Time is a global variable
Depth First Search
DFS: Example u v w 1/ x y z
DFS: Example u v w 1/ 2/ x y z
DFS: Example u v w 1/ 2/ 3/ x y z
DFS: Example u v w 1/ 2/ 4/ 3/ x y z
DFS: Example u v w 1/ 2/ 4/5 3/ x y z
DFS: Example u v w 1/ 2/ 4/5 3/6 x y z
DFS: Example u v w 1/ 2/7 4/5 3/6 x y z
DFS: Example u v w 1/8 2/7 4/5 3/6 x y z
DFS: Example u v w 1/8 2/7 9/ 4/5 3/6 x y z
DFS: Example u v w 1/8 2/7 9/ 4/5 3/6 10/ x y z
DFS: Example u v w 1/8 2/7 9/ 4/5 3/6 10/11 x y z
DFS: Example u v w 1/8 2/7 9/12 4/5 3/6 10/11 x y z
Depth First Search Analysis:
Depth First Search Analysis: Time =!(V+E)! because it is guaranteed to examine each vertex and edge
Shortest Path: BFS Extension: Use #[u] to store the predecessor variable of u Algorithm: PRINT-PATH(G,s,v) if v = s then print s else if #[v] = NIL then print no path from s to v exits else PRINT-PATH(G,s,![v]) print v
Web Crawling Google: Web can be considered a large graph Vertices = web pages Edges = hyperlinks How do you index all the available pages? BFS using the links Why not DFS?
Parenthesis Structure: DFS u v w 1/8 2/7 9/12 4/5 3/6 10/11 x y z
Parenthesis Structure: DFS u v w z y x 1 (u 2 (v 3 (y 4 (x 5 x) 6 y) 7 v) 8 u) 9 (w 10 (z 11 z) 12 w)
Finding Cycles If G contains a back edge, it is cyclic Algorithm: When exploring all vertices u adjacent to v check the color of u If u is black, then there is a cycle
Topological Sort: DFS Used to find precedence in a DAG (no cycles) Good for modeling processes that have a partial order a > b and b > c therefore a > c But may have a and b such that neither a > b nor b > c It makes a total order from from a partial order
Topological Sort: Goalie Example socks shorts T-shirt batting glove hose chest pad pants skates leg pads sweater mask catch glove blocker
Topological Sort Algorithm: Perform a DFS Topological order is finish times in decreasing order When a vertex is finished, push it onto the front of a list Time =!(V+E)
Topological Sort: Goalie Example 25/26 15/24 socks shorts 16/23 hose 17/22 pants 18/21 skates 19/20 leg pads 7/14 T-shirt 8/13 chest pad 9/12 sweater 10/11 mask 2/5 catch glove 3/4 blocker 1/6 batting glove
Topological Sort: Goalie Example 26 socks 24 shorts 23 hose(r) 22 pants 21 skates 20 leg pads 14 t-shirt 13 chest pad 12 sweater 11 mask 6 batting glove 5 catch glove 4 blocker
Topological Sort: Volume Rendering
Topological Sort: Volume Rendering
Topological Sort: Volume Rendering 6 7 5 B 3 2 4 1 A p A < B p
Topological Sort: Volume Rendering Idea: Define ordering relations by looking at shared faces. A B C E D F B < A A < C B < E C < E C < D E < F D < F Viewing direction
Strongly Connected Components Definition: A SCC of G is a maximal set of vertices C $ V such that for all u, v " C, both u! v and v! u
Strongly Connected Components Algorithm: Call DFS(G) to compute finishing times f[u] for each vertex u Compute G T (!(V+E)) Call DFS(G T ), but in the main loop of DFS, consider the vertices oin order of decreasing f[u] (as computed in step 1) Output the vertices of each tree in the depth-first forest of step 3 as a seperate SCC
Strongly Connected Components: Example
Miscellaneous Finding paths through mazes Routing wires on circuit boards Others?