Пример #1
0
def prims_algorithm(G, starting_node, draw=False, attrib=False):
    T = nx.Graph()
    T.add_node(starting_node)

    if draw:
        draw_subtree(G, T)

    while set(T.nodes()) != set(G.nodes()):
        e = min_prims_edge(G, T)
        T.add_edge(e[0], e[1], weight=cost(G, e))

        if draw:
            draw_subtree(G, T)

    if attrib:
        total_cost = sum(cost(G, e) for e in T.edges())

        print('________________Properties of the Tree T________________')
        print('________________________________________________________')
        print(f'V(T) = {list(T.nodes())}')
        print(f'E(T) = {list(T.edges())}')
        print(f'Total Cost = {total_cost}')
        print('________________________________________________________')

    return T
Пример #2
0
def prims_algorithm(G, starting_node, draw=False, attrib=False):

    T = nx.Graph()
    T.add_node(starting_node)

    if draw == True:
        draw_subtree(G, T)

    while set(T.nodes()) != set(G.nodes()):
        e = min_prims_edges(G, T)
        T.add_edge(e[0], e[1], weight=cost(G, e))
        if draw == True:
            draw_subtree(G, T)

    if attrib == True:
        total_cost = sum(cost(G, e) for e in T.edges())
        print('')
        print('------------PROPERTIES OF THE TREE T----------------')
        print('----------------------------------------------------')
        print(f'V(T) = {list(T.nodes())}')
        print(f'E(T) = {list(T.edges())}')
        print(f'Total Cost = {total_cost}')
        print('----------------------------------------------------')

    return T
Пример #3
0
def prims_algoritm(G, starting_node, draw=False, attrib=False):

    T = nx.Graph()
    T.add_nodes(starting_node)

    if draw == True:
        draw_subtree(G, T)
    while set(T.nodes()) != set(G.nodes()):
        e = min_prims_edge(G, T)
        T.add_edge([0], e[1], weight=cost(G, e))
        if draw == True:
            draw_subtree(G, T)
    if attrib == True:
        total_cost = sum(cost(G, e) for e in T.edges())
        print('')
        print('-----------------PROERTIES OF THE TREE T-----------')
        print('---------------------------------------------------')
        print(f'V(T) ={list(T.nodes())}')
        print(f'E(T) ={list(T.edges())}')
        print(f'Total Cost = {total_cost}')
        print('---------------------------------------------------')

    return T


# Function for Prim algorithm at the starting node.
# imports from subtree as well as prim cost function and minumum node edge.
def Prims(G, starting_node=0, draw=False, attrib=False):

    # Empty graph is created
    T = nx.Graph()

    #Attribute 2, starting node
    T.add_node(starting_node)

    #Attribute 3, if True graph will be drawn
    if draw == True:
        draw_subtree(G, T)
    """
    Uses min_valid_edge function to draw the a sub tree and to show a graph
    with the minimun valid edges to traverse the tree. 
    """
    while set(T.nodes()) != set(G.nodes()):
        e = min_valid_edge(G, T)
        T.add_edge(e[0], e[1], weight=cost(G, e))
        if draw == True:
            draw_subtree(G, T)

    #Attribute 4, if true info bellow will be shown
    if attrib == True:
        total_cost = sum(cost(G, e) for e in T.edges())
        print('')
        print('-------------- Properties of the Tree T --------------')
        print('-----------------------------------------------------')
        print(f'V(T) = {list(T.nodes())} ')
        print(f'E(T) = {list(T.edges())} ')
        print(f'Total Cost = {total_cost}')
        print('-----------------------------------------------------')

    return T
Пример #5
0
def prims_algorithim(G, starting_node, draw=False, showG=False):

    T = nx.Graph()
    T.add_node(starting_node)

    if draw == True:
        draw_subtree(G, T)

    #Determines that the starting nodes are not the same.
    while set(T.nodes()) != set(G.nodes()):
        e = min_prims_edge(G, T)
        T.add_edge(e[0], e[1], weight=cost(G, e))
        if draw == True:
            draw_subtree(G, T)

    # If graph valid then print out the low cost and number of edges
    if showG == True:
        total_cost = sum(cost(G, e) for e in T.edges())
        print(' ')
        print('---------PROPERTIES OF TREE T-----------')
        print('----------------------------------------')
        print(f'V(T) = {list(T.nodes())}')
        print(f'E(T) = {list(T.edges())}')
        print(f'Total Cost = {total_cost}')
        print('----------------------------------------')

    return T
