searching on graphs
play

Searching on Graphs November 16, 2016 CMPE 250 Graphs- Searching on - PowerPoint PPT Presentation

Searching on Graphs November 16, 2016 CMPE 250 Graphs- Searching on Graphs November 16, 2016 1 / 78 Breadth-First and Depth-First Search CMPE 250 Graphs- Searching on Graphs November 16, 2016 2 / 78 Breadth-First Search(BFS) Basic Idea


  1. Searching on Graphs November 16, 2016 CMPE 250 Graphs- Searching on Graphs November 16, 2016 1 / 78

  2. Breadth-First and Depth-First Search CMPE 250 Graphs- Searching on Graphs November 16, 2016 2 / 78

  3. Breadth-First Search(BFS) – Basic Idea Given a graph with N vertices and a selected vertex A : for (i = 1; there are unvisited vertices ; i++) Visit all unvisited vertices at distance i (i is the length of the shortest path between A and currently processed vertices) Queue-based implementation CMPE 250 Graphs- Searching on Graphs November 16, 2016 3 / 78

  4. BFS – Algorithm Store source vertex S in a queue and mark as processed 1 while queue is not empty 2 Read vertex v from the queue for all neighbors w: If w is not processed Mark as processed Enqueue in the queue Record the parent of w to be v (necessary only if we need the shortest path tree) CMPE 250 Graphs- Searching on Graphs November 16, 2016 4 / 78

  5. BFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 5 / 78

  6. BFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 6 / 78

  7. BFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 7 / 78

  8. BFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 8 / 78

  9. BFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 9 / 78

  10. BFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 10 / 78

  11. BFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 11 / 78

  12. BFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 12 / 78

  13. BFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 13 / 78

  14. BFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 14 / 78

  15. BFS – Complexity Step 1 : read a node from the queue O ( | V | ) times. Step 2 : examine all neighbors, i.e. we examine all edges of the currently read node. Hence the complexity of BFS is O ( | V | + | E | ) Undirected graph: 2 × | E | edges to examine CMPE 250 Graphs- Searching on Graphs November 16, 2016 15 / 78

  16. Depth-First Search Procedure dfs(s) mark all vertices in the graph as not reached invoke scan(s) Procedure scan(s) mark and visit s for each neighbor w of s if the neighbor is not reached invoke scan(w) CMPE 250 Graphs- Searching on Graphs November 16, 2016 16 / 78

  17. Depth-First Search with Stack Initialization: mark all vertices as unvisited, visit(s) while the stack is not empty: pop (v,w) if w is not visited add (v,w) to tree T visit(w) Procedure visit(v) mark v as visited for each edge (v,w) push (v,w) in the stack CMPE 250 Graphs- Searching on Graphs November 16, 2016 17 / 78

  18. Recursive DFS procedure DepthFirst(Vertex v) visit(v); for each neighbor w of v if (w is not visited) add edge (v,w) to tree T DepthFirst(w) procedure visit(v) mark v as visited CMPE 250 Graphs- Searching on Graphs November 16, 2016 18 / 78

  19. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 19 / 78

  20. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 20 / 78

  21. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 21 / 78

  22. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 22 / 78

  23. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 23 / 78

  24. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 24 / 78

  25. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 25 / 78

  26. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 26 / 78

  27. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 27 / 78

  28. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 28 / 78

  29. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 29 / 78

  30. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 30 / 78

  31. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 31 / 78

  32. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 32 / 78

  33. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 33 / 78

  34. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 34 / 78

  35. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 35 / 78

  36. DFS-example CMPE 250 Graphs- Searching on Graphs November 16, 2016 36 / 78

  37. Example Adjacency lists 1: 2, 3, 4 2: 6, 3, 1 3: 1, 2, 6, 5, 4 4: 1, 3, 5 5: 3, 4 6: 2, 3 Depth first traversal: 1, 2, 6, 3, 5, 4 The particular order is dependent on the order of nodes in the adjacency lists CMPE 250 Graphs- Searching on Graphs November 16, 2016 37 / 78

  38. Applications of Depth-First Search Graph Connectivity Connectivity Biconnectivity Articulation Points and Bridges Connectivity in Directed Graphs CMPE 250 Graphs- Searching on Graphs November 16, 2016 38 / 78

  39. Connectivity Definition: An undirected graph is said to be connected if for any pair of nodes of the graph, the two nodes are reachable from one another (i.e. there is a path between them). If starting from any vertex we can visit all other vertices, then the graph is connected CMPE 250 Graphs- Searching on Graphs November 16, 2016 39 / 78

  40. Biconnectivity A graph is biconnected, if there are no vertices whose removal will disconnect the graph. not biconnected biconnected CMPE 250 Graphs- Searching on Graphs November 16, 2016 40 / 78

  41. Articulation Points Definition: A vertex whose removal makes the graph disconnected is called an articulation point or cut-vertex C is an articulation point We can compute articulation points using depth-first search and a special numbering of the vertices in the order of their visiting. CMPE 250 Graphs- Searching on Graphs November 16, 2016 41 / 78

  42. Bridges Definition: An edge in a graph is called a bridge, if its removal disconnects the graph. Any edge in a graph, that does not lie on a cycle, is a bridge. Obviously, a bridge has at least one articulation point at its end, however an articulation point is not necessarily linked in a bridge. (C,D) and (E,D) are bridges CMPE 250 Graphs- Searching on Graphs November 16, 2016 42 / 78

  43. Example 1 C is an articulation point, there are no bridges CMPE 250 Graphs- Searching on Graphs November 16, 2016 43 / 78

  44. Example 2 C is an articulation point, (C,B) is a bridge CMPE 250 Graphs- Searching on Graphs November 16, 2016 44 / 78

  45. Example 3 B and C are articulation points, (B,C) is a bridge CMPE 250 Graphs- Searching on Graphs November 16, 2016 45 / 78

  46. Example 4 B and C are articulation points. All edges are bridges CMPE 250 Graphs- Searching on Graphs November 16, 2016 46 / 78

  47. Example 5 Biconnected graph - no articulation points and no bridges CMPE 250 Graphs- Searching on Graphs November 16, 2016 47 / 78

  48. Connectivity in Directed Graphs (I) Definition: A directed graph is said to be strongly connected if for any pair of nodes there is a path from each one to the other CMPE 250 Graphs- Searching on Graphs November 16, 2016 48 / 78

  49. Connectivity in Directed Graphs (II) Definition: A directed graph is said to be unilaterally connected if for any pair of nodes at least one of the nodes is reachable from the other Each strongly connected graph is also unilaterally connected. CMPE 250 Graphs- Searching on Graphs November 16, 2016 49 / 78

  50. Shortest Paths CMPE 250 Graphs- Searching on Graphs November 16, 2016 50 / 78

  51. Unweighted Directed Graphs What is the shortest path from V3 to V5? CMPE 250 Graphs- Searching on Graphs November 16, 2016 51 / 78

  52. Problem Data The problem: Given a source vertex s , find the shortest path to all other vertices. Data structures needed: Graph representation: Adjacency lists / adjacency matrix Distance table: distances from source vertex paths from source vertex CMPE 250 Graphs- Searching on Graphs November 16, 2016 52 / 78

  53. Problem Data Adjacency lists : Let s = V3, stored in a queue V1: V2, V4 Initialized distance table: V2: V4, V5 V3: V1, V6 Vertex Distance Parent V4: V3, V5, V6, V7 V1 -1 -1 V5: V7 V2 -1 -1 V6: - V3 0 0 V7: V6 V4 -1 -1 V5 -1 -1 V6 -1 -1 CMPE 250 Graphs- Searching on Graphs November 16, 2016 53 / 78

  54. Breadth-first search in graphs Take a vertex and examine all adjacent vertices. Do the same with each of the adjacent vertices . CMPE 250 Graphs- Searching on Graphs November 16, 2016 54 / 78

  55. Algorithm Store s in a queue, and initialize distance = 0 in the Distance Table 1 While there are vertices in the queue: 2 Read a vertex v from the queue For all adjacent vertices w : If distance = -1 (not computed) Distance = (distance to v) + 1 Parent = v Append w to the queue CMPE 250 Graphs- Searching on Graphs November 16, 2016 55 / 78

  56. Complexity Matrix representation: O ( | V | 2 ) Adjacency lists - O ( | E | + | V | ) We examine all edges ( O ( | E | ) ), and we store in the queue each vertex only once ( O ( | V | ) ). CMPE 250 Graphs- Searching on Graphs November 16, 2016 56 / 78

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend