Repository: networkx/networkx

Issue:
Minimum spanning tree doesn't return all of the nodes 
Hello,
I am having an issue with an undirected connected graph. When I get the MST, the function returns less nodes than the original graph:

```
mst = nx.minimum_spanning_tree(q)
print  mst.number_of_nodes(), q.number_of_nodes()
```

This returns:
`30831 30833`

Any idea of why is this happening, and how I can fix it? 

Thanks,
Jose



Discussion:
The nodes of the minimum spanning tree should be the same as those from the original graph. We do add the edges we've found for the tree after adding the nodes from the original graph, so it is possible that a "bug" would create extra nodes in ```mst0``` that are not in ```q```. But you are getting fewer nodes in ```mst0``` and I don't see how that could happen with the current code.

Which version of NetworkX are you using?
Is it helpful to  find out the extra nodes?      ```set(mst0) ^ set(q)```
Hey! 

Thank you for the fast response. I am using 1.11:
```
pip freeze | grep networkx                                                     
networkx==1.11                                       
```

I tried checking the nodes to get an insight.  The nodes have an attribute, but the graph is unweighted.  (The attribute is not called weight)

I mean:  what are the nodes that get added?  You can see that by using the command:
```set(mst0) ^ set(q)          #  provides the set elements that are in either set but not both```    
Hopefully it will just give you two nodes.
I'm sorry, I don't get what you are saying.  I don't get nodes added, I get _less_ nodes.

I think I was able to isolate the issue. MST doesn't play well with graphs with selfloops:

```
G1 = nx.Graph()
G1.add_nodes_from([1, 2, 3])
G1.add_edge(2, 2)

print G1.number_of_nodes() #3
print nx.minimum_spanning_tree(G1).number_of_nodes() #2
```

The output should be 3 and 3, but it's 3 and 2.  Notice that it doesn't matter if the graph is disconnected (like this one).  The issue persists for directed graphs.


Thank you for that simple example!

This weakness is partially fixed in NetworkX 2.0 (that is, with the current Github development branch OR the pypi version 2.0rc1 using ```pip install networkx==2.0rc1```).

Only partially fixed because the selfloop edges still aren't part of the mst forest, but the nodes are.
It looks like the minimum spanning tree of a single node graph with a self-loop is just the node (That node is itself a spanning tree by all definitions I have found; and including the edge will only increase the weight of the tree).

So it looks like the v2.0 reporting of the MST is correct (it includes isolated nodes but not self-loops on isolated nodes).  I will add a note to the documentation that this is the case.

V1.11 has a bug where it doesn't report any isolated nodes which have self-loops.

Thanks for catching and reporting this!

Resolving patch:
diff --git a/networkx/algorithms/tree/mst.py b/networkx/algorithms/tree/mst.py
--- a/networkx/algorithms/tree/mst.py
+++ b/networkx/algorithms/tree/mst.py
@@ -537,6 +537,8 @@ def minimum_spanning_tree(G, weight='weight', algorithm='kruskal',
     There may be more than one tree with the same minimum or maximum weight.
     See :mod:`networkx.tree.recognition` for more detailed definitions.
 
+    Isolated nodes with self-loops are in the tree as edgeless isolated nodes.
+
     """
     edges = minimum_spanning_edges(G, algorithm, weight, keys=True,
                                    data=True, ignore_nan=ignore_nan)
@@ -596,6 +598,8 @@ def maximum_spanning_tree(G, weight='weight', algorithm='kruskal',
     There may be more than one tree with the same minimum or maximum weight.
     See :mod:`networkx.tree.recognition` for more detailed definitions.
 
+    Isolated nodes with self-loops are in the tree as edgeless isolated nodes.
+
     """
     edges = maximum_spanning_edges(G, algorithm, weight, keys=True,
                                    data=True, ignore_nan=ignore_nan)


Test patch: