SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Shortest Path Algorithms: BFS, Dijkstra, and Bellman-Ford

sol avatar
31 diapositives · 446 Views · Sep 11, 2014

This presentation explains key concepts in shortest path algorithms including Breadth-First Search (BFS), Dijkstra's algorithm, and Bellman-Ford algorithm. Explore the functionality of BFS in unweighted graphs and how it employs queues for vertex enumeration. Learn about the need for priority queues in Dijkstra’s algorithm, their implementation, and time complexities. Additionally, the Bellman-Ford algorithm is introduced for its capacity to handle graphs with negative weights. We also touch on graph modeling as a tool for problem-solving in computational contexts.

Understanding Shortest Path Algorithms: BFS, Dijkstra, and Bellman-Ford - slide 1
01 / 31Understanding Shortest Path Algorithms: BFS, Dijkstra, and Bellman-Ford
1 / 17

Télécharger la présentation

Understanding Shortest Path Algorithms: BFS, Dijkstra, and Bellman-Ford
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Shortest Paths 08-07-2006 PowerPoint adapted from Alan Tam’sShortest Path, 2004 with slight modifications

  2. Breadth First Search (BFS) • Techniques to Enumerate all Vertices for Goal • BFS needs O(V) queue and O(V) set for duplicate elimination and runs in O(V + E) time • BFS can find Shortest Path if Graph is Not Weighted • BFS works because a Queue ensures a specific Order

  3. What Order? • Define d[v] be the length of shortest path from s to v • At any time, vertices are classified into: • Black With known d[v] • Grey With some known path • White Not yet touched • The Grey vertex with shortest known path has potential to become Black • The Head of Queue is always the case in an unweighted graph • Why?

  4. Weighted Graph • Queue does not promise smallest d[v] anymore • Expanding weighted edge causeds unnecessary searching of artificial vertices • We can simply pick the real vertex nearest to the starting vertex • We need “Sorted Queue” which “dequeues” vertices in increasing order of d[v]. • It is called a “Priority Queue” and negation of d[v] is called the Priority of vertex v

  5. 0 3 2 1 3 3 3 1

  6. 0 3 3 2 1 3 3 2 3 1 3

  7. Done? 0 3 3 2 1 3 5 3 2 3 1 3

  8. Done? 0 3 3 2 1 3 4 3 2 3 1 3

  9. 0 3 3 2 1 3 4 3 2 3 1 3

  10. Done? 0 3 3 2 1 3 4 3 2 3 1 3

  11. Dijkstra’s Algorithm d ← ∞ d[s] ← 0 enqueue(queue, s) whilenot empty(queue) do u = dequeue(queue) for-eachvwhere (u, v) in E if d[v] > d[u] + wuvthen d[v] = d[u] + wuv enqueue(queue, v) • Note that d[v] represents the known shortest distance of v from s during the process and may not be the real shortest distance until v is marked black.

  12. Requirements for Priority Queue • Map<V, int> • Store temporary values of d[v] • PriorityQueue<V, function: V→int > • Extract vertices in increasing order of d[v] • Initially all d[v] are infinity • When we visit a White vertex • Set d[v] • Insert v into Queue • When we visit a Gray vertex • Update d[v] • What to do to the Priority Queue?

  13. Usage: Insert: V Extract: V Decrease: E - V Array Insert: O(1) Extract: O(n) Decrease: O(1) Sorted Array Insert: O(n) Extract: O(1) Decrease: O(n) Heap Insert: O(lg n) Extract: O(lg n) Decrease: O(lg n) Fibonacci Heap Insert: O(1) Extract: Amortized O(lg n) Decrease: O(1) Implementing Priority Queue?

  14. Time Complexity

  15. Time Complexity

  16. Another Attempt • Consider the following code segment: For each edge (u, v) If d[v] > d[u] + wuv d[v] ← d[u] + wuv • Assume one of the shortest paths is (s, v1, v2, …, vk) • If d[vi] = its shortest path from s • After this loop, d[vi+1] = its shortest path from s • By MI, After k such loops, found shortest path from s to vk

  17. Bellman-Ford Algorithm • All v1, v2, …,vk distinct? Do V-1 times for-each (u, v) in E if d[v] > d[u] + wuv then d[v] ← d[u] + wuv • Order = O(VE) • Support Negative-weight Edges

  18. Another Attempt • Number vertices as v1, v2, …, vV • Define no≤k-path as a path which does not pass through vertices v1, v2, …, vk • A path s-t must either be • a no≤1-path, or • concatenation of a no≤1-path s-v1 and a no≤1-path v1-t • A no≤1-path s-t must either be • a no≤2-path, or • concatenation of a no≤2-path s-v2 and a no≤2-path v2-t • By MI …

  19. For all S For all T S T Trivial Paths Direct Path from S to T

  20. For all R For all S Direct Path from R to S R S Direct Path from R to T For all S For all T Direct Path from S to T S T For S = vV

  21. For all S For all T S T no≤(V-1)-Paths Shortest Path from S to Tvia vV

  22. For all R For all S Shortest Path from R to Svia vV R S Shortest Path from R to Tvia vV For all S For all T Shortest Path from S to Tvia vV S T For S = vV-1

  23. For all S For all T S T no≤(V-2)-Paths Shortest Path from S to Tvia vV-1,vV

  24. For all S For all T S T no≤2-Paths Shortest Path from S to Tvia v3,v4,...,vV

  25. For all R For all S Shortest Path from R to Svia v3,v4,...,vV R S Shortest Path from R to Tvia v3,v4,...vV For all S For all T Shortest Path from S to Tvia v3,v4,...,vV S T For S = V2

  26. For all S For all T S T no≤1-Paths Shortest Path from S to Tvia v2,v3,...,vV

  27. For all R For all S Shortest Path from R to Svia v2,v3,...,vV R S Shortest Path from R to Tvia v2,v3,...vV For all S For all T Shortest Path from S to Tvia v2,v3,...,vV S T For S = V1

  28. For all S For all T S T Shortest Paths Shortest Path from S to Tvia v1,v2,...,vV

  29. Warshall-Floyd Algorithm for-each (u, v) in E d[u][v] ← wuv for-eachi in V for-eachjin V for-eachkin V if d[j][k] > d[j][i] + d[i][k] d[j][k] ← d[j][i] + d[i][k] • Time Complexity: O(V3)

  30. Graph Modeling • Conversion of a problem into a graph problem • Sometimes a problem can be easily solved once its underlying graph model is recognized • Graph modeling appears almost every year in NOI or IOI (cx, 2004)

  31. Basics of Graph Modeling • A few steps: • identify the vertices and the edges • identify the objective of the problem • state the objective in graph terms • implementation: • construct the graph from the input instance • run the suitable graph algorithms on the graph • convert the output to the required format (cx, 2004)