1 / 16

Graphs: Adjacency Matrix

Graphs: Adjacency Matrix. Example:. 1. a. d. 2. 4. b. c. 3. Breadth-First Search. “ Explore” a graph, turning it into a tree One vertex at a time Expand frontier of explored vertices across the breadth of the frontier Builds a tree over the graph

Download Presentation

Graphs: Adjacency Matrix

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: Adjacency Matrix • Example: 1 a d 2 4 b c 3 zhengjin 12014/11/7

  2. Breadth-First Search • “Explore” a graph, turning it into a tree • One vertex at a time • Expand frontier of explored vertices across the breadth of the frontier • Builds a tree over the graph • Pick a source vertex to be the root • Find (“discover”) its children, then their children, etc. zhengjin 22014/11/7

  3. Breadth-First Search • Again will associate vertex “colors” to guide the algorithm • White vertices have not been discovered • All vertices start out white • Grey vertices are discovered but not fully explored • They may be adjacent to white vertices • Black vertices are discovered and fully explored • They are adjacent only to black and gray vertices • Explore vertices by scanning adjacency list of grey vertices zhengjin 32014/11/7

  4. Breadth-First Search BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What does v->d represent? What does v->p represent? zhengjin 42014/11/7

  5. Breadth-First Search: Example r s t u         v w x y zhengjin 52014/11/7

  6. Breadth-First Search: Example r s t u  0       v w x y Q: s zhengjin 62014/11/7

  7. Breadth-First Search: Example r s t u 1 0    1   v w x y Q: w r zhengjin 72014/11/7

  8. Breadth-First Search: Example r s t u 1 0 2   1 2  v w x y Q: r t x zhengjin 82014/11/7

  9. Breadth-First Search: Example r s t u 1 0 2  2 1 2  v w x y Q: t x v zhengjin 92014/11/7

  10. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2  v w x y Q: x v u zhengjin 102014/11/7

  11. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: v u y zhengjin 112014/11/7

  12. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: u y zhengjin 122014/11/7

  13. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: y zhengjin 132014/11/7

  14. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: Ø zhengjin 142014/11/7

  15. Touch every vertex: O(V) u = every vertex, but only once (Why?) So v = every vertex that appears in some other vert’s adjacency list BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What will be the running time? Total running time: O(V+E) zhengjin 152014/11/7

  16. Breadth-First Search: Properties • BFS calculates the shortest-path distance to the source node • Shortest-path distance (s,v) = minimum number of edges from s to v, or  if v not reachable from s • BFS builds breadth-first tree, in which paths to root represent shortest paths in G • Thus can use BFS to calculate shortest path from one vertex to another in O(V+E) time zhengjin 162014/11/7

More Related