1 / 222

Graph Search Algorithms

Graph Search Algorithms. Graph. a. b. Node ~ city or computer. Edge ~ road or network. c. Undirected or Directed. A surprisingly large number of computational problems can be expressed as graph problems. A directed graph G = ( V, E ), where V = {1,2,3,4,5,6} and

ghensley
Download Presentation

Graph Search Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graph Search Algorithms

  2. Graph a b Node ~ city or computer Edge ~ road or network c Undirected or Directed A surprisingly large number of computational problems can be expressed as graph problems. COSC 3101B, PROF. J. ELDER

  3. A directed graph G = (V, E), where V = {1,2,3,4,5,6} and • E = {(1,2), (2,2), (2,4), (2,5), (4,1), (4,5), (5,4), (6,3)}. • The edge (2,2) is a self-loop. (b) An undirected graph G = (V,E), where V = {1,2,3,4,5,6} and E = {(1,2), (1,5), (2,5), (3,6)}. The vertex 4 is isolated. (c) The subgraph of the graph in part (a) induced by the vertex set {1,2,3,6}. Directed and Undirected Graphs COSC 3101B, PROF. J. ELDER

  4. Trees Tree Forest Graph with Cycle COSC 3101B, PROF. J. ELDER

  5. Running Time of Graph Algorithms • Running time often a function of both |V| and |E|. • For convenience, drop the | . | in asymptotic notation, e.g. O(V+E). COSC 3101B, PROF. J. ELDER

  6. Representations: Undirected Graphs Adjacency Matrix Adjacency List COSC 3101B, PROF. J. ELDER

  7. Representations: Directed Graphs Adjacency Matrix Adjacency List COSC 3101B, PROF. J. ELDER

  8. Breadth-First Search • Goal: To recover the shortest paths from a source node s to all other reachable nodes v in a graph. • The length of each path and the paths themselves are returned. • Note: This problem is harder for general graphs than trees because of cycles! ? s COSC 3101B, PROF. J. ELDER

  9. Breadth-First Search • Idea: send out search ‘wave’ from s. • Keep track of progress by colouring vertices: • Undiscovered vertices are coloured black • Just discovered vertices (on the wavefront) are coloured red. • Previously discovered vertices (behind wavefront) are coloured grey. COSC 3101B, PROF. J. ELDER

  10. BFS FoundNot HandledQueue s b a e d g c f j i h m k l COSC 3101B, PROF. J. ELDER

  11. BFS FoundNot HandledQueue d=0 s b a s d=0 e d g c f j i h m k l COSC 3101B, PROF. J. ELDER

  12. BFS FoundNot HandledQueue d=0 s d=1 b a d=0 a e d=1 d d g g c f b j i h m k l COSC 3101B, PROF. J. ELDER

  13. BFS FoundNot HandledQueue d=0 s d=1 b a a d=1 d e d g g b c f j i h m k l COSC 3101B, PROF. J. ELDER

  14. BFS FoundNot HandledQueue d=0 s d=1 b a d=1 d e d g g b c f c d=2 j f d=2 i h m k l COSC 3101B, PROF. J. ELDER

  15. BFS FoundNot HandledQueue d=0 s d=1 b a d=1 e d g g b c f c d=2 j f d=2 m i e h m k l COSC 3101B, PROF. J. ELDER

  16. BFS FoundNot HandledQueue d=0 s d=1 b a d=1 e d g b c f c d=2 j f d=2 m i e h j m k l COSC 3101B, PROF. J. ELDER

  17. BFS FoundNot HandledQueue d=0 s d=1 b a d=1 e d g c f c d=2 j f d=2 m i e h j m k l COSC 3101B, PROF. J. ELDER

  18. BFS FoundNot HandledQueue d=0 s d=1 b a d=2 c e d f m g e c f j j d=2 i h m k l COSC 3101B, PROF. J. ELDER

  19. BFS FoundNot HandledQueue d=0 s d=1 b a d=2 e d f m g e c f j j h d=3 d=2 i i h m d=3 k l COSC 3101B, PROF. J. ELDER

  20. BFS FoundNot HandledQueue d=0 s d=1 b a d=2 e d m g e c f j j h d=3 d=2 i i h m d=3 k l COSC 3101B, PROF. J. ELDER

  21. BFS FoundNot HandledQueue d=0 s d=1 b a d=2 e d g e c f j j h d=3 d=2 i i h l m d=3 k l COSC 3101B, PROF. J. ELDER

  22. BFS FoundNot HandledQueue d=0 s d=1 b a d=2 e d g c f j j h d=3 d=2 i i h l m d=3 k l COSC 3101B, PROF. J. ELDER

  23. BFS FoundNot HandledQueue d=0 s d=1 b a d=2 e d g c f j h d=3 d=2 i i h l m d=3 k l COSC 3101B, PROF. J. ELDER

  24. BFS FoundNot HandledQueue d=0 s d=1 b a d=3 h i e d l g c f j d=2 i h m d=3 k l COSC 3101B, PROF. J. ELDER

  25. BFS FoundNot HandledQueue d=0 s d=1 b a d=3 i e d l g d=4 k c f j d=2 i h m d=3 k d=4 l COSC 3101B, PROF. J. ELDER

  26. BFS FoundNot HandledQueue d=0 s d=1 b a d=3 e d l g d=4 k c f j d=2 i h m d=3 k d=4 l COSC 3101B, PROF. J. ELDER

  27. BFS FoundNot HandledQueue d=0 s d=1 b a d=3 e d g d=4 k c f j d=2 i h m d=3 k d=4 l COSC 3101B, PROF. J. ELDER

  28. BFS FoundNot HandledQueue d=0 s d=1 b a d=4 k e d g c f j d=2 i h m d=3 k d=4 l COSC 3101B, PROF. J. ELDER

  29. BFS FoundNot HandledQueue d=0 s d=1 b a d=4 e d=5 d g c f j d=2 i h m d=3 k d=4 l COSC 3101B, PROF. J. ELDER

  30. Each vertex assigned finite d value at most once. d values assigned are monotonically increasing over time. Q contains vertices with d values {i, …, i, i+1, …, i+1} BLACK RED BLACK RED GRAY Breadth-First Search Algorithm COSC 3101B, PROF. J. ELDER

  31. Correctness v & there is an edge from u to v Basic Steps: s d u The shortest path to uhas length d There is a path to v with length d+1. COSC 3101B, PROF. J. ELDER

  32. Correctness • Vertices are discovered in order of their distance from the source vertex s. • When we discover v, how do we know there is not a shorter path to v? • Because if there was, we would already have discovered it! s v d u COSC 3101B, PROF. J. ELDER

  33. Correctness Two-step proof: COSC 3101B, PROF. J. ELDER

  34. End of Lecture 13 October 19, 2006

  35. BLACK RED BLACK RED GRAY COSC 3101B, PROF. J. ELDER

  36. s v u COSC 3101B, PROF. J. ELDER

  37. BLACK RED w GRAY State when u is dequeued: s v u COSC 3101B, PROF. J. ELDER

  38. BLACK RED BLACK RED GRAY Progress? • On every step at least one vertex is processed (turns gray). COSC 3101B, PROF. J. ELDER

  39. BLACK RED BLACK RED GRAY Running Time COSC 3101B, PROF. J. ELDER

  40. Recovering the Shortest Path For each node v, store predecessor of v in (v). s v u (v) (v) = u. Predecessor of v is COSC 3101B, PROF. J. ELDER

  41. Recovering the Shortest Path COSC 3101B, PROF. J. ELDER

  42. Colours are actually not required COSC 3101B, PROF. J. ELDER

  43. Depth First Search (DFS) • Idea: • Continue searching “deeper” into the graph, until we get stuck. • If all the edges leaving v have been explored we “backtrack” to the vertex from which v was discovered. • Does not recover shortest paths, but can be useful for extracting other properties of graph, e.g., • Topological sorts • Detection of cycles • Extraction of strongly connected components COSC 3101B, PROF. J. ELDER

  44. Depth-First Search • Explore every edge, starting from different vertices if necessary. • As soon as vertex discovered, explore from it. • Keep track of progress by colouring vertices: • Black: undiscovered vertices • Red: discovered, but not finished (still exploring from it) • Gray: finished (found everything reachable from it). COSC 3101B, PROF. J. ELDER

  45. FoundNot HandledStack <node,# edges> f d / / / / / / / / / / / / / / DFS s b a e d g c f j i h m k l COSC 3101B, PROF. J. ELDER

  46. FoundNot HandledStack <node,# edges> 1/ / / / / / / / / / / / / / DFS s b a e d g c f j i s,0 h m k l COSC 3101B, PROF. J. ELDER

  47. FoundNot HandledStack <node,# edges> 1/ / 2/ / / / / / / / / / / / DFS s b a e d g c f j i a,0 s,1 h m k l COSC 3101B, PROF. J. ELDER

  48. FoundNot HandledStack <node,# edges> 1/ / 2/ / / 3/ / / / / / / / / DFS s b a e d g c f j c,0 i a,1 s,1 h m k l COSC 3101B, PROF. J. ELDER

  49. FoundNot HandledStack <node,# edges> 1/ / 2/ / / 3/ / / / / 4/ / / / DFS s b a e d g c f h,0 j c,1 i a,1 s,1 h m k l COSC 3101B, PROF. J. ELDER

  50. FoundNot HandledStack <node,# edges> 1/ / 2/ / / 3/ / / / / 4/ / 5/ / DFS s b a e d g c k,0 f h,1 j c,1 i a,1 s,1 h m k l COSC 3101B, PROF. J. ELDER

More Related