forked from udulaindunil/Graphs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdjMatrix.cpp
107 lines (91 loc) · 1.65 KB
/
AdjMatrix.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
#include<iostream>
#include<string>
using namespace std;
class graph{
private:
struct node{
string name;
int id;
};
static const int size=4;
node *array[size];
int adjmatrix[4][4];
string cities[4]={"Colombo","Gampaha","Galle","Matara"};
public:
graph();
void addedge(int f,int s);
void addedge(string f,string s);
void display();
void connection();
};
graph::graph(){
for(int i=0;i<size;i++){
array[i]=new node;
array[i]->name=cities[i];
array[i]->id=i;
}
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
adjmatrix[i][j]=0;
}
}
}
void graph::addedge(int f,int s){
if((f>0)&&(s>0)&&(f<=size)&&(s<=size)){
adjmatrix[f-1][s-1]=1;
}
else{
cout<<"Invalid"<<endl;
}
}
void graph::addedge(string f,string s){
int temp1=0,temp2=0;
for(int i=0;i<size;i++){
if(array[i]->name==f){
temp1=i;
}
}
for(int i=0;i<size;i++){
if(array[i]->name==s){
temp2=i;
}
}
if((temp1+1)&&(temp2+1)){
adjmatrix[temp1][temp2]=1;
adjmatrix[temp2][temp1]=1;
}
}
void graph::display(){
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
cout<<adjmatrix[i][j]<<"\t";
}
cout<<endl;
}
}
void graph::connection(){
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
cout<<array[i]->name<<"--"<<array[j]->name;
if(adjmatrix[i][j]){
cout<<"\tCan"<<endl;
}
else{
cout<<"\tCan't"<<endl;
}
}
}
}
int main(){
graph nn;
// nn.addedge(3,4);
// nn.addedge(1,4);
// nn.addedge(2,4);
// nn.addedge(2,1);
nn.addedge("Colombo","Gampaha");
nn.addedge("Colombo","Galle");
nn.addedge("Gampaha","Galle");
nn.connection();
nn.display();
return 0;
}