I have some paths of nodes like[[0, 1, 2, 4], [3,4]]
, in NetworkX I can add nodes in these paths like:
networkx.add_path(graph, path)
However I don’t find corresponding method in igraph,so how to solve this problem?
Thank you for a lot.
I have some paths of nodes like[[0, 1, 2, 4], [3,4]]
, in NetworkX I can add nodes in these paths like:
networkx.add_path(graph, path)
However I don’t find corresponding method in igraph,so how to solve this problem?
Thank you for a lot.
Try this:
from igraph import Graph, plot
# Generate the Petersen graph and assign names to vertices
G = Graph.Famous("Petersen")
G.vs["name"] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
# Add a path [a, 1, 2, 3] to the graph, avoiding duplicate vertices and edges
def add_path(G, path):
path = list(map(str, path))
for i in range(len(path) - 1):
if not G.vs.select(name=path[i]):
G.add_vertex(name=path[i])
if not G.vs.select(name=path[i+1]):
G.add_vertex(name=path[i+1])
if not G.are_connected(path[i], path[i+1]):
G.add_edge(path[i], path[i+1])
add_path(G, ["a", "1", "2", "3","f"])