Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Assignment 5 - Computational Thinking

1) A Hamiltonian path in this case is the following: 2-8-7-6-3-1-9-10-11-12-4-5-7. We


made sure to reach every verticis exactly once.

2)
a)
Code:
from graph import Graph
unweighted_edges = [
("a", "b", 2),
("a", "c", 3),
("a", "d", 4),
("b", "c", 1),
("b", "f", 7),
("c", "a", 3),
("c", "b", 2),
("c", "f", 6),
("d", "e", 8),
("e", "d", 7),
("e", "f", 4),
("f", "b", 6),
("f", "c", 6)
]
G = Graph(unweighted_edges, weighted=True, directed=True)
G.show()
Output:
b) This graph is weighted and directed, since the paths between the different points are
weighted and we can see which direction is attributed to which weight .

c) The shortest path in this graph from A to F is through D or through C, in both cases it
has a weight of 9. It is so because the path becomes too long through D. Therefore, the
shortest one is 3 + 6 or 2 + 7 = 9.

d) It is a connected graph since for any two vertices there is a path, for example from a
to e or from a to f or from any other vertices.

e) There is one path to et from D to A, through E, F, C, and then finally reaching A. This
path is however fairly long.

3) a)
By using Dijkstra’s algorithm, we can determine the cheapest flight possible. We
have to follow the following steps, assign a starting point, which in this case is Dublin
and an ending point, which is Bucharest. Then, starting from Dublin we have to compare
each possible path and find the cheapest to each checkpoint, or the cheapest to
Amsterdam, Paris, Rome and Budapest. When we are done with this we can go
backwards, from the final destination to find the cheapest flight.

Paris is the cheapest, so we assign it to 180, while Rome is assigned with 280
and Amsterdam with 210. Then, we have to find the cheapest path to Budapest, which
is through Rome, and costs 400. We assign Budapest with 410. These checkpoints have
minimal distances and will not be checked again. We now have to reason backwards
from Bucharest to find the cheapest flight and we can see that the cheapest one is at
450, from Dublin to Rome to Bucharest. We have now found the cheapest flight which
cost 450 euros.
b) To draw the minimum spanning tree using Prim’s algorithm we have to make a graph as this
one:
Now we will color in green the shortest distance to each checkpoint, in red the row not needed
anymore and in blue the city that we’ve already been in.
Step 1:
Dublin Paris Rome Amsterda Budapest Bucharest
m

Dublin - 180 280 210 - -

Paris 180 - 230 - 250 300

Rome 280 230 - - 120 170

Amsterda 210 - - - 200 250


m

Budapest - 250 120 200 - 60

Bucharest - 300 170 - 60 -

Step 2:

Dublin Paris Rome Amsterda Budapest Bucharest


m

Dublin - 180 280 210 - -

Paris 180 - 230 - 250 300

Rome 280 230 - - 120 170


Amsterda 210 - - - 200 250
m

Budapest - 250 120 200 - 60

Bucharest - 300 170 - 60 -

Step 3:
Dublin Paris Rome Amsterda Budapest Bucharest
m

Dublin - 180 280 210 - -

Paris 180 - 230 - 250 300

Rome 280 230 - - 120 170

Amsterda 210 - - - 200 250


m

Budapest - 250 120 200 - 60

Bucharest - 300 170 - 60 -

Step 4:

Dublin Paris Rome Amsterda Budapest Bucharest


m
Dublin - 180 280 210 - -

Paris 180 - 230 - 250 300

Rome 280 230 - - 120 170

Amsterda 210 - - - 200 250


m

Budapest - 250 120 200 - 60

Bucharest - 300 170 - 60 -

Step 5:

Dublin Paris Rome Amsterda Budapest Bucharest


m

Dublin - 180 280 210 - -

Paris 180 - 230 - 250 300

Rome 280 230 - - 120 170

Amsterda 210 - - - 200 250


m

Budapest - 250 120 200 - 60

Bucharest - 300 170 - 60 -

To conclude, after using Prim’s algorithm we have found this as the minimum spanning tree:
The total weight of this spanning tree is 770. We have chosen the path from Dublin to
Amsterdam because it is shorter than the one from Paris to Rome ( 210 < 230)

c) We first order the costs of the flights in ascending order:


Budapest-Bucharest(60), Budapest-Rome(120), Dublin-Paris(180), Amsterdam-Budapest(200),
Dublin-Amsterdam(210), Paris-Rome(230), paris-Budapest(250), Dublin-Rome(280),
Paris-Bucharest(300).

We select each one that does not form a cycle with the previous ones and add them to the tree,
so we can choose Budapest-Bucharest, Budapest-Rome, Dublin-Paris and Dublin-Amsterdam.
Afterwards, a cycle is formed, which cannot happen. We obtain the same spanning tree as the
one shown for question b).

d) The total weight of the minimum spanning tree is 770.

4) a) The shortest path for the traveling salesman is the following: A - B - D - E - C - A, which
equals to a total of 31 + 31 + 37 + 40 + 37 = 176. We have found this value by finding the
shortest path to a further house from the point where we are situated. There can be other
possibilities
b) Indeed, there is at least one more possible route, of equal length, which goes as
following : A - B - C - E - D - A, which adds up to the same total of 176

c) To calculate the total number of different routes, we can use the Hamiltonian formula,
which is ( n - 1) ! /2. However, we do not know how the addresses are connected, the weight
between them. Therefore, it depends on the type of graph there is and also how the nodes are
connected. It could be 25, but it could also be (25 - 1) ! / 2.

5) In order to solve this issue, we can use put into equation the following situation and calculate
x, or the initial amount of money that Robin received from his grandmother. We can understand
that in the first day, he spends (x/2 + 1), and in the second day, he spends half of what is left,
plus one, so ((x - x/2 -1) * ½ -1) and so on. In the end he does not have any money left.
((x - x/2 - 1) * ½ - 1) * ½ -1 = 0
(x - x/2 - 1) * ½ - 1 = 2
X - x/2 - 1 = 6
x( 1 - ½ ) = 7
X * ½ = 7 => x = 14

To conclude, I have used equations and calculus to determine the amount of money that
Robin received initially. We can see that 14 euros is the correct amount.

You might also like