Skip to content

Commit 7d0f15d

Browse files
author
myselfshravan
committed
readme up
1 parent dea059e commit 7d0f15d

File tree

4 files changed

+90
-126
lines changed

4 files changed

+90
-126
lines changed

11.dijkstras.c

Lines changed: 69 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,76 @@
1-
// C program for Dijkstra's single source shortest path
2-
// algorithm. The program is for adjacency matrix
3-
// representation of the graph
4-
5-
#include <limits.h>
6-
#include <stdbool.h>
71
#include <stdio.h>
8-
#include <time.h>
9-
// Number of vertices in the graph
10-
#define V 9
11-
12-
// A utility function to find the vertex with minimum
13-
// distance value, from the set of vertices not yet included
14-
// in shortest path tree
15-
int minDistance(int dist[], bool sptSet[])
16-
{
17-
// Initialize min value
18-
int min = INT_MAX, min_index;
19-
20-
for (int v = 0; v < V; v++)
21-
if (sptSet[v] == false && dist[v] <= min)
22-
min = dist[v], min_index = v;
23-
24-
return min_index;
25-
}
26-
27-
// A utility function to print the constructed distance
28-
// array
29-
void printSolution(int dist[])
2+
#define INFINITY 9999
3+
#define MAX 10
4+
void dijkstra(int G[MAX][MAX], int n, int startnode);
5+
int main()
306
{
31-
printf("Vertex \t\t Distance from Source\n");
32-
for (int i = 0; i < V; i++)
33-
printf("%d \t\t\t\t %d\n", i, dist[i]);
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;
3418
}
35-
36-
// Function that implements Dijkstra's single source
37-
// shortest path algorithm for a graph represented using
38-
// adjacency matrix representation
39-
void dijkstra(int graph[V][V], int src)
19+
void dijkstra(int G[MAX][MAX], int n, int startnode)
4020
{
41-
int dist[V]; // The output array. dist[i] will hold the
42-
// shortest
43-
// distance from src to i
44-
45-
bool sptSet[V]; // sptSet[i] will be true if vertex i is
46-
// included in shortest
47-
// path tree or shortest distance from src to i is
48-
// finalized
49-
50-
// Initialize all distances as INFINITE and stpSet[] as
51-
// false
52-
for (int i = 0; i < V; i++)
53-
dist[i] = INT_MAX, sptSet[i] = false;
54-
55-
// Distance of source vertex from itself is always 0
56-
dist[src] = 0;
57-
58-
// Find shortest path for all vertices
59-
for (int count = 0; count < V - 1; count++)
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++)
6034
{
61-
// Pick the minimum distance vertex from the set of
62-
// vertices not yet processed. u is always equal to
63-
// src in the first iteration.
64-
int u = minDistance(dist, sptSet);
65-
66-
// Mark the picked vertex as processed
67-
sptSet[u] = true;
68-
69-
// Update dist value of the adjacent vertices of the
70-
// picked vertex.
71-
for (int v = 0; v < V; v++)
72-
73-
// Update dist[v] only if is not in sptSet,
74-
// there is an edge from u to v, and total
75-
// weight of path from src to v through u is
76-
// smaller than current value of dist[v]
77-
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
78-
dist[v] = dist[u] + graph[u][v];
35+
distance[i] = cost[startnode][i];
36+
pred[i] = startnode;
37+
visited[i] = 0;
7938
}
80-
81-
// print the constructed distance array
82-
printSolution(dist);
83-
}
84-
85-
// driver's code
86-
int main()
87-
{
88-
/* Let us create the example graph discussed above */
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}};
98-
99-
// Function call
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);
107-
108-
return 0;
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+
}
10976
}

12.Travelling_Salesman.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void put()
6565
int main()
6666
{
6767
get();
68-
printf("\n\nThe Path is:\n\n");
68+
printf("\n\nThe Path is: ");
6969
mincost(0);
7070
put();
7171
return 0;

README.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ Total cost of spanning tree = 8
211211
Example 1:
212212

213213
```bash
214-
Enter no. of vertices: 5
214+
Kruskal's algorithm in C
215+
========================
216+
Enter the no. of vertices: 5
215217
Enter the cost adjacency matrix:
216-
217218
0 1 2 0 1
218219
1 0 3 0 1
219220
2 3 0 6 5
220221
0 0 6 0 0
221222
1 1 5 0 0
222-
223223
The edges of Minimum Cost Spanning Tree are
224224
1 edge (1,2) = 1
225225
2 edge (1,5) = 1
@@ -255,22 +255,21 @@ Minimum cost = 13
255255
```bash
256256
Enter no. of vertices: 5
257257
Enter the adjacency matrix:
258-
0 10 0 30 100
259-
10 0 50 0 0
260-
0 50 0 20 10
261-
30 0 20 0 60
262-
100 0 10 60 0
263-
264-
Enter the starting node:0
265-
266-
Distance of node1=10
267-
Path=1<-0
268-
Distance of node2=50
269-
Path=2<-3<-0
270-
Distance of node3=30
271-
Path=3<-0
272-
Distance of node4=60
273-
Path=4<-2<-3<-0
258+
0 1 0 3 9
259+
1 0 5 0 0
260+
0 5 0 2 1
261+
3 0 2 0 6
262+
9 0 1 6 0
263+
264+
Enter the starting node: 0
265+
Distance of node1= 1
266+
Path= 1<-0
267+
Distance of node2= 5
268+
Path= 2<-3<-0
269+
Distance of node3= 3
270+
Path= 3<-0
271+
Distance of node4= 6
272+
Path= 4<-2<-3<-0
274273
```
275274

276275
## Travelling Salesman Input/Output:
@@ -301,9 +300,7 @@ The cost list is:
301300
99 10 99 6 99 99
302301
10 99 5 99 99 99
303302

304-
The Path is:
305-
306-
1->6->3->4->5->2->1
303+
The Path is: 1->6->3->4->5->2->1
307304

308305
Minimum cost: 46
309306
```

lab-manual/11.dijkstras.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ int main()
1111
for (i = 0; i < n; i++)
1212
for (j = 0; j < n; j++)
1313
scanf("%d", &G[i][j]);
14-
printf("\nEnter the starting node:");
14+
printf("\nEnter the starting node: ");
1515
scanf("%d", &u);
1616
dijkstra(G, n, u);
1717
return 0;

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