''' Function generates all paths through the ball triangle Return: Dictionary with path name and nodes Input: Array of the node indices in the different layers ''' import pandas as pd from collections import defaultdict def getAllPaths(indexArray = [ [0], [1, 2], [3, 4, 5], [6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20]] ): pathMatching = pd.DataFrame(columns=['name','path']) for layer, nodes in enumerate(indexArray): layerElements = defaultdict(list) for node in nodes: layerElements[layer] = [node] for i in range(layer-1,-1, -1): elems = [] for k in layerElements[i+1]: if (k - i - 2) in indexArray[i]: elems.append( k - i - 2) if k-i - 1 in indexArray[i]: elems.append( k - i-1 ) layerElements[i] = list(set(elems)) elems = [] for i in range(layer+1): if i == 0: elems = [layerElements[i]] else: tmp = [] for tmpPath in elems: for newElem in layerElements[i]: if newElem == tmpPath[i-1] + i or newElem == tmpPath[i-1] + i + 1: thisPath = tmpPath.copy() thisPath.append(newElem) tmp.append(thisPath) elems = tmp.copy() for idx, indPath in enumerate(elems): name= "choice_" + str(node) + "_" + str(idx) pathMatching = pd.concat([pathMatching, pd.DataFrame.from_records([{"node" : int(node), "name":name, "path":indPath}])], ignore_index=True) pathMatching['combine'] = pathMatching[['name', 'path']].values.tolist() return {str(int(key)): value["combine"].tolist() for key, value in pathMatching.groupby("node")}