-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchFunctions.template
More file actions
140 lines (121 loc) · 4.7 KB
/
SearchFunctions.template
File metadata and controls
140 lines (121 loc) · 4.7 KB
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
template <class Process, class Item, class SizeType>
void depth_first(Process f, Graph<Item>& g, SizeType search){
bool marked[g.MAXIMUM];
//Check that start vertex is a valid vertex
assert( search < g.size() );
//Set all the componets of marked to false
for (bool& mark : marked) {
mark = false;
}
//call a seperate function to carry out the search
rec_dfs(f, g, search, marked);
}
template <class Process, class Item, class SizeType>
void rec_dfs(Process f, Graph<Item>& g, SizeType v, bool marked[]){
std::set<std::size_t> connections = g.neighbors(v);
std::set<std::size_t>::iterator it;
marked[v] = true; // Mark vertex v.
f(g[v]); // process the label of vertex v with the funciotn f.
// Traverse all the neighbors, looking for unmarked vertices:
for (it = connections.begin(); it != connections.end(); ++it) {
if(!marked[*it])
rec_dfs(f, g, *it, marked);
}
}
template <class Process, class Item, class SizeType>
void breadth_first(Process f, Graph<Item>& g, SizeType v){
//Same as the depth first funciton, but using a breadth first search instead
bool marked[g.MAXIMUM];
std::set<std::size_t> connections = g.neighbors(v);
std::set<std::size_t>::iterator it;
std::queue<std::size_t> vertex_queue;
assert(v < g.size());
std::fill_n(marked, g.size(), false);
marked[v] = true; // Mark vertex v.
f(g[v]); // process the label of vertex v with the funciotn f.
vertex_queue.push(v);
do {
connections = g.neighbors(vertex_queue.front());
vertex_queue.pop();
//Mark the process the unmarked neighbors, and place them in the queue.
for (it = connections.begin(); it != connections.end(); ++it) {
if (!marked[*it]) {
marked[*it] = true;
f(g[*it]);
vertex_queue.push(*it);
}
}
} while (!vertex_queue.empty());
}
template <class Process, class Item, class SizeType>
void shortest_route(Process f, Graph<Item>& g, SizeType start, SizeType end){
//set up a list of all the distances
int distance[g.MAXIMUM];
int predecessor[g.MAXIMUM];
bool marked[g.MAXIMUM];
int vertex_on_path = INT_MAX;
for (int i = 1; i < g.MAXIMUM; ++i) {
distance[i] = INT_MAX;
marked[i] = false;
}
distance[start] = 0;
//initialize a set of vertices called allowed verticies
std::set<size_t> allowed_vertices;
assert(start < g.size());
//Step 3: loop
for (size_t allowed_size = 0; allowed_size < g.MAXIMUM-1; ++allowed_size) {
//3a: determine the minimun distance
const int min = min_distance(distance, marked, g.size());
marked[min] = true;
//f(g[min]);
//3a.2 : This is only if we care about the shortest distance
// if (min == end && distance[min] != INT_MAX) {
// std::cout << "The minimun distance is " << distance[min] << std::endl;
// break;
// }
//3b: add the vetex "next" in other words, the vertex at mindex to the set allowed_verticies
allowed_vertices.insert(min);
int sum = 0;
//3c: Revise the distance array using the vertices in the allowed_verticies
for (int v = 0; v < g.size(); ++v) {
if (allowed_vertices.find(v) == allowed_vertices.end() && g.is_edge(min, v)) {
sum = distance[min] + g.edge_at(min, v);
if (sum < distance[v]) {
distance[v] = sum;
predecessor[v] = min;
}
}
}
}
//Step 4: print all the values in the distance array
std::cout << "Values at the distance array : ";
for (auto x : distance) {
if (x < INT_MAX && x >= 0) {
std::cout << x << " ";
}
}
std::cout << std::endl << std::endl;
vertex_on_path = end;
if (distance[vertex_on_path] < 0 || distance[vertex_on_path] == INT_MAX) {
std::cout << "There is no possible path" << std::endl;
return;
}
std::cout << "The shortest distance : " << distance[vertex_on_path] << std::endl << std::endl << std::endl;
//Print the current shortest path in reverse order
std::cout <<"The shortest Path : " << vertex_on_path << " ";
while (vertex_on_path != start) {
vertex_on_path = predecessor[vertex_on_path];
std::cout << vertex_on_path << " ";
}
}
int min_distance(int dist[], bool marked[], int V){
int min = INT_MAX;
int min_dex = 0;
for (int i = 0; i < V; ++i) {
if(!marked[i] && dist[i] <= min && dist[i] > -1){
min = dist[i];
min_dex = i;
}
}
return min_dex;
}