-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
153 lines (121 loc) · 3.28 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
//
// Graph data structure
//
// Creates a random graph (directed or undirected)
//
// Perforces DFS and BFS on this graph
//
#include <iostream>
#include <vector>
#include <map>
#include <queue>
const int NUMBER_OF_VERTICES = 10;
const bool DIRECTED_GRAPH = true;
const int MIN_NUM_OF_EDGES = 1;
const int MAX_NUM_OF_EDGES = 3;
struct Vertex {
int value;
bool visited;
std::vector<Vertex*> edges;
Vertex(int in_value);
void DFS();
};
class Graph {
public:
void addVertex(int value);
void addEdge(int vertex1, int vertex2);
Vertex* getVertex(int value);
void DFS(int vertex);
void BFS(int vertex);
private:
std::map<int, Vertex*> vertices;
};
void Graph::addVertex(int value) {
Vertex* newVertex = new Vertex(value);
vertices[value] = newVertex;
}
void Graph::addEdge(int vertex1, int vertex2) {
std::cout << "Adding edge: " << vertex1 << " -> " << vertex2 << std::endl;
Vertex* vertex = vertices[vertex1];
vertex->edges.push_back(vertices[vertex2]);
}
Vertex* Graph::getVertex(int value) {
return vertices[value];
}
Vertex::Vertex(int in_value) : value(in_value) {
}
void Graph::DFS(int vertex) {
for (int v=0; v<NUMBER_OF_VERTICES; v++) {
vertices[v]->visited = false;
}
vertices[vertex]->DFS();
}
void Vertex::DFS() {
if (!visited) {
std::cout << value << std::endl;
visited = true;
}
for (int e=0; e<edges.size(); e++) {
if (!edges[e]->visited)
edges[e]->DFS();
}
}
void Graph::BFS(int vertex) {
for (int v=0; v<NUMBER_OF_VERTICES; v++) {
vertices[v]->visited = false;
}
std::queue<Vertex*> queueBFS;
queueBFS.push(vertices[vertex]);
while (!queueBFS.empty()) {
Vertex* nextVertex = queueBFS.front();
queueBFS.pop();
if (!nextVertex->visited) {
nextVertex->visited = true;
std::cout << nextVertex->value << std::endl;
for (int e=0; e<nextVertex->edges.size(); e++) {
queueBFS.push(nextVertex->edges[e]);
}
}
}
}
Graph createRandomGraph() {
Graph graph;
for (int v=0; v<NUMBER_OF_VERTICES; v++) {
graph.addVertex(v);
}
for (int v=0; v<NUMBER_OF_VERTICES; v++) {
std::vector<bool> used(NUMBER_OF_VERTICES, false);
int numOfEdges = rand() % (MAX_NUM_OF_EDGES - MIN_NUM_OF_EDGES + 1) + MIN_NUM_OF_EDGES;
used[v] = true;
Vertex* vertex = graph.getVertex(v);
for (int e=0; e<vertex->edges.size(); e++)
{
used[vertex->edges[e]->value] = true;
numOfEdges--;
}
for (int e=0; e<numOfEdges; e++) {
while (true) {
int vertex = rand() % NUMBER_OF_VERTICES;
if (!used[vertex]) {
graph.addEdge(v, vertex);
if (!DIRECTED_GRAPH)
graph.addEdge(vertex, v);
used[vertex] = true;
break;
}
}
}
}
return graph;
}
int main() {
srand(time(NULL));
Graph graph = createRandomGraph();
std::cout << std::endl;
std::cout << "DFS: " << std::endl;
graph.DFS(0);
std::cout << std::endl;
std::cout << "BFS: " << std::endl;
graph.BFS(0);
return 0;
}