Skip to content

R50-T007: Publish a runnable example for DisjointSetTreeNode (KruskalMST.js) [Graphs/KruskalMST.js] - #105

Open
stavansomaiya wants to merge 1 commit into
S-AI-Projects:masterfrom
stavansomaiya:student/e26b07f1064/R50-T007
Open

stavansomaiya wants to merge 1 commit into
S-AI-Projects:masterfrom
stavansomaiya:student/e26b07f1064/R50-T007

Conversation

@stavansomaiya

@stavansomaiya stavansomaiya commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Task R50-T007 · task 7 of 10 · Executable documentation
Issue #7 · Publish a runnable example for DisjointSetTreeNode
Student Stavan Somaiya · URN e26b07f1064
File → function Graphs/KruskalMST.js → DisjointSetTreeNode (line 1 at the pinned commit, line 35 after this PR)
Pinned source TheAlgorithms/JavaScript @ 5c39e87 · KruskalMST.js#L1
Branch stavansomaiya:student/e26b07f1064/R50-T007 → S-AI-Projects:master (one commit on 5c39e87)
Outcome A runnable @example is added and executed by a test that asserts its printed output. The implementation is unchanged.

Summary

Adds executable documentation for DisjointSetTreeNode in Graphs/KruskalMST.js. The JSDoc @example goes from a new node, through two unions, to the final parents and ranks. It is executed by a test, and the displayed output is asserted.

Decisive state transition: linking two roots of equal rank (DisjointSetTree.link, lines 73-75) is the only step that raises a rank. Every other step follows parent links or hangs a shorter tree under a taller one with no rank change.

Files changed

File Lines What changed
Graphs/KruskalMST.js +39 / −2 JSDoc with runnable @example; two classes exported (no logic change)
Graphs/test/KruskalMST.test.js +118 / −0 new test file: runs the doc example, hand-calculated states, MST example
work/R50-T007/ (9 files) evidence check.py runner, README.md write-up, evidence.mjs, results.json and logs; coursework only, kept out of any upstream PR

Related Issue

Fixes: #7

This PR covers only R50-T007. Each of the 10 tasks has its own branch, commit and PR; see All 10 tasks under Additional Notes.

Type of Change

  • Bug Fix
  • New Feature
  • UI/UX Improvement
  • Performance Improvement
  • Tests
  • Documentation
  • Refactoring / Maintenance
  • CI/CD

Changes Made

  • Graphs/KruskalMST.js: JSDoc and a runnable @example for DisjointSetTreeNode. DisjointSetTreeNode and DisjointSetTree are now also exported, so the example imports them by their real names. GraphWeightedUndirectedAdjacencyList is unchanged and there is no logic change.
  • Graphs/test/KruskalMST.test.js (new; there was no test file before). It reads the @example from the source, checks that its import line names real exports, runs it, and asserts the printed lines a true 0, b 1, b 1, 0 0. It also asserts the hand-calculated states (union('a','b') → a.parent = b, b.rank = 1; union('c','a') → c under b, ranks unchanged). The commented-out MST example at the bottom of the file becomes an assertion.
  • work/R50-T007/: check.py, trace.txt (the traced execution, which matched the hand calculation), and evidence.mjs, which also runs the example verbatim with node (real import line) and compares stdout. Plus results.

Screenshots / Recordings

There is no UI, so the recording is the terminal output of a fresh run on 23 Nov 2026. The run used a clean checkout of this branch with a new npm ci from the lockfile. The output is copied verbatim; only the temporary checkout path is replaced by a label.

Focused tests: npx vitest run … --reporter=verbose, 5/5 passed
$ npx vitest run Graphs/test/KruskalMST.test.js --reporter=verbose
 RUN  v1.2.1 <clean checkout of student/e26b07f1064/R50-T007>
 ✓ Graphs/test/KruskalMST.test.js > DisjointSetTreeNode documentation example > imports real public names from this file
 ✓ Graphs/test/KruskalMST.test.js > DisjointSetTreeNode documentation example > prints exactly what the comments next to each console.log say
 ✓ Graphs/test/KruskalMST.test.js > DisjointSetTreeNode states (calculated by hand) > smallest valid input: a new node is the root of its own set
 ✓ Graphs/test/KruskalMST.test.js > DisjointSetTreeNode states (calculated by hand) > ordinary input: union of equal ranks, then unequal ranks
 ✓ Graphs/test/KruskalMST.test.js > KruskalMST example from the bottom of KruskalMST.js > keeps the 4 cheapest edges that do not form a cycle
 Test Files  1 passed (1)
      Tests  5 passed (5)
