# Greedy Algorithms | Set 8 (Dijkstra’s Algorithm for Adjacency List Representation)

We recommend to read following two posts as a prerequisite of this post.

We have discussed Dijkstra’s algorithm and its implementation for adjacency matrix representation of graphs. The time complexity for the matrix representation is O(V^2). In this post, O(ELogV) algorithm for adjacency list representation is discussed.

As discussed in the previous post, in Dijkstra’s algorithm, two sets are maintained, one set contains list of vertices already included in SPT (Shortest Path Tree), other set contains vertices not yet included. With adjacency list representation, all vertices of a graph can be traversed in O(V+E) time using BFS. The idea is to traverse all vertices of graph using BFS and use a Min Heap to store the vertices not yet included in SPT (or the vertices for which shortest distance is not finalized yet).  Min Heap is used as a priority queue to get the minimum distance vertex from set of not yet included vertices. Time complexity of operations like extract-min and decrease-key value is O(LogV) for Min Heap.

Following are the detailed steps.
1) Create a Min Heap of size V where V is the number of vertices in the given graph. Every node of min heap contains vertex number and distance value of the vertex.
2) Initialize Min Heap with source vertex as root (the distance value assigned to source vertex is 0). The distance value assigned to all other vertices is INF (infinite).
3) While Min Heap is not empty, do following
…..a) Extract the vertex with minimum distance value node from Min Heap. Let the extracted vertex be u.
…..b) For every adjacent vertex v of u, check if v is in Min Heap. If v is in Min Heap and distance value is more than weight of u-v plus distance value of u, then update the distance value of v.

Let us understand with the following example. Let the given source vertex be 0

Initially, distance value of source vertex is 0 and INF (infinite) for all other vertices. So source vertex is extracted from Min Heap and distance values of vertices adjacent to 0 (1 and 7) are updated. Min Heap contains all vertices except vertex 0.
The vertices in green color are the vertices for which minimum distances are finalized and are not in Min Heap

Since distance value of vertex 1 is minimum among all nodes in Min Heap, it is extracted from Min Heap and distance values of vertices adjacent to 1 are updated (distance is updated if the a vertex is not in Min Heap and distance through 1 is shorter than the previous distance). Min Heap contains all vertices except vertex 0 and 1.

Pick the vertex with minimum distance value from min heap. Vertex 7 is picked. So min heap now contains all vertices except 0, 1 and 7. Update the distance values of adjacent vertices of 7. The distance value of vertex 6 and 8 becomes finite (15 and 9 respectively).

Pick the vertex with minimum distance from min heap. Vertex 6 is picked. So min heap now contains all vertices except 0, 1, 7 and 6. Update the distance values of adjacent vertices of 6. The distance value of vertex 5 and 8 are updated.

Above steps are repeated till min heap doesn’t become empty. Finally, we get the following shortest path tree.

## C++

