7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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 Searching CSE 373 Data Structures Lecture 20.
Breadth-First and Depth-First Search
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
CS261 Data Structures DFS and BFS – Edge List Representation.
IS 2610: Data Structures Graph April 5, 2004.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Search Computing 2 COMP s1. P ROBLEMS ON G RAPHS What kinds of problems do we want to solve on/via graphs? Is there a simple path from A to.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Depth-First Search Lecture 21: Graph Traversals
– Graphs 1 Graph Categories Strong Components Example of Digraph
Contest Algorithms January 2016 Introduce the main kinds of graphs, discuss two implementation approaches, and remind you about trees 6. Intro. to Graphs.
Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.
Graphs and Paths : Chapter 15 Saurav Karmakar
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
CS 367 Introduction to Data Structures Lecture 13.
Contest Algorithms January 2016 Describe a MST and the Prim and Kruskal algorithms for generating one. 8. Minimal Spanning Trees (MSTs) 1Contest Algorithms:
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
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,
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Breadth-First Search (BFS)
Graphs A New Data Structure
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs Graph transversals.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Search Related Algorithms
Chapter 11 Graphs.
CSE 373 Data Structures Lecture 16
Algorithms: Design and Analysis
Spanning Trees Longin Jan Latecki Temple University based on slides by
Presentation transcript:

7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7. Graph Traversal

Overview Graph Traversal Depth First Search (DFS) Uses of DFS cycle detection, reachability, topological sort Breadth-first Search (BFS) DFS vs. BFS

1. Graph Traversal (Visiting/Search) Given: a graph G = (V, E), directed or undirected Goal: visit every vertex once

Some Binary Tree Visit Orderings easy to implement using DFS

Uses of Traversal Orders in Binary Trees Pre-order traversal: duplicate a tree, create a prefix expression (Polish notation) 5 − (6 × 7)can be written in Polish notation as − 5 × 6 7 In-order traversal : visit nodes in a binary search trees in order Post-order traversal : delete a tree, create a postfix expression (Reverse Polish notation), topological sorting 5 + ((1 + 2) × 4) − 3 can be written down in RPN as 5 1 2 + 4 × + 3 − Contest Algorithms: 7. Graph Traversal

Some Graph Visit Orderings Preorder: Postorder: easy to implement using DFS

From Graph to Spanning Tree The edges used to visit all the vertices in a graph will form a spanning tree the tree includes every vertex, but not all the graph's edges Note: we might build a spanning forest if the graph is not connected Contest Algorithms: 7. Graph Traversal

Example search then build a spanning tree (or trees)

Two Main Traversal Algorithms Running time for both: O(V + E) Depth First Search (DFS) Usually implemented using recursion More natural / commonly used Breadth First Search (BFS) Usually implemented using a queue (+ map) Can solve shortest paths problem Uses more memory for larger graphs Contest Algorithms: 4. Backtracking

2. Depth First Search (DFS) DFS is “depth first” because it always fully explores down a path away from a vertex v before it looks at other paths leaving v. Crucial DFS properties: uses recursion: essential for graph structures choice: at a vertex there may be a choice of several edges to follow to the next vertex backtracking: "return to where you came from" avoid cycles by grouping vertices into visited and unvisited

Directed Graph Example DFS works with directed and undirected graphs. 1 3 directed, unweighted Graph G 4 5 2

Data Format (unweighted) no. of vertices multiple lines, one for each vertex each line: no. of neighbour vertices, list of vertices no weights are included; they're assumed to be 0 e.g. 6 2 1 3 2 2 3 1 1 3 2 4 5 1 2 1 3 4 5 2 see dfsData.java Contest Algorithms: 4. Backtracking

Code see UseDFS.java private static ArrayList< ArrayList<IPair>> adjList; private static ArrayList<Boolean> visited; // different orderings of vertices private static Queue<Integer> pre = new LinkedList<Integer>(); private static Queue<Integer> post = new LinkedList<Integer>(); private static Stack<Integer> revPost = new Stack<Integer>(); public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java UseDFS <data-file>"); return; } adjList = new ArrayList<>(); visited = new ArrayList<Boolean>(); : Contest Algorithms: 4. Backtracking

System.out.println("Reading graph data from \"" + args[0] + "\""); Scanner sc = new Scanner(new File(args[0])); // build adj list int numVs = sc.nextInt(); for (int i = 0; i < numVs; i++) { ArrayList<IPair> neighbors = new ArrayList<>(); int numNs = sc.nextInt(); for (int j = 0; j < numNs; j++) neighbors.add( new IPair(sc.nextInt(), 0) ); // (id,weight) pair // all the weights are 0 to represent an unweighted graph adjList.add(neighbors); } visited.addAll( Collections.nCopies(numVs, false)); // not visited : Contest Algorithms: 4. Backtracking

