I'm working on a task where I need to apply custom modifications to many independent copies of the same base graph. For each instance, I want to:
- Start from the same base graph
- Remove one or more edges
- Insert intermediate nodes
- Add new edges with custom weights
I already have all the necessary modification data (bulk of edges to remove, nodes to add, and edges to insert) prepared up front as NumPy arrays or lists — one set per graph copy.
What I’m doing now (with networkx):
import networkx as nx
# Base graph
G = nx.Graph()
G.add_edge(0, 1, DISTANCE=10)
G.add_edge(1, 2, DISTANCE=10)
# Example modification for one instance
G_copy = G.copy()
G_copy.remove_edge(0, 1)
G_copy.add_node(100)
G_copy.add_edge(0, 100, DISTANCE=4)
G_copy.add_edge(100, 1, DISTANCE=6)
I need to apply different sets of modifications (edge removals, node insertions, edge additions with weights) to thousands of independent copies of a base graph. All modification data is precomputed in bulk. The bottleneck is the sequential processing: each copy is handled one at a time in Python using loops. Even with all data available upfront, networkx
lacks support for efficient batched or parallel updates.
I'm looking for a graph library that supports fast, batched modifications across many graph instances, ideally with:
- NumPy-like vectorized graph operations
- Efficient edge/node insertions and deletions
To be concise:
I’m looking for a way to apply the following batched operations on multiple independent graph copies, using precomputed data:
G.remove_edges(first_nodes, last_nodes)
G.add_nodes(middle_nodes)
G.add_edges(first_nodes, middle_nodes, DISTANCE=first_middle_distance)
G.add_edges(middle_nodes, last_nodes, DISTANCE=middle_last_distance)
Here, first_nodes, last_nodes, middle_nodes, first_middle_distance
, and middle_last_distance
are all precomputed. The goal is to perform these modifications efficiently across many G copies in batch — ideally returning one modified graph per index in the input arrays.
I'm working on a task where I need to apply custom modifications to many independent copies of the same base graph. For each instance, I want to:
- Start from the same base graph
- Remove one or more edges
- Insert intermediate nodes
- Add new edges with custom weights
I already have all the necessary modification data (bulk of edges to remove, nodes to add, and edges to insert) prepared up front as NumPy arrays or lists — one set per graph copy.
What I’m doing now (with networkx):
import networkx as nx
# Base graph
G = nx.Graph()
G.add_edge(0, 1, DISTANCE=10)
G.add_edge(1, 2, DISTANCE=10)
# Example modification for one instance
G_copy = G.copy()
G_copy.remove_edge(0, 1)
G_copy.add_node(100)
G_copy.add_edge(0, 100, DISTANCE=4)
G_copy.add_edge(100, 1, DISTANCE=6)
I need to apply different sets of modifications (edge removals, node insertions, edge additions with weights) to thousands of independent copies of a base graph. All modification data is precomputed in bulk. The bottleneck is the sequential processing: each copy is handled one at a time in Python using loops. Even with all data available upfront, networkx
lacks support for efficient batched or parallel updates.
I'm looking for a graph library that supports fast, batched modifications across many graph instances, ideally with:
- NumPy-like vectorized graph operations
- Efficient edge/node insertions and deletions
To be concise:
I’m looking for a way to apply the following batched operations on multiple independent graph copies, using precomputed data:
G.remove_edges(first_nodes, last_nodes)
G.add_nodes(middle_nodes)
G.add_edges(first_nodes, middle_nodes, DISTANCE=first_middle_distance)
G.add_edges(middle_nodes, last_nodes, DISTANCE=middle_last_distance)
Here, first_nodes, last_nodes, middle_nodes, first_middle_distance
, and middle_last_distance
are all precomputed. The goal is to perform these modifications efficiently across many G copies in batch — ideally returning one modified graph per index in the input arrays.
1 Answer
Reset to default 0You can "wrap" the modifications you do to each graph in a list. Think each modification as a sequence of pairs (function, argument), that are applied sequentially.
For instance, you can create a function to add a node, and another to modify a parameter in an edge, and so forth. A modification can be 3 functions adding nodes, another changing distance. Another modification can be simply one node addition, and so forth.
Then you create a function that takes a graph as argument and a tuple (function, argument): it copies the graph, applies the function to a graph with an argument and returns the graph
Then you create a "list" of modifications, each onean independent sequence of funcion applications to your base graph, and parallel execute everything using map
.
Note: there are probably more elegant ways to express the code, but hopefully you get the idea
import networkx as nx
from concurrent.futures import ProcessPoolExecutor
# Base graph
G = nx.Graph()
G.add_edge(0, 1, DISTANCE=10)
G.add_edge(1, 2, DISTANCE=10)
def add_node(G, arg):
"""Adds a node and returns the modified graph"""
new_node = arg
G.add_node(new_node)
return G
def change_edge_parameter(G, arg):
"""Modifies an edge parameter and returns the modified graph"""
edge, parameter, value = arg
if edge in G.edges:
G.edges[edge][parameter] = value
return G
def apply_modifications(G, mods):
"""Applies a sequence of modifications to a copy of G"""
# each "mods" is a list of pairs (func, arg)
G = G.copy() # Ensure each graph is independent
for func, args in mods:
G = func(G, args) # Each function now returns the modified graph
return G
# Modifications list: 4 distance changes, 5 node addition, 1 distance change AND node addition
mods = [
*[[(change_edge_parameter, ((0,1), 'DISTANCE', i))] for i in (50, 100, 200, 300)],
*[[(add_node, i)] for i in (100, 200, 500, 300)],
[(add_node, 50), (change_edge_parameter, ((1,2), 'DISTANCE', 50))]
]
def modify_graph(mod):
"""Applies modifications to a new graph instance"""
return apply_modifications(G, mod)
# Run modifications in parallel
with ProcessPoolExecutor() as executor:
modified_graphs = list(executor.map(modify_graph, mods))
# Output last modified graph
print(modified_graphs[-1].nodes, modified_graphs[-1].edges(data=True) )
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744372039a4571002.html
评论列表(0条)