Skip to the workspace
Pebellslearn by building
Graph Structure & Decomposition  ›  Capstone Project

Capstone: Meridian's Network Model

50 minPieces, backbone and weak points in one report

dsa / Capstone: Meridian's Network Model

Explore each scene at your own pace.

01 · Part 1: pieces and the backbone, in one pass

Part 1: pieces and the backbone, in one pass

Capstone: Meridian's Network Model

Definition
A network model summarises a graph with four answers: how many connected pieces it has, the cheapest backbone linking each piece (a minimum spanning forest), its bridges and its articulation points. Kruskal's algorithm with union-find gives the first two, and one low-link depth-first search gives the rest.
In plain words
It is a single health report for a road network: how many separate regions exist, the cheapest set of roads that keeps each region connected, and which single roads or junctions would split it if they closed.

In short: one sorted union-find pass answers two questions — how many pieces the network falls into, and what the cheapest backbone linking each piece costs — because Kruskal's algorithm is that pass, just with the roads sorted first. One low-link DFS over the same roads then answers two more: every critical road (a bridge) and every critical junction (an articulation point). Four numbers, one sort, one union-find pass and one DFS.

Two pieces: a triangle a, b, c with roads a-b (2) and b-c (1) kept for the backbone and a-c (3) rejected as a ring, plus a separate pair x-y (5) that is the whole second piece and its one critical road

Meridian's planners want one page about the road network before anything gets routed over it. Is the city one piece, or are there parts no van can reach? What would the cheapest backbone cost, the fibre that keeps every depot talking? And where are the weak points, the roads and junctions whose closure would split the city?

Every answer is something you've already built in this section. The capstone puts them together into one function, network_report, and shows that one union-find pass and one depth-first search are enough for all four numbers.

Part 1: pieces and the backbone, in one pass

A road network can be in several pieces: a new estate whose access road isn't finished, or an island reached only by ferry. Counting pieces is union-find: start with one piece per junction, and every road that joins two different groups leaves one fewer.

What does this print?
junctions = ["a", "b", "c", "d", "e", "f"]
roads = [("a", "b"), ("c", "d"), ("d", "e"), ("e", "c")]
parent = {j: j for j in junctions}

def find(x):
    while parent[x] != x:
        x = parent[x]
    return x

pieces = len(junctions)
for a, b in roads:
    ra, rb = find(a), find(b)
    if ra != rb:
        parent[ra] = rb
        pieces -= 1
print(pieces)

Now look at what that loop is doing. It's Kruskal's algorithm without the sort. Sort the roads by cost first and the same unions build the cheapest backbone. On a network in several pieces, Kruskal's never finds a road between them and simply returns one tree per piece, a minimum spanning forest. So one pass gives two answers: pieces is junctions minus successful unions, and the backbone cost is the sum of their costs.

The backbone isn't always the cheap roads, though:

What does this print?
roads = [("a", "b", 1), ("b", "c", 2), ("c", "a", 3), ("c", "d", 100)]
parent = {j: j for j in "abcd"}

def find(x):
    while parent[x] != x:
        x = parent[x]
    return x

total = 0
for a, b, cost in sorted(roads, key=lambda r: r[2]):
    ra, rb = find(a), find(b)
    if ra != rb:
        parent[ra] = rb
        total += cost
print(total)

That road to d is a critical road, and the second prediction is no accident.

Scene 1 of 9
Project context and further resources
You're building a piece ofMeridian — the network model
This piece — network_report(): One report on the whole road network: its pieces, its backbone, and its weak points.
Scenario Meridian's planners want one page about the road network before anything gets routed over it: is the city one piece, what would the cheapest backbone cost, and which roads and junctions are single points of failure. The network report ties the section's tools into that page.
network_report.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
network_report(junctions, roads) → dictPieces, backbone cost, critical roads and critical junctions of the road network.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.