-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cpp
160 lines (131 loc) · 3.2 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
//
// Created by ahmad on 12/27/16.
//
#include <algorithm>
#include <stack>
#include <queue>
#include "Graph.h"
GraphEdgeConnection::GraphEdgeConnection(char v1, char v2)
{
vertex1.key = v1;
vertex2.key = v2;
}
bool GraphEdgeConnection::containVertex(char v)
{
return (v == vertex1.key) || (v == vertex2.key);
}
GraphVertex::GraphVertex()
{
isVisited = false;
}
bool Graph::containVertex(char v)
{
for (int i = 0; i < vertexes.size(); ++i) {
if(vertexes[i].key == v)
return true;
}
return false;
}
bool Graph::isVisited(char v)
{
for (int i = 0; i < vertexes.size(); ++i) {
if(vertexes[i].key == v)
return vertexes[i].isVisited;
}
exit(-1);
}
std::vector<char> Graph::getAllNotVisitedVertexesConnectedTo(char v)
{
std::vector<char> result;
for (int i = 0; i < edgeTable.size(); ++i) {
if(edgeTable[i].containVertex(v))
{
char c = (edgeTable[i].vertex1.key == v)? edgeTable[i].vertex2.key : edgeTable[i].vertex1.key;
if(!isVisited(c))
result.push_back(c);
}
}
std::sort(result.begin(), result.end());
return result;
}
void Graph::addEdge(char v1, char v2)
{
if( !containVertex(v1))
{
GraphVertex g1;
g1.key = v1;
vertexes.push_back(g1);
}
if( !containVertex(v2))
{
GraphVertex g2;
g2.key = v2;
vertexes.push_back(g2);
}
edgeTable.push_back(GraphEdgeConnection(v1, v2));
}
void Graph::visitVertex(char v)
{
for (int i = 0; i < vertexes.size(); ++i) {
if(vertexes[i].key == v)
{
vertexes[i].isVisited = true;
return;
}
}
}
std::vector<char> Graph::dfs(char startVertex)
{
std::vector<char> result;
char currentVertex = startVertex;
std::stack<char> stck;
stck.push(currentVertex);
result.push_back(currentVertex);
visitVertex(currentVertex);
while (! stck.empty())
{
currentVertex = stck.top();
std::vector<char> nearestVertexes = getAllNotVisitedVertexesConnectedTo(currentVertex);
if(nearestVertexes.size() == 0)
{
stck.pop();
}
else
{
currentVertex = nearestVertexes[0];
stck.push(currentVertex);
result.push_back(currentVertex);
visitVertex(currentVertex);
}
}
resetVisited();
return result;
}
std::vector<char> Graph::bfs(char startVertex)
{
std::vector<char> result;
char currentVertex = startVertex;
std::queue<char> q;
q.push(currentVertex);
visitVertex(currentVertex);
result.push_back(currentVertex);
while (! q.empty())
{
currentVertex = q.front();
q.pop();
std::vector<char> nearestVertexes = getAllNotVisitedVertexesConnectedTo(currentVertex);
for (int i = 0; i < nearestVertexes.size(); ++i) {
result.push_back(nearestVertexes[i]);
visitVertex(nearestVertexes[i]);
q.push(nearestVertexes[i]);
}
}
resetVisited();
return result;
}
void Graph::resetVisited()
{
for (int i = 0; i < vertexes.size(); ++i) {
vertexes[i].isVisited = false;
}
}