Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 Graph Traversal

Similar presentations


Presentation on theme: "Lecture 7 Graph Traversal"— Presentation transcript:

1 Lecture 7 Graph Traversal
Undirected graph Directed graph 4/28/2017 Xiaojuan Cai

2 Overview Graph Undirected graph DFS, BFS, Application Directed graph
4/28/2017 Xiaojuan Cai

3 Graph theory The Königsberg Bridge problem (Source from Wikipedia)
4/28/2017 Xiaojuan Cai

4 Graph everywhere 4/28/2017 Xiaojuan Cai

5 Graph terminology Undirected graph Directed graph 4/28/2017
Xiaojuan Cai

6 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/28/2017 Xiaojuan Cai

7 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/28/2017 Xiaojuan Cai

8 Where are we? Graph Undirected graph DFS, BFS, Application
4/28/2017 Xiaojuan Cai

9 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/28/2017 Xiaojuan Cai

10 Maze exploration 4/28/2017 Xiaojuan Cai

11 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/28/2017 Xiaojuan Cai u

12 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/28/2017 Xiaojuan Cai u

13 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/28/2017 Xiaojuan Cai

14 4/28/2017 Xiaojuan Cai

15 time <-- 0 v.pre <-- infinity v.post <-- infinity
time <-- time + 1 v.pre <-- time time <-- time + 1 v.post <-- time 4/28/2017 Xiaojuan Cai

16 Quiz: Complexity Time Space Undirected Adj. Matrix ? Adj. List
4/28/2017 Xiaojuan Cai

17 Complexity Time Space Undirected Adj. Matrix O(|V|2) O(|V|) Adj. List
O(|V| + 2|E|) 4/28/2017 Xiaojuan Cai

18 DFS correctness Proposition
DFS marks all vertices connected to s in time proportional to the sum of their degrees. Pf. Correctness: if w marked, then w connected to s (why?) if w connected to s, then w marked (if w unmarked, then consider last edge on a path from s to w that goes from a marked vertex to an unmarked one) Running time: each vertex connected to s is visited once 4/28/2017 Xiaojuan Cai

19 DFS application? 4/28/2017 Xiaojuan Cai

20 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/28/2017 Xiaojuan Cai

21 4/28/2017 Xiaojuan Cai

22 Quiz: Complexity Time Space Undirected Adj. Matrix ? Adj. List
4/28/2017 Xiaojuan Cai

23 Complexity Time Space Undirected Adj. Matrix O(|V|2) O(|V|) Adj. List
O(|V| + 2|E|) 4/28/2017 Xiaojuan Cai

24 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/28/2017 Xiaojuan Cai

25 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 u v x y w z a b BFS tree u u v y y parent w z 4/28/2017 Xiaojuan Cai

26 4/28/2017 Xiaojuan Cai

27 w.parent <-- v 4/28/2017 Xiaojuan Cai

28 Connectivity #trees == #connected components 4/28/2017 Xiaojuan Cai

29 Graph acyclicity NO back edges! 4/28/2017 Xiaojuan Cai

30 Where are we? Graph Undirected graph DFS, BFS, Application
4/28/2017 Xiaojuan Cai

31 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/28/2017 Xiaojuan Cai u

32 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/28/2017 Xiaojuan Cai

33 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

34 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/28/2017 Xiaojuan Cai

35 A directed acyclic graph is usually called a DAG.
Graph acyclicity NO back edges! A directed acyclic graph is usually called a DAG. 4/28/2017 Xiaojuan Cai

36 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/28/2017 Xiaojuan Cai

37 Applications 4/28/2017 Xiaojuan Cai

38 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/28/2017 Xiaojuan Cai

39 Topological order 4/28/2017 Xiaojuan Cai

40 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/28/2017 Xiaojuan Cai

41 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/28/2017 Xiaojuan Cai

42 Quiz Give the topological-sort algorithm on the following graph, give the topological order. (Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first.) A. BACEDFGH B. ABCDEFGH C. ABCEDFHG D. None of above

43 ? Connectivity #trees == #connected components u v w x y z 4/28/2017
Xiaojuan Cai

44 Strong connected components
4/28/2017 Xiaojuan Cai

45 Ecological food webs 4/28/2017 Xiaojuan Cai
4/28/2017 Xiaojuan Cai

46 Strong connected components
Lemma Every directed graph is a DAG of its strongly connected component. 4/28/2017 Xiaojuan Cai

47 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/28/2017 Xiaojuan Cai

48 Kosaraju-Sharir algorithm
Reversed graph 4/28/2017 Xiaojuan Cai

49 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/28/2017 Xiaojuan Cai

50 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

51 Articulation points Problem: ArticulationPoints
Input: An undirected graph G = (V , E ) Output: Articulation points. (A point v is articulate iff any path between other two points must pass through v). 4/28/2017 Xiaojuan Cai

52 Conclusion Graph Undirected graph DFS, BFS, Application Directed graph
4/28/2017 Xiaojuan Cai

53 BFS, DFS applications 4/28/2017 Xiaojuan Cai

54 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/28/2017 Xiaojuan Cai

55 BFS, DFS applications Vertex: pixel.
Edge: between two adjacent gray pixels. Blob: all pixels connected to given pixel. 4/28/2017 Xiaojuan Cai

56 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/28/2017 Xiaojuan Cai

57 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/28/2017 Xiaojuan Cai

58 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. ♠ ♣ ♦

59 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.

60 Challenges 4/28/2017 Xiaojuan Cai

61 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/28/2017 Xiaojuan Cai

62 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/28/2017 Xiaojuan Cai

63 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/28/2017 Xiaojuan Cai

64 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 4/28/2017 Xiaojuan Cai

65 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 4/28/2017 Xiaojuan Cai

66 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/28/2017 Xiaojuan Cai

67 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/28/2017 Xiaojuan Cai


Download ppt "Lecture 7 Graph Traversal"

Similar presentations


Ads by Google