You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my use of GraphHopper for network analysis, I keep finding it necessary to obtain the set of nodes, often in order to iterate over them. It would be useful to me to have the following method available for any GraphHopper graph.
/** * Returns a {@link TIntHashSet} of the nodes of this graph. * * @return The set of nodes of this graph. */protectedTIntHashSetnodeSet() {
// Initialize the Set.TIntHashSetnodeSet = newTIntHashSet();
// Get all the edges.EdgeIteratoriter = graph.getAllEdges();
// Add each source and destination node to the set.while (iter.next()) {
nodeSet.add(iter.baseNode());
nodeSet.add(iter.node());
}
returnnodeSet;
}
why do you need it :)? I mean, the nodes should be simply 0-maxNode, also when importing possible unused small networks are removed from the node set.
again, as the nodes should be nearly incident you should use a BitSet (more memory efficient) and to avoid implementation deps use the MyBitSet interface with the MyBitSetImpl.
I construct my graph using the edge method. A bi-product of edge creation is node creation, and the nodes are created according to their ids passed to edge. Nothing requires the node ids to be labeled in sequential order, so (as far as I understand) we can't use a simple for loop to cycle through the nodes -- at a certain index i, node i might not exist.
In graphhopper it is - per definition - that node ids are in sequential order. Of course you can create holes via setNode or edge method but I don't understand your use case why you need to do that.
Why is it sequential? Because:
to avoid hash lookups and use direct 'pointer' lookup in GraphStorage itself but in a lot other datastructures for the graph. Indeed this definition is the key to make GraphHopper faster and more mem efficient
to make use of efficient boolean storage like BitSet (again instead of a more expensive map ala Map<Integer, Boolean>)
then I have indirectly created the nodes 2, 3, 5, 7.
Say, for example, that I implement my own version of Dijkstra's algorithm for use in calculating betweenness centrality (where I need to calculate the number and length of all possible shortest paths).
At the end of the calculation I want to print, for each node, the number of shortest paths their length to every other node. This looks something like the following:
If I have the nodeSet() method, then it is easy to go through all the nodes using an iterator and print out the appropriate information. However, without this method, I don't see an easy way to go through all the nodes.
A possible (and simple) solution would be to add a method
intnode(intindex);
to Graph which returns the node id (passed to edge as at the start of this post) at the given index, assuming that the indices go from 0 to graph.nodes() - 1.
Probably still some missunderstanding on your or my side.
In graphhopper you don't have a mapping between an 'index' and the 'node'. They are synonyms.
If you do graph.edge(7, 2, 1, false) you'll indirectly increase the underlying node array to make it accepting at least 8 nodes with indices 0 to 7.
E.g. when importing data from OSM then I'm using a temporary BigLongIntMap object to map long-osm-ids to integer-graphhopper-nodes. And when an unknown osm ids comes through I'm creating a new mapping with an increased counter. Then I do not create a hole in the nodes array or similar ...
Oh, is this misunderstanding coming from my illustration? oh no :( -> I'll improve that
The nodes 4 and 6 just do not have edges but it does not mean that they are invalid. They could e.g. have associated node properties like lat+lon etc. For your method you could then just check if a node has an edge:
for(int i = 0; i < graph.nodes(); i++) {
if(graph.getEdges(i).next())
println(i);
}
What this means is that if we wanted to create a (theoretical) graph consisting of only the edges in your illustration and only nodes that have edges, this is technically not possible. I.e., the nodes 4 and 6 exist even though we have not added them to the graph. I don't think this will pose a problem for me now that I understand the test if(graph.getEdges(i).next().
However, this also means that if we create a graph and execute, for example, the single instruction
graph.edge(0, 999999999, 1, false);
then the graph will contain 1 billion nodes (and one edge), even though we have only indirectly added two nodes (connected by the single edge).
Activity
karussell commentedon Jan 17, 2013
We could add this to GraphUtility.
Two questions:
agouge commentedon Feb 7, 2013
edgemethod. A bi-product of edge creation is node creation, and the nodes are created according to their ids passed toedge. Nothing requires the node ids to be labeled in sequential order, so (as far as I understand) we can't use a simpleforloop to cycle through the nodes -- at a certain indexi, nodeimight not exist.karussell commentedon Feb 7, 2013
I don't understand 1.
In graphhopper it is - per definition - that node ids are in sequential order. Of course you can create holes via setNode or edge method but I don't understand your use case why you need to do that.
Why is it sequential? Because:
agouge commentedon Feb 18, 2013
This is not indispensable; I think we can close the issue.
However, just to be clear, let me explain by an example. If I add edges
then I have indirectly created the nodes 2, 3, 5, 7.
Say, for example, that I implement my own version of Dijkstra's algorithm for use in calculating betweenness centrality (where I need to calculate the number and length of all possible shortest paths).
At the end of the calculation I want to print, for each node, the number of shortest paths their length to every other node. This looks something like the following:
If I have the
nodeSet()method, then it is easy to go through all the nodes using an iterator and print out the appropriate information. However, without this method, I don't see an easy way to go through all the nodes.A possible (and simple) solution would be to add a method
to
Graphwhich returns the node id (passed toedgeas at the start of this post) at the given index, assuming that the indices go from0tograph.nodes() - 1.karussell commentedon Feb 18, 2013
Probably still some missunderstanding on your or my side.
In graphhopper you don't have a mapping between an 'index' and the 'node'. They are synonyms.
If you do graph.edge(7, 2, 1, false) you'll indirectly increase the underlying node array to make it accepting at least 8 nodes with indices 0 to 7.
E.g. when importing data from OSM then I'm using a temporary BigLongIntMap object to map long-osm-ids to integer-graphhopper-nodes. And when an unknown osm ids comes through I'm creating a new mapping with an increased counter. Then I do not create a hole in the nodes array or similar ...
agouge commentedon Feb 19, 2013
I confirm, still some misunderstanding on my side.
I'm looking at your diagram in section "2. The Graph" of your wiki's Developers page.
Could you show me how you would write a method
such that if the graph in your example were passed to this method, the output would be
?
karussell commentedon Feb 19, 2013
Oh, is this misunderstanding coming from my illustration? oh no :( -> I'll improve that
The nodes 4 and 6 just do not have edges but it does not mean that they are invalid. They could e.g. have associated node properties like lat+lon etc. For your method you could then just check if a node has an edge:
agouge commentedon Feb 19, 2013
Ah okay, I think I understand better now.
What this means is that if we wanted to create a (theoretical) graph consisting of only the edges in your illustration and only nodes that have edges, this is technically not possible. I.e., the nodes 4 and 6 exist even though we have not added them to the graph. I don't think this will pose a problem for me now that I understand the test
if(graph.getEdges(i).next().However, this also means that if we create a graph and execute, for example, the single instruction
then the graph will contain 1 billion nodes (and one edge), even though we have only indirectly added two nodes (connected by the single edge).
karussell commentedon Feb 19, 2013
Yes, this is one 'disadvantage' of this design. Which is ok as we get a fast+memory efficient storage if we follow some simple rules.
agouge commentedon Feb 19, 2013
Ok, thanks for your replies and insight!
Merge pull request #10 from graphhopper/translate_instructions
RSPS-629: Move module matrix as a package and include method matrix i…