python list 交并差

https://www.cnblogs.com/chen-kh/p/7137565.htmldef find_eulerian_tour(graph):

    # your code here

    start_edge = graph[0]

    tour = [start_edge[0]]

    tour_list = []

    return find_tour(graph ,tour, tour_list)

def find_tour(graph,tour,tour_list):

    if len(graph) == 0:

        print tour_list

        if len(tour_list) == 1:

            return tour_list[0]

        for i in range(len(tour_list)):

            for j in range(i + 1,len(tour_list)):

                uni_list = list(set(tour_list[i]).intersection(set(tour_list[j])))

                if len(uni_list) > 0:

                    index_i = tour_list[i].index(uni_list[0])

                    index_j = tour_list[j].index(uni_list[0])

                    tour_list[i][index_i:index_i] = tour_list[j][:index_j]

                    tour_list[i][index_i:index_i] = tour_list[j][index_j:-1]

                    tour_list.pop(j)

                    return find_tour(graph,tour,tour_list)

    for edge in graph:

        if edge[0] == tour[-1]:

            tour.append(edge[1])

            graph.remove(edge)

            if len(graph) == 0:

                tour_list.append(tour)

            return find_tour(graph ,tour,tour_list)

        elif edge[1] == tour[-1]:

            tour.append(edge[0])

            graph.remove(edge)

            if len(graph) == 0:

                tour_list.append(tour)

            return find_tour(graph ,tour,tour_list)

    tour_list.append(tour)

    return find_tour(graph, [graph[0][0]], tour_list)


def test():

    #print find_eulerian_tour( [(0, 1), (1, 5), (1, 7), (4, 5),(4, 8), (1, 6), (3, 7), (5, 9),(2, 4), (0, 4), (2, 5), (3, 6), (8, 9)])

    print find_eulerian_tour( [(1, 13), (1, 6), (6, 11), (3, 13),(8, 13), (0, 6), (8, 9),(5, 9), (2, 6), (6, 10), (7, 9),(1, 12), (4, 12), (5, 14), (0, 1),  (2, 3), (4, 11), (6, 9),(7, 14),  (10, 13)])

test()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前端知识体系http://www.cnblogs.com/sb19871023/p/3894452.html 前端...
    秋风喵阅读 12,565评论 7 163
  • 千言万语,不知从何说起?总之是难受。 动手记录2018年西决最后一篇感受,想过几个标题,诸如“命运的轮回”“休斯顿...
    二羽_ba63阅读 1,507评论 0 0
  • 从没有听说过你长大的故事 只知道你是颗被遗弃的种子 冬天的山头雪融了一片潮湿 以为你的人生这样到此为止 澄黄土地让...
    猫耳朵的兔子阅读 208评论 9 4
  • 第二首 给Y 南生。 Y,期待今夜的雨倾盆地下 我想大声歌唱我们的爱情 光着脚丫,和你嬉笑怒骂 电光石火,雷声隆隆...
    s林南生阅读 158评论 1 2
  • 文/卿羽 - 枫亭,残生悲 投向迎风,火树 敲窗侧听的帖 - 碎月,白雪谁 画心赭瓦,失约 满江红的侠客 - 海潮...
    卿羽的秋凉阅读 552评论 8 6