Skip to content

Commit 1262bcd

Browse files
author
myselfshravan
committed
added prims
1 parent fe2d435 commit 1262bcd

File tree

3 files changed

+290
-5
lines changed

3 files changed

+290
-5
lines changed

README.md

Lines changed: 205 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,209 @@
1-
## DAA Lab
1+
## Algorithm Lab 4th Sem
22

3-
# [All Sorting Algorithms](https://www.interviewkickstart.com/learn/sorting-algorithms)
3+
### [All Sorting Algorithms](https://www.interviewkickstart.com/learn/sorting-algorithms)
44

5-
# [Merge Sort](https://www.interviewkickstart.com/learn/merge-sort)
5+
### [Merge Sort](https://www.interviewkickstart.com/learn/merge-sort)
66

7-
# [Quick Sort](https://www.interviewkickstart.com/learn/quick-sort)
7+
### [Quick Sort](https://www.interviewkickstart.com/learn/quick-sort)
88

9+
---
10+
11+
## Algorithms Code in C
12+
13+
<details><summary><b>Prims</b></summary>
14+
15+
```c
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
19+
#define infinity 9999
20+
#define MAX 20
21+
22+
int G[MAX][MAX], spanning[MAX][MAX], n;
23+
24+
int prims();
25+
26+
int main()
27+
{
28+
int i, j, total_cost;
29+
printf("Enter no. of vertices:");
30+
scanf("%d", &n);
31+
printf("\nEnter the adjacency matrix:\n");
32+
for (i = 0; i < n; i++)
33+
for (j = 0; j < n; j++)
34+
scanf("%d", &G[i][j]);
35+
total_cost = prims();
36+
printf("\nspanning tree matrix:\n");
37+
for (i = 0; i < n; i++)
38+
{
39+
printf("\n");
40+
for (j = 0; j < n; j++)
41+
printf("%d\t", spanning[i][j]);
42+
}
43+
printf("\n\nTotal cost of spanning tree = %d", total_cost);
44+
return 0;
45+
}
46+
47+
int prims()
48+
{
49+
int cost[MAX][MAX];
50+
int u, v, min_distance, distance[MAX], from[MAX];
51+
int visited[MAX], no_of_edges, i, min_cost, j;
52+
// create cost[][] matrix,spanning[][]
53+
for (i = 0; i < n; i++)
54+
for (j = 0; j < n; j++)
55+
{
56+
if (G[i][j] == 0)
57+
cost[i][j] = infinity;
58+
else
59+
cost[i][j] = G[i][j];
60+
spanning[i][j] = 0;
61+
}
62+
// initialise visited[],distance[] and from[]
63+
distance[0] = 0;
64+
visited[0] = 1;
65+
for (i = 1; i < n; i++)
66+
{
67+
distance[i] = cost[0][i];
68+
from[i] = 0;
69+
visited[i] = 0;
70+
}
71+
min_cost = 0; // cost of spanning tree
72+
no_of_edges = n - 1; // no. of edges to be added
73+
while (no_of_edges > 0)
74+
{
75+
// find the vertex at minimum distance from the tree
76+
min_distance = infinity;
77+
for (i = 1; i < n; i++)
78+
if (visited[i] == 0 && distance[i] < min_distance)
79+
{
80+
v = i;
81+
min_distance = distance[i];
82+
}
83+
u = from[v];
84+
// insert the edge in spanning tree
85+
spanning[u][v] = distance[v];
86+
spanning[v][u] = distance[v];
87+
no_of_edges--;
88+
visited[v] = 1;
89+
// updated the distance[] array
90+
for (i = 1; i < n; i++)
91+
if (visited[i] == 0 && cost[i][v] < distance[i])
92+
{
93+
distance[i] = cost[i][v];
94+
from[i] = v;
95+
}
96+
min_cost = min_cost + cost[u][v];
97+
}
98+
return (min_cost);
99+
}
100+
```
101+
102+
</details>
103+
104+
<details><summary><b>Krushkal</b></summary>
105+
106+
```c
107+
#include <stdio.h>
108+
#include <stdlib.h>
109+
110+
int i, j, k, a, b, u, v, n, ne = 1;
111+
int min, mincost = 0, cost[9][9], parent[9];
112+
113+
int find(int);
114+
int uni(int, int);
115+
116+
void main()
117+
{
118+
printf("Kruskal's algorithm in C\n");
119+
printf("========================\n");
120+
121+
printf("Enter the no. of vertices: ");
122+
scanf("%d", &n);
123+
124+
printf("\nEnter the cost adjacency matrix:\n");
125+
for (i = 1; i <= n; i++)
126+
{
127+
for (j = 1; j <= n; j++)
128+
{
129+
scanf("%d", &cost[i][j]);
130+
if (cost[i][j] == 0)
131+
cost[i][j] = 999;
132+
}
133+
}
134+
135+
printf("The edges of Minimum Cost Spanning Tree are\n");
136+
while (ne < n)
137+
{
138+
for (i = 1, min = 999; i <= n; i++)
139+
{
140+
for (j = 1; j <= n; j++)
141+
{
142+
if (cost[i][j] < min)
143+
{
144+
min = cost[i][j];
145+
a = u = i;
146+
b = v = j;
147+
}
148+
}
149+
}
150+
151+
u = find(u);
152+
v = find(v);
153+
154+
if (uni(u, v))
155+
{
156+
printf("%d edge (%d,%d) = %d\n", ne++, a, b, min);
157+
mincost += min;
158+
}
159+
160+
cost[a][b] = cost[b][a] = 999;
161+
}
162+
163+
printf("\nMinimum cost = %d\n", mincost);
164+
}
165+
166+
int find(int i)
167+
{
168+
while (parent[i])
169+
i = parent[i];
170+
return i;
171+
}
172+
173+
int uni(int i, int j)
174+
{
175+
if (i != j)
176+
{
177+
parent[j] = i;
178+
return 1;
179+
}
180+
181+
return 0;
182+
}
183+
```
184+
185+
</details>
186+
187+
## Prims Input:
188+
189+
Enter no. of vertices: 5
190+
191+
Enter the adjacency matrix:
192+
193+
0 1 2 0 0
194+
1 0 2 0 4
195+
2 2 0 3 0
196+
0 0 3 0 2
197+
0 4 0 2 0
198+
199+
## Krushkal Input:
200+
201+
Enter no. of vertices: 5
202+
203+
Enter the cost adjacency matrix:
204+
205+
0 1 2 0 1
206+
1 0 3 0 1
207+
2 3 0 6 5
208+
0 0 6 0 0
209+
1 1 5 0 0

