Lecture 7 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

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
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.
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
Graphs – Depth First Search ORD DFW SFO LAX
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Chapter 8, Part I Graph Algorithms.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
CSC 331: Algorithm Analysis Decompositions of Graphs.
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
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,
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Topological Sort: Definition
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
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)
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
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,
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Breadth-First Search (BFS)
Lecture 11 Graph Algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Lecture 12 Graph Algorithms
Graph Search Applications
Lecture 10 Algorithm Analysis
Richard Anderson Autumn 2016 Lecture 5
Lecture 6 Graph Traversal
Lecture 11 Graph Algorithms
Presentation transcript:

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

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

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

Graph everywhere 4/28/2017 Xiaojuan Cai

Graph terminology Undirected graph Directed graph 4/28/2017 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/28/2017 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/28/2017 Xiaojuan Cai

Where are we? Graph Undirected graph DFS, BFS, Application 4/28/2017 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/28/2017 Xiaojuan Cai

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

4/28/2017 Xiaojuan Cai

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

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

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

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

DFS application? 4/28/2017 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/28/2017 Xiaojuan Cai

4/28/2017 Xiaojuan Cai

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

Complexity Time Space Undirected Adj. Matrix O(|V|2) O(|V|) Adj. List O(|V| + 2|E|) 4/28/2017 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/28/2017 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 u v x y w z a b BFS tree u u v y y parent w z 4/28/2017 Xiaojuan Cai

4/28/2017 Xiaojuan Cai

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

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

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

Where are we? Graph Undirected graph DFS, BFS, Application 4/28/2017 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/28/2017 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/28/2017 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/28/2017 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/28/2017 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/28/2017 Xiaojuan Cai

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

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

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

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

Strong connected components 4/28/2017 Xiaojuan Cai

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

Strong connected components Lemma Every directed graph is a DAG of its strongly connected component. 4/28/2017 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/28/2017 Xiaojuan Cai

Kosaraju-Sharir algorithm Reversed graph 4/28/2017 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/28/2017 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

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

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

BFS, DFS applications 4/28/2017 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/28/2017 Xiaojuan Cai

BFS, DFS applications Vertex: pixel. Edge: between two adjacent gray pixels. Blob: all pixels connected to given pixel. 4/28/2017 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/28/2017 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/28/2017 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/28/2017 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/28/2017 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/28/2017 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/28/2017 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/28/2017 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/28/2017 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/28/2017 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/28/2017 Xiaojuan Cai