Algorithm Engineering — L08
Graph vocabulary used in this lecture.
DAG (Directed Acyclic Graph):
Tree:
The topological structure of cycle-free graphs often allows efficient handling via dynamic programming, even for cases that would be hard on general graphs.
“A wise platypus will solve a graph problem with a graph algorithm, for LPs are more general, but graph algorithms are faster.”
— Confucypus
A minimum-weight path from a source to a target in a weighted graph.
A weighted graph \(G = (V, E)\) with edge weights \(w(u,v)\), a source \(s\), a target \(t\).
A path is a sequence of edges \[s = v_0 \to v_1 \to \dots \to v_k = t,\] and its length is \(\sum_i w(v_{i-1}, v_i)\).
The shortest path is one of minimum total length.
Some shifts are harder to fill than others.
In practice, this works very well with advanced pricing and column generation.
Per worker, create a DAG with the allowed transitions and the weights of the slots.
Negative weights to reward hard-to-fill slots.
The SP represents the best schedule for that worker, given the remaining demand.
DAG allows a linear DP here!
Keep a tentative distance on every node.





Tiny change to Dijkstra: add a heuristic underestimation of the remaining cost to the target.
\[f(v) = \underbrace{g(v)}_{\text{cost so far}} + \underbrace{h(v)}_{\text{estimate to } t}.\]
Dijkstra is A* with \(h \equiv 0\).
nx.astar_path(G, s, t, heuristic=h, weight="weight")
Query model
Graph assumptions
Engineering questions
With time-dependent costs, how long an arc takes depends on when you enter it (rush hour).
FIFO (“no overtaking”): arrival time is non-decreasing in departure time. Leaving later never lets you arrive earlier. Then the optimality principle holds and Dijkstra is unchanged: evaluate travel time at the current arrival time.
Dijkstra and A* need only two things: a successor function and a frontier, not the whole graph.
This sliding puzzle has ~180,000 states. A* only touched 283 by having a heuristic that lower bounds the distance to the solution.
Same structure as branch-and-bound, planning, motion planning: a graph you generate, never store. The heuristic aims at the goal configuration but is usually inexact.
Time-dependent costs look hard but are easy, negative edges and resource constraints look easy but are hard.
A very common subproblem in optimization is to find a cheapest path that respects a resource limit. That is the Resource-Constrained Shortest Path, and it is NP-hard.
Weakly NP-hard with one integer resource; strongly NP-hard with several.
A matching is a set of edges, no two sharing a vertex. Bipartite graphs admit a clean linear program; an odd cycle makes the problem harder.
A matching \(M \subseteq E\): no two edges share a vertex.
Two special cases:
Match compatible pairs of donors and patients, where the willing donor is not compatible to their own beneficiary. Maximize expected number of successful transplants.
See also the AlgLab exercise of a more complicated version: https://github.com/d-krupke/AlgLab-WS2526-material/tree/main/sheets/01_cpsat/exercises/03_organ_donor_problem
Edges carry costs. Five orders, five machines: each order runs on one machine, each machine takes one order. Minimize total cost. That is a minimum-cost perfect bipartite matching.
Pushing units through a capacitated network from a source to a sink.
Max flow
A capacitated network with source \(s\) and sink \(t\).
Goal: send as much flow as possible from \(s\) to \(t\).

Min-cost flow
A network with supplies/demands, capacities, and per-unit edge costs.
Goal: satisfy all balances at minimum total cost.

Same modeling primitive, different objective:
move flow subject to conservation and capacities.
Min-cost flow subsumes shortest path, assignment, transportation, and fixed-value flow problems.
A logistics company runs trucks between four depots (A, B, C, D) every day. Goods travel in reusable crates, and the empties piggy-back on regular deliveries — but each truck only has a few empty-crate slots per trip.
Demand is asymmetric, so over a few days some depots accumulate surpluses of empties while others run short and start delaying outbound deliveries.
Question. Given the surplus / shortage pattern and the truck slot capacities, how many crates can we redistribute in time to reduce the shortages — and what is the bottleneck?
Six tasks, fixed times, each at a station. Moving between stations costs a 30-minute changeover. Cover every task at minimum wage.
Flow on the network
Residual graph (find a path, push, repeat)
Step 3’s only path runs backward along \(c \to a\): it cancels flow pushed in step 1. A forward-only construction has no such arc; residuals provide it.
Single-commodity flow is polynomial and integral. Variants push past that polytope:
The flow backbone usually survives. The polytope stays near-integral, so a MIP solver branches only on the few messy variables. Model the flow part as a flow; the solver handles the rest.
The minimum-weight set of edges that connects every vertex of a weighted graph.
A spanning tree of a connected graph: a subset of edges that touches every vertex and has no cycle. Always exactly \(n - 1\) edges.
The minimum spanning tree (MST) is the spanning tree of least total weight.
Kruskal, Prim, Borůvka: three greedy strategies, all reaching the same optimum. For the MST, greedy is provably exact.
Every building needs a high-speed link to the central data center. Connect every building with the least total trench length.
Cheapest transitions.
Pairing/Assignment.
Balanced Production/Circulation/Consumption.
Cheapest Skeleton/Network.
While matching and flow algorithms often scale well in practice, they have a roughly cubic worst-case complexity, which for certain instances can become critical.

Please take 10–15 minutes to fill out the survey.
https://umfragen.tu-bs.de/evasys/online.php?pswd=9728N
Algorithm Engineering SS 2026 — L08 Graph & Network Algorithms