int numConnects = 0; for (int i = 0; i < numVs; i++) // for each vertex if (!visited.get(i)) { numConnects++; System.out.print("Vertex " + i + " can visit:"); dfsVisit(i); System.out.println(); } System.out.println(numConnects + " connected components"); System.out.println("Pre-order: " + pre); System.out.println("Post-order: " + post); System.out.println("Reverse Post-order: " + revPost); } // end of main() Contest Algorithms: 4. Backtracking

Recursive DFS Code private static void dfsVisit(int u) { System.out.print(" " + u); // this vertex is visited pre.add(u); visited.set(u, true); for (IPair v : adjList.get(u)) { // try all neighbors of vertex u if (!visited.get(v.getX()) ) // avoid cycle dfsVisit(v.getX()); // visit v } post.add(u); revPost.push(u); } // end of dfsVisit() Contest Algorithms: 4. Backtracking

Compilation & Execution Contest Algorithms: 4. Backtracking

Calling dfsVisit(0) call it d(0) for short Call Visited d(0) {0} d(0)-d(1) {0,1} d(0)-d(1)-d(2) {0,1,2} Skip 1, return to d(1) d(0)-d(1)-d(3) {0,1,2,3} Skip 2 d(0)-d(1)-d(3)-d(4) {0,1,2,3,4} Skip 2, return to d(3) continued

d(0)-d(1)-d(3)-d(5) {0,1,2,3,4,5} Skip 2, return to d(3) d(0)-d(1)-d(3) {0,1,2,3,4,5} Return to d(1) d(0)-d(1) {0,1,2,3,4,5} Return to d(0) d(0) {0,1,2,3,4,5} Skip 3, return

DFS Spanning Tree Since nodes are marked, the graph is searched as if it were a tree: A spanning tree is a subgraph of a graph G which contains all the verticies of G. 1 3 2 4 5 c

Example 2 see dfsData1.java 9 1 1 3 0 2 3 2 1 3 3 1 2 4 1 3 2 7 8 1 6 1 3 4 5 2 7 6 8 I've represented each undirected edge as two directed edges, so I can use the same daa structures as before. Contest Algorithms: 4. Backtracking

Execution Contest Algorithms: 4. Backtracking

3. Uses of DFS Finding cycles in a graph e.g. for finding recursion in a call graph Searching complex locations, such as mazes Reachability detection i.e. can a vertex v be reached from vertex u? useful for e-mail routing; path finding Strong connectivity Implermenting preorder, postorder traversals Topological sorting continued

Reachability DFS tree rooted at v: what are the vertices reachable from v via directed paths? E D C start at C E D A C F E D A B C F A B start at B

Strong Connectivity Each vertex can reach all other vertices a g c d e b e f g Graphs

Strong Connectivity Algorithm Pick a vertex v in G. Perform a DFS from v in G. If there’s a vertex not visited, print “no”. Let G’ be G with edges reversed. Perform a DFS from v in G’. If there’s a vertex not visited, print “no” If the algorithm gets here, print “yes”. Running time: O(V+E). a G: g c d e b f a G’: g c d e b f

Strongly Connected Components List all the subgraphs where each vertex can reach all the other vertices in that subgraph. Can also be done in O(V+E) time using DFS. a d c b e f g { a , c , g } { f , d , e , b }

Implementing Traversal Orders The different visit orders can be easily implemented by storing the nodes visited by the DFS in various kinds of data structures: Preorder: Put the vertex on a queue before the dfsVisit() recursive call Postorder: Put the vertex on a queue after the dfsVisit() recursive call Reverse postorder: Put the vertex on a stack after the dfsVisit() recursive call

TopoSort: Getting Dressed Underwear Socks Watch Trousers Shoes Shirt Belt Tie one topological sort (not unique) Jacket Socks Underwear Trousers Shoes Watch Shirt Belt Tie Jacket

A topological sort (toposort) of a DAG is a linear ordering of its vertices such that for every directed edge u  v, u comes before v in the ordering. Any DAG has at least one topological sort (and often many). Various toposorts can be generated using DFS: toposort == reverse post-order, reverse pre-order

Multiple Possible Toposorts Contest Algorithms: 4. Backtracking

Graph Visit Orderings Example Postorder: Reverse postorder: 4 same as a topological sort

TopoSort Example (fig 4.4 CP) 8 2 1 2 2 2 3 2 3 5 1 4 1 6 A DAG see dfsData2.java Contest Algorithms: 4. Backtracking

Execution since this graph is a DAG, this is also a topological sort Contest Algorithms: 4. Backtracking

4. Breadth-first Search (BFS) Process all the verticies at a given level before moving to the next level. Example: 1 2 3 undirected, unweighted 4 5 7 6

Advantage of BFS over DFS Can be used to find the shortest paths from the starting vertex to the other vertices in the graph. Contest Algorithms: 4. Backtracking

Informal Algorithm 1) Put the verticies into an ordering e.g. {0, 1, 2, 3, 4, 5, 6, 7} 2) Select a vertex, add it to the spanning tree T: e.g. 0 3) Add to T all edges (0,X) and X verticies that do not create a cycle in T i.e. (0,1), (0,2), (0,6) T = {0,1, 2, 6} 1 2 6 continued

