Я пытаюсь имитировать потоковую передачу ребер, читая построчно из файла и добавляя эти вершины и ребра в граф. Я обнаружил, что это довольно медленно, особенно добавление краев (около 30% времени выполнения). Я знаю, что add_edges () быстрее, чем add_edge (), но для меня это не имеет значения, поскольку я все равно добавляю по одному ребру за раз. Есть ли способ ускорить это? Я новичок в iGraph, поэтому приветствуются любые указатели!
Вот что у меня есть на данный момент:
def stream_graph(g_path, type_of_run, algorithm):
with open(g_path, 'r') as file:
graph = ig.Graph()
while True:
line = file.readline()
if not line:
break
line_tuple = line.split()
v0_str = str(line_tuple[0])
v1_str = str(line_tuple[1])
node1 = None
node2 = None
"""
adding nodes to graph
"""
if len(graph.vs) == 0:
# print("---adding both nodes")
node1 = graph.add_vertex(v0_str)
node2 = graph.add_vertex(v1_str)
else:
vertices_by_name = graph.vs["name"]
if v0_str not in vertices_by_name and v1_str not in vertices_by_name:
node1 = graph.add_vertex(v0_str)
node2 = graph.add_vertex(v1_str)
elif v0_str not in vertices_by_name and v1_str in vertices_by_name:
node1 = graph.add_vertex(v0_str)
node2 = graph.vs.find(name=v1_str)
# print("---adding node 1: " + str(node1.index))
elif v0_str in vertices_by_name and v1_str not in vertices_by_name:
node1 = graph.vs.find(name=v0_str)
node2 = graph.add_vertex(v1_str)
# print("---adding node 2: " + str(node2.index))
else:
# print("---adding no nodes")
node1 = graph.vs.find(name=v0_str)
node2 = graph.vs.find(name=v1_str)
"""
Adding edges between nodes
"""
if graph.get_eid(node1, node2, error=False) == -1:
graph.add_edge(node1, node2)
"""
Find subgraph of current nodes and their neighbours
"""
neighbours_v1 = graph.neighbors(node1.index)
neighbours_v2 = graph.neighbors(node2.index)
neighbours = set(neighbours_v1 + neighbours_v2)
pruned_nodes = graph.vs.select([v for v in neighbours])
pruned_graph = graph.subgraph(pruned_nodes, implementation="create_from_scratch")
"""
Do something with the graph
"""
process_graph(graph, pruned_graph, algorithm)