hierarchy.py 1.5 KB

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