NetworkX
Builds and analyzes graphs with Python objects as nodes.
- Downloads / mo
- 279.1M
- Stars
- 17.4K
- Last release
- 3.7
- License
- BSD-3-Clause
Strengths
- Uses meaningful node labels. Use strings or other hashable Python objects directly as nodes.
- Finds weighted and unweighted paths. Use one interface for directed, undirected, and weighted shortest paths.
Limitations
- Parallel coverage depends on backend. Unsupported nx-parallel functions fall back to NetworkX.
- GraphML inputs must be trusted. Only parse GraphML files from trusted sources.
Quickstart
import networkx as nx G = nx.Graph() G.add_node(1)
NetworkX examples
def girvan_newman(g_original: object, level: int) -> NodeClustering: """ The Girvan–Newman algorithm detects communities by progressively removing edges from the original graph. The algorithm removes the "most valuable" edge, traditionally the edge with the highest betweenness centrality, at each step. As the graph breaks down into pieces, the tightly knit community structure is exposed and the result can be depicted as a dendrogram. **Supported Graph Types** ========== ======== ======== Undirected Directed Weighted ========== ======== ======== Yes No No ========== ======== ======== :param g_original: a networkx/igraph object :param level: the level where to cut the dendrogram (-1 for highest modularity) :return: NodeClustering object :Example: >>> from cdlib import algorithms >>> import networkx as nx >>> G = nx.karate_club_graph() >>> com = algorithms.girvan_newman(G, level=3) :References: Girvan, Michelle, and Mark EJ Newman. `Community structure in social and biological networks. <https://www.ncbi.nlm.nih.gov/pmc/articles/PMC122977/>`_ Proceedings of the national academy of sciences 99.12 (2002): 7821-7826. """ g = convert_graph_formats(g_original, nx.Graph) gn_hierarchy = nx.algorithms.community.girvan_newman(g) coms = [] if level==-1: max_modularity = -float('inf') for current_coms in gn_hierarchy: mod = nx.algorithms.community.modularity(g, current_coms) if mod > max_modularity: max_modularity = mod coms = current_coms else: for _ in range(level): coms = next(gn_hierarchy) communities = [] for c in coms: communities.append(list(c)) return NodeClustering( communities, g_original, "Girvan Newman", method_parameters={"level": level} )