Creating nodes automatically with add_edge if they do not exist
What is the expected enhancement?
From what I understand, it's currently only possible to create edges between two nodes if the nodes already exist in the graph. Would it be possible to provide a function that adds the nodes of the edge to the graph if they do not exist and then create the edge afterwards?
Off topic but somehow related: Why is it not possible to define the index of a node when creating it? Is it possible to create a graph with non-continuous node indices? I have found #252, #496 and #503 that are relevant to my questions. However, since I have not found the answers I am looking for, I wanted to add them here. I hope this is fine. If not, I could also create another issue for my questions if this is preferred.
Can you check if extend_from_edge_list and extend_from_weighted_edge_list cover your use case? If you pass a list with only one entry, you can create nodes and add an edge between them at the same time
Off topic but somehow related: Why is it not possible to define the index of a node when creating it? Is it possible to create a graph with non-continuous node indices? I have found #252, #496 and #503 that are relevant to my questions. However, since I have not found the answers I am looking for, I wanted to add them here. I hope this is fine. If not, I could also create another issue for my questions if this is preferred.
Answering this: rustworkx manages the node indices for its users. And in a perfect world we try the most to make them contiguous! If you insert n nodes in a graph, you will notice the indices will be [0, 1, 2, ... n - 1].
However, the world is not perfect because users delete nodes. That is how the indices become non-contiguous. For example, if you deleted all odd-indexed nodes, the indices would be [0, 2, 4, 6...] and it would be a perfectly valid graph.
Thank you for explaining @IvanIsCoding!
This is where the questions come together with my original issue. I want to create a graph by adding edges from source to target nodes with a certain weight. So the method extend_from_weighted_edge_list does what I want (I'm sorry that I didn't find it by myself)!
However, since the graph I want to create is quite large, I wanted to save memory by adding only the nodes that have an edge in my graph (degree > 0). So if I add edges this way, with nodes that have a higher index that are not yet in the graph, all nodes up to the highest index in the graph will also be created. Here is an example:
import rustworkx as rx
graph = rx.PyGraph()
graph.extend_from_weighted_edge_list([(3, 4, 0.2), (4, 7, 0.3)])
print(graph.edge_indices())
print(graph.node_indices())
Will print:
EdgeIndices[0, 1]
NodeIndices[0, 1, 2, 3, 4, 5, 6, 7]
To get what I want, I have to remove all nodes without an edge (degree == 0):
graph.remove_nodes_from(rx.isolates(graph))
print(graph.node_indices())
Gives me:
NodeIndices[3, 4, 7]
If I understand you correctly, there is no other way to do this, right?
Thank you for explaining @IvanIsCoding!
This is where the questions come together with my original issue. I want to create a graph by adding edges from source to target nodes with a certain weight. So the method
extend_from_weighted_edge_listdoes what I want (I'm sorry that I didn't find it by myself)! However, since the graph I want to create is quite large, I wanted to save memory by adding only the nodes that have an edge in my graph (degree > 0). So if I add edges this way, with nodes that have a higher index that are not yet in the graph, all nodes up to the highest index in the graph will also be created. Here is an example:import rustworkx as rx graph = rx.PyGraph() graph.extend_from_weighted_edge_list([(3, 4, 0.2), (4, 7, 0.3)]) print(graph.edge_indices()) print(graph.node_indices())Will print:
EdgeIndices[0, 1] NodeIndices[0, 1, 2, 3, 4, 5, 6, 7]To get what I want, I have to remove all nodes without an edge (degree == 0):
graph.remove_nodes_from(rx.isolates(graph)) print(graph.node_indices())Gives me:
NodeIndices[3, 4, 7]If I understand you correctly, there is no other way to do this, right?
That is a clever way of doing it. The isolates function works well for that purpose.
Our general advice for sparse graphs would be to keep track of node indices that you care about in a dictionary. That way you can recover it quickly. But I think for your specific case your solution is better.