data_structures/graphs/mst.py
def prim(g):
    """
    Return the minimum spanning tree using Prim's algorithm
    :param g:
    :return:
    """
    # TODO: Check
    global infinity
    
    order = len(g.getOrder())
    c = {} # Cheapest costs
    e = {} # Best edges.
    
    F = {} # Empty tree: adjacency list
    Q = list(graph.keys()) # List of vertices not included in F
    
    def addEdge(u,v):
        F[u].append(v)
        F[v].append(u)    
    
    # Init c, e and F
    for vertex in graph:
        c[vertex] = infinity
        e[vertex] = None
        F[vertex] = []
    
    # Prim's algorithm
    while Q != []:
        # Find and remove a vertex v from Q having the minimum possible value of C[v]
        minVertex = Q[0]
        for vertex in Q:
            if c[vertex] < c[minVertex]:
                minVertex = vertex
        Q.remove(minVertex)
        v = minVertex
        
        
        # Add v to F and, if E[v] is not the special flag value, also add E[v] to F
        if e[v] != None:
            addEdge(v, e[v])
        
        
        for w in graph[v].keys():
            if w in Q and graph[v][w] < c[w]:
                c[w] = graph[v][w]
                e[w] = v
    
    # Return the tree
    return F

    Return the minimum spanning tree using Prim's algorithm
    :param g:
    :return:
    
Return the minimum spanning tree using Prim's algorithm
Return the minimum spanning tree using Prim ' s algorithm
def prim(g):
    global infinity
    order = len(g.getOrder())
    c = {} 
    e = {} 
    F = {} 
    Q = list(graph.keys()) 
    def addEdge(u,v):
        F[u].append(v)
        F[v].append(u)    
    for vertex in graph:
        c[vertex] = infinity
        e[vertex] = None
        F[vertex] = []
    while Q != []:
        minVertex = Q[0]
        for vertex in Q:
            if c[vertex] < c[minVertex]:
                minVertex = vertex
        Q.remove(minVertex)
        v = minVertex
        if e[v] != None:
            addEdge(v, e[v])
        for w in graph[v].keys():
            if w in Q and graph[v][w] < c[w]:
                c[w] = graph[v][w]
                e[w] = v
    return F
def prim ( g ) : global infinity order = len ( g . getOrder ( ) ) c = { } e = { } F = { } Q = list ( graph . keys ( ) ) def addEdge ( u , v ) : F [ u ] . append ( v ) F [ v ] . append ( u ) for vertex in graph : c [ vertex ] = infinity e [ vertex ] = None F [ vertex ] = [ ] while Q != [ ] : minVertex = Q [ 0 ] for vertex in Q : if c [ vertex ] < c [ minVertex ] : minVertex = vertex Q . remove ( minVertex ) v = minVertex if e [ v ] != None : addEdge ( v , e [ v ] ) for w in graph [ v ] . keys ( ) : if w in Q and graph [ v ] [ w ] < c [ w ] : c [ w ] = graph [ v ] [ w ] e [ w ] = v return F
Return the minimum spanning tree using Prim's algorithm
Return the minimum spanning tree using Prim ' s algorithm
"""
    Return the minimum spanning tree using Prim's algorithm
    :param g:
    :return:
    """ # TODO: Check
 # Cheapest costs
 # Best edges.
 # Empty tree: adjacency list
 # List of vertices not included in F
 # Init c, e and F
 # Prim's algorithm
 # Find and remove a vertex v from Q having the minimum possible value of C[v]
 # Add v to F and, if E[v] is not the special flag value, also add E[v] to F
 # Return the tree

None
g
None