-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreadth_first_search.h
50 lines (42 loc) · 1.11 KB
/
breadth_first_search.h
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
#ifndef CPP_ALGORITHM_BREADTH_FIRST_SEARCH_H
#define CPP_ALGORITHM_BREADTH_FIRST_SEARCH_H
#include <set>
#include <vector>
namespace Bfs
{
enum VisitStatus
{
Unvisited,
Visited,
Finished
};
struct Vertex
{
explicit Vertex(const char id)
: id(id), neighbors(std::set<Vertex*>()), predecessor(nullptr), visit(Unvisited), distance(0)
{
}
char id;
std::set<Vertex*> neighbors;
Vertex* predecessor;
VisitStatus visit;
int distance;
};
class Graph
{
public:
/**
* \brief Breadth first search algorithm.
* \details A search algorithm that traverses a graph layer by layer.
* \param start starting vertex
* \param goal goal vertex
* \return goal vertex
*/
static auto BreadthFirstSearch(Vertex& start, const Vertex& goal) -> Vertex*;
void AddVertex(Vertex& v);
void AddEdge(Vertex& u, Vertex& v);
std::vector<Vertex*> vertices;
std::vector<std::tuple<Vertex*, Vertex*>> adjacency_list;
};
}
#endif