import random # function to generate a ring-preserving network def generate_network(C, L): # assign IDs to nodes node_ids = list(range(1, C+1)) random.shuffle(node_ids) # create links between nodes links = [] for i in range(1, C+1): # link to the next node in a clockwise direction j = (i % C) + 1 links.append((node_ids[i-1], node_ids[j-1])) # link to the previous node in a counter-clockwise direction k = i - 1 if i > 1 else C links.append((node_ids[i-1], node_ids[k-1])) # link the first and last nodes to complete the ring links.append((node_ids[0], node_ids[C-1])) # randomly permute node IDs random.shuffle(node_ids) # sort links by new IDs and endpoints links = sorted([(min(node_ids.index(u)+1, node_ids.index(v)+1), max(node_ids.index(u)+1, node_ids.index(v)+1)) for (u,v) in links]) # find a ring in the permuted network ring = [] visited = set() start_node = node_ids[0] current_node = start_node while current_node not in visited: ring.append(current_node) visited.add(current_node) # follow the clockwise direction to the next node next_node = None for (u, v) in links: if u == current_node and v not in visited: next_node = v break # follow the counter-clockwise direction to the next node if next_node is None: for (u, v) in links: if v == current_node and u not in visited: next_node = u break current_node = next_node # output the IDs of nodes in the ring return ring