Bellman Ford Algorithm

Hello people…! In this post I will talk about another single source shortest path algorithm, the Bellman Ford Algorithm. Unlike Dijkstra’s Algorithm, which works only for a graph positive edge weights, the Bellman Ford Algorithm will give the shortest path from a given vertex for a graph with negative edge weights also. Due to this, the Bellman Ford Algorithm is more versatile, but, it’s speciality comes at a cost. The runtime complexity of Bellman Ford Algorithm is O(|V||E|), which is substantially more than that of Dijkstra’s Algorithm. Sometimes this algorithm is called Bellman Ford Moore Algorithm, as the same algorithm was published by another researcher.

Before we get started, there are a couple of things that we must understand. Firstly, why does Dijkstra’s Algorithm fail for negative edge weights and second, the concept of Negative Cycles.

Why does Dijkstra fail?

Consider, the graph below,

The Dijkstra’s Algorithm is based on the principle that, if S → V1 → … → Vk is the shortest path from S → Vk then D(S, Vi) ≤ D(S, Vj). But in the above given graph, clearly that principle is violated. In the above graph, the shortest path from V1 to V3 is of length 3 units but the shortest path to V4 is of length 1 unit which means that V4 is actually closer to V1 than V3, which is contradicting Dijkstra’s principle.

Negative Cycles

A Negative Cycle is a path V1 → V 2 → V3 → … Vk → V1 where the total sum of the edge weights in the path is negative. Consider the graph below –

The path B → C → D is a negative cycle as the path’s total weight would be -2. So, the distance from A → B is 2, but if we circle the cycle once, we would get the distance as 0, if we circle once more, we would get -2. Like this we could keep on circling as much as we want to reduce the shortest distance. Hence the shortest distance to the vertex B, E becomes indeterminate.

So, we want Bellman Ford Algorithm to solve these two issues. We want it to compute the shortest path even for a graph with negative edges and negative cycles. The Bellman Ford will accurately compute the shortest path for a graph with negative edges but the algorithm fails for a graph with negative cycles. But, the algorithm can tell you if a negative cycle exists or not. If it exists the solution it puts up is incorrect, otherwise, the solution given by Bellman Ford Algorithm is perfect. This sounds fine because logically there will be no shortest paths for a graph with negative cycles.

Unlike the Dijkstra’s Algorithm where we had to use a priority queue, we will require no additional data structure to code Bellman Ford Algorithm. This makes writing the code much easier. And the algorithm is pretty straight-forward too. Take a look at the pseudo-code of the Bellman Ford Algorithm given below –

```bellmanFord(G, s)
for all edges in G(V)
D(V) = INT_MAX
parent[V] = -1

D(s) = 0

for i = 1 to |G(V)| - 1
for each edge (u, v) in G(E)
if edge can be Relaxed
D(v) = D(u) + weight of edge (u, v)
parent[v] = u

for each edge in G(E)
if edge can be Relaxed
return false

return true
```

You may not understand the pseudo-code at the first look, here’s a step-by-step representation of it –

• Initialise the array which contains the shortest distances to infinity (a high integer value in the pseudo-code).
• Initialise the parent array which contains the parent vertices in the shortest path to NULL (or -1 if it is an integer array).
• Set the shortest distance of starting vertex to 0.
• Explore all the edges, and see if you can relax them. If you can, relax the edge and proceed with the exploration.
• Do the above operation |V| – 1 times.
• After that, do another exploration on the graph checking all the edges if they can be relaxed. If they can be relaxed, you can a negative cycle in the graph. Hence, return false.
• If the exploration gets finished successfully, the graph has no negative cycles and the data that you compute dis correct, so return true.

Now, what does exploring all the edges mean? If you are implementing the graph using an Adjacency List, it means to iterate over all the linked lists associated with all vertices. Now, what will be the sum of all the nodes in all Linked Lists in a given Adjacency List? Number of edges off course! So, we check all the edges from, edges of vertex 1 to vertex |V| in a linear manner. This whole operation takes O(|E|) time, which is repeated |V| – 1, so this part of the code takes O(|E||V|) time. Now, analyse the pseudo-code for a few minutes. Ask yourself how would you code this-ans-that. Now, when your mind is filled with the pseudo-code, look at the sketch below. The sketch below is sort of, “dry run” of the pseudo-code stated above –

The above sketch is self-explanatory. I hope you understand how the iterations go. In a way it looks like a very ordinary algorithm, without any greedy steps or partitions or so. The Bellman Ford Algorithm is pretty easy to code too. If you can work hard for an hour or two I’m sure you can code this algorithm. It does not require any priority queue or other tools. All you need to code Bellman Ford Algorithm is the pseudo-code. The pseudo-code is very important. Keep looking at the pseudo-code again-and-again whenever you get a doubt. I have put my code below for a reference, it is a C++ code –

