Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Negative Loop

Learn
Play
Prove
Problem context and objectives
Mission briefing

Realm network architect · Routes have costs, dependencies, and unreliable bridges

Choose the next frontier that preserves the route guarantee: The Negative Loop.

One locally tempting edge can invalidate the whole journey.

How you win

  1. 1Recognize when Bellman-Ford cycle detection matches the clues
  2. 2Keep this true after every move: after r rounds, distances cover every source path using at most r edges
  3. 3Reach the result within O(VE)

Rules and pressure

  • Target cost: O(VE)
  • State rule: after r rounds, distances cover every source path using at most r edges
Lesson 1 of 3

Live algorithm trace

Bellman-Ford cycle detection

Complete execution
1 of 5
0-1:1
edge
1-2:-2
2-1:-2

Initialize source 0 before any relaxation.

1dist[src] = 0
2relax all edges n - 1 times
3stop early if no update
4scan every edge once more
5if any reachable edge improves: return true
6return false
source = 0dist = [0,inf,inf]
Truth to preserve / Cost target
Truth to preserve

after r rounds, distances cover every source path using at most r edges

Cost target

O(VE)

Any simple path has at most n minus one edges. If a reachable distance can still improve on one extra relaxation pass, the improvement must depend on a reachable negative-weight cycle.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead