levo

Fundamentals of Algorithms

Optimisation algorithms

  • 2 things to know
  • Paper 1

Overview

Recall questions on what Dijkstra's algorithm computes, what is recorded for each vertex, why weights must be non-negative, and recovering the path itself.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Process

Why can breadth-first search not be used in place of Dijkstra's algorithm on a weighted graph?

Numbers

In a weighted graph, P–Q costs 2, P–R costs 5, Q–R costs 1 and R–S costs 4. Give the shortest distances from P to Q, R and S.

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What problem does Dijkstra's shortest path algorithm solve?

    Model answer

    It finds the shortest, meaning lowest total weight, path from a chosen starting vertex to every other vertex in a weighted graph — or to one particular destination vertex. The lengths of the edges are taken into account, not merely the number of edges.

  2. 2

    Why can breadth-first search not be used in place of Dijkstra's algorithm on a weighted graph?

    Model answer

    Because breadth-first search counts edges rather than weights, so it finds the route using the fewest edges. In a weighted graph a route with more edges can easily have a smaller total weight — a motorway detour may be quicker than a single slow lane — so the fewest-edge route is not necessarily the cheapest, and a weight-aware algorithm is needed.

  3. 3

    A weighted graph has vertices A, B, C and D with undirected edges A-B of weight 1, A-C of weight 4, B-C of weight 2, B-D of weight 6 and C-D of weight 3. Trace the shortest path from A to D.

    Model answer

    From A, B is reached at cost 1 and C at cost 4. Going on from B, C can be reached at 1 + 2 = 3, which is cheaper than the direct 4, so C's best known distance becomes 3 by the route A, B, C; and D can be reached at 1 + 6 = 7. Going on from C at distance 3, D can be reached at 3 + 3 = 6, which is cheaper than 7, so D's best known distance becomes 6. The shortest path from A to D is A, B, C, D with total weight 6.

  4. 4

    A weighted graph has vertices P, Q, R and S with undirected edges P-Q of weight 2, P-R of weight 5, Q-R of weight 1 and R-S of weight 4. Find the shortest distance from P to every other vertex.

    Model answer

    Q is reached from P directly at cost 2. R can be reached directly at 5, or via Q at 2 + 1 = 3, so its shortest distance is 3 by the route P, Q, R. S can only be reached through R, at 3 + 4 = 7 by the route P, Q, R, S. So the shortest distances from P are: Q = 2, R = 3, S = 7.

  5. 5

    In a trace of Dijkstra's algorithm, what is recorded for each vertex?

    Model answer

    The best distance from the start vertex found so far, which begins as infinity for every vertex except the start (which is zero) and is reduced whenever a cheaper route is discovered; the previous vertex on that best route, so that the path itself can be reconstructed at the end by working backwards; and whether the vertex has been finalised, meaning its shortest distance is now known for certain and it will not be examined again.

  6. 6

    Once Dijkstra's algorithm has finished, how is the actual path recovered rather than just its length?

    Model answer

    By following the record of previous vertices backwards. Start at the destination, look up the vertex recorded as coming before it on its best route, then look up that vertex's predecessor, and so on until the start vertex is reached. Reversing the sequence obtained gives the shortest path in the forward direction.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A driver is .
softwarehardwarestorage

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AThe integer 5 is 0101.
B0.75 is 1/2 + 1/4.
CThe fraction bits are 1100.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

Head means the first single element of a non-empty list.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Fundamentals of Algorithms, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.