def tree_cycle_walk_cut(partition, tree):

    tempo = 0
    tedges = set(tree.edges())
    while tempo == 0:
        edge = random.choice(tuple(partition["cut_edges"]))
        #print("picked an edge")
        if (edge[0], edge[1]) not in tedges and (edge[1],
                                                 edge[0]) not in tedges:
            tempo = 1
            tree.add_edge(edge[0], edge[1])
            ncycle = nx.find_cycle(tree, edge[0])
            print("length of tree cycle:", len(ncycle))
            cutedge = random.choice(tuple(ncycle))
            tree.remove_edge(cutedge[0], cutedge[1])
            return tree
def tree_cycle_walk_cut(partition, tree):
''' This function takes one step of 
the cycle walk on spanning trees by 
adding a cut edge to the graph

'''


    tempo=0
    tedges=set(tree.edges())
    while tempo==0:
        edge = random.choice(tuple(partition["cut_edges"]))
        #print("picked an edge")
        if (edge[0],edge[1]) not in tedges and (edge[1],edge[0]) not in tedges:
            tempo=1
            tree.add_edge(edge[0],edge[1])
            ncycle=nx.find_cycle(tree,edge[0])
            print("length of tree cycle:", len(ncycle))
            cutedge=random.choice(tuple(ncycle))
            tree.remove_edge(cutedge[0],cutedge[1])
            return tree
def tree_cycle_walk_all(partition, tree):

''' This function takes one step of 
the cycle walk on spanning trees by adding
an arbitrary edge from the graph

'''

    tempo=0
    tedges=set(tree.edges())
    while tempo==0:

        edge = random.choice(tuple(partition.graph.edges()))
        if (edge[0],edge[1]) not in tedges and (edge[1],edge[0]) not in tedges:
            tempo=1
            tree.add_edge(edge[0],edge[1])
            ncycle=nx.find_cycle(tree,edge[0])
            print("length of tree cycle:", len(ncycle))
            cutedge=random.choice(tuple(ncycle))
            tree.remove_edge(cutedge[0],cutedge[1])
            return tree