Zero-Rematch Double Elimination, Berger Round-Robin & Rotating-Partner Sit-Out Algorithms in TypeScript

Building a browser-first tournament and court scheduler looks deceptively simple until real-world...

haitengli (Open-Source)4 min read0 kali dilihat
Zero-Rematch Double Elimination, Berger Round-Robin & Rotating-Partner Sit-Out Algorithms in TypeScript

Building a browser-first tournament and court scheduler looks deceptively simple until real-world club edge cases hit: 12 teams showing up for a 16-slot knockout draw, Double Elimination losers colliding with their Round 1 opponent again in Round 2 of the Lower Bracket, or 10 Pickleball / Padel players sharing 2 courts where static laminated charts break down immediately.

Here are the four deterministic TypeScript algorithms we used when engineering the client + Cloudflare Workers engine behind CourtBracket.com.


1. Recursive NCAA/ATP Fold Seeding & First-Round BYEs

To guarantee Seed #1 and Seed #2 sit on opposite halves of a knockout bracket—and Seeds #1..#4 occupy distinct quadrants—expand iteratively from [1, 2] up to B = 2^ceil(log2(N)) by folding each seed s against (layerSize + 1 - s):

export function buildFoldedSeedOrder(bracketSize: number): number[] {
  let seeds = [1, 2];
  while (seeds.length < bracketSize) {
    const nextSize = seeds.length * 2;
    const expanded: number[] = [];
    for (const s of seeds) {
      expanded.push(s, nextSize + 1 - s);
    }
    seeds = expanded;
  }
  return seeds;
}

When N < B (for example, a 12-team draw mapped onto B = 16), every seed index > N (13..16) becomes a Round 1 BYE. Because 16 + 1 - s > 12 holds strictly for s ∈ {1, 2, 3, 4}, the top 4 seeds automatically receive first-round walkovers and Round 2 begins with an exact 8-team Quarterfinal field (see the live 16-Team & 12-Team Single Elimination Bracket Generator).


2. Anti-Rematch Cross-Flipping in Double Elimination

If Winners Bracket Round 1 losers fill the Lower Bracket top-to-bottom and Winners Round 2 losers also drop straight down top-to-bottom, players from the same upper quadrant immediately rematch in Lower Round 2.

Cross-flipping incoming drop-down vectors on alternating waves prevents premature rematches:

export function routeDropDownWave<T>(incomingLosers: T[], waveIndex: number): T[] {
  if (incomingLosers.length <= 1) return incomingLosers;
  if (waveIndex % 2= 1) {
    return [...incomingLosers].reverse();
  }
  const half= Math.floor(incomingLosers.length / 2);
  return [...incomingLosers.slice(half), ...incomingLosers.slice(0, half)];
}

And in the Grand Finals, true two-loss elimination requires conditionally activating Grand Finals Game 2 (Bracket Reset) only when the 1-loss Lower Bracket finalist defeats the undefeated Upper Bracket finalist in Game 1.


3. Berger Circle Polygon Rotation for Odd & Even Round Robins

For N teams in an all-play-all league, add a virtual BYE vertex if N is odd so M = N + (N % 2) is even. Pin index 0 and rotate indices 1..M-1 clockwise across M - 1 rounds:

export function bergerRounds(ids: string[]): Array<Array<[string, string]>> {
  const list = ids.length % 2 === 1 ? [...ids, "__BYE__"] : [...ids];
  const m = list.length;
  const rounds: Array<Array<[string, string]>> = [];

  let ring = list.slice(1);
  for (let r = 0; r < m - 1; r++) {
    const cur= [list[0], ...ring];
    const pairs: Array<[string, string]> = [];
    for (let i = 0; i < m / 2; i++) {
      const a= cur[i];
      const b= cur[m - 1 - i];
      if (a = "__BYE__" && b = "__BYE__") {
        pairs.push(r % 2= 0 ? [a, b] : [b, a]);
      }
    }
    rounds.push(pairs);
    ring= [ring[ring.length - 1], ...ring.slice(0, -1)];
  }
  return rounds;
}

4. Rotating-Partner Doubles & Sit-Out Fairness (max - min <= 1)

In social racket mixers (Pickleball Rotating Doubles Round Robin and 24/32-point Padel Americano & Mexicano), court capacity 4 * courts is often smaller than headcount N.

  1. Sit-Out Invariant: Before each round, sort players ascending by (sitOutCount, -matchesPlayed, seed) and assign the N - 4 * activeCourts players with the lowest sit-out counts to play—mathematically bounding bench variance to <= 1 across the entire session.
  2. Partner & Opponent Cost Function: Partition the 4 * activeCourts active players into 4-player pods and evaluate each pod's 3 possible 2v2 splits (AB vs CD, AC vs BD, AD vs BC) with cost = 100 * repeatPartnerCount + 12 * repeatOpponentCount.
  3. Dynamic Mexicano Pods: After each round is scored, sort the live individual leaderboard and group #1..#4 onto Court 1 (#1 & #3 vs #2 & #4), #5..#8 onto Court 2, and so forth so rallies tighten naturally every round.

Sumber artikel open-source oleh haitengli di DEV Community — Baca artikel asli. Topik: #typescript #algorithms #webdev #javascript

Sukai & bagikan artikel ini

Komentar