Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Find all cycles in an undirected graph

I have found that for directed graphs, we have a built-in command called all_simple_cycles to search for specific length cycles or all cycles. However, it is indeed strange that there is no readily available command for undirected graphs. Although, I am aware that we can convert an undirected graph into a directed graph by assigning two directions to each edge. For example:

g = graphs.CompleteGraph(4).to_directed();
all_cycles=g.all_simple_cycles()
print(all_cycles)

[[0, 1, 0], [0, 2, 0], [0, 3, 0], [1, 2, 1], [1, 3, 1], [2, 3, 2], [0, 1, 2, 0], [0, 1, 3, 0], [0, 2, 1, 0], [0, 2, 3, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 2, 3, 1], [1, 3, 2, 1]]

allcycles = []
for array in all_dcycles:
    if len(array)>=4:
       reversed_array = array[::-1] 
       if reversed_array not in allcycles :
          allcycles.append(array)
print(allcycles)

[[0, 2, 1, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 3, 2, 1], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]

Reversing the list and eliminating 2-cycle cycles seems tedious. Can SageMath discover all cycles in undirected graphs? Mathematica's FindCycle such as FindCycle[CompleteGraph[4], Infinity, All]

Find all cycles in an undirected graph

I have found that for directed graphs, we have a built-in command called all_simple_cycles to search for specific length cycles or all cycles. However, it is indeed strange that there is no readily available command for undirected graphs. Although, I am aware that we can convert an undirected graph into a directed graph by assigning two directions to each edge. For example:

g = graphs.CompleteGraph(4).to_directed();
all_cycles=g.all_simple_cycles()
print(all_cycles)

[[0, 1, 0], [0, 2, 0], [0, 3, 0], [1, 2, 1], [1, 3, 1], [2, 3, 2], [0, 1, 2, 0], [0, 1, 3, 0], [0, 2, 1, 0], [0, 2, 3, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 2, 3, 1], [1, 3, 2, 1]]

allcycles = []
for array in all_dcycles:
    if len(array)>=4:
       reversed_array = array[::-1] 
       if reversed_array not in allcycles :
          allcycles.append(array)
print(allcycles)

[[0, 2, 1, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 3, 2, 1], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]

Reversing the list and eliminating 2-cycle cycles seems tedious. Can SageMath discover all cycles in undirected graphs? Mathematica's It's somewhat similar to Mathematica's FindCycle such as FindCycle[CompleteGraph[4], (FindCycle[CompleteGraph[4], Infinity, All]All]).

Find all cycles in an undirected graph

I have found that for directed graphs, we have a built-in command called all_simple_cycles to search for specific length cycles or all cycles. However, it is indeed strange that there is no readily available command for undirected graphs. Although, I am aware that we can convert an undirected graph into a directed graph by assigning two directions to each edge. For example:

g = graphs.CompleteGraph(4).to_directed();
all_cycles=g.all_simple_cycles()
print(all_cycles)

[[0, 1, 0], [0, 2, 0], [0, 3, 0], [1, 2, 1], [1, 3, 1], [2, 3, 2], [0, 1, 2, 0], [0, 1, 3, 0], [0, 2, 1, 0], [0, 2, 3, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 2, 3, 1], [1, 3, 2, 1]]

allcycles = []
for array in all_dcycles:
    if len(array)>=4:
       reversed_array = array[::-1] 
       if reversed_array not in allcycles :
          allcycles.append(array)
print(allcycles)

[[0, 2, 1, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 3, 2, 1], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]

Reversing the list and eliminating 2-cycle cycles seems tedious. Can SageMath discover all cycles in undirected graphs? It's somewhat similar to Mathematica's FindCycle (FindCycle[CompleteGraph[4], (FindCycle[CompleteGraph[4], Infinity, All]).All]).

Find all cycles in an undirected graph

I have found that for directed graphs, we have a built-in command called all_simple_cycles to search for specific length cycles or all cycles. However, it is indeed strange that there is no readily available command for undirected graphs. Although, I am aware that we can convert an undirected graph into a directed graph by assigning two directions to each edge. For example:

g = graphs.CompleteGraph(4).to_directed();
all_cycles=g.all_simple_cycles()
print(all_cycles)

