Program to find sum of the costs of all simple undirected graphs with n nodes in Python

Suppose we have an undirected graph G with n nodes. The cost of a simple undirected graph is the sum of the costs of its nodes, where the cost of a node is Dk (D is its degree). Given n and k values, we need to find the sum of the costs of all possible simple undirected graphs with n nodes.

The result may be very large, so we return the result modulo 1005060097.

Example Walkthrough

If n = 3 and k = 2, there are eight simple graphs with 3 nodes:

  • One graph with 3 edges: cost is 22 + 22 + 22 = 12
  • Three graphs with 2 edges: cost of each is 12 + 12 + 22 = 6
  • Three graphs with 1 edge: cost of each is 02 + 12 + 12 = 2
  • One graph with 0 edges: cost is 02 + 02 + 02 = 0

Total cost = 12×1 + 6×3 + 2×3 + 0×1 = 36

Algorithm Steps

To solve this problem, we:

  • Calculate combinations using the choose() function
  • For each possible degree d, count graphs where a node has degree d
  • Sum up all costs across all possible graphs
  • Apply modulo arithmetic to handle large numbers

Implementation

def choose(n, k):
    """Calculate nCk (n choose k) combinations"""
    if k > n or k < 0:
        return 0
    
    product = 1
    for i in range(n, n-k, -1):
        product *= i
    for i in range(1, k+1):
        product //= i
    return product

def util(d, n):
    """Calculate number of graphs where a specific node has degree d"""
    return choose(n-1, d) * (2 ** choose(n-1, 2))

def solve(n, k):
    """Find sum of costs of all simple undirected graphs with n nodes"""
    MOD = 1005060097
    total = 0
    
    for d in range(n):
        # Add contribution of all nodes with degree d
        total += util(d, n) * (d ** k)
        total %= MOD
    
    # Multiply by n (total number of nodes across all graphs)
    return (total * n) % MOD

# Test the solution
n = 3
k = 2
result = solve(n, k)
print(f"Sum of costs for n={n}, k={k}: {result}")
Sum of costs for n=3, k=2: 36

How It Works

The algorithm works by:

  • choose(n, k): Calculates binomial coefficients using multiplication and division
  • util(d, n): Counts how many graphs have a node with degree d by choosing d neighbors from n-1 possible nodes
  • solve(n, k): Iterates through all possible degrees (0 to n-1) and sums the contributions

Key Points

  • We use integer division (//) instead of float division to maintain precision
  • The modulo operation is applied at each step to prevent integer overflow
  • The factor 2C(n-1,2) accounts for all possible edge configurations
  • Final multiplication by n accounts for summing costs across all nodes

Conclusion

This solution efficiently calculates the sum of costs across all possible simple undirected graphs using combinatorics and modular arithmetic. The time complexity is O(n2) due to the nested loops in the combination calculation.

Updated on: 2026-03-26T18:23:49+05:30

421 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements