ArticleslgStudy

computer science

Yen's algorithm

Yen's algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Yen's algorithm rather than just read about it. In short: In graph theory, Yen's algorithm computes single-source K-shortest loopless paths for a graph with non-negative edge cost. The algorithm was published by Jin Y.

Yen's algorithm — main illustration
Yen's algorithm — illustration

Key takeaways

  • Yen's algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Yen's algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Yen's algorithm from memory before moving on to harder problems.

Reference excerpt

In graph theory, Yen's algorithm computes single-source K-shortest loopless paths for a graph with non-negative edge cost. The algorithm was published by Jin Y. Yen in 1971 and employs any shortest path algorithm to find the best path, then proceeds to find K − 1 deviations of the best path.

Algorithm

Terminology and notation

Description The algorithm can be broken down into two parts: determining the first k-shortest path, A1, and then determining all other k-shortest paths. It is assumed that the container A will hold the k-shortest path, whereas the container B will hold the potential k-shortest paths. To determine A1, the shortest path from the source to the sink, any efficient shortest path algorithm can be used. To find the Ak, where k ranges from 2 to K, the algorithm assumes that all paths from A1 to Ak−1 have previously been found. The k iteration can be divided into two processes: finding all the deviations Aki and choosing a minimum length path to become Ak. Note that in this iteration, i ranges from 1 to Qkk. The first process can be further subdivided into three operations: choosing the Rki, finding Ski, and then adding Aki to the container B. The root path, Rki, is chosen by finding the subpath in Ak−1 that follows the first i nodes of Aj, where j ranges from 1 to k−1. Then, if a path is found, the cost of edge di(i+1) of Aj is set to infinity. Next, the spur path, Ski, is found by computing the shortest path from the spur node, node i, to the sink. The removal of previous used edges from (i) to (i+1) ensures that the spur path is different. Aki=Rki+Ski, the addition of the root path and the spur path, is added to B. Next, the edges that were removed, i.e. had their cost set to infinity, are restored to their initial values. The second process determines a suitable path for Ak by finding the path in container B with the lowest cost. This path is removed from container B and inserted into container A, and the algorithm continues to the next iteration.

Pseudocode The algorithm assumes that the Dijkstra algorithm is used to find the shortest path between two nodes, but any shortest path algorithm can be used in its place.

function YenKSP(Graph, source, sink, K): // Determine the shortest path from the source to the sink. A[0] = Dijkstra(Graph, source, sink); // Initialize the set to store the potential kth shortest path. B = []; for k from 1 to K: // The spur node ranges from the first node to the next to last node in the previous k-shortest path. for i from 0 to size(A[k − 1]) − 2: // Spur node is retrieved from the previous k-shortest path, k − 1. spurNode = A[k-1].node(i); // The sequence of nodes from the source to the spur node of the previous k-shortest path. rootPath = A[k-1].nodes(0, i); for each path p in A: if rootPath == p.nodes(0, i): // Remove the links that are part of the previous shortest paths which share the same root path. remove p.edge(i,i + 1) from Graph; for each node rootPathNode in rootPath except spurNode: remove rootPathNode from Graph; // Calculate the spur path from the spur node to the sink. // Consider also checking if any spurPath found spurPath = Dijkstra(Graph, spurNode, sink); // Entire path is made up of the root path and spur path. totalPath = rootPath + spurPath; // Add the potential k-shortest path to the heap. if (totalPath not in B): B.append(totalPath); // Add back the edges and nodes that were removed from the graph. restore edges to Graph; restore nodes in rootPath to Graph; if B is empty: // This handles the case of there being no spur paths, or no spur paths left. // This could happen if the spur paths have already been exhausted (added to A), // or there are no spur paths at all - such as when both the source and sink vertices // lie along a "dead end". break; // Sort the potential k-shortest paths by cost. B.sort(); // Add the lowest cost path becomes the k-shortest path. A[k] = B[0]; // In fact we should rather use shift since we are removing the first element B.pop(); return A;

Example

The example uses Yen's K-Shortest Path Algorithm to compute three paths from (C) to (H). Dijkstra's algorithm is used to calculate the best path from (C) to (H), which is (C)−(E)−(F)−(H) with cost 5. This path is appended to container A and becomes the first k-shortest path, A1. Node (C) of A1 becomes the spur node with a root path of itself, R21=(C). The edge, (C)−(E), is removed because it coincides with the root path and a path in container A. Dijkstra's algorithm is used to compute the spur path S21, which is (C)−(D)−(F)−(H), with a cost of 8. A21=R21+S21=(C)−(D)−(F)−(H) is added to container B as a potential k-shortest path. Node (E) of A1 becomes the spur node with R22=(C)−(E). The edge, (E)−(F), is removed because it coincides with the root path and a path in container A. Dijkstra's algorithm is used to compute the spur path S22, which is (E)−(G)−(H), with a cost of 7. A22=R22+S22=(C)−(E)−(G)−(H) is added to container B as a potential k-shortest path. Node (F) of A1 becomes the spur node with a root path, R23=(C)−(E)−(F). The edge, (F)−(H), is removed because it coincides with the root path and a path in container A. Dijkstra's algorithm is used to compute the spur path S23, which is (F)−(G)−(H), with a cost of 8. A23=R23+S23=(C)−(E)−(F)−(G)−(H) is added to container B as a potential k-shortest path. Of the three paths in container B, A22 is chosen to become A2 because it has the lowest cost of 7. This process is continued to the 3rd k-shortest path. However, within this 3rd iteration, note that some spur paths do not exist. And the path that is chosen to become A3 is (C)−(D)−(F)−(H).

Features

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Yen's algorithm

Start with the simplest possible case. Write down what Yen's algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Yen's algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Yen's algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Yen's algorithm

In research
Yen's algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Yen's algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Yen's algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Graph algorithms, Polynomial-time problems, so understanding it makes those chapters shorter.
In everyday life
Look for Yen's algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Yen's algorithm” →

How to study Yen's algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Yen's algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Yen's algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Yen's algorithm in simple terms?

In graph theory, Yen's algorithm computes single-source K-shortest loopless paths for a graph with non-negative edge cost. The algorithm was published by Jin Y.

Why does Yen's algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Yen's algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Yen's algorithm.

Tags

  • Graph algorithms
  • Polynomial-time problems

Keep exploring