Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Euler path by stack solution in Clear category for One line Drawing by kdim
from collections import defaultdict
def draw(segments):
G = defaultdict(set)
for i, j, k, l in segments:
G[(i, j)] |= {(k, l)}
G[(k, l)] |= {(i, j)}
odd = sum(len(v) % 2 for v in G.values())
if odd > 2:
return []
v = next(iter(G.keys()) if not odd else (k for k, v in G.items() if len(v) % 2))
stack = [v]
path = []
while stack:
v = stack[-1]
if G[v]:
u = G[v].pop()
stack += [u]
G[u] -= {v}
else:
path += [stack.pop()]
return path
Aug. 19, 2023