-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
109 lines (99 loc) · 3.38 KB
/
graph.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
#include <iostream>
#include <string>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <list>
using namespace std;
class graph{
protected:
map<string, map<string, double>> _graph;
set<string> _nodes;
map<string,set<string>> _outEdges;
public:
// set<string> &_nodes(){
// return _nodes;
// }
// map<string, map<string, double>> &_graph(){
// return _graph;
// }
// map<string,set<string>> &_outEdges(){
// return _outEdges;
// }
void addOut(const string &src, const string &dst){
if (_outEdges.count(src) > 0)
{
_outEdges[src].insert(dst);
}else{
set<string> temp{dst};
_outEdges[src] = temp;
}
}
void addNode(const string &_node){
_nodes.insert(_node);
}
void addEdge(const string &src, const string &dst, double weight){
addNode(src);addNode(dst);
_graph[src][dst] = weight;
_graph[dst][src] = ((double)1)/weight;
addOut(src,dst);
addOut(dst,src);
}
double getWeight(const string &src, const string &dst){
return _graph[src][dst];
}
list<string> bfs(const string &src, const string &dest){
set<string> visited;
queue<list<string>> Queue;
list<string> l;
l.push_front(src);
Queue.push(l);
while (!Queue.empty())
{
list<string> temp = Queue.front();
visited.insert(temp.front());
Queue.pop();
for(string neighbor : _outEdges[temp.front()]){
if (neighbor == dest){
//probaly not needed
visited.insert(dest);
//i added
temp.push_front(dest);
//************
return temp;
}if (visited.count(temp.front())==0){
temp.push_front(neighbor);
Queue.push(temp);
}
}
}
return list<string>{};
}
double ThereIsPath(const string &src, const string &dest){
if (0 == _nodes.count(dest) && _nodes.count(src) == 0){
cout << "There is a measure unit not that is not in src file" << endl;
return -1;
}
list<string> bfsReturn = bfs(src,dest);
if(bfsReturn.empty()){
cout << "Can't convert this 2.." << endl;
return -1;
}
//The last in the list is src and the first is the destenation
//we need to get weights and multiply them until we reach the end.
string temp = bfsReturn.back();
bfsReturn.pop_back();
string temp2;
double returnVal = 1.0;
while (!bfsReturn.empty())
{
temp2 = bfsReturn.back();
bfsReturn.pop_back();
returnVal *= getWeight(temp,temp2);
// cout << "retval: " <<returnVal << endl;
temp = temp2;
}
return returnVal;
}
};