1 / 82

CS200: Algorithm Analysis

Explore how Breadth-First Search (BFS) algorithm can be applied to directed graphs. Trace the algorithm using a directed graph, showing distances and times of discovery.

towen
Download Presentation

CS200: Algorithm Analysis

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. CS200: Algorithm Analysis

  2. BREADTH-FIRST SEARCH May be applied to directed or undirected graphs. Show example Directed Graph and trace algorithm filling in discovery (distances) times. (Find the distance of each vertex from a source vertex that is chosen arbitrarily). Omit colors (unnecessary). Use a FIFO queue with front/rear pointers so Enqueue/Dequeue can be done in constant time.

  3. BFS(source) for each u in V - {source} do d[u] = infinity; d[source] = 0; Q = {source}; while not empty Q do dequeue u from Q; for each v in ADJ[u] do if d[v] = infinity then d[v] = d[u] + 1; enqueue v on Q;

  4. Trace BFS on G using s as source. Visit adjacent vertices in alphabetical order.

  5. Breadth-First Search

  6. Breadth-First Search

  7. Breadth-First Search

  8. Breadth-First Search

  9. Breadth-First Search

  10. Breadth-First Search

  11. Breadth-First Search

  12. Breadth-First Search

  13. Breadth-First Search

  14. Breadth-First Search

  15. Breadth-First Search

  16. Breadth-First Search

  17. Breadth-First Search

  18. Breadth-First Search

  19. Breadth-First Search

  20. Breadth-First Search

  21. Breadth-First Search

  22. Breadth-First Search

  23. Breadth-First Search

  24. Breadth-First Search

  25. Breadth-First Search

  26. Breadth-First Search

  27. Breadth-First Search

  28. Breadth-First Search

  29. Breadth-First Search

  30. Breadth-First Search

  31. Breadth-First Search

  32. Breadth-First Search

  33. Idea: when processing vertices at distance i, add all vertices at distance i+1 to queue, these will be processed when done with all vertices at distance i. • Greedily expand a frontier (propagate a wave 1-edge distance at a time). Proof of correctness is delayed until we do Dijkstra’s algorithm. Unlike DFS, BFS may not reach all vertices. Why? Runtime is O(V+E) using same argument as used for DFS.

  34. INTRO. SHORTEST PATHS IN GRAPH ALGORITHMS These algorithms are a generalization of BFS, to handle the notion of weighted edge graphs. Interested in directed graphs, G = (V,E), weight function w: E–>R (BFS –>w(e) = 1 for all e in E). • Example of type of graph; a street map with distances on roads between intersections. • A communication network with probability of a connection failure or bandwidth load.

  35. Weight of a path p = v0–>v1–>...–>vk is w(p) = S k w(vi-1,vi) i=1 Shortest path in graph = path with minimum weight. Big Note: There is an optimal substructure to the problem of finding a shortest path=> we can use either a greedy or dynamic programming algorithm.

  36. Lemma 24.1: Showing optimal substructure Subpaths of shortest paths are also shortest paths. Sketch of proof with drawing. Definition: z(u,v) = weight of shortest path from u to v.

  37. But this is a contradiction: if the sub-path were not a shortest path then we could substitute a shorter sub-path to create a shorter total path.

  38. Generalization of Lemma 24.3 - Triangle Inequality: • z(u,v) <= z(u,x) + z(x,v) Proof using drawing. Shortest path u –> v can be no longer than any other path (in particular, the path that takes the shortest path from u–>x and then the shortest path from x–>v). Well-defined: • If a negative-weight cycle exists in a graph => some shortest-path may not exist because we can always get a shorter path by going around the cycle again.

  39. Example with shortest paths from s, blue lines showing precedence relation

  40. The above points are only highlights of shortest paths theory. BELLMAN-FORD ALGORITHM This is most basic single-source shortest paths algorithm. • 1. finds the shortest path weights from source s to all v in V. • 2. we won’t worry about constructing actual path, but it can be done easily.

  41. BF(G,s) for each v in V do d[v] = infinity d[s] = 0 for i = 1 to |V| -1 do {|V|-1 times} for each edge (u,v) in E do {E times} if d[v] > d[u]+w(u,v) then d[v] = d[u]+w(u,v) {relax. step} for each edge(u,v) in E do if d[v] > d[u]+w(u,v) then {rules out} no solution{neg. edge weight cycles}

  42. Example of Bellman Ford

  43. Example of Bellman Ford

  44. Example of Bellman Ford

  45. Example of Bellman Ford

  46. Example of Bellman Ford

  47. Example of Bellman Ford

More Related