diff --git a/algo1.md b/algo1.md new file mode 100644 index 0000000..db9db59 --- /dev/null +++ b/algo1.md @@ -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[] + diff --git a/vinay1.c b/vinay1.c new file mode 100644 index 0000000..a04ad35 --- /dev/null +++ b/vinay1.c @@ -0,0 +1,93 @@ +#include +#include +#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