def dfs(graph, start, visited): visited.add(start) cycle = [start] for neighbor in graph[start]: if neighbor not in visited: cycle += dfs(graph, neighbor, visited) break return cycle def create_ring_preserving_network(C, L): # create a cycle with a chord graph = {i: [] for i in range(1, C + 1)} for i in range(1, C): graph[i].append(i + 1) graph[i + 1].append(i) graph[C].append(1) graph[1].append(C) # add additional edges to make the graph have L edges i = C j = 1 while len(graph) < L: graph[i].append(j) graph[j].append(i) j += 1 if j > C: i += 1 j = i + 1 # find a Hamiltonian cycle visited = set() cycle = dfs(graph, 1, visited) # find the ring in the permuted graph permuted_graph = {} print(L) for i in range(1, C + 1): for j in range(i + 1, C + 1): if len(permuted_graph) == L: break if j in graph[i]: permuted_graph[i] = j permuted_graph[j] = i print(i, j) visited = set() start = cycle[0] ring = [start] while permuted_graph[start] not in visited: start = permuted_graph[start] visited.add(start) ring.append(start) print(' '.join(str(x) for x in ring))