-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
67 lines (55 loc) · 1.45 KB
/
graph.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef ALGORITHMS_GRAPH_H_
#define ALGORITHMS_GRAPH_H_
#include <list>
#include <set>
#include <vector>
struct edge
{
edge(int From, int To) : from(From), to(To) {};
int from;
int to;
};
struct compareEdge
{
bool operator()(std::pair<int, edge> left, std::pair<int, edge> right)
{
return left.first > right.first;
}
};
struct comparePair
{
bool operator()(std::pair<int, int> left, std::pair<int, int> right)
{
return left.first > right.first;
}
};
class graph
{
public:
std::list<int> DFS(int origin);
std::list<int> BFS(int origin);
void setGraph(const std::vector<std::list<int>> &graph);
void setGraph(const std::vector<std::vector<int>> &graph);
void toMatrix(const std::vector<std::list<int>> &adj_list);
void toList(const std::vector<std::vector<int>> &adj_matrix);
void addEdge(int A, int B, int cost, bool bidirectional = true);
int Dijkstra(int from, int to);
int BellmanFord(int from, int to);
std::list<std::pair<int, int>> Kruskal();
std::list<edge> Prim();
void initMatrix(int nrNodes);
graph();
private:
std::list<edge> getEdges();
std::vector<std::list<int>> adj_list;
std::vector<std::vector<int>> adj_matrix;
};
inline void graph::setGraph(const std::vector<std::list<int>>& graph)
{
adj_list = graph;
}
inline void graph::setGraph(const std::vector<std::vector<int>>& graph)
{
adj_matrix = graph;
}
#endif ALGORITHMS_GRAPH_H_