Given 2 random DAGs, which may or may not have common nodes. We randomly pick one node from each, and call them A and B respectively. A != B.
How to check before merging, that if we merge node B into node A (by copying edges from B to edges in A, and destroying the node B), there will be no cycles?
I want to avoid exhaustive search - when we 'virtually' merge this nodes and then for all nodes in resulting graph search for paths from this node to itself.
A Node has unique ID and roughly defined like this:
class Node:
def __init__(self, id):
self.id = id
# set of outgoing nodes
self.deps = set()
# set of incoming nodes
self.dependants = set()
# in the end, I want to write function like this:
def check(A, B):
assert isinstance(A, Node) and isinstance(B, Node)
if "there will be no cycles":
return True # we can merge
else:
return False # we can not merge
I do not need the code, algorithm description or algorithm name would be enough.