```// C / C++ program for Dijkstra's shortest path algorithm for adjacency
// list representation of graph

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

// A structure to represent a node in adjacency list
{
int dest;
int weight;
};

// A structure to represent an adjacency liat
{
};

// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
};

// A utility function to create a new adjacency list node
{
newNode->dest = dest;
newNode->weight = weight;
newNode->next = NULL;
return newNode;
}

// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;

// Create an array of adjacency lists.  Size of array will be V

// Initialize each adjacency list as empty by making head as NULL
for (int i = 0; i < V; ++i)

return graph;
}

// Adds an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest, int weight)
{
// list of src.  The node is added at the begining

// Since graph is undirected, add an edge from dest to src also
}

// Structure to represent a min heap node
struct MinHeapNode
{
int  v;
int dist;
};

// Structure to represent a min heap
struct MinHeap
{
int size;      // Number of heap nodes present currently
int capacity;  // Capacity of min heap
int *pos;     // This is needed for decreaseKey()
struct MinHeapNode **array;
};

// A utility function to create a new Min Heap Node
struct MinHeapNode* newMinHeapNode(int v, int dist)
{
struct MinHeapNode* minHeapNode =
(struct MinHeapNode*) malloc(sizeof(struct MinHeapNode));
minHeapNode->v = v;
minHeapNode->dist = dist;
return minHeapNode;
}

// A utility function to create a Min Heap
struct MinHeap* createMinHeap(int capacity)
{
struct MinHeap* minHeap =
(struct MinHeap*) malloc(sizeof(struct MinHeap));
minHeap->pos = (int *)malloc(capacity * sizeof(int));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array =
(struct MinHeapNode**) malloc(capacity * sizeof(struct MinHeapNode*));
return minHeap;
}

// A utility function to swap two nodes of min heap. Needed for min heapify
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
{
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}

// A standard function to heapify at given idx
// This function also updates position of nodes when they are swapped.
// Position is needed for decreaseKey()
void minHeapify(struct MinHeap* minHeap, int idx)
{
int smallest, left, right;
smallest = idx;
left = 2 * idx + 1;
right = 2 * idx + 2;

if (left < minHeap->size &&
minHeap->array[left]->dist < minHeap->array[smallest]->dist )
smallest = left;

if (right < minHeap->size &&
minHeap->array[right]->dist < minHeap->array[smallest]->dist )
smallest = right;

if (smallest != idx)
{
// The nodes to be swapped in min heap
MinHeapNode *smallestNode = minHeap->array[smallest];
MinHeapNode *idxNode = minHeap->array[idx];

// Swap positions
minHeap->pos[smallestNode->v] = idx;
minHeap->pos[idxNode->v] = smallest;

// Swap nodes
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);

minHeapify(minHeap, smallest);
}
}

// A utility function to check if the given minHeap is ampty or not
int isEmpty(struct MinHeap* minHeap)
{
return minHeap->size == 0;
}

// Standard function to extract minimum node from heap
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
{
if (isEmpty(minHeap))
return NULL;

// Store the root node
struct MinHeapNode* root = minHeap->array[0];

// Replace root node with last node
struct MinHeapNode* lastNode = minHeap->array[minHeap->size - 1];
minHeap->array[0] = lastNode;

// Update position of last node
minHeap->pos[root->v] = minHeap->size-1;
minHeap->pos[lastNode->v] = 0;

// Reduce heap size and heapify root
--minHeap->size;
minHeapify(minHeap, 0);

return root;
}

// Function to decreasy dist value of a given vertex v. This function
// uses pos[] of min heap to get the current index of node in min heap
void decreaseKey(struct MinHeap* minHeap, int v, int dist)
{
// Get the index of v in  heap array
int i = minHeap->pos[v];

// Get the node and update its dist value
minHeap->array[i]->dist = dist;

// Travel up while the complete tree is not hepified.
// This is a O(Logn) loop
while (i && minHeap->array[i]->dist < minHeap->array[(i - 1) / 2]->dist)
{
// Swap this node with its parent
minHeap->pos[minHeap->array[i]->v] = (i-1)/2;
minHeap->pos[minHeap->array[(i-1)/2]->v] = i;
swapMinHeapNode(&minHeap->array[i],  &minHeap->array[(i - 1) / 2]);

// move to parent index
i = (i - 1) / 2;
}
}

// A utility function to check if a given vertex
// 'v' is in min heap or not
bool isInMinHeap(struct MinHeap *minHeap, int v)
{
if (minHeap->pos[v] < minHeap->size)
return true;
return false;
}

// A utility function used to print the solution
void printArr(int dist[], int n)
{
printf("Vertex   Distance from Source\n");
for (int i = 0; i < n; ++i)
printf("%d \t\t %d\n", i, dist[i]);
}

// The main function that calulates distances of shortest paths from src to all
// vertices. It is a O(ELogV) function
void dijkstra(struct Graph* graph, int src)
{
int V = graph->V;// Get the number of vertices in graph
int dist[V];      // dist values used to pick minimum weight edge in cut

// minHeap represents set E
struct MinHeap* minHeap = createMinHeap(V);

// Initialize min heap with all vertices. dist value of all vertices
for (int v = 0; v < V; ++v)
{
dist[v] = INT_MAX;
minHeap->array[v] = newMinHeapNode(v, dist[v]);
minHeap->pos[v] = v;
}

// Make dist value of src vertex as 0 so that it is extracted first
minHeap->array[src] = newMinHeapNode(src, dist[src]);
minHeap->pos[src]   = src;
dist[src] = 0;
decreaseKey(minHeap, src, dist[src]);

// Initially size of min heap is equal to V
minHeap->size = V;

// In the followin loop, min heap contains all nodes
// whose shortest distance is not yet finalized.
while (!isEmpty(minHeap))
{
// Extract the vertex with minimum distance value
struct MinHeapNode* minHeapNode = extractMin(minHeap);
int u = minHeapNode->v; // Store the extracted vertex number

// Traverse through all adjacent vertices of u (the extracted
// vertex) and update their distance values
while (pCrawl != NULL)
{
int v = pCrawl->dest;

// If shortest distance to v is not finalized yet, and distance to v
// through u is less than its previously calculated distance
if (isInMinHeap(minHeap, v) && dist[u] != INT_MAX &&
pCrawl->weight + dist[u] < dist[v])
{
dist[v] = dist[u] + pCrawl->weight;

// update distance value in min heap also
decreaseKey(minHeap, v, dist[v]);
}
pCrawl = pCrawl->next;
}
}

// print the calculated shortest distances
printArr(dist, V);
}

// Driver program to test above functions
int main()
{
// create the graph given in above fugure
int V = 9;
struct Graph* graph = createGraph(V);

dijkstra(graph, 0);

return 0;
}
```

