-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
151 lines (118 loc) · 4.19 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
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
#ifndef GRAPH_H
#define GRAPH_H
#include <iostream>
#include <vector>
#include <list>
#include <memory> // std::shared_ptr
#include <tuple> // std::tuple std::get
#include <limits> // std::numeric_limits
#include <assert.h> // assert
#include <algorithm> // std::make_heap
#include <deque> // std::deque
#include "board.h"
namespace HEX {
typedef unsigned int uint;
struct EdgeNode {
EdgeNode(uint start_vertex, uint adjacent_vertex, float weight)
: m_start_vertex(start_vertex)
, m_adjacent_vertex(adjacent_vertex)
, m_weight(weight)
{}
friend std::ostream& operator<<(std::ostream& out, const EdgeNode& edge) {
out << edge.m_start_vertex << "->" << edge.m_adjacent_vertex << " (" << edge.m_weight << ")" << std::endl;
return out;
}
uint m_start_vertex; // the index of start VertexNode
uint m_adjacent_vertex; // the index of adjacent VertexNode
float m_weight;
};
struct VertexNode {
VertexNode()
: m_index(std::numeric_limits<uint>::max())
, m_distance(std::numeric_limits<float>::infinity())
, m_predecessor(std::numeric_limits<uint>::max())
, m_status(NodeColor::UNOCCUPIED)
{}
uint m_index;
NodeColor m_status;
float m_distance; // the distance estimation
uint m_predecessor; // the index of vertex' predecessor node in the shortest path
std::list<std::shared_ptr<EdgeNode> > m_edge_list;
};
class VertexComparision {
public:
bool operator()(const std::shared_ptr<VertexNode>& lhs, const std::shared_ptr<VertexNode>& rhs) {
return (lhs->m_distance > rhs->m_distance);
}
};
class Graph {
public:
typedef std::vector<std::shared_ptr<VertexNode> > VertexList;
typedef std::deque<std::shared_ptr<VertexNode> > PriorityDeque;
public:
// default constructor
Graph() {}
virtual ~Graph() {}
//implement of dijkstra algorithm
void dijkstra(uint s);
// returns the number of vertices in the graph
inline uint vertices_number() const {
return m_vertices.size();
}
inline VertexList get_vertices() {
return m_vertices;
}
// returns the number of edges in the graph
uint edges_number();
// returns the value associated to the edge (x, y)
float get_edge_value(uint x, uint y);
// tests whether there is an edge from node x to node y
bool is_adjacent(uint x, uint y);
// lists all nodes y such that there is an edge from x to y
VertexList neighbors(uint x);
// adds to G the edge from x to y with weight v default 1,if it is not there
void add_edge(uint x, uint y, float v = 1);
// sets the value associated to the edge(x, y) to v if there is an edge between x and y
void set_edge_value(uint x, uint y, float v);
// removes the edge from x to y,if it is there
void remove_edge(uint x, uint y);
// returns the value associated with node x
inline float get_node_value(uint x) const {
assert (x < vertices_number());
return m_vertices[x]->m_distance;
}
// sets the value associated with the node x to a if the node exists
inline void set_node_value(uint x, float a) {
if (x < m_vertices.size()) {
m_vertices[x]->m_distance = a;
}
}
// gets the node's predecessor's index if it exists in shortest path
inline uint get_node_predecessor(uint x) {
return m_vertices[x]->m_predecessor;
}
// sets the node's predecessor's index if the node exists
inline void set_node_predecessor(uint x, uint predecessor) {
if (x < m_vertices.size()) {
m_vertices[x]->m_predecessor = predecessor;
}
}
inline NodeColor get_node_status(uint x) {
assert (x >= 0 && x < m_vertices.size());
return m_vertices[x]-> m_status;
}
inline void set_node_color(uint x, NodeColor color) {
assert (x >= 0 && x < m_vertices.size());
assert (color == NodeColor::RED || color == NodeColor::BLUE);
m_vertices[x]-> m_status = color;
}
protected:
VertexList m_vertices;
PriorityDeque m_priority_deque;
private:
// no copy allowed
Graph(const Graph&);
Graph& operator=(const Graph&);
};
} // namespace HEX
#endif // GRAPH_H