Logo Search packages:      
Sourcecode: python-networkx version File versions  Download package

def networkx::operators::convert_node_labels_to_integers (   G,
  first_label = 0,
  ordering = "default",
  discard_old_labels = True 
)

Return a copy of G, with n node labels replaced with integers,
starting at first_label.

first_label: (optional, default=0)

   An integer specifying the offset in numbering nodes.
   The n new integer labels are numbered first_label, ..., n+first_label.

ordering: (optional, default="default")

   A string specifying how new node labels are ordered. 
   Possible values are: 

      "default" : inherit node ordering from G.nodes() 
      "sorted"  : inherit node ordering from sorted(G.nodes())
      "increasing degree" : nodes are sorted by increasing degree
      "decreasing degree" : nodes are sorted by decreasing degree

*discard_old_labels*
   if True (default) discard old labels
   if False, create a dict self.node_labels that maps new
   labels to old labels, and set self.dna["node_labeled"]=True

Works for Graph, DiGraph, XGraph, XDiGraph

Definition at line 334 of file operators.py.

                                                            :
    """ Return a copy of G, with n node labels replaced with integers,
    starting at first_label.

    first_label: (optional, default=0)

       An integer specifying the offset in numbering nodes.
       The n new integer labels are numbered first_label, ..., n+first_label.
    
    ordering: (optional, default="default")

       A string specifying how new node labels are ordered. 
       Possible values are: 

          "default" : inherit node ordering from G.nodes() 
          "sorted"  : inherit node ordering from sorted(G.nodes())
          "increasing degree" : nodes are sorted by increasing degree
          "decreasing degree" : nodes are sorted by decreasing degree

    *discard_old_labels*
       if True (default) discard old labels
       if False, create a dict self.node_labels that maps new
       labels to old labels, and set self.dna["node_labeled"]=True

    Works for Graph, DiGraph, XGraph, XDiGraph
    """
#    This function strips information attached to the nodes and/or
#    edges of a graph, and returns a graph with appropriate integer
#    labels. One can view this as a re-labeling of the nodes. Be
#    warned that the term "labeled graph" has a loaded meaning
#    in graph theory. The fundamental issue is whether the names
#    (labels) of the nodes (and edges) matter in deciding when two
#    graphs are the same. For example, in problems of graph enumeration
#    there is a distinct difference in techniques required when
#    counting labeled vs. unlabeled graphs.

#    When implementing graph
#    algorithms it is often convenient to strip off the original node
#    and edge information and appropriately relabel the n nodes with
#    the integer values 1,..,n. This is the purpose of this function,
#    and it provides the option (see discard_old_labels variable) to either
#    preserve the original labels in separate dicts (these are not
#    returned but inserted into the dna of the new graph, or to discard that
#    information if it is superfluous.

    N=G.number_of_nodes()+first_label
    if ordering=="default":
        mapping=dict(zip(G.nodes(),range(first_label,N)))
    elif ordering=="sorted":
        nlist=G.nodes()
        nlist.sort()
        mapping=dict(zip(nlist,range(first_label,N)))
    elif ordering=="increasing degree":
        dv_pairs=[(G.degree(n),n) for n in G]
        dv_pairs.sort() # in-place sort from lowest to highest degree
        mapping=dict(zip([n for d,n in dv_pairs],range(first_label,N)))
    elif ordering=="decreasing degree":
        dv_pairs=[(G.degree(n),n) for n in G]
        dv_pairs.sort() # in-place sort from lowest to highest degree
        dv_pairs.reverse()
        mapping=dict(zip([n for d,n in dv_pairs],range(first_label,N)))
    else:
        raise networkx.NetworkXError,\
              "unknown value of node ordering variable: ordering"

    H=relabel_nodes(G,mapping)

    H.name="("+G.name+")_with_int_labels"
    if discard_old_labels:
        H.dna["has_node_labels"]=False
        H.dna["node_labels"]=None
    else:
        H.dna["has_node_labels"]=True
        H.dna["node_labels"]=mapping
    return H
    

def _test_suite():


Generated by  Doxygen 1.6.0   Back to index