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

All books

2.11 Comparing the algorithms and choosing one — Learn

Prerequisites: all of Chapter 2.

Objectives: match a question to the algorithm that answers it; compare the algorithms by the work they do; say which conditions each one needs.

By the end of this section you can look at a problem, decide what kind it is, and choose the method — which is the skill the whole chapter has been building towards.

Which question is it?

Three different questions have appeared in this chapter, and they need three different answers. Read the question carefully before choosing.

The question asks for It is Use
the cheapest set of connections reaching every vertex a minimum spanning tree Kruskal or Prim
the cheapest route between two vertices a shortest path Dijkstra
the cheapest route through a staged network dynamic programming backward recursion

The first two are often confused, and the mistake is common. A spanning tree reaches everything; a shortest path reaches one place from another. Ask yourself: does the answer have to touch every vertex? If yes, it is a spanning tree. If no, it is a path.

Comparing by the work

Both spanning-tree algorithms give the same answer, so the choice is about the work.

That gives a practical rule:

Dijkstra's algorithm is the only one that needs a condition on the weights, and the condition is a real restriction, not a formality.

Algorithm Weights Gives
Kruskal any minimum spanning tree
Prim any minimum spanning tree
Dijkstra non-negative only shortest path from one start
Backward recursion any shortest route through a staged network

Example 1. A network has 50 vertices and 60 edges. Which spanning-tree algorithm is likely to be lighter?

60 edges is only slightly more than 50 vertices — a sparse network, so most vertices have few connections. Kruskal sorts 60 edges; Prim adds 50 vertices and scans the few edges at each. Kruskal's sorting is small here, and it is quick to do by hand, so either works and Kruskal is the natural choice for showing working.

Example 2. A network has 50 vertices and 1,200 edges. Which is likely to be lighter?

1,200 edges is a lot for 50 vertices — a dense network, where most pairs are joined. Kruskal sorts all 1,200; Prim adds 50 vertices and scans a row of about 50 entries each time. Prim is much lighter here.

Why the answers cannot be swapped

It is worth seeing why each algorithm fails on the other's question, not only that it does.

Progress check

Back to the chapter problem. You have three answers for the building: the cheapest set of cables, the shortest route between two rooms, and — if the building were laid out in zones — the cheapest route through the zones.

Write one paragraph that a client could read. Say what each answer is, which question it answers, and why the cheapest cabling is not the same as the shortest route. That paragraph is the answer to the chapter problem, and writing it is the last step.

You should now be able to

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.