Skip to content

Commit c6f6674

Browse files
author
myselfshravan
committed
prog 11 to lab mannual
1 parent c952a4b commit c6f6674

File tree

2 files changed

+97
-23
lines changed

2 files changed

+97
-23
lines changed

11.dijkstras.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <limits.h>
66
#include <stdbool.h>
77
#include <stdio.h>
8-
#include<time.h>
8+
#include <time.h>
99
// Number of vertices in the graph
1010
#define V 9
1111

@@ -39,7 +39,7 @@ void printSolution(int dist[])
3939
void dijkstra(int graph[V][V], int src)
4040
{
4141
int dist[V]; // The output array. dist[i] will hold the
42-
// shortest
42+
// shortest
4343
// distance from src to i
4444

4545
bool sptSet[V]; // sptSet[i] will be true if vertex i is
@@ -56,7 +56,8 @@ void dijkstra(int graph[V][V], int src)
5656
dist[src] = 0;
5757

5858
// Find shortest path for all vertices
59-
for (int count = 0; count < V - 1; count++) {
59+
for (int count = 0; count < V - 1; count++)
60+
{
6061
// Pick the minimum distance vertex from the set of
6162
// vertices not yet processed. u is always equal to
6263
// src in the first iteration.
@@ -73,9 +74,7 @@ void dijkstra(int graph[V][V], int src)
7374
// there is an edge from u to v, and total
7475
// weight of path from src to v through u is
7576
// smaller than current value of dist[v]
76-
if (!sptSet[v] && graph[u][v]
77-
&& dist[u] != INT_MAX
78-
&& dist[u] + graph[u][v] < dist[v])
77+
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
7978
dist[v] = dist[u] + graph[u][v];
8079
}
8180

@@ -87,25 +86,24 @@ void dijkstra(int graph[V][V], int src)
8786
int main()
8887
{
8988
/* Let us create the example graph discussed above */
90-
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
91-
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
92-
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
93-
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
94-
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
95-
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
96-
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
97-
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
98-
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
89+
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
90+
{4, 0, 8, 0, 0, 0, 0, 11, 0},
91+
{0, 8, 0, 7, 0, 4, 0, 0, 2},
92+
{0, 0, 7, 0, 9, 14, 0, 0, 0},
93+
{0, 0, 0, 9, 0, 10, 0, 0, 0},
94+
{0, 0, 4, 14, 10, 0, 2, 0, 0},
95+
{0, 0, 0, 0, 0, 2, 0, 1, 6},
96+
{8, 11, 0, 0, 0, 0, 1, 0, 7},
97+
{0, 0, 2, 0, 0, 0, 6, 7, 0}};
9998

10099
// Function call
101-
clock_t start, end;
102-
float cpu_time_used;
103-
start = clock();
104-
dijkstra(graph, 0);
105-
end = clock();
106-
cpu_time_used = ((float)(end - start)) / CLOCKS_PER_SEC;
107-
printf("\nTime complexity is: %f", cpu_time_used);
108-
100+
clock_t start, end;
101+
float cpu_time_used;
102+
start = clock();
103+
dijkstra(graph, 0);
104+
end = clock();
105+
cpu_time_used = ((float)(end - start)) / CLOCKS_PER_SEC;
106+
printf("\nTime complexity is: %f", cpu_time_used);
109107

110108
return 0;
111109
}

lab-manual/11.dijkstras.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <stdio.h>
2+
#define INFINITY 9999
3+
#define MAX 10
4+
void dijkstra(int G[MAX][MAX], int n, int startnode);
5+
int main()
6+
{
7+
int G[MAX][MAX], i, j, n, u;
8+
printf("Enter no. of vertices:");
9+
scanf("%d", &n);
10+
printf("\nEnter the adjacency matrix:\n");
11+
for (i = 0; i < n; i++)
12+
for (j = 0; j < n; j++)
13+
scanf("%d", &G[i][j]);
14+
printf("\nEnter the starting node:");
15+
scanf("%d", &u);
16+
dijkstra(G, n, u);
17+
return 0;
18+
}
19+
void dijkstra(int G[MAX][MAX], int n, int startnode)
20+
{
21+
int cost[MAX][MAX], distance[MAX], pred[MAX];
22+
int visited[MAX], count, mindistance, nextnode, i, j;
23+
// pred[] stores the predecessor of each node
24+
// count gives the number of nodes seen so far
25+
// create the cost matrix
26+
for (i = 0; i < n; i++)
27+
for (j = 0; j < n; j++)
28+
if (G[i][j] == 0)
29+
cost[i][j] = INFINITY;
30+
else
31+
cost[i][j] = G[i][j];
32+
// initialize pred[],distance[] and visited[]
33+
for (i = 0; i < n; i++)
34+
{
35+
distance[i] = cost[startnode][i];
36+
pred[i] = startnode;
37+
visited[i] = 0;
38+
}
39+
distance[startnode] = 0;
40+
visited[startnode] = 1;
41+
count = 1;
42+
while (count < n - 1)
43+
{
44+
mindistance = INFINITY;
45+
// nextnode gives the node at minimum distance
46+
for (i = 0; i < n; i++)
47+
if (distance[i] < mindistance && !visited[i])
48+
{
49+
mindistance = distance[i];
50+
nextnode = i;
51+
}
52+
// check if a better path exists through nextnode
53+
visited[nextnode] = 1;
54+
for (i = 0; i < n; i++)
55+
if (!visited[i])
56+
if (mindistance + cost[nextnode][i] < distance[i])
57+
{
58+
distance[i] = mindistance + cost[nextnode][i];
59+
pred[i] = nextnode;
60+
}
61+
count++;
62+
}
63+
// print the path and distance of each node
64+
for (i = 0; i < n; i++)
65+
if (i != startnode)
66+
{
67+
printf("\nDistance of node%d=%d", i, distance[i]);
68+
printf("\nPath=%d", i);
69+
j = i;
70+
do
71+
{
72+
j = pred[j];
73+
printf("<-%d", j);
74+
} while (j != startnode);
75+
}
76+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy