Leaving Certificate Applied Mathematics2.11 Comparing the algorithms and choosing one

All books

2.11 Comparing the algorithms and choosing one — Practise

Work independently. The questions and answers are shown together at the end.

For each question, say which kind of problem it is before you say which algorithm. The marks are for the reason, not the name.

  1. H For each question, name the algorithm that answers it: (a) the cheapest set of cables reaching every room; (b) the shortest route between two rooms; (c) the cheapest way through a network of stages.
  2. H State one reason to choose Kruskal's algorithm and one reason to choose Prim's.
  3. H Explain why running Kruskal's algorithm on a network does not give the shortest path between two of its vertices.
  4. H State the condition on the edge weights that Dijkstra's algorithm requires, and say what the corresponding condition is for Kruskal's and Prim's algorithms.
  5. H A network has 100 vertices and 400 edges. Kruskal's algorithm sorts the edges; Prim's algorithm scans rows. Explain, in terms of the sizes, which is likely to be the more work.
  6. H A complete network has 5 vertices. How many spanning trees has it, and what does this say about checking a minimum spanning tree by listing them all?

Answers and concise solutions

1. Show the answer

1. (a), then (b), then (c)

Answer: (a) Kruskal's or Prim's algorithm — a minimum spanning tree. (b) Dijkstra's algorithm — a shortest path. (c) Dynamic programming with backward recursion, which for a staged network is the same as a shortest path but solved stage by stage.

2. Show the answer

2. One reason each

Answer:

Choose Kruskal when the edges are given as a list and the network is small: it sorts once and then reads down the list, which is quick and easy to show working for. Choose Prim when the network is stored as a matrix or is dense: it never sorts, and it only scans the row of the vertex just added, which is faster when most pairs of vertices are joined.

3. Show the answer

3. Why Kruskal does not give a shortest path

Answer:

Kruskal's algorithm answers a different question. It connects every vertex for the least total weight. A shortest path connects two particular vertices. It also works the wrong way for a route: it takes the cheapest edge anywhere in the network, which may lead away from the destination. The tree it builds is chosen to reach everything cheaply, not to get from one place to another cheaply, and the route between two vertices inside that tree can be much longer than the shortest path.

4. Show the answer

4. Dijkstra's condition, then the others'

Answer:

Dijkstra's algorithm requires every edge weight to be non-negative. It settles the nearest unsettled vertex because any other route would pass through a vertex that is at least as far away, and every extra edge adds more. A negative weight breaks that argument. Kruskal's and Prim's algorithms have no such condition. A minimum spanning tree is defined for any set of weights, and the cut-property argument does not care whether a weight is positive or negative — only that it is a number that can be compared.

5. Show the answer

5. Which is more work

Answer:

Kruskal's algorithm sorts all 400 edges, so its work grows with the number of edges. Prim's algorithm adds one vertex at a time and scans the row of the vertex just added, so its work grows with the number of vertices — here 100 rows of a 100 by 100 matrix. With 400 edges against 100 vertices, Kruskal has four times as many things to sort as Prim has vertices to add, so Prim is likely to be the lighter of the two here. The comparison is in the sizes of the problem, not in the algorithms' correctness — both give the same minimum total.

6. Show the answer

6. Number of spanning trees, then what it says

Answer:

By Cayley's formula there are 55−2=1255^{5-2} = 125 spanning trees. Listing all 125 and comparing their weights would work on 5 vertices, but the count grows very fast: 1010−2=10010^{10-2} = 100 million for 10 vertices, and 100100−2100^{100-2} for 100. That is why the algorithms matter. Kruskal and Prim find the minimum without examining every possibility, and they are guaranteed to find it — which no amount of listing could promise on a network of any size.

Settings

Colour scheme

Light or dark pages. Your colour and layout stay the same.

Colour

The paper, ink and lettering. Your layout stays the same.

Quiet colours
Bold colours
Layout

How pages are arranged. Your colours stay the same.

Reading offline

Keep this book on this device and read it offline.