Makrus hat nen kleinen Pimmel

This commit is contained in:
Kai Koellemann
2023-07-16 20:00:15 +02:00
parent eaf2e7e111
commit 0fd332a6e5
3 changed files with 99 additions and 42 deletions

View File

@ -0,0 +1,28 @@
def creates_cycle(connections: list[tuple[int, int]], test: tuple[int, int]) -> bool:
"""
Returns true if the addition of the 'test' connection would create a cycle,
assuming that no cycle already exists in the graph represented by 'connections'.
https://github.com/CodeReclaimers/neat-python/blob/4928381317213ee3285204ae1f2a086286aa3a10/neat/graphs.py#L4
"""
i, o = test
if i == o:
return True
visited = {o}
while True:
num_added = 0
for a, b in connections:
if a in visited and b not in visited:
if b == i:
return True
visited.add(b)
num_added += 1
if num_added == 0:
return False