I'm trying to identify bubble structures in a directed graph (orientation is left to right). I start walking the graph from a possible start node, for example the green node. Then I add one node to all paths, adding copies of paths when paths diverge again, like this:
first iteration: [[3],[7]]
Second iteration: [[3,4],[3,5],[7,8],[7,9]]
After every iteration I want to check whether any paths intersect and save them as confirmed bubbles. Currently I'm using a nested for-loop to compare every path to one another but the amount of paths can get very large and thus the script can get very slow. The order of the path matters.
Any suggestions on how to improve the speed of comparing a path to another path in a list of paths?
def ExtendPaths(paths, outEdges):
newPaths = []
for path in paths:
nextNodes = GetNextSegment(path[len(path) - 1], outEdges)
if len(nextNodes) == 0:
j=5
else:
for node in nextNodes:
newPath = list(path)
newPath.append(node)
newPaths.append(newPath)
return newPaths
def ComparePaths(paths, putativeBubbleStartSegment):
length = len(paths)
for path1 in paths:
for path2 in paths:
if path2 != path1:
if len(set(path1).intersection(path2)) > 0:
#Bubble confirmed
Where GetNextSegment simply returns a list of nodes connected to the node given to the function (in this case, the last node of the path). outEdges is a dictionary with: node:[out,going,edges]. In ComparePaths() a bubble is confirmed when the length of the .intersection() of 2 paths is greater than 0.
A bubble is a graph structure where 2 paths diverge (from the green node, for example) and finally come together again. In this example, the bubble would go from 2 to 11 with all nodes between them.
I'm not asking for a complete bubble-finding algorithm, just for ideas on how to compare all paths to all other paths quickly.