Repository navigation
Expand file tree
/
Copy pathreachability.cpp
More file actions
70 lines (59 loc) · 1.98 KB
/
Copy pathreachability.cpp
File metadata and controls
70 lines (59 loc) · 1.98 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
#include "common.h"
#include <boost/graph/breadth_first_search.hpp>
#include <utility>
#include <vector>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include "reachability.hpp"
using namespace boost;
void reachability_bfs_visitor::discover_vertex(const Vertex &u, const Graph &g)
{
targetMap->at(u) = true;
}
void reachability_single_bfs_visitor::discover_vertex(const Vertex &u, const Graph &g)
{
if(u == *vertex) *reachability = true;
}
reachability_single_bfs_visitor::reachability_single_bfs_visitor(Vertex* vertex, bool* reachability) :
vertex(vertex), reachability(reachability) {}
bool reachability(const Graph &g, const Vertex& v, Vertex& u)
{
bool reachable = false;
reachability_single_bfs_visitor vis(&u, &reachable);
breadth_first_search(g, v, visitor(vis));
return reachable;
}
//tests the reachability of x pairs of vertices that are connected ing if they are connected in h
//expects g, h to have the same # of vertices
//returns # of vertex pairs that are reachable
int random_reachability_test(const Graph &g, const Graph &h, int x)
{
srand(time(NULL));
unsigned n = num_vertices(g);
std::multimap<Vertex, Vertex> VertexPairMap;
while(VertexPairMap.size() < x)
{
Vertex v = vertex(rand() % n, g);
Vertex u = vertex(rand() % n, g);
if(reachability(g, v, u)) VertexPairMap.emplace(v, u);
}
int reachableCount = 0;
for(auto it = VertexPairMap.begin(); it != VertexPairMap.end(); ++it)
{
if(reachability(h, it->first, it->second)) reachableCount++;
}
return reachableCount;
}
//deprecated function
std::multimap<Vertex, Vertex> reachability_test(const Graph &g, std::multimap<Vertex, Vertex>& PairMap)
{
std::multimap<Vertex,Vertex> retVal;
for(auto it = PairMap.begin(); it != PairMap.end(); ++it)
{
if(!reachability(g, it->first, it->second))
{
retVal.emplace(it->first, it->second);
}
}
return retVal;
}