1 / 61

Graph Undirected graph Directed graph

Lecture 6 Graph Traversal. Graph Undirected graph Directed graph. Overview. Graph Undirected graph DFS, BFS, Application Directed graph DFS, BFS, Application. Graph theory. The Königsberg Bridge problem (Source from Wikipedia). Graph terminology. Undirected graph. Directed graph.

Télécharger la présentation

Graph Undirected graph Directed graph

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 6 Graph Traversal • Graph • Undirected graph • Directed graph Xiaojuan Cai

  2. Overview • Graph • Undirected graph • DFS, BFS, Application • Directed graph • DFS, BFS, Application Xiaojuan Cai

  3. Graph theory The Königsberg Bridge problem (Source from Wikipedia) Xiaojuan Cai

  4. Graph terminology Undirected graph Directed graph Xiaojuan Cai

  5. 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? Xiaojuan Cai

  6. 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 ? Xiaojuan Cai

  7. Where are we? • Graph • Undirected graph • DFS, BFS, Application • Directed graph • DFS, BFS, Application Xiaojuan Cai

  8. DFS Depth-first-search. • Unroll a ball of string behind you. • Mark each visited intersection and each visited passage. • Retrace steps when no unvisited options. Xiaojuan Cai

  9. Maze exploration Xiaojuan Cai

  10. Depth-first search pre/post = 0/0 u 1/ 6 w u v v 2/ 5 y x 3/ 4 y z w x z 5/ 3 4/ 1 x w y 6/ 2 z v DFS tree u Xiaojuan Cai u

  11. Depth-first search time = 0 pre/post u 1/ 12 w u v v 2/ 11 y x 3/ 10 y z w x z 6/ 9 4/ 5 x w y 7/ 8 z v DFS tree u Xiaojuan Cai u

  12. How to figure out back edges? DFS tree: undirected u 1/ 6 v 2/ 5 • tree edge: • back edge: y 3/ 4 w x 5/ 3 4/ 1 6/ 2 z DFS tree Xiaojuan Cai

  13. Xiaojuan Cai

  14. time <-- 0 v.pre <-- infinity v.post <-- infinity time <-- time + 1 v.pre <-- time time <-- time + 1 v.post <-- time Xiaojuan Cai

  15. Quiz: Complexity Xiaojuan Cai

  16. Complexity Xiaojuan Cai

  17. DFS application? Xiaojuan Cai

  18. u w a u v 1 x 2 z 4 w v 2 4 5 3 5 b y a z x y b Breadth-first search y u x w z a v b BFS tree Xiaojuan Cai

  19. Xiaojuan Cai

  20. Quiz: Complexity Xiaojuan Cai

  21. Complexity Xiaojuan Cai

  22. u w a u v x z w v b y a z x y b BFS Application: The shortest path Discussion: How to record the shortest path? Xiaojuan Cai

  23. Xiaojuan Cai

  24. w.parent <-- v Xiaojuan Cai

  25. Connectivity #trees == #connected components Xiaojuan Cai

  26. Graph acyclicity NO back edges! Xiaojuan Cai

  27. Where are we? • Graph • Undirected graph • DFS, BFS, Application • Directed graph • DFS, BFS, Application Xiaojuan Cai

  28. DFS tree: directed time = 0 w u 1/ 8 9/ 12 w u v v 10/ 11 2/ 7 z x 3/ 6 y z y x 4/ 5 x DFS trees y z v w u Xiaojuan Cai u

  29. DFS tree w u • tree edge: • back edge: • forward edge: • cross edge: 1/ 8 9/ 12 v 10/ 11 2/ 7 z 3/ 6 y x 4/ 5 Xiaojuan Cai

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

  31. DFS needs O(|V|) space. • How to do Mark-sweep with O(1) space? roots 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). Xiaojuan Cai

  32. Graph acyclicity NO back edges! A directed acyclic graph is usually called a DAG. Xiaojuan Cai

  33. Graph acyclicity w u • tree edge: • back edge: • forward edge: • cross edge: 1/ 8 9/ 12 v 10/ 11 2/ 7 z 3/ 6 y x 4/ 5 An edge (u,v) is a back edge iff post(u) < post(v) Xiaojuan Cai

  34. Applications Xiaojuan Cai

  35. 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. Xiaojuan Cai

  36. Topological order Xiaojuan Cai

  37. w u • tree edge: • back edge: • forward edge: • cross edge: 1/ 8 9/ 12 v 10/ 11 2/ 7 z 3/ 6 y x 4/ 5 Topological sort by DFS Proposition In DAG, every edge (u,v) yields post[v] < post[u]. Xiaojuan Cai

  38. Θ(|E|+|V|) DAG: Topological sort • 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. Xiaojuan Cai

  39. Connectivity w u v ? x y z #trees == #connected components Xiaojuan Cai

  40. Strong connected components Xiaojuan Cai

  41. Ecological food webs http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.gif Xiaojuan Cai

  42. Strong connected components Lemma Every directed graph is a DAG of its strongly connected component. Xiaojuan Cai

  43. 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′. Xiaojuan Cai

  44. Kosaraju-Sharir algorithm Reversed graph Xiaojuan Cai

  45. Θ(|E|+|V|) 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. Xiaojuan Cai

  46. 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;

  47. BFS, DFS applications Xiaojuan Cai

  48. 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 enqueuewebsites to which it links(provided you haven't done so before). Xiaojuan Cai

  49. BFS, DFS applications • Vertex: pixel. • Edge: between two adjacent gray pixels. • Blob: all pixels connected to given pixel. Xiaojuan Cai

  50. 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 Xiaojuan Cai

More Related