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
|
#include "Graph/Path.h"
#include "Graph/BidirectionalBFS.h"
#include "Graph/BidirectionalBFSVisitor.h"
#include "Common/UnorderedMap.h"
#include <boost/graph/adjacency_list.hpp>
#include <gtest/gtest.h>
using namespace std;
static const bool DEBUG = false;
typedef boost::adjacency_list<boost::vecS, boost::vecS,
boost::bidirectionalS> Graph;
// note: vertex_descriptor for adjacency_list<> is int
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
// note: edge_descriptor for adjacency_list<> is int
typedef boost::graph_traits<Graph>::edge_descriptor Edge;
class TestVisitor : public BidirectionalBFSVisitor<Graph>
{
public:
typedef unordered_map<Vertex, Direction> DirMap;
typedef unordered_map<Vertex, int> RankMap;
typedef vector<Edge> EdgeList;
DirMap dirMap;
RankMap rankMap;
EdgeList commonEdges;
int rank;
TestVisitor() : rank(0) { }
BFSVisitorResult discover_vertex(const Vertex& u, const Graph&, Direction dir,
unsigned)
{
if (DEBUG)
cerr << dir << ": discover_vertex " << u << "\n";
return SUCCESS;
}
void examine_vertex(const Vertex& u, const Graph&, Direction dir)
{
if (DEBUG)
cerr << dir << ": examine_vertex " << u << "\n";
dirMap[u] = dir;
rankMap[u] = rank++;
}
void examine_edge(const Edge& e, const Graph&, Direction dir)
{
if (DEBUG)
cerr << dir << ": examine_edge " << e << "\n";
}
BFSVisitorResult tree_edge(const Edge& e, const Graph&, Direction dir)
{
if (DEBUG)
cerr << dir << ": tree_edge " << e << "\n";
return SUCCESS;
}
BFSVisitorResult common_edge(const Edge& e, const Graph&, Direction dir)
{
if (DEBUG)
cerr << dir << ": common_edge " << e << "\n";
commonEdges.push_back(e);
return SUCCESS;
}
BFSVisitorResult non_tree_edge(const Edge& e, const Graph&, Direction dir)
{
if (DEBUG)
cerr << dir << ": non_tree_edge " << e << "\n";
commonEdges.push_back(e);
return SUCCESS;
}
void gray_target(const Edge& e, const Graph&, Direction dir)
{
if (DEBUG)
cerr << dir << ": gray_target " << e << "\n";
}
void black_target(const Edge& e, const Graph&, Direction dir)
{
if (DEBUG)
cerr << dir << ": black_target " << e << "\n";
}
};
namespace {
class BidirectionalBFSTest : public ::testing::Test {
protected:
Graph linearGraph;
Graph branchingGraph;
BidirectionalBFSTest() {
add_edge(0, 1, linearGraph);
add_edge(1, 2, linearGraph);
add_edge(2, 3, linearGraph);
add_edge(0, 1, branchingGraph);
add_edge(0, 2, branchingGraph);
add_edge(1, 3, branchingGraph);
add_edge(3, 4, branchingGraph);
add_edge(4, 6, branchingGraph);
add_edge(5, 6, branchingGraph);
}
};
TEST_F(BidirectionalBFSTest, AlternatesDirection)
{
TestVisitor visitor;
bidirectionalBFS(linearGraph, 0, 3, visitor);
TestVisitor::DirMap& dir = visitor.dirMap;
ASSERT_EQ(dir[0], FORWARD);
ASSERT_EQ(dir[3], REVERSE);
ASSERT_EQ(dir[1], FORWARD);
ASSERT_EQ(dir[2], REVERSE);
}
TEST_F(BidirectionalBFSTest, FollowsBreadthFirstOrder)
{
TestVisitor visitor;
bidirectionalBFS(branchingGraph, 0, 6, visitor);
TestVisitor::RankMap& rank = visitor.rankMap;
ASSERT_TRUE(rank[1] > rank[0]);
ASSERT_TRUE(rank[2] > rank[0]);
ASSERT_TRUE(rank[3] > rank[1]);
ASSERT_TRUE(rank[3] > rank[2]);
ASSERT_TRUE(rank[3] > rank[4]);
ASSERT_TRUE(rank[3] > rank[5]);
ASSERT_TRUE(rank[4] > rank[6]);
ASSERT_TRUE(rank[5] > rank[6]);
}
TEST_F(BidirectionalBFSTest, IdentifiesCommonEdge)
{
TestVisitor visitor;
bidirectionalBFS(linearGraph, 0, 3, visitor);
TestVisitor::EdgeList& commonEdges = visitor.commonEdges;
// Note: Each common edge is visited twice. It is
// visited once by the forward traversal and once
// by the reverse traversal.
ASSERT_TRUE(commonEdges.size() == 2);
ASSERT_TRUE(commonEdges[0] == commonEdges[1]);
ASSERT_TRUE(source(commonEdges[0], linearGraph) == 1u);
ASSERT_TRUE(target(commonEdges[0], linearGraph) == 2u);
}
}
|