```/*
* Bellman-Ford Algorithm
*
* Authored by,
* Vamsi Sangam
*/

#include <cstdio>
#include <cstdlib>
#include <climits>

using namespace std;

// This is used to construct
// the Adjacency List
struct node {
int vertex, weight;
struct node * next;
};

// This is used to construct the Shortest Paths to all
// vertices, as we cannot return multiple values,
// we use a struct
struct pathInfo {
int vertex, distance, parent;
};

// Adds a new edge into the Adjacency List
// Follows Head Insertion for O(1) Insertion
struct node * add(struct node * head, int vertex, int weight)
{
struct node * p = (struct node *) malloc(sizeof(struct node));

p->vertex = vertex;
p->weight = weight;
p->next = head;

return p;
}

// Bellman-Ford Algorithm which takes the Graph (adjacencyList[]), starting vertex (startVertex),
// and an empty array shortestDistances[] as input. It applies the algorithm and keeps filling values
// into shortestDistances[]. It returns true if there are no negative edges, and vice-versa.
bool bellmanFord(struct node * adjacencyList[], int vertices, int startVertex, struct pathInfo shortestDistances[])
{
struct node * traverse;
int i, j, k;

// Initialisation
for (i = 0; i <= vertices; ++i) {
shortestDistances[i].vertex = i;
shortestDistances[i].distance = INT_MAX;
shortestDistances[i].parent = -1;
}

// Setting distance to source = 0
shortestDistances[startVertex].parent = 0;
shortestDistances[startVertex].distance = 0;

// The Algorithm that computes Shortest Distances
for (i = 1; i <= vertices - 1; ++i) {		// Runs 'vertices - 1' times = O(|V|)
for (j = 1; j <= vertices; ++j) {		// Runs as many times as the edges = O(|E|)
// The code ahead basically explores the whole of Adjcency List,
// covering one edge once, so the runtime of the code in this
// block is O(|E|)

traverse = adjacencyList[j];

while (traverse != NULL) {
if (shortestDistances[j].distance == INT_MAX) {
// Important...!
traverse = traverse->next;
continue;
}

// Checking for Relaxation
if (traverse->weight + shortestDistances[j].distance < shortestDistances[traverse->vertex].distance) {
// Relaxation
shortestDistances[traverse->vertex].distance = traverse->weight + shortestDistances[j].distance;
shortestDistances[traverse->vertex].parent = j;
}

traverse = traverse->next;
}
}
}

// Checking for Negative Cycles
for (j = 1; j <= vertices; ++j) {
traverse = adjacencyList[j];

while (traverse != NULL) {
// Checking for further relaxation
if (traverse->weight + shortestDistances[j].distance < shortestDistances[traverse->vertex].distance) {
// Negative Cycle found as further realxation is possible
return false;
}

traverse = traverse->next;
}
}

return true;
}

int main()
{
int vertices, edges, i, j, v1, v2, weight;

printf("Enter the Number of Vertices -\n");
scanf("%d", &vertices);

printf("Enter the Number of Edges -\n");
scanf("%d", &edges);

struct node * adjacency_list[vertices + 1];
//Size is made (vertices + 1) to use the
//array as 1-indexed, for simplicity

//Must initialize your array
for (i = 0; i <= vertices; ++i) {
adjacency_list[i] = NULL;
}
printf("Enter the edges -\n\n");

for (i = 1; i <= edges; ++i) {
scanf("%d%d%d", &v1, &v2, &weight);
adjacency_list[v1] = add(adjacency_list[v1], v2, weight);
}

//Printing Adjacency List
printf("\nAdjacency List -\n\n");
for (i = 1; i <= vertices; ++i) {
printf("adjacency_list[%d] -> ", i);

struct node * temp = adjacency_list[i];

while (temp != NULL) {
printf("(%d, %d) -> ", temp->vertex, temp->weight);
temp = temp->next;
}

printf("NULL\n");
}

struct pathInfo shortestDistances[vertices + 1];
int startVertex;

printf("\nEnter a Start Vertex -\n");
scanf("%d", &startVertex);

if (bellmanFord(adjacency_list, vertices, startVertex, shortestDistances)) {
printf("No Negative Cycles exist in the Graph -\n");
} else {
printf("Negative Cycles exists in the Graph -\n");
// The Bellman-Ford Algorithm does not work with negative cycles,
// all it can do is merely detect them, so when a negative cycle
// is detected, the shortestDistances array has wrong values
return 0;
}

printf("\n\nVertex    Shortest Distance to Vertex %d     Parent Vertex-\n", startVertex);
for (i = 1; i <= vertices; ++i) {
printf("%d         %d                                 %d\n", i, shortestDistances[i].distance, shortestDistances[i].parent);
}

return 0;
}
```

If you have any doubts regarding the algorithm feel free to drop a comment. I’ll surely reply to them. I hope my post helped you in learning the Bellman Ford Algorithm. If it did, let me know by commenting ! Commenting is super easy if you are a Facebook, Twitter or a Google+ user…! So, don’t hesitate..! 😉 Keep practicing… Happy Coding…! 🙂

You may also like –

More in the Navigation Page ! →

Dijkstra’s Algorithm

Hello people…! In this post I will talk about one of the fastest single source shortest path algorithms, which is, the Dijkstra’s Algorithm. The Dijkstra’s Algorithm works on a weighted graph with non-negative edge weights and ultimately gives a Shortest Path Tree. It is a Greedy Algorithm, which sort of… mimics the working of Breadth First Search and Depth First Search. It is used in a number of day-to-day scenarios. It is used in network routing, to calculate the path from a network device A and B in a network which would have the maximum bandwidth. It could also be used by the GPS in a car to calculate the shortest path between two locations. The Dijkstra’s Algorithm can be modified to solve a lot of real world problems. So let’s get started…!

The Dijkstra’s Algorithm starts with a source vertex ‘s‘ and explores the whole graph. We will use the following elements to compute the shortest paths –

• Priority Queue Q, implemented by a Min Binary Heap using C++ STL Vector.
• Another set D, which keeps the record of the shortest paths from starting vertex s. Implemented using C++ STL Vector.

