Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

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

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

4 Some Binary Tree Visit Orderings
easy to implement using DFS

5 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 × + 3 − Contest Algorithms: 7. Graph Traversal

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

7 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

8 Example search then build a spanning tree (or trees)

9 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

10 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

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

12 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 1 1 1 2 1 3 4 5 2 see dfsData.java Contest Algorithms: 4. Backtracking

13 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

14 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

15 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

16 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

17 Compilation & Execution
Contest Algorithms: 4. Backtracking

18 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

19 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

20 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

21 Example 2 see dfsData1.java 9 1 1 2 1 3 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

22 Execution Contest Algorithms: 4. Backtracking

23 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 routing; path finding Strong connectivity Implermenting preorder, postorder traversals Topological sorting continued

24 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

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

26 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

27 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 }

28 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

29 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

30 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

31 Multiple Possible Toposorts
Contest Algorithms: 4. Backtracking

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

33 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

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

35 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

36 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

37 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

38 Repeat step 3 on the verticies just added, these are on level 1
i.e. 1: add (1,3) : add (2,4) : 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) : 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

39 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

40 Resulting spanning tree:
1 2 3 4 5 7 6

41 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 3 5 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

42 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

43 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

44 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

45 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

46 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 3 5 5 7 1 2 3 4 5 7 6 Contest Algorithms: 4. Backtracking

47 Execution Contest Algorithms: 4. Backtracking

48 Example (fig 4.3 CP) see bfsData1.java 13 16 0 1 1 2 2 3 0 4
5 Contest Algorithms: 4. Backtracking

49 Execution Contest Algorithms: 4. Backtracking


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

Similar presentations


Ads by Google