From e21a20371bafa5a2003222dae4d916ae3ed60b69 Mon Sep 17 00:00:00 2001 From: Vinay Date: Tue, 9 Oct 2018 18:21:13 +0530 Subject: [PATCH 1/2] added Dijkstras algorithm --- algo1.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 algo1.md 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[] + From ebee3c0097e25c4a3bd7fc3792b628ecc0dffe95 Mon Sep 17 00:00:00 2001 From: Vinay Date: Tue, 9 Oct 2018 20:22:52 +0530 Subject: [PATCH 2/2] added program in c for dijkstras algorithm --- vinay1.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 vinay1.c 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