-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphx.cpp
More file actions
218 lines (180 loc) · 7.18 KB
/
graphx.cpp
File metadata and controls
218 lines (180 loc) · 7.18 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
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
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <vector>
#include <limits.h>
#include <math.h>
#include <map>
#include "graphx.h"
using namespace std;
graph::graph()
{
propGraph.adjacencyMatrix.resize(100, vector<float>(100, -1));
}
graph::~graph()
{
//dtor
}
graph *graph::graphAlgoritma(string jenis){
if (jenis == "mst")
return new mst;
else if (jenis == "mstBobot")
return new mstBobot;
else if (jenis == "findPath")
return new findPath;
}
void graph::addEdge(int v1, int v2, float val){
propGraph.adjacencyMatrix.at(v1).at(v2) = val;
propGraph.adjacencyMatrix.at(v2).at(v1) = val;
}
void graph::removeEdge(int v1, int v2){
propGraph.adjacencyMatrix.at(v1).at(v2) = -1;
propGraph.adjacencyMatrix.at(v2).at(v1) = -1;
}
void graph::defaultMatrix(){
propGraph.adjacencyTransversal.clear();
propGraph.adjacencyTransversal.resize(vertexCount, vector<float>(vertexCount,-1));
propGraph.visitedVertexLog.clear();
}
void graph::lihatRelasi(bool defaultMatrix){
vector< vector<float> >& matrix = defaultMatrix ? propGraph.adjacencyMatrix: propGraph.adjacencyTransversal;
//akses hanya sampai garis diagonal matrix atau batas pertemuan vertex yang sama pada matrix
for (int i = 0; i < vertexCount; i++)
for( int j = 0; j < i; j++)
if ( matrix.at(i).at(j)!= -1 )
cout << i <<"-"<< j << " ";
cout << endl;
}
///////FINDPATH
void findPath::show(){
cout << "MINIMUM SPANNING TREE GRAPH TIDAK BERBOBOT" << endl << endl;
cout << "Relasi antar node SEBELUM di proses" << endl;
lihatRelasi(true);
cout << endl << endl;
cout << "Relasi antar node SETELAH di proses" << endl;
lihatRelasi(false);
}
void findPath::proses(string jenisFindPath, int start, int dest, int vertex){
vertexCount = vertex;
defaultMatrix();
dist.resize(vertexCount, make_pair(-1, INT_MAX));
if (jenisFindPath == "djikstra")
_djikstra(start, dest);
}
void findPath::_djikstra(int source, int dest){
dist.at(source).second = 0;
for (int i = 0; i < vertexCount; i++){
int currVertex = minDistance(dist);
propGraph.visitedVertexLog.push_back(currVertex);
if (currVertex == dest){
//printDistance(dist);
getPath(dist, dest, source);
return;
}
for( int j = 0; j < vertexCount; j++)
if( find(propGraph.visitedVertexLog.begin(), propGraph.visitedVertexLog.end(), j) == propGraph.visitedVertexLog.end() && propGraph.adjacencyMatrix.at(currVertex).at(j)!= -1 && dist.at(currVertex).second + propGraph.adjacencyMatrix.at(currVertex).at(j) < dist.at(j).second )
dist.at(j).second = dist.at(currVertex).second + propGraph.adjacencyMatrix.at(currVertex).at(j), dist.at(j).first = currVertex;
}
}
vector <int> findPath::getShortest(){
getPath(dist,dest,source);
return propGraph.shortest;
}
void findPath::getDistance(vector< pair<int, int> > &dist){
cout << "Vertex Distance from Source" << endl;
for (int i = 0; i < vertexCount; i++)
if(dist.at(i).first!= -1)
cout << "vertex " << i + 1 << ": " << dist.at(i).second << ", Parent: " << dist.at(i).first + 1 << endl;
}
void findPath::getPath(vector< pair<int, int> > &dist, int dest, int source){
if ( dist.at(dest).first == -1 ){
cout << source << " ";
propGraph.shortest.push_back(source);
return;
}
propGraph.adjacencyTransversal.at(dist.at(dest).first).at(dest) = 1;
propGraph.adjacencyTransversal.at(dest).at(dist.at(dest).first) = 1;
getPath(dist, dist.at(dest).first, source );
cout << dest << " ";
propGraph.shortest.push_back(dest);
}
int findPath::minDistance(vector< pair<int, int> > &dist){
int minVal = INT_MAX, minIndex;
for (int i = 0; i < vertexCount; i++)
if( find(propGraph.visitedVertexLog.begin(), propGraph.visitedVertexLog.end(), i) == propGraph.visitedVertexLog.end() && dist.at(i).second <= minVal )
minVal = dist.at(i).second, minIndex = i;
return minIndex;
}
///////MINIMUM SPANNING TREE
void mst::show(){
cout << "MINIMUM SPANNING TREE GRAPH TIDAK BERBOBOT" << endl << endl;
cout << "Relasi antar node SEBELUM di proses" << endl;
//lihatRelasi(true);
cout << endl << endl;
cout << "Relasi antar node SETELAH di proses" << endl;
//lihatRelasi(false);
}
void mst::proses(string jenisMst, int start, int dest, int vertex){
vertexCount = vertex;
defaultMatrix();
if (jenisMst == "bfs")
_bfs(start);
else if (jenisMst == "dfs")
_dfs(start);
}
void mst::_bfs(int start){
node.push(start);
while(!node.empty()){
topNode = node.front();
for (int i = 0; i < vertexCount; i++){
if ( propGraph.adjacencyMatrix.at(topNode).at(i)!= -1 && find(propGraph.visitedVertexLog.begin(), propGraph.visitedVertexLog.end(), i) == propGraph.visitedVertexLog.end() ){
propGraph.adjacencyTransversal.at(topNode).at(i) = 1;
propGraph.adjacencyTransversal.at(i).at(topNode) = 1;
node.push(i);
propGraph.visitedVertexLog.push_back(i);
}
}
node.pop();
}
}
void mst::_dfs(int topNode = 0){
for (int i = 0; i < vertexCount; i++)
if ( propGraph.adjacencyMatrix.at(topNode).at(i)!=-1 && find(propGraph.visitedVertexLog.begin(), propGraph.visitedVertexLog.end(), i) == propGraph.visitedVertexLog.end() ){
propGraph.adjacencyTransversal.at(topNode).at(i) = 1;
propGraph.adjacencyTransversal.at(i).at(topNode) = 1;
propGraph.visitedVertexLog.push_back(i);
_dfs(i);
}
}
///////MINIMUM SPANNING TREE BERBOBOT
void mstBobot::show(){
cout << "MINIMUM SPANNING TREE GRAPH BERBOBOT_PRIM" << endl << endl;
cout << "Relasi antar node SEBELUM di proses" << endl;
lihatRelasi(true);
cout << endl << endl;
cout << "Relasi antar node SETELAH di proses" << endl;
lihatRelasi(false);
}
void mstBobot::proses(string jenisMstBobot, int start, int dest, int vertex){
vertexCount = vertex;
defaultMatrix();
if (jenisMstBobot == "prim")
_prim(start);
}
void mstBobot::_prim(int start){
propGraph.visitedVertexLog.push_back(start);
for( int n = 0; n < vertexCount-1; n++ ){
for (int j = 0; j < vertexCount; j++)
if( find(propGraph.visitedVertexLog.begin(), propGraph.visitedVertexLog.end(), j) != propGraph.visitedVertexLog.end())
for (int i = 0; i < vertexCount; i++ )
if ( propGraph.adjacencyMatrix.at(j).at(i) < lower && find(propGraph.visitedVertexLog.begin(), propGraph.visitedVertexLog.end(), i) == propGraph.visitedVertexLog.end() && propGraph.adjacencyMatrix.at(j).at(i)!= -1)
lower = propGraph.adjacencyMatrix.at(j).at(i), minIndexBaris = j, minIndexKolom = i;
propGraph.adjacencyTransversal.at(minIndexBaris).at(minIndexKolom) = 1;
propGraph.adjacencyTransversal.at(minIndexKolom).at(minIndexBaris) = 1;
propGraph.visitedVertexLog.push_back(minIndexKolom);
lower = INT_MAX;
}
}