-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdjacencyMatrix.cpp
137 lines (129 loc) · 3.31 KB
/
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
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
#include "AdjacencyMatrix.h"
AdjacencyMatrix::AdjacencyMatrix(){
this->COUNTER=0;
}
AdjacencyMatrix::AdjacencyMatrix(int Nodes,bool IsDirected){
this->Nodes=Nodes;
this->IsDirected=IsDirected;
this->COUNTER=0;
this->Init();
}
AdjacencyMatrix::~AdjacencyMatrix(){
this->VertexList.clear();
this->HasEdge.clear();
this->AMatrix.clear();
}
AdjacencyMatrix::AdjacencyMatrix(const AdjacencyMatrix&oldObj){
this->AMatrix=oldObj.AMatrix;
this->COUNTER=oldObj.COUNTER;
this->HasEdge=oldObj.HasEdge;
this->IsDirected=oldObj.IsDirected;
this->Nodes=oldObj.Nodes;
this->VertexList=oldObj.VertexList;
}
void AdjacencyMatrix::Init(){
int n=this->Nodes;
HasEdge.resize(n);
AMatrix.resize(n);
for(int i=0;i<n;i++){
HasEdge[i]=vector<bool>(this->Nodes);
AMatrix[i]=vector<double>(this->Nodes);
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
HasEdge[i][j]=false;
AMatrix[i][j]=0;
}
}
}
void AdjacencyMatrix::AddNode(string u){
if(VertexList.find(u)==VertexList.end()){
VertexList[u]=this->COUNTER++;
}
return;
}
void AdjacencyMatrix::AddEdge(string u,string v,double weight){
int U,V;
if(VertexList.find(u)!=VertexList.end()){
U=VertexList[u];
}
else{
U=this->COUNTER;
VertexList.insert(make_pair(u,AdjacencyMatrix::COUNTER));
this->COUNTER++;
}
if(VertexList.find(v)!=VertexList.end()){
V=VertexList[v];
}
else{
V=this->COUNTER;
VertexList.insert(make_pair(v,AdjacencyMatrix::COUNTER));
this->COUNTER++;
}
if(IsDirected){
HasEdge[U][V]=true;
HasEdge[V][U]=true;
AMatrix[U][V]=weight;
AMatrix[V][U]=weight;
}
else{
HasEdge[U][V]=true;
AMatrix[U][V]=weight;
}
}
double AdjacencyMatrix::GetWeight(string u,string v){
int U,V;
if(VertexList.find(u)!=VertexList.end() and VertexList.find(v)!=VertexList.end()){
U=VertexList[u];
V=VertexList[v];
if(HasEdge[U][V]){
return AMatrix[U][V];
}
}
return 0;
}
bool AdjacencyMatrix::Present(string u){
if(VertexList.find(u)!=VertexList.end()){
return true;
}
return false;
}
const unordered_map<string,int>& AdjacencyMatrix::GetVertexList()const{
return this->VertexList;
}
const vector<vector<bool>>& AdjacencyMatrix::GetEdges()const{
return this->HasEdge;
}
const vector<vector<double>>& AdjacencyMatrix::GetAMatrix()const{
return this->AMatrix;
}
const int AdjacencyMatrix::GetNodes()const{
return this->Nodes;
}
const bool AdjacencyMatrix::GetIsDirected()const{
return this->IsDirected;
}
void AdjacencyMatrix::SetVertexList(const unordered_map<string,int>&vlist){
this->VertexList.clear();
this->VertexList=vlist;
}
void AdjacencyMatrix::SetEdges(const vector<vector<bool>>&edges){
this->HasEdge.clear();
this->HasEdge=edges;
}
void AdjacencyMatrix::SetAMatrix(const vector<vector<double>>&amatrix){
this->AMatrix.clear();
this->AMatrix=amatrix;
}
void AdjacencyMatrix::SetNodes(int n){
this->Nodes=n;
}
void AdjacencyMatrix::SetIsDirected(bool isd){
this->IsDirected=isd;
}
void AdjacencyMatrix::SetCounter(int c){
this->COUNTER=c;
}
void AdjacencyMatrix::ResetCounter(){
this->COUNTER=0;
}