Skip to content

Commit d4a475f

Browse files
committed
Added Prim's algoritm.c
1 parent e5ebf3c commit d4a475f

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed

Graph/Prim's algorithm.c

+219
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/* Prim's Algorithm
2+
This is Prim's algorithm to find the lowest cost to traverse it all vertex of a graph or MST(Minimal Spanning Tree) */
3+
#include<stdio.h>
4+
#include<stdlib.h>
5+
// node type structure
6+
struct node
7+
{
8+
int no;
9+
int PI;
10+
int Key;
11+
struct node *next;
12+
};
13+
// graph type structure
14+
struct Graph
15+
{
16+
int V;
17+
int E;
18+
};
19+
// this function initialize value in Graph structure members
20+
struct Graph* create_graph(int V,int E)
21+
{
22+
// Graph structure pointer to store value
23+
struct Graph *G;
24+
G=(struct Graph*)malloc(sizeof(struct Graph));
25+
G->E=E;
26+
G->V=V;
27+
return(G);
28+
}
29+
// function to check the least priority Key or distance node no. and return it
30+
int less_priority(struct node *Start)
31+
{
32+
int less=0,no=0;
33+
less=Start->Key;
34+
no=Start->no;
35+
Start=Start->next;
36+
// until start is not equal to 0 the loop will run
37+
while(Start!=NULL)
38+
{
39+
if(Start->Key<less)
40+
{
41+
less=Start->Key;
42+
no=Start->no;
43+
}
44+
Start=Start->next;
45+
}
46+
return(no);
47+
}
48+
// this function insert the a node
49+
void insertion(struct node **Start,int no)
50+
{
51+
struct node *temp,*t;
52+
temp=(struct node*)malloc(sizeof(struct node));
53+
temp->no=no;
54+
temp->PI=0;
55+
temp->Key=1000;
56+
temp->next=NULL;
57+
if(*Start==NULL)
58+
{ temp->Key=0;
59+
*Start=temp;
60+
}
61+
else
62+
{
63+
t=*Start;
64+
while(t->next!=NULL)
65+
t=t->next;
66+
t->next=temp;
67+
}
68+
}
69+
// remove node from the queue
70+
struct node* deletion(struct node **Start)
71+
{ int check=0;
72+
struct node *temp,*t,*pre;
73+
temp=(struct node*)malloc(sizeof(struct node));
74+
// finding the less priority node no.
75+
check=less_priority(*Start);
76+
// is start no. member is equal to check variable
77+
if((*Start)->no==check)
78+
{
79+
temp->no=(*Start)->no;
80+
temp->Key=(*Start)->Key;
81+
temp->PI=(*Start)->PI;
82+
temp->next=NULL;
83+
t=*Start;
84+
*Start=(*Start)->next;
85+
free(t);
86+
}
87+
else
88+
{ t=*Start;
89+
while(t->next!=NULL)
90+
{
91+
pre=t;
92+
t=t->next;
93+
if(check==t->no)
94+
break;
95+
}
96+
temp->no=t->no;
97+
temp->PI=t->PI;
98+
temp->Key=t->Key;
99+
temp->next=NULL;
100+
pre->next=t->next;
101+
free(t);
102+
}
103+
return(temp);
104+
}
105+
// this function check whether the particular node is in the queue or not
106+
int check_in_q(int n,struct node *Start)
107+
{
108+
while(Start!=NULL)
109+
{
110+
if(Start->no==n)
111+
return(1);
112+
Start=Start->next;
113+
}
114+
return(0);
115+
}
116+
int get_n(int n,struct node *Start)
117+
{ int Key;
118+
// finding the distance of the node no. match with the n(node number) and returning the distance
119+
while(Start!=NULL)
120+
{
121+
if(n==Start->no)
122+
{
123+
Key=Start->Key;
124+
break;
125+
}
126+
Start=Start->next;
127+
}
128+
return(Key);
129+
}
130+
//this function change the Key(distance) and PI(Parent node) value with the given arguments
131+
void change(int n,struct node **Start,int Key,int PI)
132+
{
133+
struct node *temp;
134+
if((*Start)->no==n)
135+
{
136+
(*Start)->Key=Key;
137+
(*Start)->PI=PI;
138+
}
139+
else
140+
{
141+
temp=*Start;
142+
while(temp!=NULL)
143+
{
144+
if(temp->no==n)
145+
{
146+
temp->Key=Key;
147+
temp->PI=PI;
148+
break;
149+
}
150+
temp=temp->next;
151+
}
152+
}
153+
}
154+
//the main logic to find lowest cost traverse
155+
void Prims(struct node **Start,struct Graph *G)
156+
{ int e1,i,n,w;
157+
struct node *temp;
158+
int t,cost=0;
159+
// run until start is not equal to 0
160+
while(*Start!=NULL)
161+
{
162+
//take the less priority node
163+
temp=deletion(Start);
164+
// increase cost
165+
cost+=temp->Key;
166+
printf("Enter the no. of nodes connected to V%d",temp->no);
167+
scanf("%d",&e1);
168+
for(i=0;i<e1;i++)
169+
{
170+
printf("enter the node no of v%d and weight",temp->no);
171+
scanf("%d%d",&n,&w);
172+
if(1==check_in_q(n,*Start))
173+
{
174+
t=get_n(n,*Start);
175+
if(w<t)
176+
change(n,Start,w,e1);
177+
}
178+
}
179+
}
180+
printf("\n Cost=%d",cost);
181+
}
182+
// driver code
183+
int main()
184+
{
185+
struct node *Start=NULL;
186+
struct Graph *G;
187+
int i,v,e;
188+
printf("enter vertex and edges");
189+
scanf("%d %d",&v,&e);
190+
G=create_graph(v,e);
191+
for(i=0;i<G->V;i++)
192+
insertion(&Start,i+1);
193+
Prims(&Start,G);
194+
return 0;
195+
}
196+
/*
197+
Input:
198+
enter vertex and edges 3
199+
6
200+
Enter the no. of nodes connected to V1 2
201+
enter the node no of v1 and weight2
202+
5
203+
enter the node no of v1 and weight 3
204+
2
205+
Enter the no. of nodes connected to V3 2
206+
enter the node no of v3 and weight1
207+
2
208+
enter the node no of v3 and weight 2
209+
2
210+
Enter the no. of nodes connected to V2 2
211+
enter the node no of v2 and weight 1
212+
5
213+
enter the node no of v2 and weight 3
214+
2
215+
Output: Cost: 4
216+
217+
Time Complexity:O(V^2)
218+
219+
*/

0 commit comments

Comments
 (0)