Guide/Start here
Tutorial: A Complete Graph Analysis
1 min readFrom the README
On this page 7
A walkthrough that goes from raw data to insight using only fnx, demonstrating the end-to-end loop a real user would follow.
1. Build or load the graph
import franken_networkx as fnx
# Option A: load Zachary's karate club (a classic ~34-node fixture).
G = fnx.karate_club_graph()
# Option B: load from an edgelist file. Whitespace-separated, with optional
# {"weight": value} attribute literals; exact format match with nx.
# G = fnx.read_edgelist("input.edgelist", create_using=fnx.Graph)
# Option C: build from scratch.
# G = fnx.Graph()
# G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4), (4, 5)])
print(G.number_of_nodes(), "nodes,", G.number_of_edges(), "edges")
# 34 nodes, 78 edges
2. Inspect structural properties
print("density:", fnx.density(G)) # → 0.139...
print("diameter:", fnx.diameter(G)) # → 5
print("radius:", fnx.radius(G)) # → 3
print("center:", sorted(fnx.center(G))) # → nodes at min eccentricity
print("is_connected:", fnx.is_connected(G))# → True
# Degree distribution.
hist = fnx.degree_histogram(G)
for d, count in enumerate(hist):
if count:
print(f"degree {d:2d}: {count}")
3. Rank nodes by centrality
pr = fnx.pagerank(G)
btw = fnx.betweenness_centrality(G)
clo = fnx.closeness_centrality(G)
# Top 5 by each measure:
def top5(d, label):
rows = sorted(d.items(), key=lambda kv: kv[1], reverse=True)[:5]
print(f"\nTop 5 by {label}:")
for node, score in rows:
print(f" {node!s:>5} → {score:.4f}")
top5(pr, "PageRank")
top5(btw, "betweenness")
top5(clo, "closeness")
For the karate club graph, both centralities surface nodes 0 ("Mr. Hi") and 33 ("Officer"), the two factional leaders. Cross-method agreement is a useful sanity check.
4. Detect community structure
# Louvain (modularity-maximizing community detection).
comms = list(fnx.community.louvain_communities(G, seed=7))
print(f"\n{len(comms)} communities (modularity = "
f"{fnx.community.modularity(G, comms):.4f}):")
for i, c in enumerate(comms):
print(f" community {i}: {sorted(c)}")
Louvain on the karate club typically finds 3–4 communities with modularity ≈ 0.42, splitting the graph along the actual social-faction lines documented by Zachary in 1977.
5. Compute shortest paths between every pair
# all-pairs shortest path lengths (BFS for unweighted graphs).
ap = dict(fnx.all_pairs_shortest_path_length(G))
# Find the two most-distant nodes.
import heapq
furthest = max(
((u, v, ap[u][v]) for u in ap for v in ap[u] if u < v),
key=lambda triple: triple[2],
)
print(f"\nfurthest pair: {furthest[0]} → {furthest[1]}, distance {furthest[2]}")
6. Export the analysis
# Annotate nodes with PageRank before saving so downstream readers see it.
for node, score in pr.items():
G.nodes[node]["pagerank"] = score
fnx.write_graphml(G, "karate_annotated.graphml")
print("\n→ wrote karate_annotated.graphml")
That entire pipeline is byte-for-byte identical to the corresponding NetworkX code; flip fnx. → nx. and run it again to verify.
7. Verify against the NetworkX oracle
import networkx as nx
# Convert via node-link JSON so attributes are preserved exactly.
data = fnx.node_link_data(G)
G_nx = nx.node_link_graph(data)
# Re-run a load-bearing computation on each side and assert agreement.
assert fnx.diameter(G) == nx.diameter(G_nx)
assert {tuple(sorted(c)) for c in fnx.community.louvain_communities(G, seed=7)} == \
{tuple(sorted(c)) for c in nx.community.louvain_communities(G_nx, seed=7)}
print("→ fnx ↔ nx agreement verified on diameter + communities")
If that assertion fires, please file an issue. That is the contract.