Just like the other graph search algorithms, Dijkstra’s Algorithm is best understood by listing out the algorithm in a step-by-step process –

• The Initialisation –
1. D(s), which is the shortest distance to s is set to 0. It is obvious as distance between source to itself is 0.
2. For all the other vertices V, D(V) is set to infinity as we do not have a path yet to them, so we simply say that the distance to them is infinity.
3. The Priority Queue Q, is constructed which is initially holds all the vertices of the Graph. Each vertex V will have the priority D(V).
• The Algorithm –
1. Now, pick up the first (or the minimum) element from the Priority Queue Q (which removes it from Q). For the first time, this operation would obviously give s.
2. For all the vertices adjacent to s, i.e., for all vertices in adjacencyMatrix[s], check if the edge from s → v gives a shorter path. This is done by checking the following condition –

if, D(s) + (weight of edge s → v) < D(v), we found a new shorter route, so update D(v)
D(v) = D(s) + (weight of edge s → v)

3. Now pick the next element from Q, and repeat the process until there are elements left in Q.

It might look like a long and cumbersome process, but this is actually a very smart technique. It’s okay if you don’t understand it in the first reading. Give another 3-4 readings and try to picture what is happening to the graph when you implement the algorithm, in your head. After you feel you have got a hang of the algorithm, look at the sketch below for complete understanding.

Dijkstra’s Algorithm Step-by-Step

The Dijkstra’s Algorithm is a little tricky. Many don’t understand it in the first attempt. In reference to the diagram above, I will give a step-by-step explanation for each graph marked with the number on top in purple.

1. Firstly, initialize your components, the shortest distances array D, the priority queue Q, and starting vertex s. The distance from source to itself is zero. So, D(s) = 0, and the rest of the array is ∞ . The set of vertices V are inserted into the priority queue Q, with a priority D(V). Now, we start our algorithm by extracting (hence removing it from the priority queue) the minimum element from the priority queue. The minimum element in the priority queue will definitely be s (which is A here). Look at all the adjacent vertices of A. Vertices B, C, D are adjacent to A. We can go to B travelling the edge of weight 2, to C travelling an edge of weight 1, to D travelling an edge of weight 5. The values of D(B), D(C), D(D) are ∞ . We have found a new way of reaching them in 2, 1, 5 units respectively, which is less than ∞ , hence a shorter path. This is what the if-condition mentioned above does. So, we update the values of D(B), D(C), D(D) and the priorities of B, C, D, in the priority queue. With this we have finished processing the Vertex A.
2. Now, the process continues to its next iteration and we extract the minimum element from the priority queue. The minimum element would be Vertex C which would be having a priority of 1. Now, look at all the adjacent vertices to C. There’s Vertex D. From C, the it would take 1 unit of distance to reach D. But to reach C in prior, you need 1 more unit of distance. So, if you go to D, via C, the total distance would be 2 units, which is less than the current value of shortest distance discovered to D, D(D) = 5. So, we reduce the value of D(D) to 2. This reduction is also called as “Relaxation”. With that we’re done with Vertex C.
3. Now, the process continues to its next iteration and we extract the minimum element from the priority queue. Now, there are two minimum elements, B and D. You can go for anyone, it doesn’t matter. For now, we will go for Vertex D. From Vertex D, you can go to Vertex E, and Vertex F, with a total distance of 2 + 2 {D(D) + (weight of D → E)}, and 2 + 3. Which is less than ∞ , so D(E) becomes 4 and D(F) becomes 5. We’re done with Vertex D.
4. Now, the process continues to its next iteration and we extract the minimum element from the priority queue. The minimum element in the priority queue is vertex B. From vertex B, you can reach vertex F in 2 + 1 units of distance, which is less than the current value of D(F), 5. So, we relax D(F) to 3. From vertex B, you can reach vertex D in 2 + 2 units of distance, which is more than the current value of D(D), 2. This route is not considered as it is clearly proven to be a longer route. With that we’re done with vertex B.
5. Now, the process continues to its next iteration and we extract the minimum element from the priority queue. The minimum element in the priority queue is vertex E. From vertex E, you can reach vertex C in 4 + 4 units of distance, which is more than the current value of D(C), 1. This route is not considered as it is clearly proven to be a longer route. With that we’re done with vertex E.
6. Now, the process continues to its next iteration and we extract the minimum element from the priority queue. The minimum element in the priority queue is vertex F. You cannot go to any other vertex from vertex F, so, we’re done with vertex F.
7. With the removal of vertex F, our priority queue becomes empty. So, our algorithm is done…! You can simply return the array D to output the shortest paths.

Having got an idea about the overall working of the Dijkstra’s Algorithm, it’s time to look at the pseudo-code –

```dijsktra(G, S)
D(S) = 0
Q = G(V)

while (Q != NULL)
u = extractMin(Q)
for all V in adjacencyList[u]
if (D(u) + weight of edge < D(V))
D(V) = D(u) + weight of edge
decreasePriority(Q, V)
```

In the pseudo-code, G is the input graph and S is the starting vertex. I hope you understand the pseudo-code. If you don’t, feel free to comment your doubts. Now, before we code Dijkstra’s Algorithm, we must first prepare a tool, which is the Priority Queue.

The Priority Queue

The Priority Queue is implemented by a number of data structures such as the Binary Heap, Binomial Heap, Fibonacci Heap, etc. The priority queue in my code is implemented by a Binary Heap. If you are not aware about the Binary Heap, you can refer to my post on Binary Heaps. But the implementation that we will do here is different from that in my post regarding the Binary Heap. The difference is that, here we will implement the Binary Heap using C++ STL Vector, not an array. This is because a heap is meant to grow, not remain of a fixed size, so we are using a data structure that can grow, a vector. Also, when we use a vector we can apply the same traversing techniques that we used in the case of an array. And, every element of our vector will be a Pair of two integers from the utility header file. The two integers represent vertex and weight, which is actually the shortest distances and this weight property will function as the priority to the elements. So, the vertices are placed in the priority queue based on their weight property. We will have to code the operations based on this weight property. Now the functionalities that we need from our priority queue are –

• Insert – We will insert |V| elements into the Priority Queue.
• Extract Min – We return the top-most element from the Binary Heap and delete it. Finally we make the neccessary
• Decrease Priority – We decrease the priority of an element in the priority queue when we find a shorter path, as known as Relaxation.

If you know the working of the Binary Heap and have a little knowledge about the STL Library, you can code the Priority Queue in about 2-5 hours. You can keep referring to the internet of various functions and the syntaxes and any other doubts you have. Typing the code doesn’t take long, but debugging it and making it work takes the most time. That’s how you learn coding. Try your best, work on it for hours, if you don’t get it, take a break for 10-20 minutes… Come back and check my code below and try to figure out how close you were to getting it perfect…!

```/*
* Priority Queue implemented
* by a Binary Heap using
* C++ STL Vector, for
* Dijkstra's Algorithm
*
* Authored by,
* Vamsi Sangam
*/

#include <cstdio>
#include <vector>
#include <utility>

using namespace std;

// Inserts an element into the Queue
void enqueue(vector< pair<int, int> > * priorityQueue, pair<int, int> * entry)
{
(*priorityQueue).push_back(*entry);

int i = (*priorityQueue).size() - 1;
pair<int, int> temp;

while (i > 0) {
if ((*priorityQueue)[(i - 1) / 2].second > (*priorityQueue)[i].second) {
temp = (*priorityQueue)[(i - 1) / 2];
(*priorityQueue)[(i - 1) / 2] = (*priorityQueue)[i];
(*priorityQueue)[i] = temp;

i = (i - 1) / 2;
} else {
break;
}
}
}

// Iterates over the Queue to return the index of the element
int findByKey(vector< pair<int, int> > * priorityQueue, pair<int, int> entry)
{
int i;

for (i = 0; i < (*priorityQueue).size(); ++i) {
if ((*priorityQueue)[i].first == entry.first) {
break;
}
}

if (i != (*priorityQueue).size()) {
return i;
} else {
return -1;
}
}

// Decreases the priority of the element and makes neccessary adjustments
void decreasePriority(vector< pair<int, int> > * priorityQueue, int index, int newWeight)
{
(*priorityQueue)[index].second = newWeight;

int i = index;
pair<int, int> temp;

while (i > 0) {
if ((*priorityQueue)[(i - 1) / 2].second > (*priorityQueue)[i].second) {
temp = (*priorityQueue)[(i - 1) / 2];
(*priorityQueue)[(i - 1) / 2] = (*priorityQueue)[i];
(*priorityQueue)[i] = temp;

i = (i - 1) / 2;
} else {
break;
}
}
}

// Returns the minimum element, deletes it and makes neccessary changes
pair<int, int> extractMin(vector< pair<int, int> > * priorityQueue)
{
pair<int, int> min = (*priorityQueue)[0];
pair<int, int> temp;

// Swap first and last
temp = (*priorityQueue)[0];
(*priorityQueue)[0] = (*priorityQueue)[(*priorityQueue).size() - 1];
(*priorityQueue)[(*priorityQueue).size() - 1] = temp;

(*priorityQueue).pop_back();

int i = 0;
pair<int, int> parent;
pair<int, int> rightChild;
pair<int, int> leftChild;

while (i < (*priorityQueue).size()) {
parent = (*priorityQueue)[i];
printf("Currently at - (%d, %d)\n", parent.first, parent.second);

if (2 * (i + 1) < (*priorityQueue).size()) {
// both children exist
rightChild = (*priorityQueue)[2 * (i + 1)];
leftChild = (*priorityQueue)[2 * (i + 1) - 1];

if (parent.second < leftChild.second && parent.second < rightChild.second) {
break;
} else {
if (leftChild.second < rightChild.second) {
temp = (*priorityQueue)[2 * (i + 1) - 1];
(*priorityQueue)[2 * (i + 1) - 1] = (*priorityQueue)[i];
(*priorityQueue)[i] = temp;

i = 2 * (i + 1) - 1;
} else {
temp = (*priorityQueue)[2 * (i + 1) - 1];
(*priorityQueue)[2 * (i + 1) - 1] = (*priorityQueue)[i];
(*priorityQueue)[i] = temp;

i = 2 * (i + 1);
}
}
} else if ((2 * (i + 1)) >= (*priorityQueue).size() && (2 * (i + 1) - 1) < (*priorityQueue).size()) {
// only left child exists
leftChild = (*priorityQueue)[2 * (i + 1) - 1];

if (leftChild.second < parent.second) {
temp = (*priorityQueue)[2 * (i + 1) - 1];
(*priorityQueue)[2 * (i + 1) - 1] = (*priorityQueue)[i];
(*priorityQueue)[i] = temp;
}

break;
} else {
// no more children exist
break;
}
}

return min;
}

int main()
{
int n;

printf("Enter the size -\n");
scanf("%d", &n);

int vertex, weight, i;
vector< pair<int, int> > priorityQueue;
pair<int, int> entry;

for (i = 0; i < n; ++i) {
scanf("%d%d", &vertex, &weight);
entry = make_pair(vertex, weight);
enqueue(&priorityQueue, &entry);
}

printf("\n\nThe Priority Queue (Interpret as Binary Heap) -\n");

vector< pair<int, int> >::iterator itr = priorityQueue.begin();

while (itr != priorityQueue.end()) {
printf("(%d, %d) ", (*itr).first, (*itr).second);
++itr;
}
printf("\n");

pair<int, int> min = extractMin(&priorityQueue);
printf("\n\nExtract Min returned = (%d, %d), The Queue -\n", min.first, min.second);
itr = priorityQueue.begin();

while (itr != priorityQueue.end()) {
printf("(%d, %d) ", (*itr).first, (*itr).second);
++itr;
}
printf("\n");

decreasePriority(&priorityQueue, priorityQueue.size() - 1, 1);
printf("\n\ndecreasePriority() used, The Queue -\n");
itr = priorityQueue.begin();

while (itr != priorityQueue.end()) {
printf("(%d, %d) ", (*itr).first, (*itr).second);
++itr;
}
printf("\n");

return 0;
}
```

