Advanced maths

K shortest loopless paths calculator between two vertices

K shortest loopless paths expose several distinct route alternatives rather than only one optimum.

Your numbers

Vertices are indexed 0 through n−1.
Rows from,to,weight; absent weight means one. Leave empty for no edges.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

K shortest loopless paths expose several distinct route alternatives rather than only one optimum. This bounded calculator enumerates simple paths, orders them by total cost and returns the requested prefix with a reproducible tie rule. An explicit search-state limit prevents an incomplete enumeration from being presented as a complete ranking.

THE FORMULAEnumerate simple source-to-target paths; sort by total weight, retaining the first k.

A worked example

In a directed diamond with edges 0→1→3 and 0→2→3, every edge has unit cost. There are exactly two source-to-destination simple paths and both cost two. Asking for five returns those two, not repeated copies or invented looped routes.

Good to know

At most 12 vertices, nonnegative weights, k from one to 50 and 200,000 search states. Paths may not repeat vertices. Equal-cost ties use string-lexicographic vertex sequences. No negative-edge or large-network performance guarantee.

Loopless alternatives are distinct complete paths

A loopless path cannot repeat a vertex. This restriction prevents a zero-cost or low-cost cycle from generating arbitrarily many variants of the same route. The start and destination may be chosen from the supplied vertex indices; when they coincide, the supported simple path is the zero-edge path rather than a tour around the graph.

The direction selector determines whether an input row can be followed only from its first endpoint to its second, or in either direction. A route valid in undirected mode may disappear in directed mode. The same row list should therefore not be compared across the two modes without accounting for the changed reachability constraints.

A diamond has two distinct equal-cost loopless routes01230 → 1 → 30 → 2 → 3Each route cost: 2
In a directed diamond with edges 0→1→3 and 0→2→3, every edge has unit cost. There are exactly two source-to-destination simple paths and both cost two. Asking for five returns those two, not repeated copies or invented looped routes.

The Shortest path calculator for a weighted graph edge list covers the nearby task. Existing tool returns one optimal route rather than enumerating distinct loopless alternatives.

Equal-cost paths need a declared ordering

Total path cost is the sum of its edge weights. Different simple paths can tie exactly, as in the diamond example. The page uses a deterministic string-lexicographic vertex-sequence tie rule so the displayed first k routes remain reproducible; that rule is not a statement that one equal-cost route is operationally better.

If fewer than k paths exist, the result reports the available count and returns each existing route once. It does not duplicate paths to fill the request. Conversely, if the graph has many paths, the total enumeration count describes the completed bounded search, not only the number of rows selected for presentation.

Complete enumeration supports the reported ranking

Enumerating every simple path can grow very quickly even with a small vertex count. The state cap terminates excessive cases with an explicit error rather than returning the routes found so far under an unjustified shortest-first label. A partial depth-first search does not establish the ranking of unexplored alternatives.

Independent tests enumerate tiny route families with another traversal and compare a standard k-shortest-simple-path implementation. They verify endpoint membership, no repeated vertices, cost ordering and tie handling. This initial exact small-graph approach does not claim the scalability of a dedicated large-network ranked-path algorithm or support negative-edge variants.

Define loopless before asking for several alternatives

A loopless route, also called a simple path, never visits a vertex twice. Different routes can share vertices or edges with each other; the restriction applies within each individual route. The selected start and end define the ordered route direction, and the directed/undirected control determines which edge traversals are allowed.

This differs from k shortest walks, where cycles can create many additional alternatives, and from edge-disjoint routing, where routes are forbidden to share links. The calculator enumerates all bounded simple candidates before sorting them, so the returned ranking is complete within the supported graph rather than the first k paths encountered by a search.

Rank a diamond with an added direct connection

Use directed edges 0→1 cost 1, 1→3 cost 1, 0→2 cost 1, 2→3 cost 1 and 0→3 cost 3. From 0 to 3, the two cheapest routes are 0,1,3 and 0,2,3, both costing 2. The third is 0,3 with cost 3. Requesting five returns only these three if no other paths exist.

A positive-cost edge 1→0 would not create a valid route 0,1,0,3 because vertex zero repeats. A direct edge can be more expensive than a longer vertex sequence; route cost is the sum of weights, not its number of vertices. Use the table’s cost column to compare alternatives rather than visual route length.

Understand equal costs and reproducible ordering

Equal-cost candidates are ordered by a string-lexicographic representation of their zero-based vertex sequences. That gives a deterministic display but is not a second optimization criterion with practical meaning. With multi-digit vertex labels, string order can differ from numeric order; for example, the text label 10 can sort before 2.

If several routes tie at the kth cost, the form returns the requested count rather than promising every route in that tied-cost class. Increase k within the supported limit if you want to inspect more ties. The total enumerated-route metric helps show whether fewer returned routes reflect a small candidate set rather than a failed search.

Recognize combinatorial growth and application limits

Even a small dense graph can contain many simple routes. The implementation stops with an error if enumeration exceeds 200,000 states, instead of presenting a partial ranking as complete. The vertex cap and k cap alone do not prevent that growth. A smaller k does not necessarily reduce this exhaustive search to a cheap calculation.

Nonnegative weights are required. Travel time depending on arrival time, capacities, turn restrictions and maximum-hop rules are not modeled. If a route is intended as a resilient backup, inspect shared links yourself: the second cheapest path may reuse almost the entire cheapest path. Cost diversity and failure diversity are different design goals.

A few questions, answered

Are repeated vertices allowed?

No. Every returned alternative is a simple loopless path.

What if fewer than k paths exist?

The page returns the complete available family up to k without duplicating routes.

Are the returned alternatives edge-disjoint?

No. Each path is loopless internally, but different paths may share edges and vertices. Disjoint routing is a different optimization problem.

Why can requesting five return fewer routes?

The graph may contain fewer than five simple start-to-end paths. A completed enumeration reports the available candidates rather than inventing repeated routes.

Find your calculator

Start typing to explore the library.