Automated check: python3 work/R50-T007/check.py, exit 0
$ python3 work/R50-T007/check.py
[PASS] smallest valid input: new node
       input:    "new DisjointSetTreeNode('a')"
       expected: {"key": "a", "parentIsSelf": true, "rank": 0}
       actual:   {"key": "a", "parentIsSelf": true, "rank": 0}
       source:   KruskalMST.js:38-39 parent = this, rank = 0
[PASS] after makeSet a, b, c
       input:    "makeSet x3"
       expected: {"parents": ["a", "b", "c"], "ranks": [0, 0, 0]}
       actual:   {"parents": ["a", "b", "c"], "ranks": [0, 0, 0]}
       source:   KruskalMST.js:50-53 makeSet creates a root node
[PASS] after union('a', 'b') — equal ranks (decisive transition)
       input:    "union('a', 'b')"
       expected: {"parents": ["b", "b", "c"], "ranks": [0, 1, 0]}
       actual:   {"parents": ["b", "b", "c"], "ranks": [0, 1, 0]}
       source:   KruskalMST.js:73-75 x.parent = y; equal ranks -> y.rank += 1
[PASS] after union('c', 'a') — ranks 0 vs 1
       input:    "union('c', 'a')"
       expected: {"parents": ["b", "b", "b"], "ranks": [0, 1, 0]}
       actual:   {"parents": ["b", "b", "b"], "ranks": [0, 1, 0]}
       source:   KruskalMST.js:68-78 lower rank goes below, no rank change
[PASS] KruskalMST on the example at the bottom of the file
       input:    "1-2:1, 2-3:2, 3-4:1, 3-5:100, 4-5:5"
       expected: {"1": {"2": 1}, "2": {"1": 1, "3": 2}, "3": {"2": 2, "4": 1}, "4": {"3": 1, "5": 5}, "5": {"4": 5}}
       actual:   {"1": {"2": 1}, "2": {"1": 1, "3": 2}, "3": {"2": 2, "4": 1}, "4": {"3": 1, "5": 5}, "5": {"4": 5}}
       source:   KruskalMST.js: KruskalMST uses DisjointSetTree to skip 3-5
[PASS] JSDoc @example run verbatim with node (stdout vs documented comments)
       input:    "14 example lines"
       expected: ["a true 0", "b 1", "b 1", "0 0"]
       actual:   ["a true 0", "b 1", "b 1", "0 0"]
       source:   KruskalMST.js:17-33 @example block
[PASS] documented comments match the pinned output
       input:    "comments after each console.log"
       expected: ["a true 0", "b 1", "b 1", "0 0"]
       actual:   ["a true 0", "b 1", "b 1", "0 0"]
       source:   KruskalMST.js:21,29,32,33
[KILLED] mutant rank grows by 2 on equal-rank link: '        y.rank += 1\n' -> '        y.rank += 2\n'
       failed:  DisjointSetTreeNode documentation example prints exactly what the comments next to each console.log say
       failed:  DisjointSetTreeNode states (calculated by hand) ordinary input: union of equal ranks, then unequal ranks

focused tests: 5/5 passed  (node_modules/.bin/vitest run Graphs/test/KruskalMST.test.js --reporter=json --outputFile=work/R50-T007/vitest-focused.raw.json)
npm test:      11975/11975 passed  (npm test -- --reporter=json --outputFile=work/R50-T007/vitest-full.raw.json)

R50-T007: all checks passed
(exit 0)

Testing

Automated check: python3 work/R50-T007/check.py installs from the lockfile and runs the focused tests. It then prints input / expected / actual for every fixture row, runs each deliberate mutant in a temporary copy, and finally runs the repository test script in non-watch mode. It exits non-zero if any assertion or step fails, and records every command in commands.log.

To reproduce:

git fetch https://github.com/stavansomaiya/TheAlgorithms-JavaScript.git student/e26b07f1064/R50-T007
git checkout FETCH_HEAD
python3 work/R50-T007/check.py

