-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrims.cpp
More file actions
188 lines (176 loc) · 3.48 KB
/
Prims.cpp
File metadata and controls
188 lines (176 loc) · 3.48 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include<iostream>
using namespace std;
struct node
{
int pred;
int dist;
int stat;
};
struct edge
{
int u,v;
};
class graph
{
int adjmat[10][10];
struct node state[10];
struct edge tree[10];
int wt;
public:
graph()
{
wt=0;
}
int allperm(int);
void initgraph(); //initailize matrix with 0
void displayg(int v,int e); // display adj mat
void scangraph(int v,int e); // input graph
void span(int v,int e);
};
int graph::allperm(int v)
{
int i;
for(i=0;i<v;i++)
if(state[i].stat==0)
return 0;
return 1;
}
void graph::span(int v,int e)
{
int current,count,min,u1,v1;
for(int i=0;i<v;i++)
{
state[i].pred=0;
state[i].dist=999;
state[i].stat=0; //0 means not visited
}
state[0].pred=0;
state[0].dist=0;
state[0].stat=1;
current=0;
count=0;
while(allperm(v)!=1)
{
for(int i=0;i<v;i++)
{
if(adjmat[current][i]>0 && state[i].stat==0)
{
if(adjmat[current][i]< state[i].dist)
{
state[i].pred=current;
state[i].dist=adjmat[current][i];
}
}
}
min =999;
for(int i=0;i<v;i++)
{
if(state[i].stat ==0 && state[i].dist < min)
{
min=state[i].dist;
current=i;
}
}
cout<<endl<<"current"<<current;
state[current].stat=1;
u1=state[current].pred;
v1=current;
tree[count].u=u1;
tree[count].v=v1;
count++;
wt=wt+ adjmat[u1][v1];
}
cout<<endl<<"set of lines for connecting city with min. cost is"<<endl;
for(int i=0;i<count;i++)
{
cout<<"city"<<tree[i].u+1<<"->";
cout<<"city"<<tree[i].v+1<<endl;
}
}
void graph::initgraph() //constuctor to intialize Graph
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
adjmat[i][j]=0;
}
}
void graph::displayg(int v,int e)
{
int i,j;
for(i=0;i<v;i++)
{
cout<<endl;
for(j=0;j<v;j++)
cout<<adjmat[i][j]<<" ";
}
}
void graph::scangraph(int v ,int e)
{
int i,s,d,cost;
cout<<"please enter source city and destination city of the edge(city no must be between 1 to"<<v<<")";
for(i=0;i<e;i++)
{
l1:
cout<<endl<<"Edge no -> "<<i+1<<endl;
cout<<"Source city->";
cin>>s;
cout<<"Destination city-> ";
cin>>d;
cout<<"Cost of Conecting 2 city";
cin>>cost;
if((s>=1 && s<=v) && (d>=1 && d<=v))
{
if((adjmat[s-1][d-1]==0) && (adjmat[d-1][s-1] == 0))
{
adjmat[s-1][d-1]=cost;
adjmat[d-1][s-1]=cost;
}
else
{
cout<<"Edge already exist";
goto l1;
}
}
else
{
cout<<"Please Enter correct vertex no's";
goto l1;
}
}
}
int main()
{
int ch,cont,v,e;
graph g1;
do
{
cout<<endl<<"Menu";
cout<<endl<<"1.Create Graph for offices ";
cout<<endl<<"2.Display Graph";
cout<<endl<<"3.min cost cities";
cout<<endl<<"Enter Choice";
cin>>ch;
switch(ch)
{
case 1:
g1.initgraph();
cout<<"No of offices ?";
cin>>v;
cout<<"No of leased line ?";
cin>>e;
g1.scangraph(v,e);
break;
case 2:
g1.displayg(v,e);
break;
case 3:
g1.span(v,e);
break;
}
cout<<"do u want to continue?(1 for continue)";
cin>>cont;
}while(cont==1);
return 0;
}