Lecture 6 Graph Traversal

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
1 Data Structures DFS, Topological Sort Dana Shapira.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
CSC 331: Algorithm Analysis Decompositions of Graphs.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Topological Sort: Definition
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
Lecture 7 Graph Traversal
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
CS138A Elementary Graph Algorithms Peter Schröder.
Graph Search Applications, Minimum Spanning Tree
Introduction to Algorithms
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs A New Data Structure
Graphs Chapter 20.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Chapter 3. Decompositions of Graphs
Lecture 11 Graph Algorithms
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Lecture 12 Graph Algorithms
CS200: Algorithm Analysis
CS120 Graphs.
Graph Algorithms Using Depth First Search
Graph.
Graph Search Applications
Graphs Graph transversals.
Elementary Graph Algorithms
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Advanced Algorithms Analysis and Design
Search Related Algorithms
Graph Representation (23.1/22.1)
Graphs.
Chapter 22: Elementary Graph Algorithms
Depth-First Search D B A C E Depth-First Search Depth-First Search
Richard Anderson Autumn 2016 Lecture 5
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Richard Anderson Winter 2009 Lecture 6
Algorithms: Design and Analysis
Important Problem Types and Fundamental Data Structures
3.2 Graph Traversal.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Lecture 11 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some.
Presentation transcript:

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