Пример #6
0
def prims_algorithm(G,
                    starting_node,
                    draw=False,
                    attrib=False):  # Defining Prim's Algorithm

    T = nx.Graph()
    T.add_node(starting_node)

    if draw == True:  # draws the sub graph with minimum cost
        draw_subtree(G, T)

    while set(T.nodes()) != set(G.nodes()):
        e = min_prims_edges(G, T)
        T.add_edge(e[0], e[1], weight=cost(G, e))
        if draw == True:
            draw_subtree(G, T)

    if attrib == True:  # defines and returns the Total cost,
        total_cost = sum(cost(G, e) for e in T.edges())  # the number of edges,
        print('')  # and its corresponding weights of the sub-graph.
        print('_______________PROPERTIES OF THE TREE T___________________')
        print('__________________________________________________________')
        print(f'V(T) = {list(T.nodes())}')
        print(f'V(T) = {list(T.edges())}')
        print(f'Total Cost = {total_cost}')
        print('__________________________________________________________')

    return T
Пример #7
0
def prims_algorithm(G, starting_node):

    T = nx.Graph()
    T.add_node(starting_node)

    while set(T.nodes()) != set(G.nodes()):
        e = min_prims_edges(G, T)
        T.add_edge(e[0], e[1], weight=cost(G, e))

    return T
Пример #8
0
def prims_algorithm(G, start_node, draw=False, show_prop=False):
    T = nx.Graph()
    T.add_node(start_node)

    if draw == True:
        draw_subtree(G, T)

    while set(T.nodes()) != set(G.nodes()):
        e = min_prims_edge(G, T)
        T.add_edge(e[0], e[1], weight=cost(G, e))
        if draw == True:
            draw_subtree(G, T)
    if show_prop == True:
        total_cost = sum(cost(G, e) for e in T.edges())
        print('\n-----------------Tree Property-----------------')
        print('=============================================')
        print(f'V(T)={list(T.nodes())}')
        print(f'E(T)={list(T.edges())}')
        print(f'Total Cost ={total_cost}')

    return T
Пример #9
0
def prims_algorithm(G, starting_node, draw=False, detail=False):
    """Returns minimum spanning subtree of graph 'G'. Displays every iteration as 
     algorithm constructs subtree. After last iteration algorithm displays vertices, edges
     and total cost of subtree.
    
    A minimum spanning tree is a subtree of graph using all vertices while staying
    connected and having minimum possible weight while meeting first two conditions.
    
    Total cost of a tree is the sum of all edges. Also referred to as total weight.
    
    A subtree is a tree graph inside of another graph.
    
    Parameters
    ----------
    G = A networkx graph.
    starting_node = A node on 'G'.
    draw = A bool value.
    detail = A bool value.
    
    Returns
    -------
    T a subgraph of G. Including nodes, edges and weights.
    
    Examples
    --------
    >>> G = nx.read_weighted_edgelist('data/G3.txt',
                              nodetype = int)
    >>> starting_node = 2
    
    >>> draw = True
    
    >>> detail = True
    
    ------------------Tree T Details---------------
-----------------------------------------------
V(T) = [3, 5, 6, 2, 1, 0, 4, 7]
E(T) = [(3, 5), (3, 2), (5, 6), (6, 7), (2, 1), (2, 0), (0, 4)]
Total Cost = 12.0
----------------------------------------------
    """

    T = nx.Graph()
    T.add_node(starting_node)
    if draw == True:
        draw_subtree(G, T)

    while set(T.nodes()) != set(G.nodes()):
        e = min_prims_edge(G, T)
        T.add_edge(e[0], e[1], weight=cost(G, e))
        if draw == True:
            draw_subtree(G, T)

    if detail == True:
        total_cost = sum(cost(G, e) for e in T.edges())
        print('------------------Tree T Details---------------')
        print('-----------------------------------------------')
        print(f'V(T) = {list(T.nodes())}')
        print(f'E(T) = {list(T.edges())}')
        print(f'Total Cost = {total_cost}')
        print(f'----------------------------------------------')
    return T