1 / 74

Graphs and Paths Presentation: Part II - Graph Representation and BFS & DFS Algorithms

Learn about different ways to represent graphs and how to perform Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms. This presentation includes examples and explanations of adjacency lists, adjacency matrix, running time analysis, and shortest-path tree.

jasonhart
Download Presentation

Graphs and Paths Presentation: Part II - Graph Representation and BFS & DFS 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. Graphs & Paths Presentation : Part II

  2. Graph representation • Given graph G = (V, E). • May be either directed or undirected. • Two common ways to represent for algorithms: • Adjacency lists. • Adjacency matrix. • When expressing the running time of an algorithm, it’s often in terms of both |V| and |E|.

  3. Adjacency lists • Array Adj of |V| lists, one per vertex. • Vertex u’s list has all vertices v such that (u, v) ∈ E. (Works for both directed and undirected graphs.)

  4. Example: • For an undirected graph:

  5. Example • For a directed graph: Same asymptotic space and time.

  6. Adjacency matrix

  7. Adjacency matrix

  8. Breadth-first search • Input: Graph G = (V, E), either directed or undirected, and source vertex s ∈ V. • Output: d[v] = distance (smallest # of edges) from s to v, for all v ∈ V.

  9. Idea • Idea: Send a wave out from s. • First hits all vertices 1 edge from s. • From there, hits all vertices 2 edges from s. • Etc. • Use FIFO queue Q to maintain wavefront. • v ∈ Q if and only if wave has hit v but has not come out of v yet.

  10. Breadth-First Search

  11.        Breadth-First Search r s t u v w x y

  12.   0     Breadth-First Search r s t u v w x y Q s 0

  13. 1   0 1    Breadth-First Search r s t u v w x y Q r w 1 1

  14. 1  2 0 1    Breadth-First Search r s t u v w x y Q w v 1 2

  15. 1  2 0 1 2  2 Breadth-First Search r s t u v w x y Q v t x 2 2 2

  16. 1  2 0 1 2  2 Breadth-First Search r s t u v w x y Q t x 2 2

  17. 1 3 2 0 1 2  2 Breadth-First Search r s t u v w x y Q x u 2 3

  18. 1 3 2 0 1 2 3 2 Breadth-First Search r s t u v w x y Q u y 3 3

  19. 1 3 2 0 1 2 3 2 Breadth-First Search r s t u v w x y Q y 3

  20. 1 3 2 0 1 2 3 2 Breadth-First Search r s t u v w x y Q 

  21. Breadth-First Search • Run-time = O(V+E) • Shortest-path tree • the final weight is the minimum distance • keep predecessors and get the shortest path • all BFS shortest paths make a tree

  22. Breadth-First SearchExample

  23. Breadth-First Search • Since each vertex gets a finite d value at most once, values assigned to vertices are monotonically increasing over time. • Time = O(V + E). • O(V) because every vertex enqueued at most once. • O(E) because every vertex dequeued at most once and we examine (u, v) is in E only when u is dequeued. Therefore, every edge examined at most once if directed, at most twice if undirected.

  24. Depth-first search • Input: G = (V, E), directed or undirected. No source vertex given! • Output: 2 timestamps on each vertex: • d[v] = discovery time • f [v] = finishing time

  25. Depth-first search • Will methodically explore every edge. • Start over from different vertices as necessary. • As soon as we discover a vertex, explore from it. • Unlike BFS, which puts a vertex on a queue so that we explore from it later. • As DFS progresses, every vertex has a color: • WHITE = undiscovered • GRAY = discovered, but not finished (not done exploring from it) • BLACK = finished (have found everything reachable from it) • Discovery and finish times: Unique integers from 1 to 2|V|. • For all v, d[v] < f [v]. • In other words, 1 ≤ d[v] < f [v] ≤ 2|V|.

  26. Depth-First Search • Methodically explore all vertices and edges All vertices are White, t = 0 For each vertex u do if u is White then Visit(u) Procedure Visit(u) color u Gray; d[u]  tt +1 for each v adjacent to u do if v is White then Visit(v) color u Black f [u]  tt +1 Gray vertices = stack of recursive calls

  27. 3 6 8 7 2 4 1 5 Depth-First Search

  28. 3 6 8 7 2 1 4 5 Depth-First Search 1

  29. 3 6 8 7 1 4 5 Depth-First Search 1 2 2

  30. 3 6 8 7 1 5 4 Depth-First Search 1 3 2 2

  31. 3 6 8 7 1 5 4 Depth-First Search 1 3 2 2

  32. 3 6 8 7 1 5 4 Depth-First Search 1 3 2 2

  33. 3 6 8 7 1 5 4 2 Depth-First Search 1 3 2

  34. 3 6 8 7 1 5 4 2 Depth-First Search 1 4 3 2

  35. 3 6 8 7 1 5 4 2 Depth-First Search 1 4 3 2

  36. 3 6 8 7 1 5 4 2 Depth-First Search 1 4 3 2

  37. 3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 3 2

  38. 3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 3 2

  39. 3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 6 3 2

  40. 3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 6 3 2

  41. 3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 6 3 2

  42. 3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 6 3 2

  43. 3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 6 3 2

  44. 3 6 8 7 1 5 4 2 Depth-First Search 5 7 1 4 6 3 2

  45. 3 6 8 7 1 5 4 2 Depth-First Search 5 7 1 4 6 3 2

  46. 5 1 3 6 8 7 4 2 Depth-First Search 5 7 1 4 6 3 8 2

  47. 5 1 3 6 8 7 4 2 Depth-First Search 5 7 1 4 6 3 8 2

  48. 5 1 3 6 8 7 4 2 Depth-First Search 5 7 1 4 6 3 8 2

  49. 5 1 3 6 8 7 4 2 Depth-First Search 5 7 1 4 6 3 8 2

More Related