-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.cpp
209 lines (189 loc) · 5.01 KB
/
graph.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct node
{
string vertex;
int time=0;
node *next;
};
class adjmatlist
{
int m[10][10],n,i,j;
char ch;
string v[20];
node *head[20];
node *temp=NULL;
public:
adjmatlist()
{
for(i=0;i<20;i++)
{
head[i]=NULL;
}
}
void getgraph();
void adjlist();
void displaym();
void displaya();
};
void adjmatlist::getgraph()
{
cout<<"\n Enter no. of cities(max. 20) : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<" Enter name of city : ";
cin>>v[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\n If path is present between city "<<v[i]<<" and "<<v[j]<<" then press enter Y or y otherwise N or n : ";
cin>>ch;
if(ch=='y' || ch=='Y')
{
cout<<"\n Enter time required to reach city "<<v[j]<<" from "<<v[i]<<" in minutes : ";
cin>>m[i][j];
}
else if(ch=='n' || ch=='N')
{
m[i][j]=0;
}
else
{
cout<<"\n Invalid entry !!! ";
}
}
}
adjlist();
}
void adjmatlist::adjlist()
{
cout<<"\n *****************************************";
cout<<endl<<"Cities : ";
for(i=0;i<n;i++)
{
node *p=new(struct node);
p->next=NULL;
p->vertex=v[i];
head[i]=p;
cout<<"\n"<<head[i]->vertex;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(m[i][j]!=0)
{
node *p=new(struct node);
p->vertex=v[j];
p->time=m[i][j];
p->next=NULL;
if(head[i]->next==NULL)
{
head[i]->next=p;
}
else
{
temp=head[i];
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
}
}
}
void adjmatlist::displaym()
{
cout<<"********** Adjacency Matrix of Cities **********";
cout<<"\n";
for(j=0;j<n;j++)
{
cout<<"\t"<<v[j];
}
cout<<endl<<"---------------------------------------"<<endl;
for(i=0;i<n;i++)
{
cout<<"\n "<<v[i];
for(j=0;j<n;j++)
{
cout<<"\t"<<m[i][j];
}
cout<<"\n";
cout<<"---------------------------------------"<<endl;
}
}
void adjmatlist::displaya()
{
cout<<"\n ************ Adjacency list of cities **********";
for(i=0;i<n;i++)
{
if(head[i]==NULL)
{
cout<<"\n Adjacency list not present !!!";
break;
}
else
{
cout<<"\n"<<head[i]->vertex;
temp=head[i]->next;
while(temp!=NULL)
{
cout<<"-> "<<temp->vertex;
temp=temp->next;
}
}
}
cout<<"\n Path and time required to reach cities is : ";
for(i=0;i<n;i++)
{
if(head[i]==NULL)
{
cout<<"\n Adjacency list not present !!!";
break;
}
else
{
temp=head[i]->next;
while(temp!=NULL)
{
cout<<"\n"<<head[i]->vertex;
cout<<"-> "<<temp->vertex<<"\n [time required: "<<temp->time<<" min ]";
temp=temp->next;
}
}
}
}
int main()
{
int m;
adjmatlist a;
while(1)
{
cout<<"\n\n *************** Graph *************** ";
cout<<"\n 1.Enter graph";
cout<<"\n 2.Display adjacency matrix for cities";
cout<<"\n 3.Display adjacency list for cities";
cout<<"\n 4.Exit"<<endl;
cout<<"Enter your choice : ";
cin>>m;
switch(m)
{
case 1: a.getgraph();
break;
case 2: a.displaym();
break;
case 3: a.displaya();
break;
case 4: exit(0);
default: cout<<"\n Invalid choice !!!";
}
}
return 0;
}