Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Eulerian path & DFS solution in Clear category for One line Drawing by DiZ
from collections import defaultdict
def draw(segments):
def dfs(node, path=[], edges=set()):
path.append(node)
if len(edges) == len(segments):
return path
for child in graph[node]:
edge = frozenset((node, child))
if edge not in edges:
path = dfs(child, path, edges | {edge})
if path: return path
graph = defaultdict(set)
for x1, y1, x2, y2 in segments:
graph[x1, y1].add((x2, y2))
graph[x2, y2].add((x1, y1))
odds = [vertex for vertex, edges in graph.items() if len(edges)%2]
return dfs(min(odds or graph)) if len(odds) in (0,2) else []
Jan. 12, 2015
Comments: