-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cpp
288 lines (275 loc) · 8.32 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "Graph.h"
#include "Stack.h"
#include "Queue.h"
#include "MinHeap.h"
#include <set>
#include <algorithm>
#define BFS_FIRST_PATH
Graph::Graph()
{
m_pVHead = NULL;// the head pointer for the linked list of the vertics
m_vSize = 0;// the number of the vertics
}
Graph::~Graph()
{
Clear();
}
void Graph::AddVertex(int vertexKey, char* storeName)
{
Vertex* NewVertex = new Vertex(vertexKey, storeName);
if (m_pVHead == NULL)
{//first insert
m_pVHead = NewVertex;
}
else
{//not first insert
Vertex* pCur = m_pVHead;
while (pCur->GetNext() != NULL)
pCur = pCur->GetNext();
pCur->SetNext(NewVertex);
}
}
void Graph::AddEdge(int startVertexKey, int endVertexKey, int weight)
{
Vertex* v = FindVertex(startVertexKey);
v->SetSize(v->Size() + 1);
v->AddEdge(endVertexKey, weight);
}
void Graph::Print(std::ofstream& fout)
{
fout << "====== PRINT ======" << endl;
cout << "====== PRINT ======" << endl;
Vertex* moveV = m_pVHead;
for (int i = 0; i < m_vSize; i++)
{
Edge* moveE = moveV->GetHeadOfEdge();
for (int j = 0; j < m_vSize; j++)
{
if (moveE == NULL || j != moveE->GetKey())
{
fout << 0 << " ";
cout << 0 << " ";
}
else
{//print weight
fout << moveE->GetWeight() << " ";
cout << moveE->GetWeight() << " ";
moveE = moveE->GetNext();//Edge move
}
}
fout << endl;
cout << endl;
moveV = moveV->GetNext();//Vertex move
}
fout << "====================" << endl << endl;
cout << "====================" << endl << endl;
}
bool Graph::IsNegativeEdge()
{
Vertex* moveV = m_pVHead;
if (moveV == NULL)
return false;
else
{
while (moveV)
{
Edge* moveE = moveV->GetHeadOfEdge();
while (moveE)
{
if (moveE->GetWeight() < 0)
return true;//check negative
moveE = moveE->GetNext();//Edge move
}
moveV = moveV->GetNext();//Vertex move
}
}
return false;
}
std::vector<int> Graph::FindPathBfs(int startVertexKey, int endVertexKey)
{
vector<int> answer;
Queue<int> q;
bool *visit = new bool[200];
fill(visit, visit + 200, unvisited);
visit[startVertexKey] = visited;
answer.push_back(startVertexKey);
q.push(startVertexKey);
while (!visit[endVertexKey] == visited)
{//repeat endVertexKey
int front = q.getfront();
q.pop();
Edge* temp = FindVertex(front)->GetHeadOfEdge();
for (int i = 0; i < FindVertex(front)->Size(); i++)
{
if (!visit[temp->GetKey()])//only unvisit
{
q.push(temp->GetKey());
visit[temp->GetKey()] = visited;//visit
answer.push_back(temp->GetKey());//path insert
}
temp = temp->GetNext();//move next edge
}
}
return answer;
}
std::vector<int> Graph::FindShortestPathDijkstraUsingSet(int startVertexKey, int endVertexKey)
{
vector<int> answer;
vector<int> distance(this->Size(), IN_FINITY);//Initialize distance
distance[startVertexKey] = 0;
vector<int> path(this->Size(), -1);//Initialize path -1
set<pair<int, int>> wk;//weight, key
wk.insert(make_pair(0, startVertexKey));
while (!wk.empty())
{
pair<int, int> temp = *wk.begin();//make pair
wk.erase(temp);
Vertex* cur = this->FindVertex(temp.second);
Edge* moveE = cur->GetHeadOfEdge();
for (int i = 0; i < cur->Size(); i++)
{
if (distance[moveE->GetKey()] > distance[temp.second] + moveE->GetWeight())
{//Renewal
distance[moveE->GetKey()] = distance[temp.second] + moveE->GetWeight();//distance change
wk.insert(make_pair(distance[moveE->GetKey()], moveE->GetKey()));//insert key, weight
path[moveE->GetKey()] = temp.second;//path remember
}
moveE = moveE->GetNext();
}
}
vector<int> temp;
int tempkey = endVertexKey;
while (1)
{//make path
temp.push_back(tempkey);
if (tempkey == startVertexKey)
break;
tempkey = path[tempkey];
}
reverse(temp.begin(), temp.end());//reverse path
answer = temp;
return answer;
}
std::vector<int> Graph::FindShortestPathDijkstraUsingMinHeap(int startVertexKey, int endVertexKey)
{
vector<int> answer;
vector<int> distance(this->Size(), IN_FINITY);//Initialize distance
distance[startVertexKey] = 0;
vector<int> path(this->Size(), -1);//Initialize path -1
MinHeap<int, int> heap;
heap.Push(0, startVertexKey);
while (!heap.IsEmpty())
{
pair<int, int> temp = heap.Top();//make pair
heap.Pop();
Vertex* cur = this->FindVertex(temp.second);
Edge* moveE = cur->GetHeadOfEdge();
for (int i = 0; i < cur->Size(); i++)
{
if (distance[moveE->GetKey()] > distance[temp.second] + moveE->GetWeight())
{//Renewal
distance[moveE->GetKey()] = distance[temp.second] + moveE->GetWeight();
heap.Push(distance[moveE->GetKey()], moveE->GetKey());
path[moveE->GetKey()] = temp.second;//path remember
}
moveE = moveE->GetNext();
}
}
vector<int> temp;
int tempkey = endVertexKey;
while (1)
{//make path
temp.push_back(tempkey);
if (tempkey == startVertexKey)
break;
tempkey = path[tempkey];
}
reverse(temp.begin(), temp.end());//reverse path
answer = temp;
return answer;
}
std::vector<int> Graph::FindShortestPathBellmanFord(int startVertexKey, int endVertexKey)
{
vector<int> answer;
vector<int> distance(this->Size(), IN_FINITY);//Initialize distance
distance[startVertexKey] = 0;
vector<int> path(this->Size(), -1);//Initialize path -1
Vertex* moveV;
Edge* moveE;
for (int i = 0; i < Size(); i++)
{
for (int j = 0; j < Size(); j++)
{
moveV = FindVertex(j);
moveE = moveV->GetHeadOfEdge();
while (moveE)
{
if (distance[moveV->GetKey()] != IN_FINITY && distance[moveE->GetKey()] > distance[moveV->GetKey()] + moveE->GetWeight())
{//Renewal
distance[moveE->GetKey()] = distance[moveV->GetKey()] + moveE->GetWeight();//change distance
path[moveE->GetKey()] = moveV->GetKey();//change path
if (i == Size() - 1)
{//Negative cycle exists
distance.resize(0);
return distance;
}
}
moveE = moveE->GetNext();
}
}
}
vector<int> temp;
int tempkey = endVertexKey;
while (1)
{//make path
temp.push_back(tempkey);
if (tempkey == startVertexKey)
break;
tempkey = path[tempkey];
}
reverse(temp.begin(), temp.end());//reverse path
answer = temp;
return answer;
}
std::vector<vector<int>> Graph::FindShortestPathFloyd()
{
vector<vector<int>> answer(Size(), vector<int>(Size(), IN_FINITY));
Vertex* moveV = m_pVHead;
for (int i = 0; i < Size(); i++)
{//Initialization
Edge* moveE = moveV->GetHeadOfEdge();
for (int j = 0; j < Size(); j++)
{
if (i == j)
answer[i][j] = 0;//from i to j is 0
if (!moveE)
break;
if (moveE->GetKey() == j)
{
answer[i][j] = moveE->GetWeight();
moveE = moveE->GetNext();
}
}
if(moveV->GetNext())
moveV = moveV->GetNext();
}
for (int k = 0; k < Size(); k++)
{//Floyd
for (int i = 0; i < Size(); i++)
{
for (int j = 0; j < Size(); j++)
{//Renewal - i->j = min(i->j, i->k->j)
answer[i][j] = min(answer[i][j], answer[i][k] + answer[k][j]);
}
}
}
for (int i = 0; i < Size(); i++)
{
if (answer[i][i] < 0)
{//Negative cycle exists
answer.resize(0);
return answer;
}
}
return answer;
}