Knowee
Questions
Features
Study Tools

Write the Dijkstra algorithm for shortest path in a graph

Question

Write the Dijkstra algorithm for shortest path in a graph

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is the Dijkstra's algorithm for finding the shortest path in a graph:

  1. 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.

  2. 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.

  3. While sptSet doesn’t include all vertices:

    • Pick a vertex u which is not there in sptSet and has minimum distance value.
    • Include u to sptSet.
    • Update distance value of all adjacent vertices of u. For every adjacent vertex v, if sum of distance value of u (from source) and weight of edge u-v, is less than the distance value of v, then update the distance value of v.

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.

This problem has been solved

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.

1/3

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.