-
Notifications
You must be signed in to change notification settings - Fork 29
added program in C #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vinay72
wants to merge
2
commits into
Sharpach:master
Choose a base branch
from
vinay72:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,28 @@ | ||
Dijkstra's algorithm | ||
|
||
In the following algorithm, the code u ← vertex in Q with min dist[u], searches for the vertex u in the vertex set Q that has the least dist[u] value. length(u, v) returns the length of the edge joining (i.e. the distance between) the two neighbor-nodes u and v. The variable alt on line 18 is the length of the path from the root node to the neighbor node v if it were to go through u. If this path is shorter than the current shortest path recorded for v, that current path is replaced with this alt path. The prev array is populated with a pointer to the "next-hop" node on the source graph to get the shortest route to the source. | ||
|
||
1 function Dijkstra(Graph, source): | ||
2 | ||
3 create vertex set Q | ||
4 | ||
5 for each vertex v in Graph: // Initialization | ||
6 dist[v] ← INFINITY // Unknown distance from source to v | ||
7 prev[v] ← UNDEFINED // Previous node in optimal path from source | ||
8 add v to Q // All nodes initially in Q (unvisited nodes) | ||
9 | ||
10 dist[source] ← 0 // Distance from source to source | ||
11 | ||
12 while Q is not empty: | ||
13 u ← vertex in Q with min dist[u] // Node with the least distance | ||
14 // will be selected first | ||
15 remove u from Q | ||
16 | ||
17 for each neighbor v of u: // where v is still in Q. | ||
18 alt ← dist[u] + length(u, v) | ||
19 if alt < dist[v]: // A shorter path to v has been found | ||
20 dist[v] ← alt | ||
21 prev[v] ← u | ||
22 | ||
23 return dist[], prev[] | ||
|
This file contains hidden or 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,93 @@ | ||
#include<stdio.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move your implementation file into the correct folder. Check CONTRIBUTING.md |
||
#include<conio.h> | ||
#define INFINITY 9999 | ||
#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); | ||
|
||
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); | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need an algorithm description. Please check CONTRIBUTING.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dijkstra’s Algorithm
Create cost matrix C[ ][ ] from adjacency matrix adj[ ][ ]. C[i][j] is the cost of going from vertex i to vertex j. If there is no edge between vertices i and j then C[i][j] is infinity.
Array visited[ ] is initialized to zero.
for(i=0;i<n;i++)
visited[i]=0;
If the vertex 0 is the source vertex then visited[0] is marked as 1.
Create the distance matrix, by storing the cost of vertices from vertex no. 0 to n-1 from the source vertex 0.
for(i=1;i<n;i++)
distance[i]=cost[0][i];
Initially, distance of source vertex is taken as 0. i.e. distance[0]=0;
for(i=1;i<n;i++)
– Choose a vertex w, such that distance[w] is minimum and visited[w] is 0. Mark visited[w] as 1.
– Recalculate the shortest distance of remaining vertices from the source.
– Only, the vertices not marked as 1 in array visited[ ] should be considered for recalculation of distance. i.e. for each vertex v
if(visited[v]==0)
distance[v]=min(distance[v],
distance[w]+cost[w][v])
Time Complexity
The program contains two nested loops each of which has a complexity of O(n). n is number of vertices. So the complexity of algorithm is O(n2).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I've mistyped the word "need". We don't need an algorithm description.