Guava Graph library ElementOrder on Edges 而不是 Nodes

Guava Graph library ElementOrder on Edges instead of Nodes

我有这个直接的 图结构 使用 Guava Graph library 我想更好地理解是否可以对 adjacents/edges 进行排序(不是 node 命令)。为了澄清:

import com.google.common.graph.ElementOrder;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;

public class MyNodeTest {

    public static void main(String[] args) {
        MutableGraph<String> graph = GraphBuilder.undirected().nodeOrder(ElementOrder.insertion()).build();

        graph.addNode("A");
        graph.addNode("C");
        graph.addNode("D");
        graph.addNode("B");
        graph.addNode("E");

        graph.putEdge("A", "B");
        graph.putEdge("A", "C");
        graph.putEdge("A", "D");
        graph.putEdge("A", "E");

        System.out.println("My default Insertion.order Nodes: " + graph.nodes());
        System.out.println("Adj. Order that I couldn't understand: " + graph.adjacentNodes("A"));
        System.out.println("Successor. Order that I couldn't understand: " + graph.successors("A"));
        System.out.println("Pred. Order that I couldn't understand: " + graph.predecessors("A"));
    }
}

我的结果是:

My default Insertion.order Nodes: [A, C, D, B, E]
Adj. Order that I couldn't understand: [D, E, B, C]
Successor. Order that I couldn't understand: [D, E, B, C]
Pred. Order that I couldn't understand: [D, E, B, C]

废话不多说,我的意思是:

使用.nodeOrder(ElementOrder.insertion()) 可以对节点本身进行排序。尽管如此,我更感兴趣的是对与给定 node 关联的 edges 进行排序,如果我分别使用 AB, C, D, E 中的 putEdge结果恰恰是这样而不是上面显示的。

有什么见解吗?

提前致谢。

万一有人遇到同样的问题,以下是我的解决方法(免责声明:不是最佳解决方案,而是可行的解决方案)。

    MutableNetwork<String, UUID> graph = NetworkBuilder.undirected().edgeOrder(ElementOrder.insertion()).build();

    graph.addNode("A");
    graph.addNode("C");
    graph.addNode("D");
    graph.addNode("B");
    graph.addNode("E");

    graph.addEdge("A", "B", UUID.randomUUID());
    graph.addEdge("A", "C", UUID.randomUUID());
    graph.addEdge("A", "D", UUID.randomUUID());
    graph.addEdge("A", "E", UUID.randomUUID());

    System.out.println("My default Insertion.order Nodes: " + graph.nodes());
    System.out.println("Adj. Order that I couldn't understand: " + graph.adjacentNodes("A"));
    System.out.println("Successor. Order that I couldn't understand: " + graph.successors("A"));
    System.out.println("Pred. Order that I couldn't understand: " + graph.predecessors("A"));

结果:

My default Insertion.order Nodes: [A, C, D, B, E]
Adj. Order that I couldn't understand: [B, C, D, E]
Successor. Order that I couldn't understand: [B, C, D, E]
Pred. Order that I couldn't understand: [B, C, D, E]

MutableNetwork.edgeOrder(ElementOrder.insertion()) 的技巧。这里的缺点与创建此数据结构所需的 K,V 有关。

此致