我遇到了一个问题,我们想设计一个有效的算法来寻找一条重量最轻的简单路径。(具有最小权重的简单路径)。
我认为这不是多项式解,但有些朋友说可能是O(n)或O(n^2)或O(n lg n)。好了!
程序员还是专家,能帮我吗?有什么伪码吗?
编辑:
这个问题的输入是一个边上有整数权的树T。权重可以是负的、零的,也可以是正的。路径的长度是路径中边的权重之和。如果没有重复顶点,路径就很简单。
输出:在给定的树中找到最小权重的简单路径。
发布于 2015-02-11 10:06:26
下面是线性解的伪代码:
res = 0 // A path from a node to itself has a weight 0
// Runs depth first search from the v vertex and returns the weight of
// the shortest path that starts in a descendant of v and ends in v
def dfs(v):
// The shortest path from a descendant of v to v
minLength = 0
// The list of all lengths of paths coming from children
childLengths = []
for edge in edges(v):
childLength = dfs(edge.to)
// Update the result with a vertical path from the child
res = min(res, childLength + edge.weight)
// Update the minLength based on the child's value
minLength = min(minLength, childLength + edge.weight)
childLengths.add(childLength + edge.weight)
if childLengths.size() >= 2:
// Update the result based on two smallest children values.
// Finds the the first and the second minimum in a list in a linear time.
min1 = firstMin(childLengths)
min2 = secondMin(childLengths)
res = min(res, min1 + min2)
return minLength
dfs(root)
return res
如果树没有根,我们可以选择任何一个顶点作为根。
发布于 2015-02-11 09:42:41
在一棵树中,每个节点之间只有一条简单的路径(这里有个证据)。
因此,即使您尝试每一对节点找到之间的路径,您也会得到一个O(n^3)算法。
一种更好的方法是为每一个节点在一次访问中找到每个其他节点的成本。这使得算法降低到O(n^2)。
发布于 2015-02-11 09:53:16
有一个简单的O(n)算法,它是基于这样一个事实:在一棵树中总是有一些叶子,如果我们删除一片叶子,剩下的图仍然是树。这样我们就可以一个一个地删除树叶,然后用这种方法处理所有的树(所有的边缘)。
您需要保持为每个节点找到最轻的路径。
让我们假设你正在处理一些叶子,节点n是连接到的节点叶,我们想要确保你在注视着任何一条穿过连接这个n和叶子的边缘的路径。让我们来看看照片
此处的方向仅指从较早删除的节点到其“父节点”的方向,该节点稍后将被删除,它可能取决于处理树叶的顺序n。
如果我们有一条穿过叶子的最短路径,它可以把叶子的任何“子”和其他的“子”连接起来,或者它可以把叶子的任何“子”连接到树的其余部分。
在这两种情况下,保持从任何已处理节点到当前节点的最短路径就足够了。当我们处理叶子时,我们选择最轻的为叶建立的路径,并增加与它的连接的权重,并将其与已经为n保存的最短路径进行比较,这样我们将不可避免地将n的两条最短路径进行比较,并能够将它们结合在一起,并且在处理n的所有“子”时,都会保存n的最短路径。
这是伪码。
for each (node in graph)
{
node.shortest_path = 0; //zero length path from this node to this node
}
leaves = new list(all leaves in graph); //O(n) to find
int minWeight = 0; //any zero length path is minimum weight path currently found
//note that we a going to add new elements in list during the iteration
//and we will need to iterate through all of them including new added
//so in usual programming languages you will need to use for cycle
for each (leaf in leaves)
{
//we will have last node in tree with no connection
//because we will delete it on previous iteration
//and we don't need to process this node
//for this problem it is last node,
//but it may be not last if we have several trees instead of one
if (no point connected to leaf) continue;
n = only one node still connected to leaf
connection = connection between n and leaf
//check if we can make a short path which goes through n
//from one already processed "child" node to another
if (leaf.shortest_path +
connection.weight
+ n.shortest_path < minWeight )
{
minWeight = leaf.shortest_path+connection.weight+n.shortest_path;
}
//and we need to keep lightest path from any processed "child" to n
if (leaf.shortest_path + connection.weight < n.shortest_path )
{
n.shortest_path = leaf.shortest_path + connection.weight;
if(n.shortest_path < minWeight )
{
minWeight = n.shortest_path;
}
}
//delete connection and
connection.delete();
//if n is now leaf add it to leaf list
if (n is leaf)
{
leaves.Add(n);
}
}
因此,在主循环中,我们精确地处理了每个节点一次,因此我们具有O(n)复杂度。如果需要非零长路径,但该算法没有找到,这意味着所有的边都有正权,最轻的非零长路径是最轻的边。
https://stackoverflow.com/questions/28460252
复制