ESO207A: Data Structures and Algorithms: Theoretical Assignment 2 Due Date: 7th October, 2023
ESO207A: Data Structures and Algorithms: Theoretical Assignment 2 Due Date: 7th October, 2023
Theoretical Assignment 2
Due Date: 7th October, 2023
1
Question 3. PnC (20 points)
You are given an array A = [a1 , a2 , a3 , . . . , an ] consisting of n distinct, positive integers. In one
operation, you are allowed to swap the elements at any two indices i and j in the present array for a
cost of max(ai , aj ). You are allowed to use this operation any number of times.
Let Π be a permutation of {1, 2, . . . , n}. For an array A of length n, let A(Π) be the permuted array
A(Π) = [aΠ(1) , aΠ(2) , . . . , aΠ(n) ].
We define the score of an array A of length n as
i=n−1
X
S(A) = |ai+1 − ai |
i=1
(a) (5 points) Explicitly characterise all the permutations A(Π0 ) = [aΠ0 (1) , aΠ0 (2) , aΠ0 (3) . . . , aΠ0 (n) ] of
A such that
S(A(Π0 )) = min S(A(Π))
Π
We call such permutations, a “good permutation”. In short, a good permutation of an array has
minimum score over all possible permutations.
(b) (15 points) Provide an algorithm which computes the minimum cost required to transform the
given array A into a good permutation, A(Π0 ).
The cost of a transformation is defined as the sum of costs of each individual operation used in the
transformation.
You will only be awarded full marks if your algorithm works correctly in O(n log n) in the worst
case, otherwise you will only be awarded partial marks, if at all.
(c) (0 points) Bonus: Prove that your algorithm computes the minimum cost of converting any array
A into a good permutation.
Some examples are given below for the sake of clarity:
• Regarding the operation:
Array (i, j) Cost
A = [7, 2, 5, 4, 1] (1, 3) max(a1 , a3 ) = max(7, 5) = 7
P1 = [5, 2, 7, 4, 1] (2, 5) max(2, 1) = 2
P2 = [5, 1, 7, 4, 2] (3, 5) max(7, 2) = 7
Final Array = [5, 1, 2, 4, 7] – –
In essence, the order of operations contributes significantly to the cost of a transformation.
• Regarding the cost of a transformation:
The cost of transforming the array A = [7, 2, 5, 4, 1] to P3 = [5, 1, 2, 4, 7] using the exact sequencce
of operations mentioned above is 7 + 2 + 7 = 16.
• Regarding permutations:
Let Π be such that [1, 2, 3, 4, 5] [5, 3, 1, 4, 2] and A = [7, 2, 5, 4, 1], then A(Π) = [5, 1, 2, 4, 7].
2
• The dist definition and notation used is the same as that in lectures.
• It is guaranteed that t is always reachable from s using some sequence of edges in E, even after any
edge is destroyed.
• To help you, Batman gives you an n × n matrix Mn×n . You have to update M [u, v] to contain the
value of dist(s, t) if the edge (u, v) is destroyed, for each (u, v) ∈ E.
• You can assume that you are provided the edges in adjacency list representation.
• The edge (u, v) is considered the same as the edge (v, u).
(a) (12 points) Batman expects an algorithm that works in O(|V | · (|V | + |E|) = O(n · (n + m)).
(b) (4 points) He also wants you to provide him with proof of runtime of your algorithm, i.e., a Time-
Complexity Analysis of the algorithm you provide.
(c) (4 points) Lastly, you also need to provide proof of correctness for your algorithm.
• Let Vd ⊆ V be the set of vertices that are at a distance equal to d from s in G, then