## Python

```# A Python program for Dijkstra's shortest
# list representation of graph

from collections import defaultdict
import sys

class Heap():

def __init__(self):
self.array = []
self.size = 0
self.pos = []

def newMinHeapNode(self, v, dist):
minHeapNode = [v, dist]
return minHeapNode

# A utility function to swap two nodes
# of min heap. Needed for min heapify
def swapMinHeapNode(self,a, b):
t = self.array[a]
self.array[a] = self.array[b]
self.array[b] = t

# A standard function to heapify at given idx
# This function also updates position of nodes
# when they are swapped.Position is needed
# for decreaseKey()
def minHeapify(self, idx):
smallest = idx
left = 2*idx + 1
right = 2*idx + 2

if left < self.size and self.array[left][1] \
< self.array[smallest][1]:
smallest = left

if right < self.size and self.array[right][1]\
< self.array[smallest][1]:
smallest = right

# The nodes to be swapped in min
# heap if idx is not smallest
if smallest != idx:

# Swap positions
self.pos[ self.array[smallest][0] ] = idx
self.pos[ self.array[idx][0] ] = smallest

# Swap nodes
self.swapMinHeapNode(smallest, idx)

self.minHeapify(smallest)

# Standard function to extract minimum
# node from heap
def extractMin(self):

# Return NULL wif heap is empty
if self.isEmpty() == True:
return

# Store the root node
root = self.array[0]

# Replace root node with last node
lastNode = self.array[self.size - 1]
self.array[0] = lastNode

# Update position of last node
self.pos[lastNode[0]] = 0
self.pos[root[0]] = self.size - 1

# Reduce heap size and heapify root
self.size -= 1
self.minHeapify(0)

return root

def isEmpty(self):
return True if self.size == 0 else False

def decreaseKey(self, v, dist):

# Get the index of v in  heap array

i = self.pos[v]

# Get the node and update its dist value
self.array[i][1] = dist

# Travel up while the complete tree is
# not hepified. This is a O(Logn) loop
while i > 0 and self.array[i][1] < self.array[(i - 1) / 2][1]:

# Swap this node with its parent
self.pos[ self.array[i][0] ] = (i-1)/2
self.pos[ self.array[(i-1)/2][0] ] = i
self.swapMinHeapNode(i, (i - 1)/2 )

# move to parent index
i = (i - 1) / 2;

# A utility function to check if a given
# vertex 'v' is in min heap or not
def isInMinHeap(self, v):

if self.pos[v] < self.size:
return True
return False

def printArr(dist, n):
print "Vertex\tDistance from source"
for i in range(n):
print "%d\t\t%d" % (i,dist[i])

class Graph():

def __init__(self, V):
self.V = V
self.graph = defaultdict(list)

# Adds an edge to an undirected graph

# Add an edge from src to dest.  A new node
# node is added at the begining. The first
# element of the node has the destination
# and the second elements has the weight
newNode = [dest, weight]
self.graph[src].insert(0, newNode)

# Since graph is undirected, add an edge
# from dest to src also
newNode = [src, weight]
self.graph[dest].insert(0, newNode)

# The main function that calulates distances
# of shortest paths from src to all vertices.
# It is a O(ELogV) function
def dijkstra(self, src):

V = self.V  # Get the number of vertices in graph
dist = []   # dist values used to pick minimum
# weight edge in cut

# minHeap represents set E
minHeap = Heap()

#  Initialize min heap with all vertices.
# dist value of all vertices
for v in range(V):
dist.append(sys.maxint)
minHeap.array.append( minHeap.newMinHeapNode(v, dist[v]) )
minHeap.pos.append(v)

# Make dist value of src vertex as 0 so
# that it is extracted first
minHeap.pos[src] = src
dist[src] = 0
minHeap.decreaseKey(src, dist[src])

# Initially size of min heap is equal to V
minHeap.size = V;

# In the following loop, min heap contains all nodes
# whose shortest distance is not yet finalized.
while minHeap.isEmpty() == False:

# Extract the vertex with minimum distance value
newHeapNode = minHeap.extractMin()
u = newHeapNode[0]

# Traverse through all adjacent vertices of
# u (the extracted vertex) and update their
# distance values
for pCrawl in self.graph[u]:

v = pCrawl[0]

# If shortest distance to v is not finalized
# yet, and distance to v through u is less
# than its previously calculated distance
if minHeap.isInMinHeap(v) and dist[u] != sys.maxint and \
pCrawl[1] + dist[u] < dist[v]:
dist[v] = pCrawl[1] + dist[u]

# update distance value
# in min heap also
minHeap.decreaseKey(v, dist[v])

printArr(dist,V)

# Driver program to test the above functions
graph = Graph(9)
graph.dijkstra(0)

# This code is contributed by Divyanshu Mehta
```

