Ejemplo n.º 1
0
def read_graph_from_file(filename):
    '''Parse edges from the file and load them into a graph object.'''
    edges = read_edges_from_file(filename)
    g = Graph()
    for v, u in edges:
        g.add_edge(v, u)
    return g
Ejemplo n.º 2
0
def read_weighted_undirected_graph(filename):
    g = Graph()
    with open(filename) as f:
        for line in f:
            try:
                v1, v2, w = line.split()
                g.add_edge(v1, v2, {'weight': int(w)})
            except:
                pass
    return g
Ejemplo n.º 3
0
def read_weighted_undirected_graph(filename):
    """A helper to read the edges of a graph described in the file
    into a graph object."""
    g = Graph()
    with open(filename) as f:
        for line in f:
            try:
                v1, v2, w = line.split()
                g.add_edge(v1, v2, {'weight': int(w)})
            except:
                pass
    return g
Ejemplo n.º 4
0
    '''Use Prim's algorithm to compute the minimum spanning tree of the weighted undirected graph
    described by the contents of the file named filename.'''
    tree_edges = []
    # TODO compute the edges of a minimum spanning tree
    write_tree_edges_to_file(tree_edges, filename + '.mst')


if __name__ == '__main__':
    #读取信息

    g = Graph()
    with open("5000 input") as f:
        for line in f:
            try:
                v1, v2, w = line.split()
                g.add_edge(v1, v2, {'weight': int(w)})
            except:
                pass
    node_sets = initialize_disjoint_set(g.get_nodes())
    node_count = len(node_sets)
    #edges = [(g.attributes_of(v, u)['weight'], v, u) for u, v in g.get_edges()]
    #edges.sort()
    tree_edges = []

    #查看读取信息
    #print("node_sets is: ",node_sets)
    #print("node_count is: ",node_count)
    #print("edges is: ",edges)

    #put all the G's node in the queue
    queue = list(node_sets.keys())