Example usage

  Blog    |     January 28, 2026

The "Hidden Subcontract Chain" refers to a scenario where a main contractor hires subcontractors, who in turn hire their own subcontractors, creating a multi-tiered hierarchy. The goal is to trace the chain of subcontracting relationships from the main contractor down to the end subcontractors (leaf nodes). This chain is "hidden" because the relationships may not be immediately obvious and require systematic traversal to uncover.

Key Concepts:

  1. Directed Graph: Represent subcontractor relationships as a directed graph where edges point from a contractor to its subcontractor.
  2. Leaf Nodes: End subcontractors with no further hires (no outgoing edges).
  3. Path Traversal: Use Depth-First Search (DFS) to explore all possible paths from the main contractor to leaf nodes.

Solution Approach:

  1. Graph Construction: Build an adjacency list from the subcontractor relationships.
  2. DFS Traversal: Starting from the main contractor, recursively explore all paths:
    • Avoid Cycles: Track nodes in the current path to prevent infinite loops.
    • Leaf Check: When a node has no outgoing edges, record the path as a complete chain.
  3. Output: Return all valid chains from the main contractor to leaf nodes.

Example:

Input:

  • Subcontractor relationships: [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'E'), ('C', 'F')]
  • Main contractor: 'A'

Graph:

A → B → D
A → C → E
A → C → F

Chains:

  1. A → B → D
  2. A → C → E
  3. A → C → F

Python Implementation:

def find_subcontract_chains(edges, start):
    # Build adjacency list
    graph = {}
    for u, v in edges:
        if u not in graph:
            graph[u] = []
        graph[u].append(v)
        if v not in graph:
            graph[v] = []  # Ensure leaf nodes are in the graph
    results = []
    stack = [(start, [start])]  # (current_node, path_so_far)
    while stack:
        node, path = stack.pop()
        # If node has no outgoing edges, it's a leaf
        if not graph.get(node, []):
            results.append(path)
        else:
            for neighbor in graph[node]:
                if neighbor not in path:  # Avoid cycles
                    new_path = path + [neighbor]
                    stack.append((neighbor, new_path))
    return results
edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'E'), ('C', 'F')]
start = 'A'
print(find_subcontract_chains(edges, start))
# Output: [['A', 'C', 'F'], ['A', 'C', 'E'], ['A', 'B', 'D']] (order may vary)

Explanation:

  1. Graph Construction: The adjacency list graph maps each contractor to its list of subcontractors.
  2. DFS with Stack:
    • Stack Initialization: Start with the main contractor and its path.
    • Processing Nodes: For each node, if it has no subcontractors (leaf), add the path to results.
    • Recursive Exploration: For each subcontractor, if not already in the current path, extend the path and push it onto the stack.
  3. Cycle Avoidance: The condition neighbor not in path ensures no node is revisited in the same path, preventing infinite loops.

This approach efficiently uncovers all hidden subcontract chains by leveraging DFS and careful path management. The solution handles cycles and ensures all valid paths are identified.


Request an On-site Audit / Inquiry

SSL Secured Inquiry