- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个节点/顶点列表,以及连接这些节点的线/边列表。这些列表没有以任何方式排序或排序,但包含特定数据集的所有边和节点。
边是由笛卡尔坐标定义的线段,(x1,y1)和(x2,y2),每个节点位置也用(x,y)形式的坐标表示。附上图片depicts a typical test case ,清楚地显示了两棵树,根为 R1 和 R2,每个节点,包括叶节点(标记为 Lx,突出显示的橙色文本和蓝色圆圈)都显示有相应的坐标。
class Node
{
Point coordinates; // x,y coordinates of node <int,int>
Node parentNode; // parent node of current node. ( may not be necessary as parentID may suffice to keep reference to parent node)
List<Node> children; // List of all child nodes of this node
List<Edge> edges; // list of all edges connected to this node
string Data; // relevant data of each node
long nodeID; // current nodes ID
long parentID; // ID of current node's parent node
}
每条边表示为:
class Edge
{
Point p1; // first end coordinates of line segment
Point p2; // coordinates of the other end of the segment
}
从附图中可以清楚地看出,边 N1-N2 将表示为 p1= (0,0), p2=(20,20) 或 p1 =(20,20), p2 = (0, 0).顺序是随机的。
假设 1:节点 R1 和 R2 可以清楚地识别为根节点,因为它们上面的节点类型。 (带红色外圈的同心圆)。假设 2:直接连接到节点的所有边的列表也是可用的,例如,节点 N8 将具有段:N8-L7,N8-R2,N8-N9,N8-N7。
我的问题是如何在 C# 中编写一个函数,它有两个输入,一个边列表和一个节点列表,并返回一个根节点,或者引用子节点的树的根节点,这也将与附图中描述的内容相同/真实。
List<Node> getRootNodes(List<Node> nodes, List<Edge> edges)
{
// implementation here
List<Node> roots = new List<Node>();
//more logic
//
//
return roots; //returned list may have more than one root node!
}
我已经能够列出每个节点的边缘,但无法找到构建树的方法。我读过 Kruskal's Algorithm , 但我不确定我是否可以适应这个问题。我不确定它是否会保留图中显示的顺序。
所有代码都在 C# 中,但任何 C 风格的语言解决方案都可以。
注意:我在本网站上看到的答案假设树节点的父节点和子节点的顺序是已知的。我可以看出两个节点由边连接,但不能确定哪个节点是父节点,哪个是子节点。
谢谢,
格雷格·M
最佳答案
你说有两个假设:
N8-L7, N8-R2, N8-N9, N8-N7
.我假设还有段 L7-N8, R2-N8, N9-N8, N7-N8
.如果没有,您可以根据您提到的现有分割轻松地构建它们。
您在回答我的问题时还说,根没有父节点,每个节点只有一个父节点。这使这变得容易得多。
首先,创建一个以节点名称为键的字典,值为它所连接的节点列表。这将是 Dictionary<string, List<string>>
.在上面的示例中,您将拥有:
key value
N8 L7, R2, N9, N7
L7 N8
R2 N8
N9 N8
N7 N8
在上面的列表中,只有 N8 是完全填充的。您的字典将包含所有节点的所有连接。
构建这个:
var segmentDict = new Dictionary<string, List<string>>();
foreach (var segment in SegmentList)
{
List<string> nodeConnections;
if (!segmentDict.TryGetValue(segment.From, out nodeConnections))
{
// This node is not in dictionary. Create it.
nodeConnections = new List<string>();
segmentDict.Add(segment.From, nodeConnections);
}
nodeConnections.Add(segment.To);
}
现在,我们将构建一个新的 Dictionary<string, List<string>>
最初是空的。这将是最后一棵树,每个节点只有子节点。
因为您知道根节点没有父节点,并且一个节点只有一个父节点,所以您可以从根节点开始建立连接。扫描字典并将每个根节点添加到队列中,并在 finalTree
中创建一个条目有一个空的子列表:
var finalTree = new Dictionary<string, List<string>>();
var nodeQueue = new Queue<string>();
foreach (var nodeName in segmentDict.Keys)
{
if (nodeName.StartsWith("R")) // if it's a root node
{
finalTree.Add(nodeName, new List<string>()); // add tree node
nodeQueue.Enqueue(nodeName); // and add node to queue
}
}
现在,开始处理队列。对于您从队列中提取的每个节点名称:
finalTree
中创建一个条目对于那个节点。segmentDict
获取该节点的连接列表.finalTree
中没有该节点的条目,将节点添加到队列中,并将其添加到 finalTree
中此节点的连接列表中.重复直到队列为空。
代码看起来像这样:
while (nodeQueue.Count > 0)
{
var fromNode = nodeQueue.Dequeue();
var nodeChildren = finalTree[fromNode];
foreach (var toNode in segmentDict[fromNode])
{
if (finalTree.ContainsKey(toNode))
{
// This node has already been seen as a child node.
// So this connection is from child to parent. Ignore it.
break;
}
nodeChildren.Add(toNode); // update children for this node
finalTree.Add(toNode, new List<string>()); // create tree entry for child node
nodeQueue.Enqueue(toNode); // add child to queue
}
}
我在这里所做的是从根到叶跟随树,所以我第一次遇到节点时,我知道它是父子链接而不是子父链接。所以所有的父子链接都被删除了。
然后,你可以通过 finalTree
得到你的树并对每个根节点进行深度优先遍历:
foreach (var kvp in finalTree)
{
if (kvp.Key.StartsWith("R"))
{
PrintTree(kvp.Key, kvp.Value);
}
}
void PrintTree(string nodeName, List<string> children)
{
Console.WriteLine("node {1} has children {2}.", nodeName, string.Join(",", children);
foreach (var child in children)
{
PrintTree(child, finalTree[child]);
}
}
当然,你会想要美化输出,但这展示了如何从根部遍历树。
关于algorithm - 从坐标列表和连接它们的边列表创建一个 k-ary 树。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54057573/
如标题所示,ans_list是一个答案列表,ans_index是一个数字(答案在词汇表中的索引,但与atm无关) 这里生成的 tree.anslist 是什么? (例如,仅针对第一个),忽略迭代。 f
我目前将用户的输入存储在逗号分隔的列表中,如下所示: Userid | Options 1 | 1,2,5 用户在一个数组形式中勾选一组选项,然后用逗号连接起来 1,2,5 然后 MySQ
我目前将用户的输入存储在逗号分隔的列表中,如下所示: Userid | Options 1 | 1,2,5 用户在一个数组形式中勾选一组选项,然后用逗号连接起来 1,2,5 然后 MySQ
我想知道如何完全展平列表和包含它们的东西。除其他外,我想出了一个解决方案,它可以将具有多个元素的东西滑倒并将它们放回原处,或者在滑倒后将具有一个元素的东西拿走。 这与 How do I “flatte
我想知道如何完全展平列表和包含它们的东西。除其他外,我想出了一个解决方案,它可以将具有多个元素的东西滑倒并将它们放回原处,或者在滑倒后将带有一个元素的东西拿走。 这与 How do I “flatte
这个问题已经有答案了: Convert nested list to 2d array (3 个回答) 已关闭 7 年前。 java中有没有快捷方式可以转换 List> 到 String[][] ?
我在排序时遇到问题 List> 。我创建了一个自定义比较器,在其中编写了对数据进行排序的代码。 public class CustomComparator implements Comparator
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Java Generics: Cannot cast List to List? 我只是想知道为什么下面的java代
试图想出一个 LINQy 方法来做到这一点,但我什么也没想到。 我有一个对象列表<>,其中包含一个属性,该属性是逗号分隔的字母代码列表: lst[0].codes = "AA,BB,DD" lst[1
假设我有这些任务: points = [] point = (1, 2) 我怎么会这样做: points += point 它工作得很好,并且给了我点 = [1, 2]。但是,如果我这样做: poin
如何在 scala 中将 List[Task[List[Header]]] 类型转换为 Task[List[Header]]。 我有一个方法返回 Task[List[Header]] 并多次调用 do
如何在 Java 中查找二维列表的元素? 我有一个参数为 List> 的函数我想知道如何找到这个列表的行和列。 最佳答案 如果你喜欢 List> obj 然后你就可以像这样访问 obj.get(cur
分配 List到 List工作正常。 分配 List>到 List>不编译。 代码 public class Main { public static void main(String[] a
我正在用 Java 编写一个方法,该方法必须接收并迭代 Serializable 的 List。 有什么区别: public void myMethod(List list) { } 和 public
我看到很多人想用 mvvm 更新网格/列表/树的一部分,但他们不想刷新整个列表。 对于所有遇到此问题的人,我做了以下示例。 希望这对你有用。 最佳答案 这是一个简单的例子。整个代码中最重要的是: Bi
我正在为现有的 C++ 库编写包装器,该库使用列表,其中 T 是自定义结构。我被建议使用 vector 而不是列表,但我试图避免修改库。 为了更好地理解这个场景,我做了一个简单的应用程序,使用一个列表
List list List list 这两种声明有什么区别吗? 谢谢, 最佳答案 是的。 List可以包含所有派生自 Base 的不同事物的混合物. List包含同质项(从某种意义上说,它们必须全部
有人可以尽可能详细地解释以下类型之间的区别吗? List List List 让我更具体一点。我什么时候想使用 // 1 public void CanYouGiveMeAnAnswer(List l
我有一个元组列表,每个元组都是一对列表。所以我的数据看起来像: mylist = [(['foo', 'bar'], ['bar', 'bar']),(['bar', 'bar'],['bar', '
也许是一个时髦的标题,但我遇到了以下问题: 给定一个类型为 (a * b) list 的列表,我想创建一个类型为 (a * b list) list 的新列表。一个例子: 给定列表 let testL
我是一名优秀的程序员,十分优秀!