How to add items to a list using recursion?

The problem arose as a continuation of my experiments with graph theory, since I will also add an image of my graph: enter image description here

But I also have a description of this graph in several arrays: First of all, an array with all the connections:

arrAllConnections = [['F', 'G'], ['G', 'F'], ['G', 'N'], ['N', 'G'], ['N', 'E'], ['E', 'N'], ['E', 'D'], ['D', 'E'], ['D', 'C'], ['C', 'D'], ['C', 'B'], ['B', 'C'], ['B', 'A'], ['A', 'B'], ['A', 'E'], ['E', 'A'], ['B', 'X8'], ['X8', 'B'], ['X8', 'X3'], ['X3', 'X8'], ['X3', 'X2'], ['X2', 'X3'], ['C', 'T'], ['T', 'C'], ['T', 'T1'], ['T1', 'T'], ['T1', 'Y'], ['Y', 'T1'], ['Y', 'L'], ['L', 'Y'],['L', 'P'], ['P', 'L'], ['P', 'Z'], ['Z', 'P'], ['Z', 'Y'], ['Y', 'Z'], ['L', 'K3'], ['K3', 'L'], ['Z', 'K1'], ['K1', 'Z'], ['K1', 'K2'], ['K2', 'K1']] 

I thought it contained additional information and added a line like this:

 arrAllConnections = [list(i) for i in set(map(tuple, arrAllConnections))] 

There are also some nodes that I want to connect to other node groups. node groups are loops, and here is a list of loops:

 arrWithWhatToConnect = [['A', 'B', 'C', 'D', 'E'], ['Z', 'P', 'L', 'Y']] 

The nodes that should be connected are mainly nodes outside the loops, but they can be "inside" (for example: the "E" node):

 arrWhatToConnect = ['F', 'G', 'N', 'X2', 'X3', 'K3', 'K1', 'K2', 'E'] 

If you look at the image, you can notice several things:

  • X2 connected to X3 , which is connected to X8 , but since the latter is not in the arrWhatToConnect list, the process must be stopped;
  • E node is already in the loop, so the process will be stopped as soon as the E is found in the loop;
  • For other nodes, for example. F needs recursion, in short it should be like this: if F not in cycle/arrWithWhatToConnect, but in arrWhatToConnect -> check next node and repeat check for cycle/arrWithWhatToConnect or/|| arrWhatToConnect if F not in cycle/arrWithWhatToConnect, but in arrWhatToConnect -> check next node and repeat check for cycle/arrWithWhatToConnect or/|| arrWhatToConnect .

Finally, here is what I am trying to get:

 for 'E' node -> ['A', 'B', 'C', 'D', 'E'] for 'F' node -> ['F', 'G', 'N', 'A', 'B', 'C', 'D', 'E'] etc 

These are lists containing the shortest paths between selected nodes and loops.

My code is:

 arrWhatToConnect = ['F', 'G', 'N', 'X2', 'X3', 'K3', 'K1', 'K2', 'E'] #THERE IS NO X8!!!!! arrWithWhatToConnect = [['A', 'B', 'C', 'D', 'E'], ['Z', 'P', 'L', 'Y']] arrAllConnections = [['F', 'G'], ['G', 'F'], ['G', 'N'], ['N', 'G'], ['N', 'E'], ['E', 'N'], ['E', 'D'], ['D', 'E'], ['D', 'C'], ['C', 'D'], ['C', 'B'], ['B', 'C'], ['B', 'A'], ['A', 'B'], ['A', 'E'], ['E', 'A'], ['B', 'X8'], ['X8', 'B'], ['X8', 'X3'], ['X3', 'X8'], ['X3', 'X2'], ['X2', 'X3'], ['C', 'T'], ['T', 'C'], ['T', 'T1'], ['T1', 'T'], ['T1', 'Y'], ['Y', 'T1'], ['Y', 'L'], ['L', 'Y'],['L', 'P'], ['P', 'L'], ['P', 'Z'], ['Z', 'P'], ['Z', 'Y'], ['Y', 'Z'], ['L', 'K3'], ['K3', 'L'], ['Z', 'K1'], ['K1', 'Z'], ['K1', 'K2'], ['K2', 'K1']] nullFirstConnect = [] #arrAllConnections = (sorted(item for item in arrAllConnections)) arrAllConnections = [list(i) for i in set(map(tuple, arrAllConnections))] print(arrAllConnections, 'REMOVED?') def connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConnections, item, olderItems): arrAllConDel = arrAllConnections print('CONTENTS OF TRUNCATED ARR CONNECTIONS', arrAllConDel) arrConnected = [] for cycle in arrWithWhatToConnect: if item in cycle: arrConnected.extend(cycle) arrConnected.extend(olderItems) print('My item in cycle', item, cycle, arrConnected) else: print('Not in cycle', item) print('GOING TO CHECK ELSEWHERE!') olderItems.extend(item) olderItems.extend(olderItems) for connection in arrAllConnections: item1 = connection[0] item2 = connection[1] if item in connection: if item == item1: print('connection is', connection, 'item', item, 'item1', item1, 'item2', item2) if item2 in arrWhatToConnect: del arrAllConDel[arrAllConnections.index(connection)] connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConDel, item2, olderItems) elif item == item2: if item1 in arrWhatToConnect: del arrAllConDel[arrAllConnections.index(connection)] connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConDel, item1, olderItems) return arrConnected for item in arrWhatToConnect: print(item) print(connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConnections, item, nullFirstConnect)) 

This is my first attempt to write any recursion (or, if I do not remember, this is a deliberate attempt to write recursion;)). Everything seems more or less clear and even working. But return statements are not enough, which affects memory. As it seems, I should add a visitNodes array. If you can show me how to improve the code, please do it. Thank you Preferably in python without any modules like nx etc., I would like to understand what is going on inside.

+6
source share

Source: https://habr.com/ru/post/986048/


All Articles