Write the Dijkstra algorithm for shortest path in a graph
Question
Write the Dijkstra algorithm for shortest path in a graph
Solution
Sure, here is the Dijkstra's algorithm for finding the shortest path in a graph:
-
Create a set
sptSet(shortest path tree set) that keeps track of vertices included in shortest path tree, i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty. -
Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.
-
While
sptSetdoesn’t include all vertices:- Pick a vertex
uwhich is not there insptSetand has minimum distance value. - Include
utosptSet. - Update distance value of all adjacent vertices of
u. For every adjacent vertexv, if sum of distance value ofu(from source) and weight of edgeu-v, is less than the distance value ofv, then update the distance value ofv.
- Pick a vertex
Here is a Python implementation of the above algorithm:
import sys
class Graph():
def __init__(self, vertices):
self.V = vertices
self.graph = [[0 for column in range(vertices)] for row in range(vertices)]
def printSolution(self, dist):
print("Vertex \tDistance from Source")
for node in range(self.V):
print(node, "\t", dist[node])
def minDistance(self, dist, sptSet):
min = sys.maxsize
for v in range(self.V):
if dist[v] < min and sptSet[v] == False:
min = dist[v]
min_index = v
return min_index
def dijkstra(self, src):
dist = [sys.maxsize] * self.V
dist[src] = 0
sptSet = [False] * self.V
for cout in range(self.V):
u = self.minDistance(dist, sptSet)
sptSet[u] = True
for v in range(self.V):
if (self.graph[u][v] > 0 and sptSet[v] == False and dist[v] > dist[u] + self.graph[u][v]):
dist[v] = dist[u] + self.graph[u][v]
self.printSolution(dist)
This code creates a class Graph that represents a graph. The dijkstra function implements Dijkstra's single source shortest path algorithm for a graph represented using adjacency matrix representation.
Similar Questions
From a given vertex in a weighted connected graph, find shortest paths to other vertices using Dijkstra’s algorithm.
Dijkstra's algorithm
• Analyze the time complexity of Dijkstra's algorithm and discuss how it changes with different graph representations.
How does Dijkstra's algorithm find the shortest path in a graph?A) By brute force checking all possible pathsB) By maintaining a priority queue of nodes and their tentative distances from the start nodeC) By calculating the average distance between nodesD) By finding the maximum flow in a graph
What is a crucial requirement for Dijkstra’s algorithm (for finding shortest path) to work correctly?Select one:a. All edge weights in the graph must be equal.b. The graph must be fully connected.c. The graph must be a tree.d. The graph must not contain any negative edge weights.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.