There are many other functionalities that a Priority Queue can give. But for now, we’ll need only these.

Joining the pieces

After you have your tool ready, you are all good to code Dijkstra’s Algorithm. Coding the Dijkstra’s Algorithm is easy but can go really weird. This is because you need to handle and co-ordinate many data structures at once. You’ll have to manage the adjacency list, the priority queue, the shortest distance array, and most importantly your loops…! You will surely end up spending more time in debugging the code, which is perfectly the right way of doing it. All throughout the coding, keep revising the step-by-step process explained above. If you don’t get it you, don’t fret, I put my code below. Before you look at my code, I would like to mention a few things –

• We cannot have infinity in programming, so the shortest distances are initialised to the highest possible value in integer range, present in a macro, INT_MAX, in the header file climits.
• The header file utility must be included to use pairs.
```/*
* Dijkstra's Algorithm in C++
* using Binary Heap as Priority
* Queue implemented using
* C++ STL Vector
*
* Authored by,
* Vamsi Sangam
*/

#include <cstdio>
#include <cstdlib>
#include <climits>
#include <vector>
#include <utility>

using namespace std;

// Our Vertex for Graph
struct node {
int vertex, weight;
struct node * next;
};

// To construct our Adjacency List
// Follows Head Insertion to give O(1) insertion
struct node * addEdge(struct node * head, int vertex, int weight)
{
struct node * p = (struct node *) calloc(1, sizeof(struct node));

p->vertex = vertex;
p->weight = weight;
p->next = head;

return p;
}

// Adds vertices to the Priority Queue, Vertices a represented
// as pairs of vertex number and its shortest distance.
// This is logically a Binary Heap Insertion
void enqueue(vector< pair<int, int> > * priorityQueue, pair<int, int> * entry)
{
(*priorityQueue).push_back(*entry);

int i = (*priorityQueue).size() - 1;
pair<int, int> temp;

while (i > 0) {
// Checking the priority of the parent, if greater, swap, else, we are done
if ((*priorityQueue)[(i - 1) / 2].second > (*priorityQueue)[i].second) {
temp = (*priorityQueue)[(i - 1) / 2];
(*priorityQueue)[(i - 1) / 2] = (*priorityQueue)[i];
(*priorityQueue)[i] = temp;

i = (i - 1) / 2;
} else {
break;
}
}
}

// Finds for a Vertex in the Priority Queue and
// returns its index as in its vector implementation
int findByKey(vector< pair<int, int> > * priorityQueue, pair<int, int> entry)
{
int i;

// Linear Search
for (i = 0; i < (*priorityQueue).size(); ++i) {
if ((*priorityQueue)[i].first == entry.first) {
break;
}
}

if (i != (*priorityQueue).size()) {
return i;
} else {
return -1;
}
}

// Decreases the priority of a given entry in the
// Priority Queue who's location is given by 'index'
// to 'newWeight' and re-arranges the Binary Heap
void decreasePriority(vector< pair<int, int> > * priorityQueue, int index, int newWeight)
{
// Decreasing Priority
(*priorityQueue)[index].second = newWeight;

int i = index;
pair<int, int> temp;

// Adjusting the Binary Heap, similar re-arrangement as done in enqueue()
while (i > 0) {
if ((*priorityQueue)[(i - 1) / 2].second > (*priorityQueue)[i].second) {
temp = (*priorityQueue)[(i - 1) / 2];
(*priorityQueue)[(i - 1) / 2] = (*priorityQueue)[i];
(*priorityQueue)[i] = temp;

i = (i - 1) / 2;
} else {
break;
}
}
}

// Picks up the minimum element of the Priority Queue and re-arranges
// the Binary Heap and finally returns the Minimum Element picked.
// Functionally resembles Delete operation in Binary Heap, but additionally
// returns the deleted element which is the minimum element.
pair<int, int> extractMin(vector< pair<int, int> > * priorityQueue)
{
pair<int, int> min = (*priorityQueue)[0];		// Min Element Stored
pair<int, int> temp;

// Swap first and last elements
temp = (*priorityQueue)[0];
(*priorityQueue)[0] = (*priorityQueue)[(*priorityQueue).size() - 1];
(*priorityQueue)[(*priorityQueue).size() - 1] = temp;

(*priorityQueue).pop_back();					// Deleted Min Element

int i = 0;
pair<int, int> parent;			// These three variables
pair<int, int> rightChild;		// are for readability of
pair<int, int> leftChild;		// the if conditions ahead

while (i < (*priorityQueue).size()) {
parent = (*priorityQueue)[i];

if (2 * (i + 1) < (*priorityQueue).size()) {
// both children exist
rightChild = (*priorityQueue)[2 * (i + 1)];
leftChild = (*priorityQueue)[2 * (i + 1) - 1];

if (parent.second < leftChild.second && parent.second < rightChild.second) {
// Parent has lesser priority than its children, so wer're done
break;
} else {
if (leftChild.second < rightChild.second) {
// Left-child has a lesser priority, so, swap
temp = (*priorityQueue)[2 * (i + 1) - 1];
(*priorityQueue)[2 * (i + 1) - 1] = (*priorityQueue)[i];
(*priorityQueue)[i] = temp;

i = 2 * (i + 1) - 1;
} else {
// Right-child has a lesser priority, so, swap
temp = (*priorityQueue)[2 * (i + 1) - 1];
(*priorityQueue)[2 * (i + 1) - 1] = (*priorityQueue)[i];
(*priorityQueue)[i] = temp;

i = 2 * (i + 1);
}
}
} else if ((2 * (i + 1)) >= (*priorityQueue).size() && (2 * (i + 1) - 1) < (*priorityQueue).size()) {
// only left child exists
leftChild = (*priorityQueue)[2 * (i + 1) - 1];

if (leftChild.second < parent.second) {
// Left-child has a lesser priority, so, swap
temp = (*priorityQueue)[2 * (i + 1) - 1];
(*priorityQueue)[2 * (i + 1) - 1] = (*priorityQueue)[i];
(*priorityQueue)[i] = temp;
}

break;
} else {
// no more children exist
break;
}
}

return min;
}

// The Dijkstra's Algorithm sub-routine which takes the Graph as Adjcaency List,
// number of vertices, a starting vertex, and an empty shortestDistances array as
// input and computest the shortest paths and fills them up in the shortestDistances array
void dijkstra(struct node * adjacencyList[], int vertices, int startVertex, pair<int, int> shortestDistances[])
{
int i;

// Initially no routes to vertices are know, so all are infinity,
// here, we initialize to a very high integer value
for (i = 0; i <= vertices; ++i) {
shortestDistances[i].first = INT_MAX;
shortestDistances[i].second = -1;
}

// Setting distance to source to zero
shortestDistances[startVertex].first = 0;
shortestDistances[startVertex].second = 0;

struct node * trav;
vector< pair<int, int> > priorityQueue;
pair<int, int> min;

// Making a the vertex that corresponds to the
// 'startVertex' which will have a priority of zero
// and we begin to intialise the Priority Queue
pair<int, int> entry = make_pair(startVertex, 0);
enqueue(&priorityQueue, &entry);

// Initialising Priority Queue
for (i = 1; i <= vertices; ++i) {
if (i == startVertex) {
continue;
} else {
// Priorities are set to a high integer value
entry = make_pair(i, INT_MAX);
enqueue(&priorityQueue, &entry);
}
}

// We have the tools ready..! Let's roll...!!
while (priorityQueue.size() != 0) {		// Untill there are vertices to be processed in Queue
min = extractMin(&priorityQueue);	// Greedily process the nearest vertex

trav = adjacencyList[min.first];	// Checking all the vertices adjacent to 'min'
while (trav != NULL) {
if (shortestDistances[trav->vertex].first > shortestDistances[min.first].first + trav->weight) {
// We have discovered a new shortest route
// Make the neccesary adjustments in data
entry = make_pair(trav->vertex, trav->weight);

int index = findByKey(&priorityQueue, entry);

decreasePriority(&priorityQueue, index, trav->weight);
shortestDistances[trav->vertex].first = shortestDistances[min.first].first + trav->weight;
shortestDistances[trav->vertex].second = min.first;
}

trav = trav->next;
}
}
}

void PrintShortestPath(pair<int, int> shortestDistances[], int vertex, int startVertex)
{
if (vertex == startVertex) {
printf("%d ", startVertex);
return;
} else {
PrintShortestPath(shortestDistances, shortestDistances[vertex].second, startVertex);
printf("%d ", vertex);
}
}

int main()
{
int vertices, edges, i, j, v1, v2, w;

printf("Enter the Number of Vertices -\n");
scanf("%d", &vertices);

printf("Enter the Number of Edges -\n");
scanf("%d", &edges);

struct node * adjacencyList[vertices + 1];
//Size is made (vertices + 1) to use the
//array as 1-indexed, for simplicity

//Must initialize your array
for (i = 0; i <= vertices; ++i) {
adjacencyList[i] = NULL;
}

printf("\n");

for (i = 1; i <= edges; ++i) {
scanf("%d%d%d", &v1, &v2, &w);
adjacencyList[v1] = addEdge(adjacencyList[v1], v2, w);
}

//Printing Adjacency List
printf("\nAdjacency List -\n\n");
for (i = 1; i <= vertices; ++i) {
printf("adjacencyList[%d] -> ", i);

struct node * temp = adjacencyList[i];

while (temp != NULL) {
printf("%d(%d) -> ", temp->vertex, temp->weight);
temp = temp->next;
}

printf("NULL\n");
}

int startVertex;

printf("Choose a Starting Vertex -\n");
scanf("%d", &startVertex);

pair<int, int> shortestDistances[vertices + 1];
// shortestDistances.first = shortest distance calculated
// shortestDistances.second = parent node

dijkstra(adjacencyList, vertices, startVertex, shortestDistances);

printf("\n\nDijkstra's Algorithm Used - Shortest Paths from Vertex %d -\n", startVertex);
for (i = 1; i <= vertices; ++i) {
printf("Vertex %d, Distance = %d, Parent = %d\n", i, shortestDistances[i].first, shortestDistances[i].second);
}
printf("\n");

PrintShortestPath(shortestDistances, vertices /* The Last Vertex */, startVertex);

return 0;
}
```

