mediumAdvanced GraphsLinkedInGoogle
Grid-Point Spanner
A fiber contractor connects every village (point) at minimum trenching cost using Manhattan distance between any pair.
Given n points, connect all with minimum total cost where cost between two points is their Manhattan distance.
Choose approach
(0,0)
u
(2,2)
(3,10)
(5,2)
(7,0)
1/5
Narrationstep 1 of 5
Add point 0 with min-edge cost 0. Running total = 0.
Variables
added=0
cost=0
total=0
How to think about it
- Complete graph on n points -> n² edges; Prim's dense is cheapest.
- `dist[v]` = minimum edge from any inMST node to v so far.
- Each iteration admits one node and relaxes its neighbors.
Edge cases to catch in interview
- Single point -> 0.
- Collinear points -> MST is the straight chain.
Examples
Example 1
3{"points":[[0,0],[1,1],[1,0],[0,1]]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed