forked from Synergise-IIT-Bhubaneswar/Beta-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dijkstra's Algorithm in different languages
- Loading branch information
1 parent
0c57e76
commit 84c9c26
Showing
2 changed files
with
134 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/*Dijkstra's algorithm is the iterative algorithmic process to provide us with the shortest path | ||
from one specific starting node to all other nodes of a graph.Dijkstra's algorithm makes use of | ||
weights of the edges for finding the path that minimizes the total distance (Positive weights) among the | ||
source node and all other nodes. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <conio.h> | ||
#define INFINITY 9999 // limit defined | ||
#define MAX 10 | ||
|
||
void dijkstra(int G[MAX][MAX], int n, int startnode); | ||
int main() | ||
{ | ||
|
||
int G[MAX][MAX], i, j, n, u; | ||
printf("Enter no. of vertices:"); | ||
scanf("%d", &n); | ||
printf("\nEnter the adjacency matrix:\n"); | ||
for (i = 0; i < n; i++) | ||
for (j = 0; j < n; j++) | ||
scanf("%d", &G[i][j]); | ||
printf("\nEnter the starting node:"); | ||
scanf("%d", &u); | ||
dijkstra(G, n, u); | ||
getch(); | ||
return 0; | ||
} | ||
|
||
void dijkstra(int G[MAX][MAX], int n, int startnode) | ||
{ | ||
|
||
int cost[MAX][MAX], distance[MAX], pred[MAX]; | ||
int visited[MAX], count, mindistance, nextnode, i, j; | ||
// pred[] stores the predecessor of each node | ||
// count gives the number of nodes seen so far | ||
// create the cost matrix | ||
for (i = 0; i < n; i++) | ||
for (j = 0; j < n; j++) | ||
{ | ||
if (G[i][j] == 0) | ||
cost[i][j] = INFINITY; | ||
|
||
else | ||
cost[i][j] = G[i][j]; | ||
} | ||
|
||
// initialize pred[],distance[] and visited[] | ||
|
||
for (i = 0; i < n; i++) | ||
{ | ||
|
||
distance[i] = cost[startnode][i]; | ||
pred[i] = startnode; | ||
visited[i] = 0; | ||
} | ||
distance[startnode] = 0; | ||
visited[startnode] = 1; | ||
count = 1; | ||
|
||
while (count < n - 1) | ||
{ | ||
|
||
mindistance = INFINITY; | ||
// nextnode gives the node at minimum distance | ||
|
||
for (i = 0; i < n; i++) | ||
if (distance[i] < mindistance && !visited[i]) | ||
{ | ||
|
||
mindistance = distance[i]; | ||
nextnode = i; | ||
} | ||
|
||
// check if a better path exists through nextnode | ||
|
||
visited[nextnode] = 1; | ||
for (i = 0; i < n; i++) | ||
if (!visited[i]) | ||
|
||
if (mindistance + cost[nextnode][i] < distance[i]) | ||
{ | ||
|
||
distance[i] = mindistance + cost[nextnode][i]; | ||
pred[i] = nextnode; | ||
} | ||
|
||
count++; | ||
} | ||
|
||
// print the path and distance of each node | ||
|
||
for (i = 0; i < n; i++) | ||
if (i != startnode) | ||
{ | ||
|
||
printf("\nDistance of node%d=%d", i, distance[i]); | ||
printf("\nPath=%d", i); | ||
j = i; | ||
do | ||
{ | ||
|
||
j = pred[j]; | ||
printf("<-%d", j); | ||
|
||
} while (j != startnode); | ||
} | ||
} | ||
|
||
/* | ||
Time Complexity=o(V^2) | ||
Space Complexity=o(V^2) | ||
Output:- | ||
Enter no. of vertices:5 | ||
Enter the adjacency matrix: | ||
0 10 0 30 0 100 | ||
10 0 0 0 50 | ||
PS C:\Users\hp\Synergise2021\Beta-Algo\c\graph> gcc dijkstraalgo.c | ||
PS C:\Users\hp\Synergise2021\Beta-Algo\c\graph> ./a.exe | ||
10 0 20 0 30 | ||
30 10 0 0 20 | ||
Enter the starting node:0 | ||
Distance of node1=10 | ||
Path=1<-0 | ||
Distance of node2=50 | ||
Path=2<-3<-0 | ||
Distance of node3=30 | ||
Path=3<-0 | ||
Distance of node4=60 | ||
Path=4<-3<-0 | ||
*/ |
This file was deleted.
Oops, something went wrong.