Repeat step 3 on the verticies just added, these are on level 1 i.e. 1: add (1,3) 2: add (2,4) 6: nothing T = {0, 1, 2, 3, 4, 6} Repeat step 3 on the verticies just added, these are on level 2 i.e. 3: add (3, 5) 4: nothing T = {0, 1, 2, 3, 4, 5, 6} level 1 1 2 6 3 4 1 2 6 level 2 3 4 5 continued

Repeat step 3 on the verticies just added, these are on level 3 Repeat step 3 on the verticies just added, these are on level 3 i.e. 5: add (5,7) T = {0, 1, 2, 3, 4, 5, 6, 7} Repeat step 3 on the verticies just added, these are on level 4 i.e. 7: nothing, so stop 1 2 6 3 4 level 3 5 7 continued

Resulting spanning tree: 1 2 3 4 5 7 6

Data Format (undirected, unweighted) no. of vertices, no. of edges pairs of numbers: one pair is an edge (a b) which must be recorded twice in the list: once for a --> b, once for b --> a the last number is the start node for the search e.g. 8 11 0 1 0 2 0 6 1 3 1 6 2 3 2 4 3 5 4 5 4 6 5 7 This is different from UseAdjList.java in part 5, which built a directed weighted graph This represents the graph on the previous slide. see bfsData.java Contest Algorithms: 4. Backtracking

Code public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java UseBFS <data-file>"); return; } Scanner sc = new Scanner(new File(args[0])); int numVs = sc.nextInt(); int numEs = sc.nextInt(); ArrayList<ArrayList<IPair>> adjList = new ArrayList<>(); for (int i = 0; i < numVs; i++) { ArrayList<IPair> neighbors = new ArrayList<IPair>(); adjList.add(neighbors); // add neighbor list to Adjacency List for (int i = 0; i < numEs; i++) { int a = sc.nextInt(); int b = sc.nextInt(); adjList.get(a).add(new IPair(b, 0)); // a --> b adjList.get(b).add(new IPair(a, 0)); // b --> a int vStart = sc.nextInt(); // start searching here : see UseBFS.java Contest Algorithms: 4. Backtracking

ArrayList<Integer> distTo = new ArrayList<Integer>(); // for recording distance from start vertex to other vertices distTo.addAll(Collections.nCopies(numVs, -1)); distTo.set(vStart, 0); // start from source ArrayList<Integer> pathTo = new ArrayList<Integer>(); // for recording path info from start vertex to other vertices pathTo.addAll(Collections.nCopies(numVs, -1)); Queue<Integer> q = new LinkedList<Integer>(); q.offer(vStart); while (!q.isEmpty()) { int u = q.poll(); for (IPair v : adjList.get(u)) { // for each neighbour of u if (distTo.get(v.getX()) == -1) { // if v not visited distTo.set(v.getX(), distTo.get(u)+1); // then v is reachable from u q.offer(v.getX()); pathTo.set(v.getX(), u); // path to v is u } Contest Algorithms: 4. Backtracking

System.out.println("\nShortest paths from vertex " + vStart + " to:"); for(int i=0; i < numVs; i++) { System.out.print(" " + i + " (" + distTo.get(i) + "): "); showPath(i, vStart, pathTo); System.out.println(); } } // end of main() Contest Algorithms: 4. Backtracking

private static void showPath(int u, int vStart, ArrayList<Integer> pathTo) { if (u == vStart) { System.out.print(u); return; } showPath(pathTo.get(u), vStart, pathTo); System.out.print(" -> " + u); } // end of showPath() Contest Algorithms: 4. Backtracking

Example see bfsData.java 8 11 0 1 0 2 0 6 1 3 1 6 2 3 2 4 3 5 4 5 4 6 8 11 0 1 0 2 0 6 1 3 1 6 2 3 2 4 3 5 4 5 4 6 5 7 1 2 3 4 5 7 6 Contest Algorithms: 4. Backtracking

Execution Contest Algorithms: 4. Backtracking

Example (fig 4.3 CP) see bfsData1.java 13 16 0 1 1 2 2 3 0 4 0 1 1 2 2 3 0 4 1 5 2 6 3 7 5 6 4 8 8 9 5 10 6 11 7 12 9 10 10 11 11 12 5 Contest Algorithms: 4. Backtracking

Execution Contest Algorithms: 4. Backtracking