0% found this document useful (0 votes)
4 views

Graph Algorithm

Uploaded by

William Jobs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Graph Algorithm

Uploaded by

William Jobs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Dijkstra's Algorithm

Dijkstra's algorithm finds the shortest path from one vertex to all other vertices.

It does so by repeatedly selecting the nearest unvisited vertex and calculating


the distance to all the unvisited neighboring 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.

Demonstration of Dijkstra’s Algorithm


Consider the graph below as an example. The image below shows the initial
infinite distances to other vertices from the starting vertex D. The distance
value for vertex D is 0 because that is the starting point.
So vertex A gets the distance changed from inf to 4, and vertex E gets the
distance changed to 2. This is called 'relaxing'.

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 distance to vertex C is calculated to be 2+4=6, which is less than infinity,


so the distance to vertex C is updated.

Similarly, the distance to node G is calculated and updated to be 2+5=7.

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 C is marked as visited, and the next vertex to be visited is G because it


has the lowest distance between the remaining unvisited vertices.

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.

Applications of Dijkstra’s shortest path algorithm


1. Digital Mapping Services in Google Maps: Many times we have tried to find
the distance in G-Maps, from one city to another, or from your location to the
nearest desired location. There is the Shortest Path Algorithm, as there are
various routes/paths connecting them but it has to show the minimum distance,
so Dijkstra’s Algorithm is used to find the minimum distance between two
locations along the path.
2. Social Networking Applications: In many applications you might have seen
the app suggests the list of friends that a particular user may know. How do you
think many social media companies implement this feature efficiently, especially
when the system has over a billion users? The standard Dijkstra algorithm can
be applied using the shortest path between users measured through
handshakes or connections among them.
Exercise:

Using Dijkstra's algorithm to find the shortest paths from vertex C in this graph:

What is the next vertex to be visited after C is visited?

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.

Demonstration of Bellman-Ford Algorithm


The Bellman-Ford algorithm is actually quite straight forward, because it checks
all edges, using the adjacency matrix. Each check is to see if a shorter distance
can be made by going from the vertex on one side of the edge, via the edge, to
the vertex on the other side of the edge.

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

You might also like