Recorded results (committed in results.json; tool versions in versions.txt):

Check Command Result
Install from lockfile npm ci --ignore-scripts --no-audit --no-fund ✅ exit 0
Focused tests vitest run Graphs/test/KruskalMST.test.js ✅ 5/5 passed
Fixture evidence (input / expected / actual / source line) node work/R50-T007/evidence.mjs ✅ 7/7 rows match
Mutant (temporary copy): rank grows by 2 on equal-rank link applied by check.py ✅ caught by prints exactly what the comments next to each console.log say
Repository test script, non-watch npm test (vitest run) ✅ 11975/11975 passed
Code style npm run check-style (Prettier) ✅ passes
Spelling (CI settings) codespell ✅ no new findings

Acceptance criteria → evidence:

# Done when (quoted from #7) Evidence in this PR
1 The checked behavior is tied to DisjointSetTreeNode in Graphs/KruskalMST.js; every assertion has an explicit expected value. ✅ The doc-example test asserts the literal printed lines a true 0, b 1, b 1, 0 0. The state tests assert literal parents and ranks, and the MST test asserts the literal kept edges.
2 Use the smallest valid input and one ordinary input for DisjointSetTreeNode from Graphs/KruskalMST.js. Write the literal inputs and independently calculated expected outputs in the tests before editing the implementation. The check must name the source line establishing each precondition. ✅ Smallest: new DisjointSetTreeNode('a') is its own parent with rank 0 (lines 38-39). Ordinary: union('a', 'b') then union('c', 'a'), worked out by hand in the README and confirmed by trace.txt. The rank rule is cited at lines 73-75.
3 Existing supported behavior and the public interface are preserved unless a failing regression demonstrates and documents a defect. ✅ GraphWeightedUndirectedAdjacencyList is unchanged, and the two classes are only newly exported. No logic change. The full suite passes (11975/11975).
4 The documentation example is executed by a check and its displayed output is asserted. ✅ prints exactly what the comments next to each console.log say reads the @example straight from the source, checks that its import names real exports, runs it, and compares each printed line with the documented one. evidence.mjs also runs the example verbatim with node.
5 The explanation identifies the decisive state transition rather than paraphrasing every line. ✅ The JSDoc names the decisive transition: linking two roots of equal rank (DisjointSetTree.link, lines 73-75) is the only step that raises a rank.

Evidence files: check.py · README.md (contract, fixture, baseline, findings) · evidence.mjs · results.json · vitest-focused.json

Checklist

  • I have tested my changes locally.
  • I have added or updated tests, if needed.
  • I have updated documentation, if needed.
  • I have followed the project's coding guidelines.
  • I have run the relevant tests and checks.
  • I have addressed previous review feedback, if applicable. (No review feedback yet.)

Additional Notes

Sensitivity check: changing y.rank += 1 to y.rank += 2 in a temporary copy makes the doc-example test fail (prints b 2, documented b 1).

Scope: the PR changes only Graphs/KruskalMST.js, its test file and work/R50-T007/. work/R50-T007/ is coursework evidence and would be removed from any later upstream PR.

All 10 tasks (one PR each)
Task Issue Branch Pull request
R50-T001: Lock down the contract of setBit #1 R50-T001 #99
R50-T002: Define the supported boundaries of checkKeywordValidity #2 R50-T002 #100
R50-T003: Cross-check getParentPosition #3 R50-T003 #101
R50-T004: Test an invariant of swap #4 R50-T004 #102
R50-T005: Make verification repeatable for newGeneration #5 R50-T005 #103
R50-T006: Simplify one responsibility in countLetters #6 R50-T006 #104
R50-T007: Publish a runnable example for DisjointSetTreeNode #7 R50-T007 #105 (this PR)
R50-T008: Cover the failure behavior of TrieNode #8 R50-T008 #106
R50-T009: Measure the work done by fibonacci #9 R50-T009 #107
R50-T010: Strengthen the tests of OpenKnightTour #10 R50-T010 #108

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stavan Somaiya — R50-T007: Publish a runnable example for DisjointSetTreeNode (KruskalMST.js) [Graphs/KruskalMST.js]

1 participant