Shortest Path in a Graph: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 7: Line 7:
* [[Graph_Search#Breadth-First_Search_.28BFS.29|Graph Search | Breadth-First Search]]
* [[Graph_Search#Breadth-First_Search_.28BFS.29|Graph Search | Breadth-First Search]]
=Overview=
=Overview=
There are several algorithms that compute the shortest path between two vertices in a graph, and they can be used or not depending on the characteristics of the graph, such as whether is directed or undirected, the edges have weights, the weights are. negative or not


The BFS algorithm as described above can be used, with a very small constant-time addition, to keep track of the layer each newly discovered node is in, relative to the start node, and that will automatically indicate the shortest path between the start node s and a reachable node v. It works by annotating the start vertex with 0 and then annotating each new node with  D + 1, where D is the distance of the node we discovered the new node from.
The BFS algorithm as described above can be used, with a very small constant-time addition, to keep track of the layer each newly discovered node is in, relative to the start node, and that will automatically indicate the shortest path between the start node s and a reachable node v. It works by annotating the start vertex with 0 and then annotating each new node with  D + 1, where D is the distance of the node we discovered the new node from.


⚠️ Only breadth-first search gives the guarantee of the shortest path.
⚠️ Only breadth-first search gives the guarantee of the shortest path.
=TODO=
=TODO=
<font color=darkgray>Reshape this page to accommodate [[Dijkstra's Algorithm]].</font>
<font color=darkgray>Reshape this page to accommodate [[Dijkstra's Algorithm]].</font>

Revision as of 19:15, 14 October 2021

External

Internal

Overview

There are several algorithms that compute the shortest path between two vertices in a graph, and they can be used or not depending on the characteristics of the graph, such as whether is directed or undirected, the edges have weights, the weights are. negative or not

The BFS algorithm as described above can be used, with a very small constant-time addition, to keep track of the layer each newly discovered node is in, relative to the start node, and that will automatically indicate the shortest path between the start node s and a reachable node v. It works by annotating the start vertex with 0 and then annotating each new node with D + 1, where D is the distance of the node we discovered the new node from.

⚠️ Only breadth-first search gives the guarantee of the shortest path.

TODO

Reshape this page to accommodate Dijkstra's Algorithm.

Algorithm

The algorithm is (differences to the canonical BFS algorithm are emphasized):

BFS_with_Shortest_Path(graph G, start vertex s)
  # All nodes are assumed unexplored
  initialize a Queue Q (FIFO)
  mark s as explored
  annotate s with distance 0
  place s in Q
  while Q has elements
    remove the head of the queue v
    for each edge (v, w):
      if w unexplored:
        mark w as explored
        annotate w with a distance dist(w) = dist(v) + 1
        add w to Q

The distance computed on reachable node gives the "layer" and the distance from the start node s.

BFS Layers.png