krushkal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void main()
1212
printf("Kruskal's algorithm in C\n");
1313
printf("========================\n");
1414

15-
printf("Enter the no. of vertices:\n");
15+
printf("Enter the no. of vertices: ");
1616
scanf("%d", &n);
1717

1818
printf("\nEnter the cost adjacency matrix:\n");

prims.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#define infinity 9999
5+
#define MAX 20
6+
7+
int G[MAX][MAX], spanning[MAX][MAX], n;
8+
9+
int prims();
10+
11+
int main()
12+
{
13+
int i, j, total_cost;
14+
printf("Enter no. of vertices:");
15+
scanf("%d", &n);
16+
printf("\nEnter the adjacency matrix:\n");
17+
for (i = 0; i < n; i++)
18+
for (j = 0; j < n; j++)
19+
scanf("%d", &G[i][j]);
20+
total_cost = prims();
21+
printf("\nspanning tree matrix:\n");
22+
for (i = 0; i < n; i++)
23+
{
24+
printf("\n");
25+
for (j = 0; j < n; j++)
26+
printf("%d\t", spanning[i][j]);
27+
}
28+
printf("\n\nTotal cost of spanning tree = %d", total_cost);
29+
return 0;
30+
}
31+
32+
int prims()
33+
{
34+
int cost[MAX][MAX];
35+
int u, v, min_distance, distance[MAX], from[MAX];
36+
int visited[MAX], no_of_edges, i, min_cost, j;
37+
// create cost[][] matrix,spanning[][]
38+
for (i = 0; i < n; i++)
39+
for (j = 0; j < n; j++)
40+
{
41+
if (G[i][j] == 0)
42+
cost[i][j] = infinity;
43+
else
44+
cost[i][j] = G[i][j];
45+
spanning[i][j] = 0;
46+
}
47+
// initialise visited[],distance[] and from[]
48+
distance[0] = 0;
49+
visited[0] = 1;
50+
for (i = 1; i < n; i++)
51+
{
52+
distance[i] = cost[0][i];
53+
from[i] = 0;
54+
visited[i] = 0;
55+
}
56+
min_cost = 0; // cost of spanning tree
57+
no_of_edges = n - 1; // no. of edges to be added
58+
while (no_of_edges > 0)
59+
{
60+
// find the vertex at minimum distance from the tree
61+
min_distance = infinity;
62+
for (i = 1; i < n; i++)
63+
if (visited[i] == 0 && distance[i] < min_distance)
64+
{
65+
v = i;
66+
min_distance = distance[i];
67+
}
68+
u = from[v];
69+
// insert the edge in spanning tree
70+
spanning[u][v] = distance[v];
71+
spanning[v][u] = distance[v];
72+
no_of_edges--;
73+
visited[v] = 1;
74+
// updated the distance[] array
75+
for (i = 1; i < n; i++)
76+
if (visited[i] == 0 && cost[i][v] < distance[i])
77+
{
78+
distance[i] = cost[i][v];
79+
from[i] = v;
80+
}
81+
min_cost = min_cost + cost[u][v];
82+
}
83+
return (min_cost);
84+
}

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