繁体   English   中英

查找从一个节点到最远节点的距离BOOST

[英]Find distance from a node to the one farthest from it BOOST

我需要在最小生成树中确定从所有节点到最远离它的节点的距离。 到目前为止,我已经做到了这一点,但对于找到距节点最长的距离却一无所知。

#include<iostream>
#include<boost/config.hpp>
#include<boost/graph/adjacency_list.hpp>
#include<boost/graph/kruskal_min_spanning_tree.hpp>
#include<boost/graph/prim_minimum_spanning_tree.hpp>

using namespace std;
using namespace boost;

int main()
{
typedef adjacency_list< vecS, vecS, undirectedS, property <vertex_distance_t,int>, property< edge_weight_t, int> > Graph;
int test=0,m,a,b,c,w,d,i,no_v,no_e,arr_w[100],arr_d[100];
cin>>test;
m=0;
while(m!=test)
{
cin>>no_v>>no_e;
Graph g(no_v);
property_map <Graph, edge_weight_t>:: type weightMap=get(edge_weight,g);
bool bol;
graph_traits<Graph>::edge_descriptor ed;

for(i=0;i<no_e;i++)
{
cin>>a>>b>>c;
tie(ed,bol)=add_edge(a,b,g);
weightMap[ed]=c;
}

property_map<Graph,edge_weight_t>::type weightM=get(edge_weight,g);
property_map<Graph,vertex_distance_t>::type distanceMap=get(vertex_distance,g);
property_map<Graph,vertex_index_t>::type indexMap=get(vertex_index,g);

vector< graph_traits<Graph>::edge_descriptor> spanning_tree;

kruskal_minimum_spanning_tree(g,back_inserter(spanning_tree));

vector<graph_traits<Graph>::vector_descriptor>p(no_v);

prim_minimum_spanning_tree(g,0,&p[0],distancemap,weightMap,indexMap,default_dijkstra_visitor());



w=0;

for(vector<graph_traits<Graph>::edge_descriptor>::iterator eb=spanning_tree.begin();eb!=spanning_tree.end();++eb) //spanning tree weight
{
w=w+weightM[*eb];
}

arr_w[m]=w;
d=0;

graph_traits<Graph>::vertex_iterator vb,ve;

for(tie(vb,ve)=vertices(g),.

arr_d[m]=d;
m++;
}

for( i=0;i<test;i++)
{
cout<<arr_w[i]<<endl;
}

return 0;
}

如果我有一个节点为1 2 3 4的生成树,则需要在生成树中找到距1 2 3 4的最长距离(并且最长距离可以包括许多边,而不仅仅是一个边)。

我不会给您确切的代码如何执行此操作,但是会给您并提供如何执行操作的想法。

首先,MST(最小生成树)的结果称为树。 考虑一下定义。 可以说这是一个图,其中存在从每个节点到每个其他节点的路径,并且没有循环。 或者,您可以说给定图是一棵树,并且对于每个u和v,仅存在从顶点u到v的一条路径。

根据定义,您可以定义以下内容

function DFS_Farthest (Vertex u, Vertices P)
begin
    define farthest is 0
    define P0 as empty set
    add u to P

    foreach v from neighbours of u and v is not in P do
    begin
        ( len, Ps ) = DFS_Farthest(v, P)
        if L(u, v) + len > farthest then
        begin
            P0 is Ps union P
            farthest is len + L(u, v)
        end
    end

    return (farthest, P0)
end

然后,您将为图形调用中的每个顶点v调用DFS_Farthest(v, empty set) ,它会给您(最远的P),其中最远的是最远节点的距离,P是可从其重构路径的一组顶点从v到最远的顶点。

现在来描述它在做什么。 首先签名。 第一个参数是您想知道最远的那个顶点。 第二个参数是一组禁止的顶点。 因此它说:“嘿,给我从v到最远顶点的最长路径,以使P的顶点不在该路径中”。

接下来是这foreach事情。 在这里,您正在寻找当前顶点中最远的顶点,而无需访问P中已经存在的顶点(当前顶点已经存在)。 当您找到更长的路径时,当前找不到farthest路径和P0 注意, L(u, v)是边{u,v}的长度。

最后,您将返回这些长度和禁止的顶点(这是到最远顶点的路径)。

这只是简单的DFS(深度优先搜索)算法,您可以在其中记住已经访问过的顶点。

现在介绍时间复杂度。 假设您可以在O(1)中获得给定顶点的邻居(取决于您拥有的数据结构)。 函数仅对每个顶点访问一次。 因此至少为O(N)。 要知道每个顶点中最远的顶点,您必须为每个顶点调用此函数。 这使您解决问题的时间复杂度至少为O(n ^ 2)。

我的猜测是,使用动态编程可以实现更好的解决方案,但这只是一个猜测。 通常,在图中找到最长的路径是NP难题。 这使我怀疑可能没有更好的解决方案。 但这是另一个猜测。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM