2
0
mirror of https://github.com/boostorg/graph.git synced 2026-01-19 04:12:11 +00:00

added second part of iff test in check_transitive_closure

[SVN r20552]
This commit is contained in:
Jeremy Siek
2003-10-29 21:10:02 +00:00
parent 0658669b79
commit 668d9c7599
2 changed files with 63 additions and 10 deletions

View File

@@ -14,6 +14,7 @@
#include <boost/graph/vector_as_graph.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_list_io.hpp>
#include <boost/graph/graph_utility.hpp>
#include <cstdlib>
@@ -38,20 +39,38 @@ void generate_graph(int n, double p, vector< vector<int> >& r1)
r1[i].push_back(j);
}
// (i,j) is in E' if j is reachable from i
// (i,j) is in E' iff j is reachable from i
// Hmm, is_reachable does not detect when there is a non-trivial path
// from i to i. It always returns true for is_reachable(i,i).
// This needs to be fixed/worked around.
template <typename Graph, typename GraphTC>
bool check_transitive_closure(Graph& g, GraphTC& tc)
{
std::vector<default_color_type> color_map_vec(num_vertices(g));
typename graph_traits<GraphTC>::vertex_iterator i, i_end;
for (tie(i, i_end) = vertices(tc); i != i_end; ++i) {
typename graph_traits<GraphTC>::out_edge_iterator j, j_end;
for (tie(j, j_end) = out_edges(*i, tc); j != j_end; ++j)
if (!is_reachable(source(*j, g), target(*j, g), g, &color_map_vec[0]))
return false;
{
typename graph_traits<GraphTC>::vertex_iterator i, i_end;
for (tie(i, i_end) = vertices(tc); i != i_end; ++i) {
typename graph_traits<GraphTC>::out_edge_iterator j, j_end;
for (tie(j, j_end) = out_edges(*i, tc); j != j_end; ++j) {
std::vector<default_color_type> color_map_vec(num_vertices(g));
if (!is_reachable(source(*j, g), target(*j, g), g, &color_map_vec[0]))
return false;
}
}
}
{
typename graph_traits<GraphTC>::vertex_iterator i, i_end;
for (tie(i, i_end) = vertices(g); i != i_end; ++i) {
typename graph_traits<GraphTC>::vertex_iterator j, j_end;
for (tie(j, j_end) = vertices(g); j != j_end; ++j) {
std::vector<default_color_type> color_map_vec(num_vertices(g));
if (i != j && is_reachable(*i, *j, g, &color_map_vec[0])) {
typename graph_traits<GraphTC>::edge_descriptor e;
bool b;
tie (e, b) = edge(*i, *j, tc);
if (!b) return false;
}
}
}
}
return true;
}
@@ -73,8 +92,10 @@ bool test(int n, double p)
if(check_transitive_closure(g1, g1_tc))
return true;
else {
//cout << "Original graph was " << multiline << g1_c << endl;
//cout << "Result is " << multiline << g1 << endl;
cout << "Original graph was ";
print_graph(g1, identity_property_map());
cout << "Result is ";
print_graph(g1_tc, identity_property_map());
return false;
}
}

View File

@@ -0,0 +1,32 @@
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/transitive_closure.hpp>
#include <iostream>
using namespace std;
using namespace boost;
typedef adjacency_list<> graph_t;
int main(int argc, char *argv[]) {
graph_t g(5),g_TC;
add_edge(0,2,g);
add_edge(1,0,g);
add_edge(1,2,g);
add_edge(1,4,g);
add_edge(3,0,g);
add_edge(3,2,g);
add_edge(4,2,g);
add_edge(4,3,g);
transitive_closure(g,g_TC);
cout << "original graph: 0->2, 1->0, 1->2, 1->4, 3->0, 3->2, 4->2, 4->3"
<< endl;
cout << "transitive closure: ";
graph_t::edge_iterator i,iend;
for(tie(i,iend) = edges(g_TC);i!=iend;++i) {
cout << source(*i,g_TC) << "->" << target(*i,g_TC) << " ";
}
cout << endl;
}