简体   繁体   English

连接n个节点的图中k个节点的最小路径

[英]Minimum path to connect k nodes in a graph with n nodes

Given a graph with n nodes and n-1 edges, find the lowest amount of edges needed to connect k nodes. 给定具有n个节点和n-1个边的图,找到连接k个节点所需的最小边数。 You are given m numbers k <= n for which you have to solve the problem. 您将获得m个数字k <= n,您必须为此解决问题。 The edges are unweighted and the graph does not necessarily describe a binary tree. 边缘未加权,图形不一定描述二叉树。

For a graph with n=5 and k=4, a possible path could be 1-2-5-2-3. 对于n = 5且k = 4的图,可能的路径可以是1-2-5-2-3。

My approach was a greedy method in which I start from the node with the highest rank and try to add edges from there, but I did not have much success in trying to formulate an algorithm. 我的方法是一种贪婪的方法,我从具有最高等级的节点开始并尝试从那里添加边缘,但我在尝试制定算法方面没有太大的成功。

Try to find longest path in your tree, you will take every edge on that route once, than choose random node that has an edge, that connect that node to already choosen elements. 尝试在树中找到最长路径,您将获取该路径上的每个边缘一次,而不是选择具有边缘的随机节点,将该节点连接到已经选择的元素。

So find longest path in your tree if that path has k or more nodes it is the end, you choose element on that path, every edge will be taken once 因此,如果该路径具有k个或更多节点,那么在树中找到最长路径,您选择该路径上的元素,每个边缘将被取一次
Else choose a random, untaken already node, that is connected by any edge directly to one of already taken nodes, as long as you do not have k nodes in your collection. 否则,只要您的集合中没有k个节点,就可以选择一个随机的,未被捕获的节点,该节点通过任何边缘直接连接到已捕获的节点之一。

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

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