guavagraph-algorithmmutablemap

Guava Graph library ElementOrder on Edges instead of Nodes


I have this straight-forward Graph structure using the Guava Graph library and I'd like to understand better if that is possible to sort the adjacents/edges (not the node order). For the sake of clarification:

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 outcome is:

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]

Without further ado, what I mean is:

Using .nodeOrder(ElementOrder.insertion()) it is possible to sort the nodes themselves. Nonetheless, I'm more interested in sorting the edges associated with a given node in a way that if I used the putEdge respectively from A with B, C, D, E the outcome is precisely this instead of the above shown.

Any insight?

Thanks in advance.


Solution

  • In case someone faces the same question, here is how I solved it (disclaimer: not the optimal, but a working solution).

        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"));
    

    And the results:

    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]
    

    The MutableNetwork has the .edgeOrder(ElementOrder.insertion()) that does the trick. The cons here are associated with the K,V needed to create this data structure.

    Regards