points = set([(11, 9), (10, 9), (9, 6), (6, 2), (8, 5), (5, 4), (4, 2), (2,
    1), (3, 1), (7, 3)])


class PathFinder(object):

    def __init__(self, points):
        self.graph = self._make_graph(points)
self.hierarchy = {}

def _make_graph(self, points):
    graph = {}
for p in points:
    p0, p1 = p[0], p[1]
less, more = min(p), max(p)
if less not in graph:
    graph[less] = set([more])
else:
    graph[less].add(more)
return graph

def dfs(self, start):
    visited = set()
stack = [start]
_count = 0
while stack:
    _count += 1
vertex = stack.pop()
if vertex not in visited:
    visited.add(vertex)
if vertex in self.graph:
    stack.extend(v for v in self.graph[vertex])
print('Start: {s} | Count: {c} |'.format(c=_count, s=start), end=' ')
return visited

def dfsh(self, start):
    visited = set()
stack = [start]
_count = 0
while stack:
    _count += 1
vertex = stack.pop()
if vertex not in visited:
    if vertex in self.hierarchy:
        visited.update(self.hierarchy[vertex])
else:
    visited.add(vertex)
if vertex in self.graph:
    stack.extend([v for v in self.graph[vertex]])
self.hierarchy[start] = visited
print('Start: {s} | Count: {c} |'.format(c=_count, s=start), end=' ')
return visited
p = PathFinder(points)
print(p.dfsh(1))
print(p.dfsh(2))
print(p.dfsh(9))
print(p.dfsh(6))
print(p.dfsh(2))
print() 
print(p.dfs(1))
print(p.dfs(2))
print(p.dfs(9))
print(p.dfs(6))
print(p.dfs(2))
Эффективность поиска пути в Python