#NEWLINE# def prim_mst( G , weight = 'weight' ) : #NEWLINE# #INDENT# "Return a minimum spanning tree or forest of an undirected \n    weighted graph.\n\n    A minimum spanning tree is a subgraph of the graph (a tree) with\n    the minimum sum of edge weights.\n\n    If the graph is not connected a spanning forest is constructed.  A\n    spanning forest is a union of the spanning trees for each\n    connected component of the graph.\n\n    Parameters\n    ----------\n    G : NetworkX Graph\n    \n    weight : string\n       Edge data key to use for weight (default 'weight').\n\n    Returns\n    -------\n    G : NetworkX Graph\n       A minimum spanning tree or forest. \n    \n    Examples\n    --------\n    >>> G=nx.cycle_graph(4)\n    >>> G.add_edge(0,3,weight=2) # assign weight 2 to edge 0-3\n    >>> T=nx.prim_mst(G)\n    >>> print(sorted(T.edges(data=True)))\n    [(0, 1, {}), (1, 2, {}), (2, 3, {})]\n\n    Notes\n    -----\n    Uses Prim's algorithm.\n\n    If the graph edges do not have a weight attribute a default weight of 1\n    will be used.\n    " #NEWLINE# T = nx . Graph ( nx . prim_mst_edges ( G , weight = weight , data = True ) ) #NEWLINE# if ( len ( T ) != len ( T ) ) : #NEWLINE# #INDENT# T . add_nodes_from ( [ n for ( n , d ) in G . degree ( ) . items ( ) if ( d == 0 ) ] ) #NEWLINE# #UNINDENT# for n in T : #NEWLINE# #INDENT# T . node [ n ] = G . node [ n ] . copy ( ) #NEWLINE# #UNINDENT# T . graph = G . graph . copy ( ) #NEWLINE# return T