Lecture 6 Graph Traversal Undirected graph Directed graph 4/21/2019 Xiaojuan Cai
Overview Graph Undirected graph DFS, BFS, Application Directed graph 4/21/2019 Xiaojuan Cai
Graph theory The Königsberg Bridge problem (Source from Wikipedia) 4/21/2019 Xiaojuan Cai
Graph terminology Undirected graph Directed graph 4/21/2019 Xiaojuan Cai
Adjacency Matrix v.s. Adjacency List G=<V, E> Directed: n + m Undirected: n + 2m Directed: n2 Undirected: n2 For every edge connected with v ... Is u and v connected with an edge? 4/21/2019 Xiaojuan Cai
Important graph problems Path. Is there a directed path from s to t ? Shortest path. What is the shortest directed path from s to t ? Topological sort. Can you draw the digraph so that all edges point upwards? Strong connectivity. Is there a directed path between all pairs of vertices? Transitive closure. For each vertices v and w, is there a path from v to w ? 4/21/2019 Xiaojuan Cai
Where are we? Graph Undirected graph DFS, BFS, Application 4/21/2019 Xiaojuan Cai
DFS Depth-first-search. Unroll a ball of string behind you. 忒修斯 克里特岛迷宫 米诺斯 阿里阿德涅公主 米诺牛 剑和线团 Shannon的故事 Depth-first-search. Unroll a ball of string behind you. Mark each visited intersection and each visited passage. Retrace steps when no unvisited options. 4/21/2019 Xiaojuan Cai
Maze exploration 4/21/2019 Xiaojuan Cai
Depth-first search pre/post = 0/0 u 1/ 6 u v w v 2/ 5 y x y 3/ 4 z w z 4/ 1 5/ 3 w x y 6/ 2 z v DFS tree u 4/21/2019 Xiaojuan Cai u
Depth-first search time = 0 pre/post u 1/ 12 u v w v 2/ 11 y x y 3/ 10 z w z x 4/ 5 6/ 9 w x y 7/ 8 z v DFS tree u 4/21/2019 Xiaojuan Cai u
DFS tree: undirected How to figure out back edges? u 1/ 6 v 2/ 5 tree edge: back edge: y 3/ 4 w x 4/ 1 5/ 3 6/ 2 z DFS tree 4/21/2019 Xiaojuan Cai
4/21/2019 Xiaojuan Cai
time <-- 0 v.pre <-- infinity v.post <-- infinity time <-- time + 1 v.pre <-- time time <-- time + 1 v.post <-- time 4/21/2019 Xiaojuan Cai
Quiz: Complexity Time Space Undirected Adj. Matrix ? Adj. List 4/21/2019 Xiaojuan Cai
Complexity Time Space Undirected Adj. Matrix O(|V|2) O(|V|) Adj. List O(|V| + 2|E|) 4/21/2019 Xiaojuan Cai
DFS application? 4/21/2019 Xiaojuan Cai
Breadth-first search u v w x y z a b u 1 x 2 v 2 y 3 w z 4 u v x y w z 5 a 5 BFS tree 4/21/2019 Xiaojuan Cai
4/21/2019 Xiaojuan Cai
Quiz: Complexity Time Space Undirected Adj. Matrix ? Adj. List 4/21/2019 Xiaojuan Cai
Complexity Time Space Undirected Adj. Matrix O(|V|2) O(|V|) Adj. List O(|V| + 2|E|) 4/21/2019 Xiaojuan Cai
BFS Application: The shortest path u x v y a b w z u v w x y z a b Discussion: How to record the shortest path? 4/21/2019 Xiaojuan Cai
4/21/2019 Xiaojuan Cai
w.parent <-- v 4/21/2019 Xiaojuan Cai
Connectivity #trees == #connected components 4/21/2019 Xiaojuan Cai
Graph acyclicity NO back edges! 4/21/2019 Xiaojuan Cai
Where are we? Graph Undirected graph DFS, BFS, Application 4/21/2019 Xiaojuan Cai
DFS tree: directed time = 0 u w w 1/ 8 9/ 12 u v v 10/ 11 2/ 7 z x y 3/ 6 z y x 4/ 5 x DFS trees y z v w u 4/21/2019 Xiaojuan Cai u
DFS tree u w tree edge: back edge: forward edge: cross edge: 1/ 8 9/ 12 v 10/ 11 2/ 7 z type edge(u,v) tree [u [v ]v ]u back [v [u ]u ]v forward cross [v ]v [u ]u 3/ 6 y x 4/ 5 4/21/2019 Xiaojuan Cai
Quiz Run DFS on the following graph. Which type are the following edges: CB, DC, FC (Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first.) A. tree edge B. back edge C. forward edge D. cross edge
Application: Garbage collector Mark-sweep algorithm. [McCarthy, 1960] Mark: mark all reachable objects. Sweep: if object is unmarked, it is garbage (so add to free list). Memory cost. Uses 1 extra mark bit per object (plus DFS stack). roots DFS needs O(|V|) space. How to do Mark-sweep with O(1) space? 4/21/2019 Xiaojuan Cai
A directed acyclic graph is usually called a DAG. Graph acyclicity NO back edges! A directed acyclic graph is usually called a DAG. 4/21/2019 Xiaojuan Cai
An edge (u,v) is a back edge iff Graph acyclicity u w tree edge: back edge: forward edge: cross edge: 1/ 8 9/ 12 v 10/ 11 2/ 7 z type edge(u,v) tree [u [v ]v ]u back [v [u ]u ]v forward cross [v ]v [u ]u 3/ 6 y x 4/ 5 An edge (u,v) is a back edge iff post(u) < post(v) 4/21/2019 Xiaojuan Cai
Applications 4/21/2019 Xiaojuan Cai
DAG: Topological sort Problem: TopoSort Input: A DAG (directed acyclic graph) G = (V , E ) Output: A linear ordering of its vertices in such a way that if (v,w) ∈ E, then v appears before w in the ordering. 4/21/2019 Xiaojuan Cai
Topological order 4/21/2019 Xiaojuan Cai
Topological sort by DFS Proposition In DAG, every edge (u,v) yields post[v] < post[u]. u v x y w z 1/ 2/ 3/ 4/ 5 6 7 8 9/ 10/ 11 12 tree edge: back edge: forward edge: cross edge: type edge(u,v) tree [u [v ]v ]u back [v [u ]u ]v forward cross [v ]v [u ]u 4/21/2019 Xiaojuan Cai
DAG: Topological sort Θ(|E|+|V|) TOPOLOGICAL-SORT(G) Call DFS(G) to compute finishing times of each vertex v As each vertex is finished, insert it onto the front of a linked list Return the linked list of vertices. Θ(|E|+|V|) 4/21/2019 Xiaojuan Cai
? Connectivity #trees == #connected components u v w x y z 4/21/2019 Xiaojuan Cai
Strong connected components 4/21/2019 Xiaojuan Cai
Ecological food webs 4/21/2019 Xiaojuan Cai http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.gif 4/21/2019 Xiaojuan Cai
Strong connected components Lemma Every directed graph is a DAG of its strongly connected component. 4/21/2019 Xiaojuan Cai
Some properties Property 1 If dfs is started at node u, then it will terminate precisely when all nodes reachable from u have been visited. Property 2 The node that receives the highest post number in DFS must lie in a source strongly connected component. Property 3 If C and C′ are scc, and there is an edge from a node in C to a node in C′, then the highest post number in C is bigger than the highest post number in C′. 4/21/2019 Xiaojuan Cai
Kosaraju-Sharir algorithm Reversed graph 4/21/2019 Xiaojuan Cai
Kosaraju-Sharir algorithm Run DFS on GR. Run the undirected connected components algorithm on G, and during the DFS, process the vertices in decreasing order of their post numbers from step 1. Θ(|E|+|V|) 4/21/2019 Xiaojuan Cai
Tarjan’s algorithm (briefly) //color[u] = 0: unvisited; 1: visited and in Stack, 2: visited and not in Stack try all vertex u, if color[u] = 0, DFS(u) DFS(u): Push u into stack, color[u] = 1, pre[u], low[u] = ++time try all neighbor v of u if color[v] = 0, DFS(v), low[u] = min{low[u],low[v]} else if color[v] = 1, low[u] = min{low[u],pre[v]} if low[u]==pre[u] Pop v from stack, color[v] = 2, until v=u; One Dfs, More efficient than Kosaraju-Sharir
BFS, DFS applications 4/21/2019 Xiaojuan Cai
BFS, DFS applications BFS. Choose root web page as source s. Maintain a Queue of websites to explore. Maintain a SET of discovered websites. Dequeue the next website and enqueue websites to which it links (provided you haven't done so before). 4/21/2019 Xiaojuan Cai
BFS, DFS applications Vertex: pixel. Edge: between two adjacent gray pixels. Blob: all pixels connected to given pixel. 4/21/2019 Xiaojuan Cai
BFS, DFS applications Every data structure is a digraph. Vertex = object. Edge = reference. Roots. Objects known to be directly accessible by program (e.g., stack). Reachable objects. Objects indirectly accessible by program (starting at a root and following a chain of pointers). roots 4/21/2019 Xiaojuan Cai
BFS, DFS applications Every program is a digraph. Vertex = basic block of instructions Edge = jump. Dead-code elimination. Find (and remove) unreachable code. Infinite-loop detection. Determine whether exit is unreachable. 4/21/2019 Xiaojuan Cai
Quiz Which of the following statements are true for undirected graph? A. If u is connected to v and v is connected to w, then u is connected to w. B. Removing any edge from a connected graph G breaks the graph into two connected components. C. An acyclic graph G is connected if and only if it has V-1 edges. D. If you add two edges to a graph G, its number of components can decrease by at most 2. ♠ ♣ ♦
Quiz Which of the following statements are true for directed graph? A. A digraph on V vertices with fewer than V edges contains more than one strong component. B. If we modify the Kosaraju-Sharir algorithm to replace the second DFS with BFS, then it will still find the strong components. C. If u is strongly connected to v and v is strongly connected to w, then u is strongly connected to w. D. If you add two edges to a digraph, its number of strong components can decrease by at most 2.
Challenges 4/21/2019 Xiaojuan Cai
Challenge 1 Problem. Is a graph bipartite? How difficult? 0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 Problem. Is a graph bipartite? How difficult? Any programmer could do it. Need to be a typical diligent SE222 student. Hire an expert. Intractable. No one knows. Impossible. 1 2 6 3 4 5 0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 1 2 6 3 4 5 4/21/2019 Xiaojuan Cai
Challenge 1 Problem. Is a graph bipartite? How difficult? 0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 Problem. Is a graph bipartite? How difficult? Any programmer could do it. Need to be a typical diligent SE222 student. Hire an expert. Intractable. No one knows. Impossible. 1 2 6 3 4 5 ✓ 0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 1 2 6 3 4 5 4/21/2019 Xiaojuan Cai
Challenge 2 Problem. Find a cycle. How difficult? 0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 Problem. Find a cycle. How difficult? Any programmer could do it. Need to be a typical diligent SE222 student. Hire an expert. Intractable. No one knows. Impossible. 1 2 6 3 4 5 ✓ 1 2 6 3 4 5 4/21/2019 Xiaojuan Cai
Challenge 3 Problem. Find a cycle that uses every edge exactly once. (Eulerian tour) How difficult? Any programmer could do it. Need to be a typical diligent SE222 student. Hire an expert. Intractable. No one knows. Impossible. 0-1 0-2 0-5 0-6 1-2 2-3 2-4 3-4 4-5 4-6 6 4 2 1 5 3 0-1-2-3-4-2-0-6-4-5-0 ✓ 4/21/2019 Xiaojuan Cai
Challenge 4 Problem. Find a cycle that uses every vertex exactly once. (Hamiltonian tour) How difficult? Any programmer could do it. Need to be a typical diligent SE222 student. Hire an expert. Intractable. No one knows. Impossible. 0-1 0-2 0-5 0-6 1-2 2-6 3-4 3-5 4-5 4-6 1 2 6 3 4 5 0-5-3-4-6-2-1-0 ✓ 4/21/2019 Xiaojuan Cai
Challenge 5 Problem. Are two graphs identical except for vertex names? (Graph isomorphism) How difficult? Any programmer could do it. Need to be a typical diligent SE222 student. Hire an expert. Intractable. No one knows. Impossible. 0-1 0-2 0-5 0-6 3-4 3-5 4-5 4-6 6 4 2 1 5 3 0↔4, 1↔3, 2↔2, 3↔6, 4↔5, 5↔0, 6↔1 0-4 1-4 1-5 2-4 5-6 ✓ 4/21/2019 Xiaojuan Cai
Challenge 6 Problem. Lay out a graph in the plane without crossing edges? How difficult? Any programmer could do it. Need to be a typical diligent SE222 student. Hire an expert. Intractable. No one knows. Impossible. 1 0-1 0-2 0-5 0-6 3-4 3-5 4-5 4-6 2 6 3 4 5 ✓ 1 2 6 3 4 5 4/21/2019 Xiaojuan Cai