Exemplo n.º 1
0
        stack.append(currv)

        while stack:
            if g.outdegree(currv) != 0:
                stack.append(currv)
                # 模拟一个iterator
                w = next(self._iter_next_adj(g.adj(currv)))
                g.remove_edge(currv, w)
                currv = w
            else:
                # 此时说明找到了一个环
                res.append(currv)
                currv = stack.pop()

        return res[::-1]

    def _iter_next_adj(self, adj):
        yield from sorted(adj)


if __name__ == '__main__':
    filename = 'play_with_graph_algorithms/chapter13/ug.txt'
    g = Graph(filename, directed=True)
    directed_eluer_loop = DirectedEulerLoop(g)
    print(directed_eluer_loop.result())

    filename = 'play_with_graph_algorithms/chapter13/ug2.txt'
    g = Graph(filename, directed=True)
    directed_eluer_loop = DirectedEulerLoop(g)
    print(directed_eluer_loop.result())
Exemplo n.º 2
0
        res = []
        cur = end
        while cur != start:
            res.append(cur)
            cur = pre[cur]
        res.append(start)
        return res

    def maxmatching(self):
        return self._maxmatching


if __name__ == "__main__":
    print('bfs:')
    filename = 'play_with_graph_algorithms/chapter15/g.txt'
    g = Graph(filename)
    hungarian = Hungarian(g)
    print(hungarian.maxmatching())

    filename = 'play_with_graph_algorithms/chapter15/g2.txt'
    g = Graph(filename)
    hungarian = Hungarian(g)
    print(hungarian.maxmatching())

    print('dfs:')
    filename = 'play_with_graph_algorithms/chapter15/g.txt'
    g = Graph(filename)
    hungarian = Hungarian(g, use_dfs=True)
    print(hungarian.maxmatching())

    filename = 'play_with_graph_algorithms/chapter15/g2.txt'