graphit/graph_algorithms.py
def degree(graph, nodes=None, weight=None):
    """
    Return the degree of nodes in the graph
    
    The degree (or valency) of a graph node are the number of edges
    connected to the node, with loops counted twice.
    The method supports weighted degrees in which the connected
    nodes are multiplied by a weight factor stored as attribute in
    the edges.
    
    :param graph:  Graph to return the degree of nodes for
    :type graph:   Graph class instance
    :param nodes:  nodes to return the degree for, None uses all
                   nodes in the graph.
    :type nodes:   list
    :param weight: Weight factor attribute in the edges
    :type weight:  string
    :return:       degree of each node
    :rtype:        list of tuples (node, degree)
    """

    if nodes is None:
        nodes = graph.nodes
    else:
        not_in_graph = [nid for nid in nodes if nid not in graph.nodes]
        if not_in_graph:
            logger.error('Nodes {0} not in graph'.format(not_in_graph))

    results = {}
    if weight:
        for node in nodes:
            results[node] = sum([graph.edges[(node, n)].get(weight, 1) for n in graph.adjacency[node]])
            if node in graph.adjacency[node]:
                results[node] += graph.edges[(node, node)].get(weight, 1)
    else:
        for node in nodes:
            results[node] = len(graph.adjacency[node])
            if node in graph.adjacency[node]:
                results[node] += 1

    return results

    Return the degree of nodes in the graph
    
    The degree (or valency) of a graph node are the number of edges
    connected to the node, with loops counted twice.
    The method supports weighted degrees in which the connected
    nodes are multiplied by a weight factor stored as attribute in
    the edges.
    
    :param graph:  Graph to return the degree of nodes for
    :type graph:   Graph class instance
    :param nodes:  nodes to return the degree for, None uses all
                   nodes in the graph.
    :type nodes:   list
    :param weight: Weight factor attribute in the edges
    :type weight:  string
    :return:       degree of each node
    :rtype:        list of tuples (node, degree)
    
Return the degree of nodes in the graph
The degree (or valency) of a graph node are the number of edges
connected to the node, with loops counted twice.
The method supports weighted degrees in which the connected
nodes are multiplied by a weight factor stored as attribute in
the edges.
Return the degree of nodes in the graph The degree ( or valency ) of a graph node are the number of edges connected to the node with loops counted twice . The method supports weighted degrees in which the connected nodes are multiplied by a weight factor stored as attribute in the edges .
def degree(graph, nodes=None, weight=None):
    if nodes is None:
        nodes = graph.nodes
    else:
        not_in_graph = [nid for nid in nodes if nid not in graph.nodes]
        if not_in_graph:
            logger.error('Nodes {0} not in graph'.format(not_in_graph))
    results = {}
    if weight:
        for node in nodes:
            results[node] = sum([graph.edges[(node, n)].get(weight, 1) for n in graph.adjacency[node]])
            if node in graph.adjacency[node]:
                results[node] += graph.edges[(node, node)].get(weight, 1)
    else:
        for node in nodes:
            results[node] = len(graph.adjacency[node])
            if node in graph.adjacency[node]:
                results[node] += 1
    return results
def degree ( graph , nodes = None , weight = None ) : if nodes is None : nodes = graph . nodes else : not_in_graph = [ nid for nid in nodes if nid not in graph . nodes ] if not_in_graph : logger . error ( 'Nodes {0} not in graph' . format ( not_in_graph ) ) results = { } if weight : for node in nodes : results [ node ] = sum ( [ graph . edges [ ( node , n ) ] . get ( weight , 1 ) for n in graph . adjacency [ node ] ] ) if node in graph . adjacency [ node ] : results [ node ] += graph . edges [ ( node , node ) ] . get ( weight , 1 ) else : for node in nodes : results [ node ] = len ( graph . adjacency [ node ] ) if node in graph . adjacency [ node ] : results [ node ] += 1 return results
Return the degree of nodes in the graph
The degree (or valency) of a graph node are the number of edges
connected to the node, with loops counted twice.
Return the degree of nodes in the graph The degree ( or valency ) of a graph node are the number of edges connected to the node with loops counted twice .
"""
    Return the degree of nodes in the graph
    
    The degree (or valency) of a graph node are the number of edges
    connected to the node, with loops counted twice.
    The method supports weighted degrees in which the connected
    nodes are multiplied by a weight factor stored as attribute in
    the edges.
    
    :param graph:  Graph to return the degree of nodes for
    :type graph:   Graph class instance
    :param nodes:  nodes to return the degree for, None uses all
                   nodes in the graph.
    :type nodes:   list
    :param weight: Weight factor attribute in the edges
    :type weight:  string
    :return:       degree of each node
    :rtype:        list of tuples (node, degree)
    """
degree of each node
degree of each node
list of tuples (node, degree)
graph
Graph to return the degree of nodes for
Graph to return the degree of nodes for
nodes
nodes to return the degree for, None uses all
nodes in the graph.
nodes to return the degree for None uses all nodes in the graph .
weight
Weight factor attribute in the edges
Weight factor attribute in the edges