コード例 #1
0
ファイル: prims.py プロジェクト: walikhawaja01/Final-Project-
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.
コード例 #2
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
コード例 #3
0
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
コード例 #4
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
コード例 #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)

    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 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, draw=False, attr=False):
    T = nx.Graph()
    T.add_node(starting_node)

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

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

    if attr == True:
        total_cost = sum(cost(G, e) for e in T.edges())
        print('')
        print('T Nodes = ', list(T.nodes()))
        print('T Edges = ', list(T.edges()))
        print('Cost = ', total_cost)

    return T
コード例 #8
0
ファイル: test.py プロジェクト: EachFuture23/UHD-projectoo
                              nodetype = int)
T = nx.Graph()
T.add_node(2)
 

print('Iteration 1')
for e in set((G.edges())) - set(T.edges()):
     for v in T.nodes():
         if v in e:
             print(f'Edge {e} has cost {cost(G, e)}')
                       
             
print('')             
T. add_edge(0, 2, weight = cost(G, (0,2)))

draw_subtree(G, T)



print('Iteration 2')
for e in set((G.edges())) - set(T.edges()):
    for v in T.nodes():
         if v in e:
             print(f'Edge {e} has cost {cost(G, e)}')
             
print('')             
T. add_edge(0, 1, weight = cost(G, (0,1)))

draw_subtree(G, T)