-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_AdjacencyMatrix.cpp
82 lines (69 loc) · 1.38 KB
/
graph_AdjacencyMatrix.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
#include<iostream>
using namespace std;
class Graph
{
private:
int vertex;
int edge;
int **graph;
public:
Graph(int v)
{
this->edge=0;
this->vertex=v;
this->graph=new int*[v];
for(int i=0; i<v; i++)
{
graph[i]=new int[v];
}
for(int i=0; i<this->vertex; i++)
{
for(int j=0; j<this->vertex; j++)
{
this->graph[i][j]=0;
}
}
}
bool addEdge(int u,int v,int weight)
{
if(u >= this->vertex || v >= this->vertex){
return false;
}
graph[u][v]=weight;
graph[v][u]=weight;
this->edge++;
return true;
}
bool removeEdge(int u,int v)
{
if(u >= this->vertex || v >= this->vertex){
return false;
}
graph[u][v]=0;
graph[v][u]=0;
return true;
}
void printGraph()
{
printf("The number of vertex used: %d\n",this->edge);
for(int i=0; i<this->vertex; i++)
{
for(int j=0; j<this->vertex; j++)
{
cout<<this->graph[i][j]<<" ";
}
cout<<endl;
}
}
};
int main()
{
Graph g(4);
g.addEdge(0,1,5);
g.addEdge(1,3,2);
g.addEdge(0,2,1);
g.addEdge(2,3,5);
g.removeEdge(2,3);
g.printGraph();
return 0;
}