Cores
=====

Unit tests for cores decomposition in cores.py
----------------------------------------------

>>> from networkx import *
>>> from networkx.algorithms.core import *
>>> from networkx.generators.degree_seq import *


Below, graph G is the example graph in Figure 1 from Batagelj and
Zaversnik's paper titled An O(m) Algorithm for Cores Decomposition of
Networks, 2003, http://arXiv.org/abs/cs/0310049.  With nodes labeled
as shown, the 3-core is given by nodes 1-8, the 2-core by nodes 9-16,
the 1-core by nodes 17-20 and node 21 is in the 0-core.


>>> t1=convert_node_labels_to_integers(tetrahedral_graph(),1)
>>> t2=convert_node_labels_to_integers(t1,5)
>>> G=union(t1,t2)
>>> G.add_edges_from( [(3,7), (2,11), (11,5), (11,12), (5,12), (12,19),\
...             (12,18), (3,9), (7,9), (7,10), (9,10), (9,20),\
...             (17,13), (13,14), (14,15), (15,16), (16,13)])
>>> G.add_node(21)

    .. class:: doctest-block
    .. image:: cores_Batagelj_example.png

>>> cores=find_cores(G)
>>> nodes_by_core=[]
>>> for val in [0,1,2,3]:
...    nodes_by_core.append( sorted([k for k in cores if cores[k]==val]) )
>>> nodes_by_core[0]
[21]
>>> nodes_by_core[1]
[17, 18, 19, 20]
>>> nodes_by_core[2]
[9, 10, 11, 12, 13, 14, 15, 16]
>>> nodes_by_core[3] 
[1, 2, 3, 4, 5, 6, 7, 8]

Create the graph H resulting from the degree sequence
[0,1,2,2,2,2,3] when using the Havel-Hakimi algorithm. 


>>> degseq=[0,1,2,2,2,2,3]
>>> H=havel_hakimi_graph(degseq)

     .. class:: doctest-block
     .. image:: cores_H.png

>>> cores=find_cores(H)
>>> nodes_by_core=[]
>>> for val in [0,1,2]:
...    nodes_by_core.append( sorted([k for k in cores if cores[k]==val]) )
>>> nodes_by_core[0]
[0]
>>> nodes_by_core[1]
[1, 3]
>>> nodes_by_core[2]
[2, 4, 5, 6]
