Find edges in a cycle networkx python
Question: I would like to make an algorithm to find if an edge belongs to a cycle, in an
undirected graph, using networkx in Python. I am thinking to use `cycle_basis`
and get all the cycles in the graph. My problem is that `cycle_basis` returns
a list of nodes. How can I convert them to edges?


Answer: You can construct the edges from the cycle by connecting adjacent nodes.

    
    
    In [1]: import networkx as nx
    
    In [2]: G = nx.Graph()
    
    In [3]: G.add_cycle([1,2,3,4])
    
    In [4]: G.add_cycle([10,20,30])
    
    In [5]: basis = nx.cycle_basis(G)
    
    In [6]: basis
    Out[6]: [[2, 3, 4, 1], [20, 30, 10]]
    
    In [7]: edges = [zip(nodes,(nodes[1:]+nodes[:1])) for nodes in basis]
    
    In [8]: edges
    Out[8]: [[(2, 3), (3, 4), (4, 1), (1, 2)], [(20, 30), (30, 10), (10, 20)]]
    