This is the Dijkstra’s Algorithm. The code is well commented with explanation. If you don’t understand anything or if you have any doubts. Feel free to comment them. Now talking about the complexity of Dijkstra’s Algorithm. We perform |V| enqueue operations into the priority queue, which take O(log N), here, N is |V|, so this takes O(|V| log |V|). And at most |E| decrease priority operations which will take O(|V|) time. The extract-min is also called |V| times which will take O(|V| log |V|) time. So, the overall complexity of Dijkstra’s Algorithm we wrote is O(|V| log |V| + |E||V|). Dijkstra’s Algorithm can be improved by using a Fibonacci Heap as a Priority Queue, where the complexity reduces to O(|V| log |V| + |E|). But the Fibonacci Heap is an incredibly advanced and difficult data structure to code. We’ll talk about that implementation later.

I really hope my post has helped you in understanding the Dijkstra’s Algorithm. If it did, let me know by commenting. I tried my best to keep it as simple as possible. If you have any doubts, you can comment them too and I will surely reply to them. This algorithm is a particularly tough one. So, good luck… Keep practicing and… Happy Coding…! 🙂

You may also like –

More in the Navigation Page ! →

Snakes and Ladders Game Code

Hello people…! In this post I will explain you how to find the shortest path to win the Snakes and Ladder game by using the Breadth First Search (BFS) Algorithm. If you don’t know the algorithm, I suggest you read my post about it. Now, Graph Theory has many applications and I love working with things that have real-world applications, well, off course the other data structures too have their uses but the speciality of Graph Theory is its applications have the closest association with our day-to-day activities. And to show you this and to set an example on how the BFS is actually put into action, I am taking up the example of the very popular game, Snakes and Ladder. Now, this game needs no introduction. We all must have played it in our childhood. I will explain you… How by using Graphs and BFS we can find the Shortest Path to win the game, and, we will state that path and the number of moves of dice it takes too. Have a good look at the Snakes and Ladder board below, we will be using it as an example throughout this post.

