Lecture 6 Graph Traversal Graph Undirected graph Directed
- Slides: 61
Lecture 6 Graph Traversal • Graph • Undirected graph • Directed graph 6/19/2021 Xiaojuan Cai 1
Overview • Graph • Undirected graph ‣ • Directed graph ‣ 6/19/2021 DFS, BFS, Application Xiaojuan Cai 2
Graph theory The Königsberg Bridge problem (Source from Wikipedia) 6/19/2021 Xiaojuan Cai 3
Graph terminology Undirected graph 6/19/2021 Directed graph Xiaojuan Cai 5
Adjacency Matrix v. s. Adjacency List G=<V, E> Directed: n + m Undirected: n + 2 m Directed: n 2 Undirected: n 2 For every edge connected with v. . . Is u and v connected with an edge? 6/19/2021 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 ? 6/19/2021 Xiaojuan Cai 7
Where are we? • Graph • Undirected graph ‣ • Directed graph ‣ 6/19/2021 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. 6/19/2021 Xiaojuan Cai 9
Maze exploration 6/19/2021 Xiaojuan Cai 10
Depth-first search pre/post = 0/0 u v w 1/ 6 u v 2/ 5 x y y 3/4 z z 4/ 1 x w x y v DFS tree u 6/19/2021 Xiaojuan Cai w z 5/ 3 6/2 11
Depth-first search time = 0 u v w pre/post 1/ 12 u v 2/ 11 x y y 3/10 z z 4/ 5 x w x y v DFS tree u 6/19/2021 Xiaojuan Cai w z 6/ 9 7/8 12
DFS tree: undirected 1/ 6 u How to figure out back edges? v 2/ 5 • tree edge: • back edge: y 3/4 4/ 1 x DFS tree 6/19/2021 w z 5/ 3 6/2 Xiaojuan Cai 13
6/19/2021 Xiaojuan Cai 14
time <-- 0 v. pre <-- infinity v. post <-- infinity time <-- time + 1 v. pre <-- time + 1 v. post <-- time 6/19/2021 Xiaojuan Cai 15
Quiz: Complexity Time Undirected 6/19/2021 Adj. Matrix ? Adj. List ? Xiaojuan Cai Space ? 16
Complexity Time Undirected 6/19/2021 Adj. Matrix O(|V|2) Adj. List O(|V| + 2|E|) Xiaojuan Cai Space O(|V|) 17
DFS application? 6/19/2021 Xiaojuan Cai 19
Breadth-first search u v w a 1 x x y z u 2 b u v x y w z a b 2 v 3 y z 4 4 w b 5 5 a BFS tree 6/19/2021 Xiaojuan Cai 20
6/19/2021 Xiaojuan Cai 21
Quiz: Complexity Time Adj. Matrix ? Undirected ? Adj. List 6/19/2021 Space Xiaojuan Cai ? 22
Complexity Time Adj. Matrix 2 O(|V| ) Undirected O(|V|) Adj. List 6/19/2021 Space O(|V| + 2|E|) Xiaojuan Cai 23
BFS Application: The shortest u path u v w a x v y x y z b z w b a Discussion: How to record the shortest path? 6/19/2021 Xiaojuan Cai 24
6/19/2021 Xiaojuan Cai 26
w. parent <-- v 6/19/2021 Xiaojuan Cai 27
Connectivity #trees == #connected components 6/19/2021 Xiaojuan Cai 28
Graph acyclicity NO back edges! 6/19/2021 Xiaojuan Cai 29
Where are we? • Graph • Undirected graph ‣ • Directed graph ‣ 6/19/2021 DFS, BFS, Application Xiaojuan Cai 30
DFS tree: directed time = 0 u v w 1/ 8 w u 9/12 v 2/ 7 x y z y 4/ 5 x z 10/ 11 3/6 x DFS trees y zv w u 6/19/2021 Xiaojuan Cai 31
DFS tree 1/ 8 w u • tree edge: 9/12 • back edge: 10/ 11 • forward edge: • cross edge: z v 2/ 7 y 4/ 5 6/19/2021 x 3/6 type edge(u, v) tree [u [v ]v ]u back [v [u ]u ]v forward [u [v ]v ]u cross [v ]v [u ]u Xiaojuan Cai 32
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 C. forward edge B. back edge D. cross edge
Application: Garbage collector Mark-sweep algorithm. [Mc. Carthy, 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? 6/19/2021 Xiaojuan Cai 34
Graph acyclicity NO back edges! A directed acyclic graph is usually called a DAG. 6/19/2021 Xiaojuan Cai 35
Graph acyclicity 1/ 8 w u • tree edge: 9/12 • back edge: 10/ 11 • forward edge: • cross edge: z v 2/ 7 y 4/ 5 3/6 x An edge (u, v) is a back edge iff post(u) < post(v) 6/19/2021 type edge(u, v) tree [u [v ]v ]u back [v [u ]u ]v forward [u [v ]v ]u cross [v ]v [u ]u Xiaojuan Cai 36
Applications 6/19/2021 Xiaojuan Cai 37
DAG: Topological sort Problem: Topo. Sort 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. 6/19/2021 Xiaojuan Cai 38
Topological order 6/19/2021 Xiaojuan Cai 39
Topological sort by DFS Proposition In DAG, every edge (u, v) yields post[v] < post[u]. 1/ 8 w u • tree edge: 9/12 • back edge: 10/ 11 • forward edge: • cross edge: z v 2/ 7 y 4/5 6/19/2021 x 3/6 type edge(u, v) tree [u [v ]v ]u back [v [u ]u ]v forward [u [v ]v ]u cross [v ]v [u ]u Xiaojuan Cai 40
DAG: Topological sort TOPOLOGICAL-SORT(G) 1. Call DFS(G) to compute finishing times of each vertex v 2. As each vertex is finished, insert it onto the front of a linked list 3. Return the linked list of vertices. 6/19/2021 Xiaojuan Cai ) | V | + | E | ( Θ 41
Connectivity #trees == #connected components 6/19/2021 u v w x y z Xiaojuan Cai ? 43
Strong connected components 6/19/2021 Xiaojuan Cai 44
Ecological food webs http: //www. twingroves. district 96. k 12. il. us/Wetlands/Salamander/Sal. Graphics/salfoodweb. gif 6/19/2021 Xiaojuan Cai 45
Strong connected components Lemma Every directed graph is a DAG of its strongly connected component. 6/19/2021 Xiaojuan Cai 46
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′. 6/19/2021 Xiaojuan Cai 47
Kosaraju-Sharir algorithm Reversed graph 6/19/2021 Xiaojuan Cai 48
Kosaraju-Sharir algorithm 1. Run DFS on GR. 2. 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. ) | V | + | E | Θ( 6/19/2021 Xiaojuan Cai 49
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 6/19/2021 Xiaojuan Cai 53
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). 6/19/2021 Xiaojuan Cai 54
BFS, DFS applications • Vertex: pixel. • Edge: between two adjacent gray pixels. • Blob: all pixels connected to given pixel. 6/19/2021 Xiaojuan Cai 55
BFS, DFS applications roots 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). 6/19/2021 Xiaojuan Cai 56
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. 6/19/2021 Xiaojuan Cai 57
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 6/19/2021 Xiaojuan Cai 60
Challenge 1 0 Problem. Is a graph bipartite? How difficult? • Any programmer could do it. • Need to be a typical diligent SE 222 student. • Hire an expert. • Intractable. • No one knows. • Impossible. 6/19/2021 Xiaojuan Cai 1 2 3 0 -1 0 -2 0 -5 0 -6 1 -3 2 -4 4 -5 4 -6 6 4 5 0 1 2 3 0 -1 0 -2 0 -5 0 -6 1 -3 2 -4 4 -5 4 -6 6 4 5 61
Challenge 1 0 Problem. Is a graph bipartite? How difficult? • Any programmer could do it. • Need to be a typical diligent SE 222 ✓ student. • Hire an expert. • Intractable. • No one knows. • Impossible. 6/19/2021 Xiaojuan Cai 1 2 3 0 -1 0 -2 0 -5 0 -6 1 -3 2 -4 4 -5 4 -6 6 4 5 0 1 2 3 0 -1 0 -2 0 -5 0 -6 1 -3 2 -4 4 -5 4 -6 6 4 5 62
Challenge 2 0 Problem. Find a cycle. 1 How difficult? • Any programmer could do it. • Need to be a typical diligent SE 222 ✓ student. • Hire an expert. • Intractable. • No one knows. • Impossible. 6/19/2021 Xiaojuan Cai 2 3 0 -1 0 -2 0 -5 0 -6 1 -3 2 -4 4 -5 4 -6 6 4 5 0 1 2 3 6 4 5 63
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 SE 222 ✓ student. • Hire an expert. • Intractable. • No one knows. • Impossible. 6/19/2021 Xiaojuan Cai 0 1 2 3 5 0 -1 -2 -3 -4 -2 -0 -6 -4 -5 -0 6 4 0 -1 0 -2 0 -5 0 -6 1 -2 2 -3 2 -4 3 -4 4 -5 4 -6 64
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 SE 222 student. • Hire an expert. • Intractable. ✓ • No one knows. • Impossible. 6/19/2021 Xiaojuan Cai 0 1 2 3 5 0 -5 -3 -4 -6 -2 -1 -0 6 4 0 -1 0 -2 0 -5 0 -6 1 -2 2 -6 3 -4 3 -5 4 -6 65
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 SE 222 student. • Hire an expert. • Intractable. • No one knows. ✓ • Impossible. 0 1 2 3 0 -1 0 -2 0 -5 0 -6 3 -4 3 -5 4 -6 6 4 5 0 -4 0 -5 0 -6 1 -4 1 -5 2 -4 3 -4 5 -6 3 2 4 6 1 5 0 6/19/2021 Xiaojuan Cai 0↔ 4, 1↔ 3, 2↔ 2, 3↔ 6, 4↔ 5, 5↔ 0, 6↔ 1 66
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 SE 222 student. • Hire an expert. ✓ • Intractable. • No one knows. • Impossible. 6/19/2021 Xiaojuan Cai 0 -1 0 -2 0 -5 0 -6 3 -4 3 -5 4 -6 1 2 0 6 3 4 5 0 1 2 3 6 4 5 67
- Undirected graph
- Underlying undirected graph
- Contoh graf berarah
- Undirected graph algorithms
- Syntax directed translation example
- Undirected
- Graph traversal in data structure
- Dfs in discrete mathematics
- Graph traversal methods
- Bfs tree
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Rooute finder
- Dari gambar diatas, yang termasuk trail adalah
- Properties of directed acyclic graph
- A vertex is if its indgree(v)=1 and outdegree(v)=0.
- G = (v, e)
- Graphs
- Algoritma traversal
- Post order traversal
- Binary tree traversal
- Tree traversal in data structure
- Inorder traversal visualization
- Nat traversal problem solution
- In order traversal
- Moxtra latency
- Bfs traversal
- Binary tree traversal techniques
- Disadvantages of threaded binary tree
- Inorder traversal
- Bellman ford algorithm complexity
- Nat type symmetric
- Balance factor of avl tree
- Iterative inorder
- Recover a tree from preorder traversal
- A(b-c) * (d-e)/(f(g-h))
- Pre order traversal
- Huffman tree visualization
- Traversal algorithm in distributed system
- Tree traversal
- Algoritma traversal
- The dom tree
- Problem tree powerpoint template
- Breadth first search
- Bvh traversal
- Inorder traversal
- Inorder traversal
- Pre order tree traversal
- Understanding the efficiency of ray traversal on gpus
- Euler tour traversal
- Preorder traversal with stack
- Algoritma graf
- Concept tree
- Nat
- Lcr is related to which tree traversal
- Graph neural network lecture
- Gene shuffling
- Self directed play
- Partitioning a segment formula
- Veterans directed home and community based care
- Force directed scheduling
- Sglib1
- Input string