Php 具有随机链接到阵列中其他节点的节点的线性阵列,最短路径
信息: 我有一个100个节点的数组,[0..99]。每个节点可以有任意数量的链接节点: eg1,0链接到5,10,15,20。 eg2,1链接到30,40,50。 eg3等 所有100个节点都至少有一个链接节点,节点不知道谁链接到它们 问题: 如果提供了起点和终点,如何找到最短的链接路径 例如,开始=5,结束=80,链接路径(示例):[5]->10->24->36->[80]Php 具有随机链接到阵列中其他节点的节点的线性阵列,最短路径,php,delphi,shortest-path,Php,Delphi,Shortest Path,信息: 我有一个100个节点的数组,[0..99]。每个节点可以有任意数量的链接节点: eg1,0链接到5,10,15,20。 eg2,1链接到30,40,50。 eg3等 所有100个节点都至少有一个链接节点,节点不知道谁链接到它们 问题: 如果提供了起点和终点,如何找到最短的链接路径 例如,开始=5,结束=80,链接路径(示例):[5]->10->24->36->[80] 我使用的是Pascal和/或PHP,但了解我想要的[代码也有帮助]。大量阅读/算法: . 实际上,每一条边(你称之为“链
我使用的是Pascal和/或PHP,但了解我想要的[代码也有帮助]。大量阅读/算法:
. 实际上,每一条边(你称之为“链接”)的权重都是相等的。大量阅读/算法:
. 实际上,每一条边(你称之为“链接”)的权重都是相等的。这是树/图还是森林?如果它是一个林,路径可能并不总是被定义的。如果这是一个树/图,请尝试使用广度优先搜索
可以这样想:比如说,你正在执行一项秘密任务,在你的邻居中寻找可爱的小鸡。你从自己的家开始,并将其标记为起点。你接下来会去拜访你最近的邻居,对吗?所以,我们要做的就是——将所有连接到起始端的节点推送到队列中。现在,对该队列中的所有节点重复邻居搜索。继续这样做直到你得到你的女孩,呃,结束。这是树/图形还是森林?如果它是一个林,路径可能并不总是被定义的。如果这是一个树/图,请尝试使用广度优先搜索
可以这样想:比如说,你正在执行一项秘密任务,在你的邻居中寻找可爱的小鸡。你从自己的家开始,并将其标记为起点。你接下来会去拜访你最近的邻居,对吗?所以,我们要做的就是——将所有连接到起始端的节点推送到队列中。现在,对该队列中的所有节点重复邻居搜索。继续这样做,直到你得到你的女孩,呃,结束。从开始节点开始进行广度优先遍历,找到结束节点后立即退出。从开始节点开始进行广度优先遍历,找到结束节点后立即退出。这有循环吗?i、 它是DAG吗 如果没有周期:
List<Node> GetShortestPath(Node startNode, Node endNode)
{
//If this is the node you are looking for...
if (startNode.ReferenceEquals(endNode))
{
//return a list with just the end node
List<Nodes> result = new List<Nodes>();
result.Add(endNode);
return result;
}
List<Node> bestPath = null;
foreach(Node child in startNode.Children)
{
//get the shortest path from this child
List<Node> childPath = GetShortestPath(child, endNode);
if (childPath != null &&
( bestPath == null || childPath.Count < bestPath.Count))
{
bestPath = childPath;
}
}
bestPath.Insert(0, startNode);
return bestPath;
}
List GetShortestPath(Node startNode,Node endNode)
{
//如果这是您要查找的节点。。。
if(startNode.ReferenceEquals(endNode))
{
//返回仅包含结束节点的列表
列表结果=新列表();
result.Add(endNode);
返回结果;
}
列表最佳路径=空;
foreach(startNode.Children中的节点子节点)
{
//从这个孩子那里获取最短路径
List childPath=GetShortestPath(子节点,endNode);
if(childPath!=null&&
(bestPath==null | | childPath.Count
[编辑:添加了一个循环示例]
如果可能存在循环:
List<Node> GetShortestPath(Node startNode, Node endNode)
{
List<Node> nodesToExclude = new List<Node>();
return GetShortestPath(startNode, endNOde, nodesToExclude);
}
List<Node> GetShortestPath(Node startNode, Node endNode, List<Node> nodesToExclude)
{
nodesToExclude.Add(startNode);
List<Node> bestPath = null;
//If this is end node...
if (startNode.ReferenceEquals(endNode))
{
//return a list with just the child node
List<Nodes> result = new List<Nodes>();
result.Add(endNode);
return result;
}
foreach(Node child in startNode.Children)
{
if (!nodesToExclude.Contains(child))
{
//get the shortest path from this child
List<Node> childPath = GetShortestPath(child, endNode);
if (childPath != null &&
( bestPath == null || childPath.Count < bestPath.Count))
{
bestPath = childPath;
}
}
}
nodesToExclude.Remove(startNode);
bestPath.Insert(0, child);
return bestPath;
}
List GetShortestPath(Node startNode,Node endNode)
{
List nodesToExclude=新列表();
返回GetShortestPath(startNode、endNOde、nodesToExclude);
}
List GetShortestPath(节点开始节点、节点结束节点、列表节点结束排除)
{
nodesToExclude.Add(startNode);
列表最佳路径=空;
//如果这是结束节点。。。
if(startNode.ReferenceEquals(endNode))
{
//返回仅包含子节点的列表
列表结果=新列表();
result.Add(endNode);
返回结果;
}
foreach(startNode.Children中的节点子节点)
{
如果(!nodesToExclude.Contains(子项))
{
//从这个孩子那里获取最短路径
List childPath=GetShortestPath(子节点,endNode);
if(childPath!=null&&
(bestPath==null | | childPath.Count
这有循环吗?i、 它是DAG吗
如果没有周期:
List<Node> GetShortestPath(Node startNode, Node endNode)
{
//If this is the node you are looking for...
if (startNode.ReferenceEquals(endNode))
{
//return a list with just the end node
List<Nodes> result = new List<Nodes>();
result.Add(endNode);
return result;
}
List<Node> bestPath = null;
foreach(Node child in startNode.Children)
{
//get the shortest path from this child
List<Node> childPath = GetShortestPath(child, endNode);
if (childPath != null &&
( bestPath == null || childPath.Count < bestPath.Count))
{
bestPath = childPath;
}
}
bestPath.Insert(0, startNode);
return bestPath;
}
List GetShortestPath(Node startNode,Node endNode)
{
//如果这是您要查找的节点。。。
if(startNode.ReferenceEquals(endNode))
{
//返回仅包含结束节点的列表
列表结果=新列表();
result.Add(endNode);
返回结果;
}
列表最佳路径=空;
foreach(startNode.Children中的节点子节点)
{
//从这个孩子那里获取最短路径
List childPath=GetShortestPath(子节点,endNode);
if(childPath!=null&&
(bestPath==null | | childPath.Count
[编辑:添加了一个循环示例]
如果可能存在循环:
List<Node> GetShortestPath(Node startNode, Node endNode)
{
List<Node> nodesToExclude = new List<Node>();
return GetShortestPath(startNode, endNOde, nodesToExclude);
}
List<Node> GetShortestPath(Node startNode, Node endNode, List<Node> nodesToExclude)
{
nodesToExclude.Add(startNode);
List<Node> bestPath = null;
//If this is end node...
if (startNode.ReferenceEquals(endNode))
{
//return a list with just the child node
List<Nodes> result = new List<Nodes>();
result.Add(endNode);
return result;
}
foreach(Node child in startNode.Children)
{
if (!nodesToExclude.Contains(child))
{
//get the shortest path from this child
List<Node> childPath = GetShortestPath(child, endNode);
if (childPath != null &&
( bestPath == null || childPath.Count < bestPath.Count))
{
bestPath = childPath;
}
}
}
nodesToExclude.Remove(startNode);
bestPath.Insert(0, child);
return bestPath;
}
List GetShortestPath(Node startNode,Node endNode)
{
List nodesToExclude=新列表();
返回GetShortestPath(startNode、endNOde、nodesToExclude);
}
List GetShortestPath(节点开始节点、节点结束节点、列表节点结束排除)
{
nodesToExclude.Add(startNode);
列表最佳路径=空;
//如果这是结束节点。。。
if(startNode.ReferenceEquals(endNode))
{
//返回仅包含子节点的列表
列表结果=新列表();
result.Add(endNode);
返回结果;
}
foreach(startNode.Children中的节点子节点)
{
如果(!nodesToExclude.Contains(子项))
{
//得到