undirected_dfs应该检测无向图的“所有”周期。 我实现了访问者的“back_edge”和“tree_edge”,它只发现了3个周期。
图表构建于:
boost::add_vertex(g); //0
boost::add_vertex(g); //1
boost::add_vertex(g); //2
boost::add_vertex(g); //3
boost::add_vertex(g); //4
boost::add_edge(0, 1, g);
boost::add_edge(0, 2, g);
boost::add_edge(0, 4, g);
boost::add_edge(1, 2, g);
boost::add_edge(1, 3, g);
boost::add_edge(2, 3, g);
boost::add_edge(2, 4, g);
[0 -> 1 -> 2 -> 0] Discovered
[1 -> 2 -> 3 -> 1] Discovereded
[0 -> 1 -> 2 -> 4 -> 0]Discover
[0 -> 1 -> 3 -> 2 -> 4 -> 0]
[0 -> 2 -> 4 -> 0]
[0 -> 1 -> 3 -> 2 -> 0]
以下代码我做错了什么?
#include <string>
#include <fstream>
#include <iostream>
#include <stack>
#include <map> //pair
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/undirected_dfs.hpp>
#include<boost/graph/properties.hpp>
#include <boost/graph/named_function_params.hpp> //for named parameter http://www.boost.org/doc/libs/1_58_0/libs/graph/doc/bgl_named_params.html
#include <boost/cstdlib.hpp> // for exit_success;
#include <boost/utility.hpp>
#include <boost/graph/visitors.hpp>
#include <boost/cstdlib.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/graph/graphviz.hpp>
//template du graph http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/adjacency_list.html
typedef boost::adjacency_list<
boost::vecS, //OutEdgeList
boost::vecS, //VertexList
boost::undirectedS //Directed
> Graph;
typedef boost::graph_traits < Graph >::vertex_descriptor Vertex;
typedef boost::graph_traits < Graph >::edge_descriptor Edge;
struct detect_loops : public boost::dfs_visitor<>
{
using colormap = std::map<Graph::vertex_descriptor, boost::default_color_type>;
colormap vertex_coloring;
using edgeColorMap = std::map<Graph::edge_descriptor, boost::default_color_type>;
edgeColorMap edge_coloring;
template <class Graph>
void tree_edge(Edge e, const Graph& g) {
std::cout << "tree_edge: " << boost::source(e, g) << " --> " << boost::target(e, g) << std::endl;
edgeVisited.push(e);
if (vertexVisited.empty()) {
vertexVisited.push(boost::source(e, g));
}
vertexVisited.push(boost::target(e, g));
}
template <class Graph>
void back_edge(Edge e, const Graph& g) {
Vertex v2;
std::cout << " Cycle detected with back-edge= " << e << std::endl;
std::cout << " vertexVisited size= " << vertexVisited.size() << std::endl;
std::cout << "Cycle end= " << boost::target(e, g) << std::endl;
//std::cout << "vertexVisited.top= " << vertexVisited.top() << std::endl;
while ( vertexVisited.top() != boost::target(e, g) )
{
std::cout << " Cycle middle=" << vertexVisited.top() << std::endl;
v2 = vertexVisited.top();
vertexVisited.pop();
}
std::cout << "Cycle starting= " << vertexVisited.top() << std::endl;
vertexVisited.push(v2);
}
//interface to the main.
std::vector<Vertex> GetCycles() const { return Cycle; }
private:
std::vector<Vertex> Cycle;
//std::stack<Edge> visited;
std::stack<Edge> edgeVisited;
std::stack<Vertex> vertexVisited;
};
Graph make(Graph &g)
{
boost::add_vertex(g); //0
boost::add_vertex(g); //1
boost::add_vertex(g); //2
boost::add_vertex(g); //3
boost::add_vertex(g); //4
boost::add_edge(0, 1, g);
boost::add_edge(0, 2, g);
boost::add_edge(0, 4, g);
boost::add_edge(1, 2, g);
boost::add_edge(1, 3, g);
boost::add_edge(2, 3, g);
boost::add_edge(2, 4, g);
std::ofstream f("d:\\tmp\\dot\\s13.dot");
boost::write_graphviz(f, g);
std::system( std::string("dot -Tsvg -Grankdir=LR -Nfontsize=24 d:\\tmp\\dot\\s13.dot > d:\\tmp\\dot\\s13.svg").c_str() );
return g;
}
//---------------------------------------------------------
//---------------------------------------------------------
int main()
{
Graph g;
detect_loops vis;
make(g);
std::ofstream f("d:\\tmp\\s13.dot");
boost::write_graphviz(f, g);
std::system(
std::string("dot -Tsvg -Grankdir=LR -Nfontsize=24 d:\\tmp\\s13.dot > d:\\tmp\\s13.svg").c_str()
);
boost::undirected_dfs(g, vis, make_assoc_property_map(vis.vertex_coloring), make_assoc_property_map(vis.edge_coloring));
std::vector<Vertex> vctr = vis.GetCycles();
return boost::exit_success;
}
答案 0 :(得分:1)
首先,请注意,任何较大尺寸的周期都可能由较小尺寸的周期组成。对于您的情况,可以正确检测较小尺寸的周期。例如,[0 -> 1 -> 2 -> 0]
和[1 -> 2 -> 3 -> 1]
都是Discovered
,但[0 1 3 2 0]
未检测到combination of these two smaller ones
。您可以简单地创建一个method
来获取所有周期并检查它们之间是否有任何共同点,然后它组合所有节点并返回union
个新周期。在[0 -> 1 -> 2 -> 0]
和[1 -> 2 -> 3 -> 1]
中,1
和2
位于common
,因此如果path
中有0
在第一个周期中{1 and 2}
,在第二个周期中path
从3
到{1 and 2}
,path
肯定有0
来自3
也是return
。因此,它可以union
两个周期的[0 1 3 2 0]
AT LEAST ONE NODE
共同scala> val cdc_new_acct_df = delta_src_rename_df.join(hist_tgt_tbl_Y_df ,delta_src_rename_df(**s"$delta_primary_key_col"**) === hist_tgt_tbl_Y_df(s"$primary_key_col"),"left_outer" )
cdc_new_acct_df: org.apache.spark.sql.DataFrame = [delta_acct_nbr: string, delta_primary_state: string, delta_zip_code: string, delta_load_tm: string, delta_load_date: string, hash_key_col: string, delta_hash_key: int, delta_eff_start_date: string, acct_nbr: string, account_sk_id: bigint, primary_state: string, zip_code: string, eff_start_date: string, eff_end_date: string, load_tm: string, hash_key: string, eff_flag: string]
。