About

Decrease by Constant & Combinatorial Objects

Decrease-and-Conquer by a Constant and Combinatorial Generation

Decrease-and-conquer exploits the relationship between a solution to a given instance of size nn and a solution to a smaller instance of size n−1n - 1 (decrease-by-a-constant, typically by 1).

Diagram loads as you scroll.


1. Insertion Sort

Insertion Sort sorts an array by iteratively inserting the next unsorted element into its proper position among the previously sorted elements:

InsertionSort(A[0..n-1]):
    for i = 1 to n - 1 do:
        v = A[i]
        j = i - 1
        while j >= 0 and A[j] > v do:
            A[j + 1] = A[j]
            j = j - 1
        A[j + 1] = v

Efficiency Analysis

Cworst(n)=∑i=1n−1i=n(n−1)2∈Θ(n2)C_{\text{worst}}(n) = \sum_{i=1}^{n-1} i = \frac{n(n-1)}{2} \in \Theta(n^2) Cbest(n)=∑i=1n−11=n−1∈Θ(n)C_{\text{best}}(n) = \sum_{i=1}^{n-1} 1 = n - 1 \in \Theta(n)

2. Topological Sorting

A topological sort of a Directed Acyclic Graph (DAG) is a linear ordering of its vertices such that for every directed edge (u,v)(u, v), vertex uu comes before vv in the ordering.

Method 1: DFS-Based Method (Reverse Postorder)

  1. Run Depth-First Search (DFS) on the DAG.
  2. Note the order in which vertices are popped off the traversal stack (traversal completion/dead ends).
  3. Reversing this order yields a valid topological sort.

Method 2: Source-Removal Method

  1. Identify a source vertex (a vertex with in-degree 0).
  2. Delete that source vertex and all its outgoing edges from the graph.
  3. Append the deleted vertex to the topological order.
  4. Repeat until all vertices are removed (or if no source exists, a cycle is detected).

3. Algorithms for Generating Combinatorial Objects

Generating Permutations: The Johnson-Trotter Algorithm

The Johnson-Trotter algorithm generates all n!n! permutations of {1,2,…,n}\{1, 2, \dots, n\} such that each subsequent permutation is obtained from the previous one by swapping a single pair of adjacent elements (minimal-change requirement).

Rules

  1. Assign each integer 1,…,n1, \dots, n an arrow indicating its direction: left (←\leftarrow) or right (→\rightarrow).
  2. Initially, set all directions pointing left:
←1 ←2 ←3 … ←n\leftarrow 1 \ \leftarrow 2 \ \leftarrow 3 \ \dots \ \leftarrow n
  1. An element kk is called mobile if its arrow points to an immediately adjacent neighbor with a strictly smaller value.
  2. Step:
    • Find the largest mobile integer mm.
    • Swap mm with the neighbor it points to.
    • Reverse the directions of all elements strictly greater than mm (k>mk > m).
    • Repeat until no mobile elements remain.

Visual Trace for n=3n = 3 (3!=63! = 6 Permutations)

StepStateLargest Mobile ElementAction
1←1 ←2 ←3\leftarrow 1 \ \leftarrow 2 \ \leftarrow 33 (points to 2)Swap 3 and 2
2←1 ←3 ←2\leftarrow 1 \ \leftarrow 3 \ \leftarrow 23 (points to 1)Swap 3 and 1
3←3 ←1 ←2\leftarrow 3 \ \leftarrow 1 \ \leftarrow 22 (3 is not mobile; 2 points to 1)Swap 2 and 1; reverse direction of 3>2  ⟹  →33 > 2 \implies \rightarrow 3
4→3 ←2 ←1\rightarrow 3 \ \leftarrow 2 \ \leftarrow 13 (points to 2)Swap 3 and 2
5←2 →3 ←1\leftarrow 2 \ \rightarrow 3 \ \leftarrow 13 (points to 1)Swap 3 and 1
6←2 ←1 →3\leftarrow 2 \ \leftarrow 1 \ \rightarrow 3None mobile!Algorithm terminates (6 permutations generated)

Generating Subsets: Binary Reflected Gray Code

A Gray code lists all 2n2^n subsets of an nn-element set such that each subset differs from its predecessor by the inclusion or exclusion of exactly one item.

Recursive definition:

G2=[00,01,11,10]G_2 = [00, 01, 11, 10] G3=[000,001,011,010,110,111,101,100]G_3 = [000, 001, 011, 010, 110, 111, 101, 100]

Exercises

Exercise 1

Apply the Johnson-Trotter algorithm from state ←2 ←4 ←3 ←1\leftarrow 2 \ \leftarrow 4 \ \leftarrow 3 \ \leftarrow 1. Identify all mobile elements, the largest mobile element, and the next permutation.

Show solution ↓
Solution

Examine each element:

  • 1: Points left to 3 (1<31 < 3), not mobile.
  • 3: Points left to 4 (3<43 < 4), not mobile.
  • 4: Points left to 2 (4>24 > 2), mobile!
  • 2: Points left out of bounds, not mobile.

Largest mobile element: 4.

  • Swap 4 with 2   ⟹  ←4 ←2 ←3 ←1\implies \leftarrow 4 \ \leftarrow 2 \ \leftarrow 3 \ \leftarrow 1.
  • Reverse directions of all elements >4> 4: None exist.

Resulting state: ←4 ←2 ←3 ←1\leftarrow 4 \ \leftarrow 2 \ \leftarrow 3 \ \leftarrow 1.

Exercise 2

Why does Insertion Sort outperform Bubble Sort on partially sorted data with kk inversions?

Show solution ↓
Solution

The number of key comparisons in Insertion Sort is bounded by n−1+kn - 1 + k, where kk is the number of inversions. When an array is nearly sorted (k=O(n)k = O(n)), Insertion Sort executes in linear time Θ(n)\Theta(n). Bubble Sort still requires full scans and multiple adjacent swaps, incurring substantial branch and memory overhead.