Skip to content

Commit e30e47d

Browse files
authored
Create Prims (not complete)
1 parent 7a01775 commit e30e47d

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

Prims (not complete)

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
#define V 9
5+
6+
int min( bool visited[] , int dist[])
7+
{
8+
int m = INT_MAX;
9+
int min_index = 0 ;
10+
for(int i=0;i<V;i++)
11+
{
12+
if(visited[i] == false)
13+
{
14+
if(m < dist[i])
15+
{
16+
m = dist[i];
17+
min_index = i;
18+
}
19+
}
20+
}
21+
}
22+
23+
void print(int dist[])
24+
{
25+
for(int i=0;i<V;i++)
26+
{
27+
cout<<dist[i]<<" ";
28+
}
29+
}
30+
31+
void prims(int graph[V][V] )
32+
{
33+
int dist[V];
34+
bool visited[V];
35+
for(int i=0;i<V;i++)
36+
{
37+
dist[i] = INT_MAX;
38+
visited[i] = false;
39+
}
40+
dist[0]=0;
41+
42+
for(int i=0;i<V;i++)
43+
{
44+
int u = min(visited , dist);
45+
46+
visited[u] = true;
47+
48+
for(int j = 0;j<V;j++)
49+
{
50+
if((graph[u][j] != 0)&& (visited[j] == false))
51+
{
52+
if(graph[u][i] < dist[i])
53+
{
54+
dist[i] = graph[u][i];
55+
}
56+
}
57+
}
58+
59+
}
60+
61+
print(dist);
62+
63+
64+
65+
66+
}
67+
68+
int main()
69+
{
70+
71+
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
72+
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
73+
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
74+
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
75+
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
76+
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
77+
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
78+
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
79+
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
80+
81+
prims(graph);
82+
83+
return 0;
84+
}

0 commit comments

Comments
 (0)