回顾我在Python中实现的Prim算法

时间:2016-07-23 03:15:38

标签: python algorithm prims-algorithm

为什么我的实现对于大输入来说很慢?

正如您在下面看到的,我的代码适用于给定的输入,但随着我将输入的大小增加到10,000行矩阵,它会运行数天。

我尝试对较小的输入进行性能分析,没有严重的内存消耗。我一直在从文件中读取这可能是问题,但 iotop 根本没有显示IO读/写。

输入

 0    31    24    33    26
31     0    32    38    33
26    32     0    29    23
33    38    29     0    31
26    33    23    31     0

代码

import sys
import linecache


DEBUG = sys.argv[2] if len(sys.argv) > 2 else False

if __name__ == "__main__":

    line = linecache.getline(sys.argv[1], 1)
    line = line.replace("  ", " ").strip().split()
    neighbours = list([int(_) for _ in line])
    vertices_found = set(range(len(neighbours)))
    if DEBUG:
        print("Initial Vertices: " + str(vertices_found))

    vertices_visited = set()
    vertices_visited.add(0) # start vertex
    edge_weight = 0

    while vertices_found != vertices_visited:
        lowest_v = None
        lowest_col = None
        lowest_nn = 0

        # set lowest_nn to max possible value
        # this step is redundant but useful
        # we can avoid this by setting lowest_nn to some random big value
        for v in vertices_visited:
            # get neighbours from file
            line = linecache.getline(sys.argv[1], v+1)
            line = line.replace("  ", " ").strip().split()
            neighbours = list([int(_) for _ in line])

            for nn in neighbours:
                if nn >= lowest_nn:
                    lowest_nn = nn + 1

        for v in vertices_visited:
            # get neighbours from file
            line = linecache.getline(sys.argv[1], v+1)
            line = line.replace("  ", " ").strip().split()
            neighbours = list(map(int, line))

            for col, nn in enumerate(neighbours):
                if nn > 0 and nn <= lowest_nn and col not in vertices_visited:
                    lowest_v = v
                    lowest_col = col
                    lowest_nn = nn

        if lowest_col is not None:
            vertices_visited.add(lowest_col)
            edge_weight += lowest_nn
            if DEBUG:
                print("Visited Vertices: " + str(vertices_visited))

    print("Minimum Spanning Tree Weight: %d" % edge_weight)

如何运行?

将代码保存在code.py中并将输入保存在input.txt中,然后运行以下命令。

python code.py input.txt 1

0 个答案:

没有答案