MCPcopy
hub / github.com/networkx/networkx / DegreeView

Class DegreeView

networkx/classes/reportviews.py:478–543  ·  view source on GitHub ↗

A DegreeView class to act as G.degree for a NetworkX Graph Typical usage focuses on iteration over `(node, degree)` pairs. The degree is by default the number of edges incident to the node. Optional argument `weight` enables weighted degree using the edge attribute named in the `wei

Source from the content-addressed store, hash-verified

476
477
478class DegreeView(DiDegreeView):
479 """A DegreeView class to act as G.degree for a NetworkX Graph
480
481 Typical usage focuses on iteration over `(node, degree)` pairs.
482 The degree is by default the number of edges incident to the node.
483 Optional argument `weight` enables weighted degree using the edge
484 attribute named in the `weight` argument. Reporting and iteration
485 can also be restricted to a subset of nodes using `nbunch`.
486
487 Additional functionality include node lookup so that `G.degree[n]`
488 reported the (possibly weighted) degree of node `n`. Calling the
489 view creates a view with different arguments `nbunch` or `weight`.
490
491 Parameters
492 ==========
493 graph : NetworkX graph-like class
494 nbunch : node, container of nodes, or None meaning all nodes (default=None)
495 weight : string or None (default=None)
496
497 Notes
498 -----
499 DegreeView can still lookup any node even if nbunch is specified.
500
501 Examples
502 --------
503 >>> G = nx.path_graph(3)
504 >>> DV = G.degree()
505 >>> assert DV[2] == 1
506 >>> assert G.degree[2] == 1
507 >>> assert sum(deg for n, deg in DV) == 4
508
509 >>> DVweight = G.degree(weight="span")
510 >>> G.add_edge(1, 2, span=34)
511 >>> DVweight[2]
512 34
513 >>> DVweight[0] # default edge weight is 1
514 1
515 >>> sum(span for n, span in DVweight) # sum weighted degrees
516 70
517
518 >>> DVnbunch = G.degree(nbunch=(1, 2))
519 >>> assert len(list(DVnbunch)) == 2 # iteration over nbunch only
520 """
521
522 def __getitem__(self, n):
523 weight = self._weight
524 nbrs = self._succ[n]
525 if weight is None:
526 return len(nbrs) + (n in nbrs)
527 return sum(dd.get(weight, 1) for dd in nbrs.values()) + (
528 n in nbrs and nbrs[n].get(weight, 1)
529 )
530
531 def __iter__(self):
532 weight = self._weight
533 if weight is None:
534 for n in self._nodes:
535 nbrs = self._succ[n]

Callers 1

degreeMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…