Program to find out special types of subgraphs in a given graph in Python

A special graph is defined as having exactly one "head" vertex connected to exactly t "feet" vertices. Given an undirected, unweighted graph, we need to find how many such special subgraphs can be formed where each subgraph uses disjoint vertices (no vertex appears in multiple subgraphs).

The key insight is that feet vertices have degree 1 (connected only to their head), while head vertices have degree equal to the number of feet they connect to.

Special Graph Structure Head Foot Foot Foot Special graph with t=3 feet Head has degree 3, each foot has degree 1

Algorithm Steps

The algorithm works by:

  • Building an adjacency list representation of the graph
  • Identifying potential feet (vertices with degree 1)
  • Grouping feet by their connected head vertex
  • Counting excess feet that cannot form complete special graphs

Example

Let's trace through the algorithm with a sample graph ?

def solve(n, t, edges):
    # Build adjacency list
    graph = [[] for _ in range(n + 1)]
    for edge in edges:
        u, v = edge
        graph[u].append(v)
        graph[v].append(u)
    
    # Find feet vertices (degree 1) and group by their head
    head_to_feet = {}
    available_nodes = n
    
    for i in range(1, n + 1):
        degree = len(graph[i])
        if degree == 1:
            # This is a foot vertex
            head = graph[i][0]
            if head not in head_to_feet:
                head_to_feet[head] = []
            head_to_feet[head].append(i)
        elif degree == 0:
            # Isolated vertex cannot be used
            available_nodes -= 1
    
    # Count excess feet that cannot form complete special graphs
    excess_feet = 0
    for head in head_to_feet:
        num_feet = len(head_to_feet[head])
        if num_feet > t:
            excess_feet += num_feet - t
    
    # Maximum special graphs = available nodes - excess feet
    return available_nodes - excess_feet

# Test with the given example
edges = [(1,4), (2,4), (3,4), (5,3), (6,3)]
result = solve(6, 2, edges)
print(f"Maximum special subgraphs: {result}")
Maximum special subgraphs: 4

How It Works

The algorithm identifies that:

  • Vertices 1, 2 are feet connected to head 4
  • Vertices 5, 6 are feet connected to head 3
  • With t=2, each head can form one special graph using exactly 2 feet
  • Since both heads have exactly 2 feet, no excess feet exist
  • Total nodes available: 6, excess feet: 0, so maximum special graphs: 6 - 0 = 6

Complete Implementation

def count_special_subgraphs(n, t, edges):
    """
    Count maximum number of special subgraphs in a graph.
    
    Args:
        n: Number of vertices
        t: Number of feet required per special graph
        edges: List of edges as tuples
    
    Returns:
        Maximum number of vertex-disjoint special subgraphs
    """
    # Build adjacency list (1-indexed)
    graph = [[] for _ in range(n + 1)]
    for u, v in edges:
        graph[u].append(v)
        graph[v].append(u)
    
    # Group feet vertices by their head
    head_to_feet = {}
    available_nodes = n
    
    for vertex in range(1, n + 1):
        degree = len(graph[vertex])
        
        if degree == 1:
            # Foot vertex - connected to exactly one head
            head = graph[vertex][0]
            if head not in head_to_feet:
                head_to_feet[head] = []
            head_to_feet[head].append(vertex)
            
        elif degree == 0:
            # Isolated vertex cannot be used
            available_nodes -= 1
    
    # Calculate excess feet
    excess_feet = 0
    for head, feet_list in head_to_feet.items():
        if len(feet_list) > t:
            excess_feet += len(feet_list) - t
    
    return available_nodes - excess_feet

# Example usage
edges = [(1,4), (2,4), (3,4), (5,3), (6,3)]
n, t = 6, 2

result = count_special_subgraphs(n, t, edges)
print(f"Graph has {n} vertices, each special graph needs {t} feet")
print(f"Maximum special subgraphs possible: {result}")
Graph has 6 vertices, each special graph needs 2 feet
Maximum special subgraphs possible: 4

Conclusion

This algorithm efficiently finds the maximum number of special subgraphs by identifying feet vertices (degree 1) and grouping them by their head vertices. The solution runs in O(V + E) time complexity where V is vertices and E is edges.

Updated on: 2026-03-26T14:22:29+05:30

549 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements