Loading…
DS · Graphs · Program 36 · Bonus
Graph partitioning and neighborhood properties.
1import networkx as nx2from networkx.algorithms.community import kernighan_lin_bisection3 4G = nx.karate_club_graph()5 6A, B = kernighan_lin_bisection(G, seed=42)7print("Partition A:", sorted(A))8print("Partition B:", sorted(B))9print("Edges cut:", nx.cut_size(G, A, B))10 11node = 012print(f"\nNeighbors of node {node}:", sorted(G.neighbors(node)))13print(f"Degree of node {node}:", G.degree(node))14print(f"Clustering Coefficient of node {node}:", nx.clustering(G, node))Partition A: [8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33] Partition B: [0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 16, 17, 19, 21] Edges cut: 10 Neighbors of node 0: [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 17, 19, 21, 31] Degree of node 0: 16 Clustering Coefficient of node 0: 0.15
Run program 36 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.