-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.cpp
109 lines (105 loc) · 3.46 KB
/
functions.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
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
#include "functions.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
using namespace std;
vector<Graph::Vertex> Functions::BFS(Graph g) {
Graph::Vertex start = g.getFirstVertex();
// queue for BFS
queue<Graph::Vertex> q;
q.push(start);
// use a map to keep tack of visited vertex. Default initialization is unvisited, false.
map<string, bool> visited;
// initialize the start vertex as visited, true.
visited[start.getVertexId()] = true;
// vector to store the BFS path
vector<Graph::Vertex> result;
while(!q.empty()) {
// take the first element
Graph::Vertex curr = q.front();
q.pop();
// store path
result.push_back(curr);
// find unvisited adjacent vertex and update
for (Graph::Vertex& v: g.getAdjacentVertex(curr)) {
if (!visited[v.getVertexId()]) {
q.push(v);
visited[v.getVertexId()] = true;
}
}
}
return result;
}
vector<Graph::Vertex> Functions::BFS(Graph g, Graph::Vertex start) {
// queue for BFS
queue<Graph::Vertex> q;
q.push(start);
// use a map to keep tack of visited vertex. Default initialization is unvisited, false.
map<string, bool> visited;
// initialize the start vertex as visited, true.
visited[start.getVertexId()] = true;
// vector to store the BFS path
vector<Graph::Vertex> result;
while(!q.empty()) {
// take the first element
Graph::Vertex curr = q.front();
q.pop();
// store path
result.push_back(curr);
// find unvisited adjacent vertex and update
for (Graph::Vertex& v: g.getAdjacentVertex(curr)) {
if (!visited[v.getVertexId()]) {
q.push(v);
visited[v.getVertexId()] = true;
}
}
}
return result;
}
map<string, string> Functions::Dijkstra(Graph g, Graph::Vertex start, Graph::Vertex end) {
// priority queue for Dijkstra
typedef pair<double, string> iPair;
priority_queue<iPair, vector<iPair>, greater<iPair> > pq;
// record each vertex's distance and path
map<string, double> dist;
map<string, string> path;
map<string, bool> visited;
// initialization
for (Graph::Vertex& v: g.getAllVertices()) {
string vertexId = v.getVertexId();
dist[vertexId] = __DBL_MAX__;
path[vertexId] = "";
}
// initialize the start point
string startId = start.getVertexId();
pq.push(iPair(0, startId));
dist[startId] = 0;
visited[startId] = true;
while(pq.top().second != end.getVertexId()) {
// take the first element
string curr = pq.top().second;
Graph::Vertex temp = g.getVertex(curr);
pq.pop();
// visited vertex
visited[curr] = true;
// check adjacent vertices
for(Graph::Vertex& v: g.getAdjacentVertex(temp)) {
string vertexId = v.getVertexId();
if (!visited[vertexId]) {
Graph::Edge edge = g.getEdge(temp, v);
double weight = edge.getDistance();
// update distance if necessary
if ((weight + dist[curr]) < dist[vertexId]) {
dist[vertexId] = weight + dist[curr];
path[vertexId] = curr;
pq.push(iPair(dist[vertexId], vertexId));
}
}
}
}
return path;
}