Can the Message Get Through? — Shortest Paths & MST

Determine whether a message can travel between two machines in a small office network, possibly relaying through others.

Shortest Paths & MST · Easy

The problem

A network has n machines numbered 0 to n - 1 and some two-way cables. Each pair [a, b] in cables joins machine a and machine b in both directions. Work out whether a message can get from machine source to machine destination, passing through any number of other machines.

The story

A small office network has 1 to 8 machines numbered from 0, joined by up to 14 cables. Every cable carries traffic in both directions, and no two cables join the same pair. A technician needs to know whether a message can travel from one given machine to another, hopping across any number of machines on the way. A machine can always reach itself.

What to return

Constraints

Example

Input
n = 4, cables = [[0, 1], [1, 2]], source = 0, destination = 2
Output
true

Solve it

More in Shortest Paths & MST

All algorithm problems