You can see we can reach the finish block by an number of ways, but how do you find out the best…? More importantly, how do you put it as code…? That’s what we are going to do. Now, think of how you can represent the game board in terms of a Graph, by Graph I mean in terms of Vertices and Edges. Don’t be too hard on yourself… Just take the first 7 blocks and try working out on paper what would be the Edge and what would be the Vertices. If you ponder upon this, it is very easy to tell that the numbered blocks on the game board will be our Vertices, then, what will be the Edges…? This depends on how you can go from one block to another on rolling the dice. For now, forget about the ladders and the snakes, just draw the graph for a small portion of the board. It should look somewhat similar to what is in the picture below –

As you see we would have six ways to leave block 1, depending on the dice roll. Same would be the case for block 2, or, block 3, and so on. Now, there is a very important point to be noted here… Remember, this is Directed Graph…! Because, once you roll 5 and go to block 6, there’s no way to come back. So, the directions are important. Now, let us push up the complexity a bit by adding a ladder to our above sketch in block 6, think what would happen to the edges…?

If you roll 5 from block 1 you will jump directly to block 27. So is for block 2 when you roll out 4, or, block 3 when you roll out 3 and so on. Now, “logically” speaking, the block 6 does not exists in our graph…! Think about the statement for a while. Whenever you reach block, you are directly jumping to block 27, you don’t stay there. Now, if you were constructing an Adjacency List for this graph…. In the list of adjacent vertices for Vertex 1, would you have Vertex 6 in the list, or Vertex 27…? Vertex 27 of course…! Being at Vertex 6 means being at Vertex 27…! That is why, our edge arrow did not end at Vertex 6… See it…? One more thing, in your Adjacency List, in the list of adjacent vertices for Vertex 6, what would you have…? Nothing…! Because you cannot come to a situation where you would have to stay on Vertex 6 and roll the dice. So the adjacent nodes list corresponding to Vertex 6 should be empty. These two things are very important, when you implement the Adjacency List for the Snake and Ladder board.

Same would be the case, if a snake was there at a block. Logically that block will not exist as a vertex in our adjacency list, and the corresponding edges must be removed. The only difference being that, the edge created due to a snake can lead you to a block of lower value.

We assume that getting caught by a snake is always unfavorable, and will not add to the progress of a short path. Just to get a better idea of the scenario of a ladder and snake, I have depicted what the Adjacency List would look like for the above two examples shown in the pictures.

