Closed kishan-25 closed 1 month ago
I hope this message finds you well! I’d like to request that this issue be assigned to me under the label of Hacktoberfest2024. Thank you!
Already exists. Bring something new n unique buddy. More repos are available for pull requests, and they’re part of Hacktoberfest 2024. Feel free to tag me in your Hacktoberfest certificate or badges LinkedIn post! 😄
👉 Follow my GitHub and connect with me on LinkedIn! ⭐ Star these repos to stay updated for next year’s Hacktoberfest!
Dijkstra's Algorithm Implementation Overview
Overview Dijkstra's algorithm is a well-known graph search algorithm designed to find the shortest path from a source node to all other nodes in a weighted graph. This implementation utilizes an adjacency matrix representation.
Key Components
Graph Representation
graph[53][53]
), wheregraph[i][j]
indicates the weight of the edge between nodesi
andj
. A weight of0
signifies no edge.Distance Array
distance[53]
) stores the shortest distance from the source node to each node in the graph. All distances are initialized to infinity (INT_MAX
), except for the source node, which is set to0
.Stat Array
stat[53]
) tracks which nodes have been finalized (i.e., the shortest path to them has been found). Initially, all nodes are marked as unvisited (false
).Finding the Minimum Distance Node
mindistance(int distance[], bool stat[])
function identifies the unvisited node with the smallest distance value, essential for selecting the next node to process.Algorithm Steps
Initialization
dijkstra
function initializes the distance and stat arrays. The distance to the source node is set to0
, while all other distances are set to infinity.Main Loop
0
to52
) in a loop:mindistance
to find the unvisited node with the smallest distance.stat
entry totrue
.Relaxation
m
, the algorithm checks each of its neighbors (nodesi
):i
is unvisited and there is an edge fromm
toi
(i.e.,graph[m][i]
is not0
), it checks if the distance toi
can be minimized by going throughm
.distance[i]
.Output
Example Usage
Conclusion This implementation effectively demonstrates Dijkstra's algorithm for finding the shortest paths in a graph. The choice of an adjacency matrix allows for straightforward graph representation and manipulation. The algorithm's time complexity is (O(V^2)), where (V) is the number of vertices, making it suitable for the graph size (53 vertices).