0

我使用Guava Graph 库有这个直截了当的Graph 结构,如果可以对(而不是顺序)进行排序,我想更好地理解。为了澄清:adjacents/edgesnode

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())它可以对节点本身进行排序。尽管如此,我对edges与给定关联的排序node方式更感兴趣,如果我putEdge分别使用 fromAB, C, D, E结果正是这个而不是上面显示的。

有什么见解吗?

提前致谢。

4

1 回答 1

0

如果有人面临同样的问题,这就是我解决它的方法(免责声明:不是最佳解决方案,而是一个可行的解决方案)。

    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创建此数据结构的需要相关。

问候

于 2019-10-15T13:01:46.347 回答