[[0, 1, 0], [0, 2, 0], [0, 3, 0], [1, 2, 1], [1, 3, 1], [2, 3, 2], [0, 1, 2, 0], [0, 1, 3, 0], [0, 2, 1, 0], [0, 2, 3, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 2, 3, 1], [1, 3, 2, 1]]

allcycles = []
for array in all_dcycles:
    if len(array)>=4:
       reversed_array = array[::-1] 
       if reversed_array not in allcycles :
          allcycles.append(array)
print(allcycles)

[[0, 2, 1, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 3, 2, 1], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]

Reversing the list and eliminating 2-cycle cycles all 2-cycles seems tedious. Can SageMath discover all cycles in undirected graphs? It's somewhat similar to Mathematica's FindCycle (FindCycle[CompleteGraph[4], Infinity, All]).

Find all cycles in an undirected graph

I have found that for directed graphs, we have a built-in command called all_simple_cycles to search for specific length cycles or all cycles. However, it is indeed strange that there is no readily available command for undirected graphs. Although, I am aware that we can convert an undirected graph into a directed graph by assigning two directions to each edge. For example:

g = graphs.CompleteGraph(4).to_directed();
all_cycles=g.all_simple_cycles()
print(all_cycles)

[[0, 1, 0], [0, 2, 0], [0, 3, 0], [1, 2, 1], [1, 3, 1], [2, 3, 2], [0, 1, 2, 0], [0, 1, 3, 0], [0, 2, 1, 0], [0, 2, 3, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 2, 3, 1], [1, 3, 2, 1]]

allcycles = []
for array in all_dcycles:
    if len(array)>=4:
       reversed_array = array[::-1] 
       if reversed_array not in allcycles :
          allcycles.append(array)
print(allcycles)

[[0, 2, 1, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 3, 2, 1], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]

Reversing the list and eliminating all 2-cycles seems tedious. Can SageMath discover all cycles in undirected graphs? It's somewhat similar to Mathematica's FindCycle (FindCycle[CompleteGraph[4], Infinity, All]).

Find all cycles in an undirected graph

I have found that for directed graphs, we have a built-in command called all_simple_cycles to search for specific length cycles or all cycles. However, it is indeed strange that there is no readily available command for undirected graphs. Although, we can convert an undirected graph into a directed graph by assigning two directions to each edge. For example:

g = graphs.CompleteGraph(4).to_directed();
all_cycles=g.all_simple_cycles()
print(all_cycles)

[[0, 1, 0], [0, 2, 0], [0, 3, 0], [1, 2, 1], [1, 3, 1], [2, 3, 2], [0, 1, 2, 0], [0, 1, 3, 0], [0, 2, 1, 0], [0, 2, 3, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 2, 3, 1], [1, 3, 2, 1]]

allcycles = []
for array in all_dcycles:
    if len(array)>=4:
       reversed_array = array[::-1] 
       if reversed_array not in allcycles :
          allcycles.append(array)
print(allcycles)

[[0, 2, 1, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 3, 2, 1], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]

Reversing the list and eliminating all 2-cycles seems tedious. Can Does SageMath discover all cycles in undirected graphs? provide a direct command for undirected graphs to find all cycles or cycles of a specific length? It's somewhat similar to Mathematica's FindCycle (FindCycle[CompleteGraph[4], Infinity, All]).

Find all cycles in an undirected graph

I have found that for directed graphs, we have a built-in command called all_simple_cycles to search for specific length cycles or all cycles. However, it is indeed strange that there is no readily available command for undirected graphs. Although, we can convert an undirected graph into a directed graph by assigning two directions to each edge. For example:

g = graphs.CompleteGraph(4).to_directed();
all_cycles=g.all_simple_cycles()
print(all_cycles)

[[0, 1, 0], [0, 2, 0], [0, 3, 0], [1, 2, 1], [1, 3, 1], [2, 3, 2], [0, 1, 2, 0], [0, 1, 3, 0], [0, 2, 1, 0], [0, 2, 3, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 2, 3, 1], [1, 3, 2, 1]]

allcycles = []
for array in all_dcycles:
    if len(array)>=4:
       reversed_array = array[::-1] 
       if reversed_array not in allcycles :
