Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
prim.c File Reference

Prim's algorithm implementation in C to find the MST of a weighted, connected graph. More...

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
Include dependency graph for prim.c:

Macros

#define MAX   20
 for IO operations More...
 
#define INF   999
 

Functions

uint16_t minimum (uint16_t arr[], uint16_t N)
 Finds index of minimum element in edge list for an arbitrary vertex. More...
 
void prim (uint16_t G[][MAX], uint16_t MST[][MAX], uint16_t V)
 Used to find MST of user-generated adj matrix G. More...
 
static void test (uint16_t G[][MAX], uint16_t MST[][MAX], uint16_t V)
 Self-test implementations. More...
 
void user_graph (uint16_t G[][MAX], uint16_t MST[][MAX], uint16_t V)
 Function user_graph(); gets user input adj. More...
 
int main (int argc, char const *argv[])
 Main function. More...
 

Detailed Description

Prim's algorithm implementation in C to find the MST of a weighted, connected graph.

Author
Timothy Maloney

Prim's algorithm uses a greedy approach to generate the MST of a weighted connected graph. The algorithm begins at an arbitrary vertex v, and selects a next vertex u, where v and u are connected by a weighted edge whose weight is the minimum of all edges connected to v. @references Page 319 "Introduction to the Design and Analysis of Algorithms" - Anany Levitin

To test - run './prim -test' prim() will find the MST of the following adj. matrix:

0 1 2 3 1 0 4 6 2 4 0 5 3 6 5 0

The minimum spanning tree for the above weighted connected graph is given by the following adj matrix:

0 1 2 3 1 0 0 0 2 0 0 0 3 0 0 0

The following link provides a visual representation of graphs that can be used to test/verify the algorithm for different adj matrices and their weighted, connected graphs.

Macro Definition Documentation

◆ MAX

#define MAX   20

for IO operations

for string comparison for assert() for uint16_t

Function Documentation

◆ main()

int main ( int  argc,
char const *  argv[] 
)

Main function.

Parameters
argccommandline argument count (ignored)
argvcommandline array of arguments (ignored)
Returns
0 on exit

< weighted, connected graph G

< adj matrix to hold minimum spanning tree of G

< number of vertices in V in G

186{
187
188 uint16_t G[MAX][MAX]; ///< weighted, connected graph G
189 uint16_t MST[MAX][MAX]; ///< adj matrix to hold minimum spanning tree of G
190 uint16_t V; ///< number of vertices in V in G
191
192
193 if(argc == 2 && strcmp(argv[1],"-test") == 0)
194 {
195 test(&(*G),&(*MST),V);
196 }
197 else
198 {
199 user_graph(&(*G),&(*MST),V);
200 }
201
202 return 0;
203}
static void test()
Self-test implementations.
Definition: decimal_to_any_base.c:94
#define MAX
for IO operations
Definition: prim.c:36
void user_graph(uint16_t G[][MAX], uint16_t MST[][MAX], uint16_t V)
Function user_graph(); gets user input adj.
Definition: prim.c:145
Here is the call graph for this function:

◆ minimum()

uint16_t minimum ( uint16_t  arr[],
uint16_t  N 
)

Finds index of minimum element in edge list for an arbitrary vertex.

Parameters
arrgraph row
Nnumber of elements in arr
Returns
index of minimum element in arr
46{
47 uint16_t index = 0;
48 uint16_t min = INF;
49
50 for (uint16_t i = 0; i < N; i++)
51 {
52 if (arr[i] < min)
53 {
54 min = arr[i];
55 index = i;
56 }
57 }
58 return index;
59}
#define min(a, b)
shorthand for minimum value
Definition: kohonen_som_topology.c:43

◆ prim()

void prim ( uint16_t  G[][MAX],
uint16_t  MST[][MAX],
uint16_t  V 
)

Used to find MST of user-generated adj matrix G.

Returns
void
66{
67 uint16_t u, v;
68 uint16_t E_t[MAX], path[MAX];
69 uint16_t V_t[MAX], no_of_edges;
70
71 E_t[0] = 0; // edges for current vertex
72 V_t[0] = 1; // list of visited vertices
73
74 for (uint16_t i = 1; i < V; i++)
75 {
76 E_t[i] = G[i][0];
77 path[i] = 0;
78 V_t[i] = 0;
79 }
80
81 no_of_edges = V - 1;
82
83 while (no_of_edges > 0)
84 {
85 u = minimum(E_t, V);
86 while (V_t[u] == 1)
87 {
88 E_t[u] = INF;
89 u = minimum(E_t, V);
90 }
91
92 v = path[u];
93 MST[v][u] = E_t[u];
94 MST[u][v] = E_t[u];
95 no_of_edges--;
96 V_t[u] = 1;
97
98 for (uint16_t i = 1; i < V; i++)
99 {
100 if (V_t[i] == 0 && G[u][i] < E_t[i])
101 {
102 E_t[i] = G[u][i];
103 path[i] = v;
104 }
105 }
106 }
107}
uint16_t minimum(uint16_t arr[], uint16_t N)
Finds index of minimum element in edge list for an arbitrary vertex.
Definition: prim.c:45
Here is the call graph for this function:

◆ test()

static void test ( uint16_t  G[][MAX],
uint16_t  MST[][MAX],
uint16_t  V 
)
static

Self-test implementations.

Returns
void
114{
115
116 uint16_t test[4][4] = {{0,1,2,3},{1,0,4,6},{2,4,0,5},{3,6,5,0}};
117 uint16_t solution[4][4] = {{0,1,2,3},{1,0,0,0},{2,0,0,0},{3,0,0,0}};
118
119 V = 4;
120
121 for(uint16_t i = 0; i < V; ++i)
122 {
123 for(uint16_t j = 0; j < V; ++j)
124 {
125 G[i][j] = test[i][j];
126 }
127 }
128
129 prim(&(*G),&(*MST),V);
130
131 for(uint16_t i = 0; i < V; ++i)
132 {
133 for(uint16_t j = 0; j < V; ++j)
134 {
135 assert(MST[i][j] == solution[i][j]);
136 }
137 }
138}
void prim(uint16_t G[][MAX], uint16_t MST[][MAX], uint16_t V)
Used to find MST of user-generated adj matrix G.
Definition: prim.c:65
Here is the call graph for this function:

◆ user_graph()

void user_graph ( uint16_t  G[][MAX],
uint16_t  MST[][MAX],
uint16_t  V 
)

Function user_graph(); gets user input adj.

matrix and finds MST of that graph

Returns
void
146{
147 printf("Enter the number of vertices: ");
148 scanf(" %hd", &V);
149
150 assert(V <= MAX);
151
152 printf("Enter the adj matrix\n");
153 uint16_t i, j;
154 for (i = 0; i < V; ++i)
155 {
156 for (j = 0; j < V; ++j)
157 {
158 printf("G[%d][%d]: ", i, j);
159 scanf(" %hd", &G[i][j]);
160 if (G[i][j] == 0)
161 G[i][j] = INF;
162 }
163 }
164
165 prim(&(*G),&(*MST),V);
166
167 printf("minimum spanning tree:\n");
168 for (i = 0; i < V; ++i)
169 {
170 printf("\n");
171 for (j = 0; j < V; ++j)
172 {
173 printf("%d\t", MST[i][j]);
174 }
175 }
176}
Here is the call graph for this function: