hierarchy.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. Flow Hierarchy.
  3. """
  4. import networkx as nx
  5. __all__ = ["flow_hierarchy"]
  6. @nx._dispatchable(edge_attrs="weight")
  7. def flow_hierarchy(G, weight=None):
  8. """Returns the flow hierarchy of a directed network.
  9. Flow hierarchy is defined as the fraction of edges not participating
  10. in cycles in a directed graph [1]_.
  11. Parameters
  12. ----------
  13. G : DiGraph or MultiDiGraph
  14. A directed graph
  15. weight : string, optional (default=None)
  16. Attribute to use for edge weights. If None the weight defaults to 1.
  17. Returns
  18. -------
  19. h : float
  20. Flow hierarchy value
  21. Raises
  22. ------
  23. NetworkXError
  24. If `G` is not a directed graph or if `G` has no edges.
  25. Notes
  26. -----
  27. The algorithm described in [1]_ computes the flow hierarchy through
  28. exponentiation of the adjacency matrix. This function implements an
  29. alternative approach that finds strongly connected components.
  30. An edge is in a cycle if and only if it is in a strongly connected
  31. component, which can be found in $O(m)$ time using Tarjan's algorithm.
  32. References
  33. ----------
  34. .. [1] Luo, J.; Magee, C.L. (2011),
  35. Detecting evolving patterns of self-organizing networks by flow
  36. hierarchy measurement, Complexity, Volume 16 Issue 6 53-61.
  37. DOI: 10.1002/cplx.20368
  38. http://web.mit.edu/~cmagee/www/documents/28-DetectingEvolvingPatterns_FlowHierarchy.pdf
  39. """
  40. # corner case: G has no edges
  41. if nx.is_empty(G):
  42. raise nx.NetworkXError("flow_hierarchy not applicable to empty graphs")
  43. if not G.is_directed():
  44. raise nx.NetworkXError("G must be a digraph in flow_hierarchy")
  45. scc = nx.strongly_connected_components(G)
  46. return 1 - sum(G.subgraph(c).size(weight) for c in scc) / G.size(weight)