CYCLE_LIMIT = 3 def rem_routes(routes): loop_routes = routes while len(loop_routes) > 0: circ, paths, loop_routes = largest_loop(loop_routes) if not circ: break partial_paths = [] partial_routes = loop_routes while len(partial_routes) > 0: lp = longest_path(partial_routes) partial_paths.append((lp[-1].dst, lp[0].src)) partial_routes = [x for x in partial_routes if x not in lp] return partial_paths def route_deps(routes): routes_map = {} for r in routes: if r.src in routes_map: routes_map[r.src].append(r) else: routes_map[r.src] = [r] if r.dst not in routes_map: routes_map[r.dst] = [] return routes_map def largest_loop(routes): deps = route_deps(routes) circ_paths = [find_loop(r.src, r.src, deps, set(), []) for r in routes] circ, paths = max(circ_paths, key=lambda p: len(p[1])) # print filter(lambda p:not p[0],circ_paths) rem_paths = [x for x in routes if x not in paths] return (circ, paths, rem_paths) def longest_path(routes): def route_path(r, deps): if r.dst not in deps or len(deps[r.dst]) == 0: return [r] all_dep_paths = [([r] + route_path(rt, deps)) for rt in deps[r.dst]] rp = max(all_dep_paths, key=lambda p: len(p)) return rp deps = route_deps(routes) all_paths = [route_path(r, deps) for r in routes] longest = max(all_paths, key=len) return longest def find_loop(elem, dep_elem, deps, checked, path): found_loop = False if dep_elem not in checked and dep_elem in deps: checked.add(dep_elem) for c in deps[dep_elem]: path.append(c) if elem == c.dst and len(path) <= CYCLE_LIMIT: found_loop = True break (loop, path) = find_loop(elem, c.dst, deps, checked, path) if loop: found_loop = True break else: del(path[-1]) return (found_loop, path)