Chapter 2: Review
The chapter problem, solved
Wiring the new building. Seven rooms, a list of possible cable runs each with a length, and a van that has to travel between two of them by the shortest route. Two questions, one diagram.
The cheapest cabling. A set of cables that reaches every room with no redundant loop is a spanning tree, and every spanning tree on 7 rooms has cables. The question is which 6, and that is Kruskal's or Prim's algorithm.
Run Kruskal: sort the cable runs by length, take each one unless it would close a loop, and stop at 6. The total length is the least possible. Run Prim from any room and you should get the same total — if the two disagree, one run has an arithmetic mistake in it, and finding it is worth the two minutes.
The shortest van route. This is a different question. Use Dijkstra's algorithm from the room the van leaves. Settle the nearest unsettled room, update its neighbours' tentative distances, and repeat until the destination is settled. Trace the route back by following the updates.
Why the two answers differ. The spanning tree connects every room for the least total; the shortest path connects two rooms for the least total. The cheapest set of cables need not contain the shortest route between any particular pair of rooms, and the shortest route is not a set of cables at all — it is a single chain of them. The two totals may happen to be equal on a small network, and that is a coincidence, not a rule.
And if the building were laid out in zones, with the van visiting one room in each zone in turn, that would be a third question. It is a staged network, and backward recursion answers it: start at the last zone, write the cost from each room to the finish, and work back one zone at a time. Dijkstra would give the same answer; backward recursion does less work because it uses the zones.
Mixed practice — choose the method first
Work through these without notes. For each one, decide which kind of problem it is and say why, before you calculate anything. Two of them are deliberately questions you should refuse to answer with the algorithm that first comes to mind.
- A network has vertices and edges , , , , . Find the minimum spanning tree and its total weight.
- For the same network, find the shortest distance from to and name the route.
- For the same network, how many walks of length 2 are there from to ? Write the adjacency matrix first.
- A network has degrees . How many edges has it, and how many odd-degree vertices? Does it have an Eulerian trail?
- A network has 9 vertices and 12 edges. How many edges must be removed to leave a spanning tree, and what must be true of each edge removed?
- A staged network has stages ; ; ; with edges , , , , , , , . Find the least total cost from to by backward recursion, and write down the route.
- A network has a directed edge of weight and a question asks for the shortest path. State which algorithm you would normally use, and why you cannot use it here.
- A complete network has 6 vertices. How many edges has it, and how many spanning trees?
- A network is stored as an adjacency matrix. Explain how to find the out-degree of a vertex, the in-degree, and whether the network is directed.
- A council must connect 12 villages by road for the least total length, and separately must find the shortest route from the depot to the hospital. Explain why these are two different problems and name the algorithm for each.
Answers and concise solutions
1. Show the answer
1. The minimum spanning tree and its total weight
Answer: Sort the edges by weight: , , , , . Kruskal takes and , which join , and . It takes next, which brings in , and stops with three edges. The tree is , , , and its total weight is . It is minimal because each edge left out is at least as heavy as every edge on the cycle it would close: closes ––, whose heaviest edge is , and closes ––, whose heaviest edge is . No swap can lower the total.
2. Show the answer
2. The shortest distance and the route
Answer: The simple routes from to are ––, which is ; ––, which is ; and –––, which is . The least is , along ––. Dijkstra settles , then at and at , and the update from gives ; the route is read back from that update.
3. Show the answer
3. The number of walks of length 2
Answer: With the order the adjacency matrix has a wherever an edge joins two vertices. Row is and column is . The entry of in row , column is the dot product of those two: . The two walks are –– and ––.
4. Show the answer
4. The edges, the odd vertices and the trail
Answer: The handshake lemma says the degree sum is twice the number of edges, so the network has edges. Two of the vertices have odd degree. A connected network has an Eulerian trail when it has exactly 0 or 2 odd-degree vertices, so this one has a trail, and it must start at one degree-3 vertex and finish at the other.
5. Show the answer
5. How many edges to remove, and which
Answer: A spanning tree on 9 vertices has edges, so edges must be removed. Each removed edge must lie on a cycle: removing an edge that carries the only route between two parts would disconnect the network and leave no spanning tree.
6. Show the answer
6. The least total cost and the route
Answer: Work back one stage at a time. From the cost to is , and from it is . From the cheaper way on is through : . From both ways cost : . From the cost is . The least total cost is , and one route that achieves it is –––.
7. Show the answer
7. The algorithm and why it fails
Answer: You would normally use Dijkstra's algorithm. It settles the nearest unsettled vertex and never reopens it, which is safe only when adding another edge cannot make a route cheaper. With an edge of weight , a route with more edges can cost less, so a settled distance can be wrong. The algorithm would return a number, and that number would not be the shortest path.
8. Show the answer
8. The edges and the spanning trees
Answer: Every pair of vertices is joined, so the number of edges is . Cayley's formula says a complete network on vertices has spanning trees, so here it is . The divide by 2 in the edge count is there because each edge is counted once from each end.
9. Show the answer
9. How to read the three facts
Answer: The out-degree of a vertex is the sum of its row, because the row holds the edges that leave it. The in-degree is the sum of its column, because the column holds the edges that arrive. The network is undirected exactly when the matrix is symmetric: every entry above the diagonal equals its mirror below it. If any pair of mirror entries differs, the network is directed.
10. Show the answer
10. The two problems and their algorithms
Answer: The first problem connects every village for the least total length, so its answer is a spanning tree: 11 roads, with no loop. Kruskal's algorithm or Prim's algorithm finds it. The second connects two named places, so its answer is one path, and the roads elsewhere do not matter. Dijkstra's algorithm finds it. The cheapest set of roads need not contain the shortest route between any particular pair, so one answer does not give the other.
What you can now do
- describe a network precisely — vertex, edge, weight, degree, path, cycle, tree;
- use the handshake lemma, and use the parity of the degree sum to show a network cannot exist;
- tell a connected network from a disconnected one, and a directed network from an undirected one;
- write a network as an adjacency matrix and read it back, including in-degree and out-degree;
- multiply matrices, and use a power of an adjacency matrix to count walks;
- find a minimum spanning tree by Kruskal's algorithm and by Prim's algorithm, and say why each is correct;
- find a shortest path by Dijkstra's algorithm, and say why it needs non-negative weights;
- solve a multi-stage problem by backward recursion, and state Bellman's principle;
- choose the right algorithm for a question, and say why the others do not answer it.
Thinking back
Look at the route you chose at the start of the chapter — the bus that takes a particular way, the delivery that reaches your area on a particular day, the signal that reaches your house by one cable rather than another.
You can now model it. Draw the places as vertices and the connections as edges, and put a number on each edge — distance, or time, or cost, whichever the question is really about. Then decide which question you are asking. Is it "how do I connect everything cheaply", or "how do I get from here to there cheaply"? They are different, and the answer to one is not the answer to the other.
Write down which algorithm you would use and why. If your model is rough — you are not sure of the numbers, or of which connections exist — that is worth writing down too. Deciding what to leave out is what makes a model useful, and saying what you left out is part of the applied mathematics, not an apology for it. It is also exactly what the modelling project asks you to do.