620 likes | 700 Views
Learn about graph theory, traversal techniques like Depth-First Search (DFS) and Breadth-First Search (BFS), important graph problems, and applications. Explore topics such as connectivity, acyclicity, topological sort, and more. Dive into maze exploration and complexity analysis.
E N D
Lecture 6 Graph Traversal • Graph • Undirected graph • Directed graph Xiaojuan Cai
Overview • Graph • Undirected graph • DFS, BFS, Application • Directed graph • DFS, BFS, Application Xiaojuan Cai
Graph theory The Königsberg Bridge problem (Source from Wikipedia) Xiaojuan Cai
Graph terminology Undirected graph Directed graph 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? 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 ? Xiaojuan Cai
Where are we? • Graph • Undirected graph • DFS, BFS, Application • Directed graph • DFS, BFS, Application Xiaojuan Cai
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
Maze exploration Xiaojuan Cai
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
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
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
time <-- 0 v.pre <-- infinity v.post <-- infinity time <-- time + 1 v.pre <-- time time <-- time + 1 v.post <-- time Xiaojuan Cai
Quiz: Complexity Xiaojuan Cai
Complexity Xiaojuan Cai
DFS application? Xiaojuan Cai
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
Quiz: Complexity Xiaojuan Cai
Complexity Xiaojuan Cai
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
w.parent <-- v Xiaojuan Cai
Connectivity #trees == #connected components Xiaojuan Cai
Graph acyclicity NO back edges! Xiaojuan Cai
Where are we? • Graph • Undirected graph • DFS, BFS, Application • Directed graph • DFS, BFS, Application Xiaojuan Cai
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
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
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
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
Graph acyclicity NO back edges! A directed acyclic graph is usually called a DAG. Xiaojuan Cai
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
Applications 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. Xiaojuan Cai
Topological order Xiaojuan Cai
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
Θ(|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
Connectivity w u v ? x y z #trees == #connected components Xiaojuan Cai
Strong connected components Xiaojuan Cai
Ecological food webs http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.gif Xiaojuan Cai
Strong connected components Lemma Every directed graph is a DAG of its strongly connected component. 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′. Xiaojuan Cai
Kosaraju-Sharir algorithm Reversed graph Xiaojuan Cai
Θ(|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
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;
BFS, DFS applications 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 enqueuewebsites to which it links(provided you haven't done so before). Xiaojuan Cai
BFS, DFS applications • Vertex: pixel. • Edge: between two adjacent gray pixels. • Blob: all pixels connected to given pixel. 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 Xiaojuan Cai