Graph Algorithm
Graph Algorithm
Dijkstra's algorithm finds the shortest path from one vertex to all other vertices.
How it works:
1. Set initial distances for all vertices: 0 for the source vertex, and infinity
for all the other.
2. Choose the unvisited vertex with the shortest distance from the start to
be the current vertex. So the algorithm will always start with the source
as the current vertex.
3. For each of the current vertex's unvisited neighbor vertices, calculate the
distance from the source and update the distance if the new, calculated,
distance is lower.
4. We are now done with the current vertex, so we mark it as visited. A
visited vertex is not checked again.
5. Go back to step 2 to choose a new current vertex, and keep repeating
these steps until all vertices are visited.
6. In the end we are left with the shortest path from the source vertex to
every other vertex in the graph.
After relaxing vertices A and E, vertex D is considered visited, and will not be
visited again.
The next vertex to be chosen as the current vertex must the vertex with the
shortest distance to the source vertex (vertex D), among the previously
unvisited vertices. Vertex E is therefore chosen as the current vertex after
vertex D.
The distance to all adjacent and not previously visited vertices from vertex E
must now be calculated, and updated if needed.
The calculated distance from D to vertex A, via E, is 2+4=6. But the current
distance to vertex A is already 4, which is lower, so the distance to vertex A is
not updated.
The next vertex to be visited is vertex A because it has the shortest distance
from D of all the unvisited vertices.
The calculated distance to vertex C, via A, is 4+3=7, which is higher than the
already set distance to vertex C, so the distance to vertex C is not updated.
Vertex A is now marked as visited, and the next current vertex is vertex C
because that has the lowest distance from vertex D between the remaining
unvisited vertices.
Vertex F gets updated distance 6+5=11, and vertex B gets updated distance
6+2=8.
Calculated distance to vertex G via vertex C is 6+5=11 which is higher than the
already set distance of 7, so distance to vertex G is not updated.
Vertex F already has a distance of 11. This is lower than the calculated distance
from G, which is 7+5=12, so the distance to vertex F is not updated.
Vertex G is marked as visited, and B becomes the current vertex because it has
the lowest distance of the remaining unvisited vertices.
The new distance to F via B is 8+2=10, because it is lower than F's existing
distance of 11.
Vertex B is marked as visited, and there is nothing to check for the last
unvisited vertex F, so Dijkstra's algorithm is finished.
Every vertex has been visited only once, and the result is the lowest distance
from the source vertex D to every other vertex in the graph.
Using Dijkstra's algorithm to find the shortest paths from vertex C in this graph:
Bellman-Ford Algorithm
The Bellman-Ford algorithm is best suited to find the shortest paths in a
directed graph, with one or more negative edge weights, from the source vertex
to all other vertices.
It does so by repeatedly checking all the edges in the graph for shorter paths,
as many times as there are vertices in the graph (minus 1)
How it works:
1. Set initial distance to zero for the source vertex, and set initial distances
to infinity for all other vertices.
2. For each edge, check if a shorter distance can be calculated, and update
the distance if the calculated distance is shorter.
3. Check all edges (step 2) (V−1) times. This is as many times as there
are vertices (V), minus one.
4. Optional: Check for negative cycles. This will be explained in better detail
later.
And this check of all edges is done V−1 times, with V being the number of
vertices in the graph.
This is how the Bellman-Ford algorithm checks all the edges in the adjacency
matrix in our graph 5-1=4 times:
The first four edges that are checked in our graph are A->C, A->E, B->C, and
C->A.
The next edges to be checked are the edges going out from vertex E, which
leads to updated distances for vertices B and C.
The Bellman-Ford algorithm have now checked all edges 1 time. The algorithm
will check all edges 3 more times before it is finished, because Bellman-Ford will
check all edges as many times as there are vertices in the graph, minus 1.
The algorithm starts checking all edges a second time, starting with checking
the edges going out from vertex A. Checking the edges A->C and A->E do not
lead to updated distances.
The next edge to be checked is B->C, going out from vertex B. This leads to an
updated distance from vertex D to C of 5-4=1.
Checking the next edge C->A, leads to an updated distance 1-3=-2 for vertex A