Advanced maths

Chinese postman route calculator for a weighted graph

The Chinese postman problem asks for a shortest closed walk that traverses every edge at least once.

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

The Chinese postman problem asks for a shortest closed walk that traverses every edge at least once. For a connected undirected graph, this calculator pairs odd-degree vertices by minimum shortest-path cost, duplicates those paths and constructs an Euler route on the augmented multigraph. The added length is shown separately.

THE FORMULApostman length=Σ edge lengths+minimum pairing cost among odd-degree vertices.

A worked example

A three-vertex path has two unit edges and two odd endpoints. Returning to the start while covering both edges requires repeating the path, so the optimum closed walk has length four. Its original edge sum is two and its required repeated length is two.

Good to know

Connected simple undirected input, nonnegative edge lengths, at most 20 vertices and 16 odd-degree vertices. Closed routes only. This covers edges rather than visiting every vertex once, and does not solve directed or rural-postman variants.

A postman route covers edges rather than only vertices

A closed postman walk must return to its start and include every original edge at least once. Visiting each vertex is insufficient when some connecting edges have not been traversed. This is why a traveling-salesman route or a generic shortest path does not answer the same coverage question, even on a small network.

The graph must be connected after all supplied vertices are considered. An isolated listed vertex makes the spanning closed-edge-coverage model inconsistent with this implementation’s connected input requirement. Edge lengths are nonnegative, preventing artificial benefit from looping repeatedly around a negative-cost route before performing the required service.

A closed postman walk repeats a three-vertex path012Unit edge lengthsBase length: 2Repeated length: 2Route total: 4
A three-vertex path has two unit edges and two odd endpoints. Returning to the start while covering both edges requires repeating the path, so the optimum closed walk has length four. Its original edge sum is two and its required repeated length is two.

The Euler path and circuit calculator from a graph edge list covers the nearby task. An Euler checker uses each edge once; postman routing adds optimal repeated traversals when degrees are odd.

Odd vertices identify required repetitions

A connected graph already has an Euler circuit when every vertex has even degree. Otherwise, its odd-degree vertices must receive extra incident edge traversals in pairs. Shortest-path distances between odd vertices define a finite minimum pairing problem, and those paired paths supply the least additional length for this undirected closed variant.

Adding an edge traversal does not create a new physical connection in the original network. It duplicates an existing route segment in the augmented multigraph. The result therefore separates base edge cost from repeated cost and lists the duplicated shortest paths, allowing a reader to inspect exactly where the extra travel occurs.

The augmented walk verifies the construction

After augmentation, the Euler construction uses every original and duplicated edge copy once. The returned vertex walk has one more vertex entry than its number of traversed copies. Coverage checks verify that no edge copy was omitted while following the final route, supplementing the independent cost calculation from the odd-vertex pairing.

A path example has only two odd vertices, so the necessary repeated route is obvious. Larger reference tests compare exhaustive odd-vertex pairings and recompute walk length from original edge weights. The method does not support directed imbalance, open endpoints or rural-postman subsets, each of which changes the route constraints and optimization problem.

Why odd degrees determine the repeated travel

A connected undirected graph has a closed Euler walk using every edge once exactly when every vertex degree is even. If some degrees are odd, additional copies of paths must make all degrees even before a closed walk is possible. Every duplicated path toggles parity at its two endpoints, so the odd vertices must be paired.

The shortest closed postman route starts with the sum of all original edge lengths, adds a minimum-cost pairing of odd vertices using shortest-path distances, then follows an Euler route in the augmented multigraph. The extra copies are traversal instructions; the input itself remains a simple graph and does not need duplicate edge rows.

Check a weighted path and a cycle

For edges 0–1 length 2 and 1–2 length 3, original total length is 5 and vertices 0 and 2 are odd. Their shortest connecting path has length 5, so the required repeated length is also 5. A closed route 0,1,2,1,0 has length 10 and covers both original edges.

For a triangle with all three edge lengths one, all degrees are even. No repeats are needed and the closed route length is 3. Adding a zero-length edge still changes vertex degrees and topology even though it adds no length. Inspect both the repeated-path table and the returned closed walk when auditing a result.

Read the route as edge coverage rather than vertex visitation

The postman problem requires every original edge to be traversed at least once. A traveling-salesperson tour instead focuses on visiting vertices and can omit many original edges. A visually short vertex tour is therefore not evidence that this edge-covering route is too long. Likewise, the open postman variant can have different endpoint parity and a smaller total than the closed version here.

The listed vertex sequence can repeat vertices and edges legitimately. Consecutive entries specify a traversal of an original edge or an added copy along an original shortest path. Different Euler walks through the same augmented graph can have the same optimal length, so the exact vertex ordering is not usually unique.

Keep the assumptions consistent with a real route

All entered vertices must belong to one connected graph, and lengths must be nonnegative. A disconnected road system cannot be covered by one continuous walk without adding connecting travel. One-way streets need a directed formulation; changing a direction into an undirected link can produce an impossible practical route.

The form permits at most 16 odd-degree vertices because it solves their pairing exactly. That bound is on the difficult pairing subproblem, not merely the total number of roads. Service times, turn restrictions, required subsets of edges and different vehicle start/end locations are not included. Add such costs outside the output only if doing so does not change which route would be optimal.

A few questions, answered

Is this traveling salesman?

No. Postman routing covers every edge and can repeat vertices and edges.

Why duplicate shortest paths?

They pair odd-degree vertices at minimum added cost so an Euler circuit becomes possible.

Why does the route revisit the same road?

Repeating paths can be necessary to make every degree even and return to the start after covering all edges.

Can it solve a route that ends elsewhere?

No. This form solves the closed undirected postman problem. Allowing a different endpoint changes the parity requirements and optimum.

Find your calculator

Start typing to explore the library.