-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadjmatt.cpp
More file actions
74 lines (63 loc) · 1.27 KB
/
adjmatt.cpp
File metadata and controls
74 lines (63 loc) · 1.27 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
#include <iostream>
using namespace std;
class graph{
//int v,e;
int vertices;
int** adjmat;
public:
graph(int v,int e){
if(v<=0){
cout<<"invalid"<<endl;
vertices=0;
adjmat=nullptr;
return;
}
vertices=v;
adjmat = new int*[v];
for(int i=0;i<v;i++){
adjmat[i]=new int[v];
for(int j=0;j<v;j++){
adjmat[i][j]=0;
}
}
cout<<"input"<<endl;
int u,ver;
for(int i=0;i<e;i++){
cout<<i<<endl;
cin>>u>>ver;
if(u<0 || u>=v || ver<0 || ver>=v){
cout<<"invalid"<<endl;
i--;
continue;
}
adjmat[u][ver]=1;
adjmat[ver][u]=1;
}
}
~graph(){
if(vertices>0 && adjmat) {
for(int i=0;i<vertices;i++){
delete[] adjmat[i];
}
delete[] adjmat;
}
}
void display(){
if(vertices<=0) return;
cout<<"display"<<endl;
for(int i=0;i<vertices;i++){
for(int j=0;j<vertices;j++){
cout<<adjmat[i][j]<<" ";
}
cout<<endl;
}
}
};
int main(){
int v,e;
cout<<"v,e"<<endl;
cin>>v>>e;
graph ob(v,e);
ob.display();
return 0;
}