Skip to content

Edges are always loaded from small node index to large node index #6

Closed

Description

@agouge

I initialize a graph

GraphStorage graph = new GraphStorage(new RAMDirectory());
graph.createNew(10);

Now regardless of whether I add the directed edge 3 --> 2

graph.edge(3, 2, 1, false);

or the directed edge 2 --> 3

graph.edge(2, 3, 1, false);

if I print the edges with the following method inserted in the GraphStorage class

public void printEdges() {
        EdgeWriteIterator iter = this.getAllEdges();
        while (iter.next()) {
            System.out.println(
                    "Start node: " + iter.fromNode()
                    + ", End node: " + iter.node()
                    + ", Distance: " + iter.distance()
                    + ", Both directions: " 
                    + CarStreetType.isBoth(iter.flags()));
        }
    }

then the output is always

Start node: 2, End node: 3, Distance: 1.0, Both directions: false

For my application, if I add the edge using

graph.edge(3, 2, 1, false);

I need the output to be

Start node: 3, End node: 2, Distance: 1.0, Both directions: false

Activity

  1. karussell commented on Dec 18, 2012

    @karussell
    Member

    The all-edge-iterator returns edges in no particular order. The nodes of the edge itself are always A,B where A<B but the API does not make guarantees yet. If you need a specific order you could iterate through the nodes and return the edges via:

    for(int n = 0; n < nodes; n++) {
       graph.getOutgoing(n);
       // OR graph.getEdges(n);
    }
  2. agouge commented on Dec 19, 2012

    @agouge
    Author

    The following gives correct results, but I still have a question:

        public void printEdges() {
            TIntHashSet nodeSet = nodeSet();
            TIntIterator nodeIterator = nodeSet.iterator();
            while (nodeIterator.hasNext()) {
                int node = nodeIterator.next();
                EdgeWriteIterator incomingEdge = getIncoming(node);
                getEdges(node);
                System.out.println("Incoming edges of " + node);
                while (incomingEdge.next()) {
                    System.out.println(
                            "Start node: " + incomingEdge.node()
                            + ", End node: " + incomingEdge.fromNode()
                            + ", Distance: " + incomingEdge.distance()
                            + ", Both directions: "
                            + CarStreetType.isBoth(incomingEdge.flags()));
                }
                EdgeWriteIterator outgoingEdge = getOutgoing(node);
                System.out.println("Outgoing edges of " + node);
                while (outgoingEdge.next()) {
                    System.out.println(
                            "Start node: " + outgoingEdge.fromNode()
                            + ", End node: " + outgoingEdge.node()
                            + ", Distance: " + outgoingEdge.distance()
                            + ", Both directions: "
                            + CarStreetType.isBoth(outgoingEdge.flags()));
                }
                System.out.println();
            }
        }

    This makes sense for an outgoing edge, where fromNode() should give the origin node and node() (which I might have called toNode()) gives the destination node.

    But for an incoming edge, I have to switch these two, so the way they work doesn't really correspond to their javadoc definitions. For an incoming edge, the origin node is given by node() and the destination node is given by fromNode().

  3. karussell commented on Dec 19, 2012

    @karussell
    Member

    I see what you mean. It is a bit misleading as I do not mean the direction of an edge, just where we 'picked up' the edge. The EdgeIterator is of course not limited to directed edges ...

  4. added a commit that references this issue on Dec 19, 2012
  5. karussell commented on Dec 19, 2012

    @karussell
    Member

    Are the javadocs making more sense now? And yes, you'll still have to switch 'start' and 'end' nodes. But these words do not make any sense for undirected edges ...

  6. agouge commented on Dec 19, 2012

    @agouge
    Author

    The javadoc are clearer now, thanks. Is there any change of adding sourceNode() : int and destinationNode() : int methods?

  7. karussell commented on Dec 19, 2012

    @karussell
    Member

    What would be the meaning of them if the edge is not directed? You can find out how an edge is directed via CarStreetType.isForward(iter.flags()); or isBackward

  8. agouge commented on Dec 19, 2012

    @agouge
    Author

    Basically I want just looking for a simple way to print out all the edges of a directed graph, making clear which node is the source node and which node is the destination node.

    The last printEdges() method I posted works (even if baseNode() and node() do not seem very intuitive to me) except that every edge is printed twice---once as an outgoing edge and once as an incoming edge.

  9. agouge commented on Dec 19, 2012

    @agouge
    Author

    I just answered my own question: I only need to print all the outgoing edges (or, equivalently, all the incoming edges) of every node in the graph.

    I just have to keep in mind the for an incoming edge, node() means the source node, while for an outgoing edge, node() means the destination node.

  10. karussell commented on Dec 19, 2012

    @karussell
    Member

    even if baseNode() and node() do not seem very intuitive to me

    Let me know if you find more intuitive names. Again: having the undirected case in mind. Or would you prefer to just stick with directed edges/roads even in the case they are undirected (ie. not restricted to one way)? Well, I have to think about this last solution.

  11. added a commit that references this issue on Jun 23, 2020
  12. added a commit that references this issue on Oct 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions