-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.cpp
65 lines (60 loc) · 1.29 KB
/
algorithms.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
#include "engine.h"
void engine::BFS(vertex& entry)
{
for (auto& vertex : vertices)
{
vertex->reset();
vertex->toggle_distance_display(true);
}
entry.set_color(vertex_color::gray);
entry.set_distance(0);
bfs_queue = {};
bfs_queue.push(&entry);
if (!step_by_step)
while (!bfs_queue.empty())
BFS_step();
}
void engine::BFS_step()
{
if (bfs_queue.empty())
return;
auto u = bfs_queue.front();
bfs_queue.pop();
for(auto adj : u->get_adjacents())
{
if(adj->get_color() == vertex_color::white)
{
adj->set_color(vertex_color::gray);
adj->set_distance(u->get_distance() + 1);
adj->set_parent(u);
bfs_queue.push(adj);
}
}
u->set_color(vertex_color::black);
}
void engine::DFS()
{
for (auto& vertex : vertices)
{
vertex->reset();
vertex->toggle_distance_display(true);
}
dfs_time = 0;
bfs_queue = {};
for (auto& vertex : vertices)
if (vertex->get_color() == vertex_color::white)
DFS_step(vertex.get());
}
void engine::DFS_step(vertex* a)
{
a->set_d(++dfs_time);
a->set_color(vertex_color::gray);
for(auto vertex : a->get_adjacents())
if(vertex->get_color() == vertex_color::white)
{
vertex->set_parent(a);
DFS_step(vertex);
}
a->set_color(vertex_color::black);
a->set_f(++dfs_time);
}