This repository was archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.hpp
More file actions
177 lines (148 loc) · 4.57 KB
/
Graph.hpp
File metadata and controls
177 lines (148 loc) · 4.57 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef GRAPH
#define GRAPH
#include <iostream>
#include <map>
#include <vector>
#include <limits>
#define INF std::numeric_limits<double>::max()
template <typename T>
class Graph {
public:
Graph() {
}
auto updateVertex(int vertexId, T data) {
vertices[vertexId]->data = data;
return vertices[vertexId];
}
void AddVertex(int vId, T value) {
if (vertices[vId])
return;
vertices[vId] = new Vertex(vId, value);
};
void AddEdge(int start_id, int end_id, double cost = 0) {
vertices[start_id]->AddEdge(end_id, cost);
};
const T& GetVertexData(int vertex_id) const {
return vertices[vertex_id]->data;
};
void printGraph() {
for (const auto& vertex: vertices) {
std::string edges = "";
const auto& vertexEdges = vertex.second->GetEdges();
for (auto edgeIndex = 0; edgeIndex < (int)vertexEdges.size(); edgeIndex++) {
if (edgeIndex == 0)
edges += std::to_string(vertexEdges[edgeIndex]->GetDestID());
else
edges += ", " + std::to_string(vertexEdges[edgeIndex]->GetDestID());
}
std::cout << vertex.first << ": " << edges << "\n";
}
}
std::vector<int> GetAllVertexIDs() const {
std::vector<int> ids;
for (const auto& vertex: vertices) {
ids.push_back(vertex.first);
}
return ids;
};
std::map<int, double> DijkstraSPF(int start_id) const
{
// Create a set to store vertices that are being
// prerocessed
set< pair<double, int> > setds; //weight , id
// Create a map for distances and initialize all
// distances as infinite (INF) , the key of the map is the vertex id
vector<int> keys ;
for(std::map<int,int>::iterator it = vertices.begin(); it != vertices.end(); ++it) {
keys.push_back(it->first);
std::cout << "Key: " << it->first << std::endl();
}
std::map<int, double> dist;
for(auto id: keys) {
dist[id] = INF;
}
// Insert source itself in Set and initialize its
// distance as 0.
setds.insert(make_pair(0, start_id));
dist[src] = 0;
/* Looping till all shortest distance are finalized
then setds will become empty */
while (!setds.empty())
{
// The first vertex in Set is the minimum distance
// vertex, extract it from set.
pair<double, int> tmp = *(setds.begin());
setds.erase(setds.begin());
// vertex label is stored in second of pair (it
// has to be done this way to keep the vertices
// sorted distance (distance must be first item
// in pair)
int u = tmp.second;
// 'i' is used to get all adjacent vertices of a vertex
list< pair<int, int> >::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
{
// Get vertex label and weight of current adjacent
// of u.
int v = (*i).first;
int weight = (*i).second;
// If there is shorter path to v through u.
if (dist[v] > dist[u] + weight)
{
/* If distance of v is not INF then it must be in
our set, so removing it and inserting again
with updated less distance.
Note : We extract only those vertices from Set
for which distance is finalized. So for them,
we would never reach here. */
if (dist[v] != INF)
setds.erase(setds.find(make_pair(dist[v], v)));
// Updating distance of v
dist[v] = dist[u] + weight;
setds.insert(make_pair(dist[v], v));
}
}
}
std::cout<<"Vertex Distance from Source\n" ;
for(std::map<int,int>::iterator it = dist.begin(); it != dist.end(); ++it) {
printf("%d \t\t %d\n", it->first, it->second);
}
}
private:
class Vertex;
std::map<int, Vertex*> vertices;
class Edge {
public:
Edge(int end_id, double cost) : dest_id(end_id), cost(cost) {
}
const int GetDestID() {
return dest_id;
};
const double GetCost() {
return cost;
};
private:
int dest_id;
double cost;
};
class Vertex {
public:
Vertex(int id, T value) : id(id), data(value) {
}
void AddEdge(int end_id, double cost) {
Edge* temp_edge = new Edge(end_id, cost);
edges.push_back(temp_edge);
}
const T& GetData() {
return data;
};
const std::vector<Edge*>& GetEdges() {
return edges;
};
private:
int id;
T data;
std::vector<Edge*> edges;
};
};
#endif