Search code examples
pythonnetworkxgraph-theory

What is an iterable of networkx graphs?


I am trying to compose 30 graphs into one graph, using nx.compose_all().

The input according to documentation must be an 'iterable over networkx graphs' object. I'm unsure what this means - a list of graphs doesn't seem to be something Python can build?

If someone can explain what an iterable over graph objects is and how to get one that would be great.

I've searched through google for 'iterable over networkx graphs' but nothing valid comes back.


Solution

  • An iterable in Python is any object capable of returning its members one at a time. Common examples include lists, tuples, strings, dictionaries, and sets. When the documentation mentions an "iterable over networkx graphs", it simply means a collection of networkx graphs that you can loop over.

    For example, if you have multiple graphs like G1, G2, ..., G30, you could put them into a list and use that as an argument for nx.compose_all()

    import networkx as nx
    
    # Creating multiple graphs
    G1 = nx.Graph()
    G1.add_edge('a', 'b')
    
    G2 = nx.Graph()
    G2.add_edge('b', 'c')
    
    G3 = nx.Graph()
    G3.add_edge('c', 'd')
    
    # Put them into a list
    graphs = [G1, G2, G3]
    
    # Use the list as an argument for nx.compose_all
    G_all = nx.compose_all(graphs)