Output:
```Vertex   Distance from Source
0                0
1                4
2                12
3                19
4                21
5                11
6                9
7                8
8                14```

Time Complexity: The time complexity of the above code/algorithm looks O(V^2) as there are two nested while loops. If we take a closer look, we can observe that the statements in inner loop are executed O(V+E) times (similar to BFS). The inner loop has decreaseKey() operation which takes O(LogV) time. So overall time complexity is O(E+V)*O(LogV) which is O((E+V)*LogV) = O(ELogV)
Note that the above code uses Binary Heap for Priority Queue implementation. Time complexity can be reduced to O(E + VLogV) using Fibonacci Heap. The reason is, Fibonacci Heap takes O(1) time for decrease-key operation while Binary Heap takes O(Logn) time.

Notes:
1) The code calculates shortest distance, but doesn’t calculate the path information. We can create a parent array, update the parent array when distance is updated (like prim’s implementation) and use it show the shortest path from source to different vertices.

2) The code is for undirected graph, same dijekstra function can be used for directed graphs also.

3) The code finds shortest distances from source to all vertices. If we are interested only in shortest distance from source to a single target, we can break the for loop when the picked minimum distance vertex is equal to target (Step 3.a of algorithm).

4) Dijkstra’s algorithm doesn’t work for graphs with negative weight edges. For graphs with negative weight edges, Bellman–Ford algorithm can be used, we will soon be discussing it as a separate post.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
4 Average Difficulty : 4/5.0
Based on 48 vote(s)