-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprims.cpp
67 lines (59 loc) · 1.74 KB
/
prims.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define V 9
void dijkstra(int graph[V][V], int src)
{
int dist[V];
bool vis[V];
vector<int> adj[V];
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if(graph[i][j]!=0)
{
adj[i].push_back(j);
}
}
}
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, vis[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++)
{
int min = INT_MAX;
int min_index;
for (int v = 0; v < V; v++)
{
if (vis[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
}
vis[min_index] = true;
for (int v = 0; v < adj[min_index].size(); v++)
{
if (vis[adj[min_index][v]]==false && graph[min_index][adj[min_index][v]] < dist[adj[min_index][v]])
dist[adj[min_index][v]] = graph[min_index][adj[min_index][v]];
}
}
int sum =0;
for (int i = 0; i < V; i++)
{
sum = sum + dist[i];
}
cout<<"MST cost is"<<" "<<sum;
}
int main()
{
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
dijkstra(graph, 0);
return 0;
}