Algorithms Worksheet 6
Algorithms Worksheet 6
Unit 12 Algorithms
Task 1
1. The questions below refer to the following weighted graph.
(a) The priority queue at the start is shown below. Complete the priority queue to show
temporary “costs” and mark these “costs” at each vertex.
A= B= C= D= E= F=
(b) Dijkstra’s algorithm is used to find the shortest distance form the start node A to every
other node.
Show the temporary distances assigned to each node, and the state of the priority
queue after A and C have been visited.
Priority queue:
1
Worksheet 6 Optimisation algorithms
Unit 12 Algorithms
Once these distances have been added to the priority queue, the algorithm proceeds
as follows:
While the priority queue is not empty:
Remove the node at the front of the queue. This is the current node.
For each neighbour, compute new distance by adding together the temporary
distance at the current node and the length of the edge going to that neighbour.
If the new distance is less than the neighbour’s current distance, replace the
neighbour’s distance by the new distance.
(c) Which is the next node to be visited? What will be the state of the priority queue, and
the temporary distance at F, after this node has been visited?
Priority queue:
(d) Will any further changes be made to temporary distances after this step? Explain.
2. Use Dijkstra’s algorithm to find the shortest distance from A to every other node. Colour
each node as it is completed or visited (dequeued) and enter the temporary distances on
the graph, changing them if and when required to end up with the shortest distances.
2
Worksheet 6 Optimisation algorithms
Unit 12 Algorithms
Priority queue
Task 2
3. Which of the following statements are true about the Travelling Salesman problem?
(c) Assuming a specific start city, the number of possible routes for 8 cities is double
the number of routes for 4 cities
(d) Assuming a specific start city, there are fewer than 700 possible routes for 7 cities.
4. What is a tractable problem? Include in your answer the time complexities of tractable and
intractable problems, using Big-O notation.
3
Worksheet 6 Optimisation algorithms
Unit 12 Algorithms
n 8 16 128
log2n
n log2n
n2
n3
340,282,366,920,938,463,463,374,607,431,
2n
768,211,456 (a 39-digit number)
(b) Algorithms for problems A, B and C have time complexities O(n log2n), O(n!) and O(n4).
Which of these problems are tractable, and which are intractable?
What is the big-O notation for a brute force algorithm to crack this password?
Log on to a site to test the strength of the password. You could try
https://howsecureismypassword.net/ or type into Google “How secure is my password” to
find a different site.
How long will it take an average PC to crack the password?
If you use a mixture of eight uppercase and lowercase letters, digits and 28 other symbols,
what is the big-O notation for the time complexity of the brute force algorithm?