Algorithms Worksheet 6 Answers
Algorithms Worksheet 6 Answers
Algorithms Worksheet 6 Answers
Unit 12 Algorithms
Answers
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.
(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:
D=7 B = 10 E = 10 F=∞
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?
D will be visited next. The temporary distance at F will be changed to 27.
Priority queue:
B = 10 E = 10 F = 27
(d) Will any further changes be made to temporary distances after this step? Explain.
One further change will be made. B is visited next and neighbour E’s temporary
distance is already less than 10 + 9, so is left unchanged. E is then visited and F, now
27, is changed to 10 + 12 = 22. F is then dequeued and the algorithm completes.
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.
Shortest distances shown in first column of priority queue.
Show the state of the priority queue as each node is visited.
2
Worksheet 6 Optimisation algorithms
Unit 12 Algorithms
Priority queue
F = 10 B = 15 E=∞ H=∞
E = 15 B = 15 H=∞
B = 15 H = 27
H = 27
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.
A tractable problem has a polynomial-time solution or better, ie O(n), O(n2), O(n3) etc or
O(log n), O(n log n)
3
Worksheet 6 Optimisation algorithms
Unit 12 Algorithms
n 8 16 128
log2n 3 4 7
n log2n 24 64 896
n2 64 256 16,384
340,282,366,920,938,463,463,374,607,431,
2n 256 65,536
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?
B is intractable, the others are tractable
6. Choose a password of 8 lowercase alphabetic characters. Write the password below:
e.g. veryeasy
What is the big-O notation for a brute force algorithm to crack this password?
There are 26 x 26 x 26 x 26 x 26 x 26 x 26 x 26 possible combinations of letters. This is
O(26n)
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? 52 seconds
How long would it take to crack a password with 16 lowercase letters?
345 thousand years
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?
Total of 26+26+10+28 = 90 different symbols, O(90 n)
How long will it take an average PC to crack it?
8 days for h&r+BQ.f
Is cracking a password an intractable problem? Yes – for a password of anything other than
a few characters, it will be impossible to crack within a reasonable time.
Is cracking a password an insoluble problem? No – it can theoretically be solved, given
enough time. There may be other more efficient algorithms which can be used to crack a
password.