Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 08 · DSA · Worked problems, in English · about 17 min to read

Play it as a journeyTutor comes with a plan

Clinic #26: Redundant Connection

Graphs
Graphs · Open board

Dots and the lines between them, and in its last panel the tool this clinic is about: every dot points at another dot, the one at the top of each chain names the group, and two short questions (“walk up to the top”, “point one top at the other”) answer whether two dots are already joined.

Before you start. A graph is a set of dots joined by lines; Graphs (BFS, DFS, Dijkstra, topological sort) builds it from scratch, and here the dots are houses and the lines are cables. Engineers write the cost of reading every item in a list once as O(n), said “order n”: double the list, double the work. A full walk of the list for every item is O(n²): double the list, four times the work, and at ten thousand items a hundred million steps.

Lesson 65 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • The cable crew: one cable too many
  • By hand: five houses, five cables, and the word cycle
  • The wrong first idea: “both houses already have a cable”
  • The tell: a tree plus one edge, read in order
  • Every house points at another: the parent, and the root at the top
  • find and union: two questions that do all the work
  • The invariant: one group per root, before every cable
  • Brute force: pull each cable and check what is left · O(n²)
  • Better: before each cable, walk to see if it is needed · O(n²)
  • Best: union-find with short chains · O(n α(n)), effectively O(n)
  • Four logs, four answers
  • Timings
  • Why the two habits matter: the star log
  • In an interview
  • The union-find ladder: easy, medium, hard

Terms it explains: O(n), O(n²), cycle, tree, node, edge, parent, root, find(x), union(a, b), union-find, find, union, Path compression, O(n α(n)), best, twelve lines, Number of Provinces, Redundant Connection II, A*, adjacency list, adjacency matrix, Bellman-Ford, BFS, DAG, DFS, Dijkstra, directed vs undirected, in-degree, MST, relax an edge, sparse vs dense, topological sort, vertex/edge, weighted.

Lab

Every find walks a chain of houses up to the root. In the worst order, how long can that chain get, and what keeps it short?

Drag Cables joined to the end, then switch The habits and watch Tallest chain fall from 31 to 5.

Open The short chains

Review DSA