-
Notifications
You must be signed in to change notification settings - Fork 1
/
Prim.c
129 lines (103 loc) · 3.35 KB
/
Prim.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/**
* GROUP PROJECT CISC360-010
* PART THREE: Prim algorithm structure
*
* Prim's algorithm is to find Minimum Spanning Tree
* of a given connected, undirected and weighted graph.
* The program is for adjacency matrix representation of the graph
*
* The orinal code is from:
* http ://www.geeksforgeeks.org/greedy-algorithms-set-5-prims-mst-
* for-adjacency-list-representation/
*/
// Structure
//__________________________________________________________________________
// a structure to represent a weighted edge in graph
struct Edge
{
int src, dest, weight;
};typedef struct Edge Edge;
// a structure to represent a connected, undirected and weighted graph
struct Graph
{
// V-> Number of vertices, E-> Number of edges
int V, E;
// graph is represented as an array of edges. Since the graph is
// undirected, the edge from src to dest is also edge from dest
// to src. Both are counted as 1 edge here.
struct Edge* edge;
}; typedef struct Graph Graph;
// Search Function
//__________________________________________________________________________
void PrimMST(Graph *graph)
{
int V = graph->V, E = graph->E;
Edge *edge = graph->edge;
int* reachset = (int *)malloc(sizeof(int) * V);
int* unreachset = (int *)malloc(sizeof(int) * V);
int* ST = (int *)malloc(sizeof(int) * V);
int MSTWeight = 0;
int i;
// print the contents of result[] to display the built MST
printf("===============================================\n");
printf("Following are the edges in the constructed MST\n");
for (i = 0; i < V; ++i) {
printf("Edge: %d -- %d (Weight: %d)\n", edge[i].src, edge[i].dest,
edge[i].weight);
MSTWeight += edge[i].weight;
}
printf("Total Weight: %d\n", MSTWeight);
printf("===============================================\n");
return;
}
// Creates a graph with V vertices and E edges
Graph* createGraph(int V, int E)
{
Graph* graph = (Graph*) malloc(sizeof(Graph));
graph->V = V;
graph->E = E;
graph->edge = (Edge*) malloc(graph->E * sizeof(Edge));
return graph;
}
// Main
//__________________________________________________________________________
// Driver program to test above functions
int main()
{
/* Let us create following weighted graph
10
0--------1
| \ |
6| 5\ |15
| \ |
2--------3
4 */
int V = 4; // Number of vertices in graph
int E = 5; // Number of edges in graph
Graph* graph = createGraph(V, E);
// add edge 0-1
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = 10;
// add edge 0-2
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 6;
// add edge 0-3
graph->edge[2].src = 0;
graph->edge[2].dest = 3;
graph->edge[2].weight = 5;
// add edge 1-3
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 15;
// add edge 2-3
graph->edge[4].src = 2;
graph->edge[4].dest = 3;
graph->edge[4].weight = 4;
PrimMST(graph);
return 0;
}