allcycles:
          allcycles.append(array)
print(allcycles)

[[0, 2, 1, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 3, 2, 1], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]

Reversing the list and eliminating all 2-cycles seems tedious. Does SageMath provide a direct command for undirected graphs to find all cycles or cycles of a specific length? It's somewhat similar to Mathematica's FindCycle (FindCycle[CompleteGraph[4], Infinity, All]).

Find all cycles in an undirected graph

I have found that for directed For directed graphs, we have a built-in command called all_simple_cycles to search for specific length cycles or all cycles. However, it is indeed strange that there is no readily available command for undirected graphs. Although, we can convert an undirected graph into a directed graph by assigning two directions to each edge. For example:

g = graphs.CompleteGraph(4).to_directed();
all_cycles=g.all_simple_cycles()
print(all_cycles)

[[0, 1, 0], [0, 2, 0], [0, 3, 0], [1, 2, 1], [1, 3, 1], [2, 3, 2], [0, 1, 2, 0], [0, 1, 3, 0], [0, 2, 1, 0], [0, 2, 3, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 2, 3, 1], [1, 3, 2, 1]]

allcycles = []
for array in all_dcycles:
    if len(array)>=4:
       reversed_array = array[::-1] 
       if reversed_array not in allcycles:
          allcycles.append(array)
print(allcycles)

[[0, 2, 1, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 3, 2, 1], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]

Reversing the list and eliminating all 2-cycles seems tedious. Does SageMath provide a direct command for undirected graphs to find all cycles or cycles of a specific length? It's somewhat similar to Mathematica's FindCycle (FindCycle[CompleteGraph[4], Infinity, All]).

Find all cycles in an undirected graph

For directed graphs, we have a built-in command called all_simple_cycles to search for specific length cycles or all cycles. However, it is indeed strange that there is no readily available command for undirected graphs. Although, we can convert an undirected graph into a directed graph by assigning two directions to each edge. For example:

g = graphs.CompleteGraph(4).to_directed();
all_cycles=g.all_simple_cycles()
print(all_cycles)

[[0, 1, 0], [0, 2, 0], [0, 3, 0], [1, 2, 1], [1, 3, 1], [2, 3, 2], [0, 1, 2, 0], [0, 1, 3, 0], [0, 2, 1, 0], [0, 2, 3, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 2, 3, 1], [1, 3, 2, 1]]

allcycles = []
for array in all_dcycles:
    if len(array)>=4:
       reversed_array = array[::-1] 
       if reversed_array not in allcycles:
          allcycles.append(array)
print(allcycles)

[[0, 2, 1, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 3, 2, 1], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]

Reversing the list lists and eliminating all 2-cycles seems tedious. Does SageMath provide a direct command for undirected graphs to find all cycles or cycles of a specific length? It's somewhat similar to Mathematica's FindCycle (FindCycle[CompleteGraph[4], Infinity, All]).

Find all cycles in an undirected graph

For directed graphs, we have a built-in command called all_simple_cycles to search for specific length cycles or all cycles. However, it is indeed strange that there is no readily available command for undirected graphs. Although, we can convert an undirected graph into a directed graph by assigning two directions to each edge. For example:

g = graphs.CompleteGraph(4).to_directed();
all_cycles=g.all_simple_cycles()
print(all_cycles)

[[0, 1, 0], [0, 2, 0], [0, 3, 0], [1, 2, 1], [1, 3, 1], [2, 3, 2], [0, 1, 2, 0], [0, 1, 3, 0], [0, 2, 1, 0], [0, 2, 3, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 2, 3, 1], [1, 3, 2, 1]]

allcycles = []
for array in all_dcycles:
    if len(array)>=4:
       reversed_array = array[::-1] 
       if reversed_array not in allcycles:
          allcycles.append(array)
print(allcycles)

[[0, 2, 1, 0], [0, 3, 1, 0], [0, 3, 2, 0], [1, 3, 2, 1], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]

Reversing lists and eliminating all 2-cycles seems tedious. Does SageMath provide a direct command for undirected graphs to find all cycles or cycles of a specific length? It's somewhat similar to Mathematica's FindCycle (FindCycle[CompleteGraph[4], Infinity, All]).