我有一个随机排列的边,如下所示
edges = [e1 e2 e3...en]我现在通过测试两个边来生成一组节点。
nodes = []
for edge1 in edges:
for edge2 in edges:
if edge1 == edge2 continue
nodes.append[create_node(edge1, edge2)]我需要做的是现在创建一个连接节点的图形,一个边。就像这样:
图例
但是,在这种情况下,我只有一组可以看到的边,并且创建了一组节点,在这些节点中,我知道两个父边中的每一个。正如您所看到的,每个边可以是两个节点的一部分。
我不太清楚该如何处理这个问题
发布于 2016-03-11 05:59:58
假设节点具有以下结构
class Node{
String name;
List<Node> friendNodes = new ArrayList();
}<node name: Node>的映射,让名称为nodeMap。从现在开始保持空着。- Check if nodes n1 and n2 are present in nodeMap. If absent add to the nodeMap;
- If edge is directed, suppose from n1 to n2, then add n2 to friendNodes of node n1;
- If edge is undirected, add n2 to friendNodes of node n1 and add n1 friendNodes of node n2.
https://stackoverflow.com/questions/35931577
复制相似问题