That pic should really clarify all the confusion about the graph. If you still don’t get it, feel free to comment your query…! Now, what do we do once we have the Adjacency List ready…? We just call the Breadth First Search method on that list…! Wait… Really…?! That’s it…? Yes…! You see the hardest part here in solving the Snakes and Ladder by graphs is correctly determining what your Vertices and Edges are. Once you get that, all you have to do is the Breadth First Search in the resultant graph. Then you can get the shortest path from Vertex 1 to Vertex 100. Now, try getting the Adjacency Lit correct and simply call the BFS method. I’m sure you will succeed if you put in a little dedication. But for those who tried and failed. I have put my code below. Before you look at my code, there are a few logics I have used –

• In the beginning of the program, I added all theSorry for the bad snip…! edges as though the Game Board had no snakes or ladders at all (these number of edges is what I printed), then, I removed the
respective edges concerning the snakes and ladders one-by-one.
• When a Vertex ‘n’ has a ladder or a snake, we are supposed to
eplace the corresponding edges as I depicted in the pictures above, for that, I replaced the Vertex n‘s edge with the new value, in Vertices (n – 1), (n – 2), (n – 3), (n – 4), (n – 5), (n – 6). Because it is only in these vertices that you can find an edge of vertex n.
• I put all this replacing stuff in a function replace() which takes the Linked List and searches for a value ‘num’ and replaces it with the value ‘replaced_num’ when it finds it.
• I used an extra element in my array to make them 1 – index based.
• The number of moves to complete the shortest path would be the level of the last vertex, Vertex 100. Why…?! Think about it for a minute and you’ll get it…!
• I backtracked from Vertex 100, finding who it’s parent was, then, who the parent’s parent was and so on to find the path and finally printed it in reverse order.
```/*
* Snake and Ladder Shortest Path in C
* using Adjacency List and
* Breadth First Search.
*
* Authored by,
* Vamsi Sangam
*/

#include <stdio.h>
#include <stdlib.h>

struct node {
int val;
struct node * next;
};

struct node * add(struct node * head, int num)
{
struct node * p = (struct node *) malloc(sizeof(struct node));

p->val = num;
p->next = head;

return p;
}

void breadth_first_search(struct node * list[], int vertices, int parent[], int level[])
{
struct node * temp;
int i, par, lev, flag = 1;

lev = 0;
level[1] = lev;

while (flag) {
flag = 0;
for (i = 1; i <= vertices; ++i) {
if (level[i] == lev) {
flag = 1;
temp = list[i];
par = i;

while (temp != NULL) {
if (level[temp->val] != -1) {
temp = temp->next;
continue;
}

level[temp->val] = lev + 1;
parent[temp->val] = par;
temp = temp->next;
}
}
}

++lev;
}
}

void replace(struct node * head, int num, int replaced_num)
{
struct node * p = head;

while (p->next != NULL) {
if (p->val == num) {
break;
}

p = p->next;
}

p->val = replaced_num;
}

int main()
{
int vertices, edges, i, j, v1, v2;

vertices = 100;		//For a 10X10 board
edges = 0;			//Just to count the edges

struct node * list[vertices + 1];

int parent[vertices + 1];
int level[vertices + 1];

for (i = 0; i <= vertices; ++i) {
//Initialising our arrays
list[i] = NULL;
parent[i] = 0;
level[i] = -1;
}

for (i = 1; i <= vertices; ++i) {
for (j = 1; j <= 6 && j + i <= 100; ++j) {
list[i] = add(list[i], i + j);
++edges;
}
}

printf("Edges added so far = %d\n", edges);

int num_ladders, num_snakes;

printf("Enter the Number of Ladders and Snakes -\n");
scanf("%d%d", &num_ladders, &num_snakes);

printf("Enter the Ladder Edges -\n");
//Dealing with Ladder Edges
for (i = 0; i < num_ladders; ++i) {
scanf("%d%d", &v1, &v2);

j = v1 - 6;

if (j < 1) {
j = 1;
}

for (; j < v1; ++j) {
replace(list[j], v1, v2);
}
}

printf("Enter the Snake Edges -\n");
//Dealing with Snakes Edges
for (i = 0; i < num_snakes; ++i) {
scanf("%d%d", &v1, &v2);

j = v1 - 6;

if (j < 0) {
j = 0;
}

for (; j < v1; ++j) {
//Replacing Vertex value v1 by v2
replace(list[j], v1, v2);
}
}

//Printing Adjacency List
printf("\nAdjacency List -\n");
for (i = 1; i <= vertices; ++i) {
printf("%d -> ", i);

struct node * temp = list[i];

while (temp != NULL) {
printf("%d -> ", temp->val);
temp = temp->next;
}

printf("NULL\n");
}

breadth_first_search(list, vertices, parent, level);
printf("\nNumber of Moves required = %d\n", level[vertices]);

/*Just a little mechanism to print the shortest path*/
int path[level[vertices] + 1];

j = 100;
path[0] = j;

for (i = 1; i <= level[vertices]; ++i) {
path[i] = parent[j];
j = parent[j];
}

printf("\nShortest Path -\n");
for (i = level[vertices]; i >= 0; --i) {
printf("%d -> ", path[i]);
}
printf("Finish...!\n");

return 0;
}
```

You can think of extending this to a 20 ✗ 20 Snakes and Ladder board or an nn board. You just have to add n2 edges to the graph. Solving puzzles by Graph Theory is real fun. I hope this post helped you. If you have any doubts, feel free to comment them. Keep practicing… and… Happy Coding…! 🙂

You may also like –

More in the Navigation Page ! →