DA2023_MakeUpFirstTest_withSolutions

You might also like

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

Design of Algorithms L.

EIC016
First Test Make-Up with Solutions Spring 2023

Design of Algorithms (L.EIC016)


Make-Up First Test with Solutions
Spring 2023
Faculdade de Engenharia da Universidade do Porto (FEUP)
Departamento de Engenharia Informática (DEI)

Problem 1. [2 points]
Consider the directed graph below which obviously contains cycles.

2 3

5 4

For this graph, describe an efficient algorithm that detects “back edges” and derives a topological
sorting of the graph nodes ignoring those edges. Determine a possible topological sorting of the nodes
for this particular graph. Discuss the time complexity of your algorithmic solution.

Solution:
The best approach to this problem is to carry out a Breath-First Traversal of the graph and detect
nodes that are already “alive” or labelled as already visited. In this particular graph, this would detect
the edges (6,2) and (4,1) as “back edges”. By removing them, or ignoring them, the traversal would
generate the order 0, 1, 2, 3, 5, 4, 6, 7 which is a valid topological sorting order. The time complexity
of this algorithm is that of a BFS or O(E+V).

1
Design of Algorithms L.EIC016
First Test Make-Up with Solutions Spring 2023

Question 2. [3 points]
Consider the recursive function G depicted below with one integer argument and other arguments
omitted here for simplicity. The G function makes use of another function, F which does some work
that is either polynomial, logarithmic or even constant time with respect to the input instance’s size,
n.

public int G (int n, …) {


if (n < 1) {
return 1;
}

F(n, …);

x = 0;
for(i = 0; i < 9; i++){
x = max(x,G(n / 3, ...));
}
return;
}

Derive a recurrence that models the execution time complexity of G and derive its asymptotic time
complexity using the Master Theorem as discussed in class. Clearly, you need to examine various
complexity scenarios regarding F itself. Discuss the overall complexity when F(n, …) is both linear and
quadratic with respect to n. Would it make sense to invest in a more efficient implementation of F to
be, say, logarithmic with respect to n?. Why or why not?

Solution:
The recurrence implied by this code is 𝑇(𝑛) = 9𝑇(𝑛⁄3) + 𝑓(𝑛) ignoring the simple constants implied
by the arithmetic operation in the return statement of the function and the complexity of the base
case. For this case, a = 9, b = 3 and so, logb(a) = 2. So, if F is 𝛩(n1), we get T(n)= 𝛩(n2), and if F is
𝛩(n2), we have T(n)= 𝛩(n2 logn). So, with F more time efficient than linear would not make any
difference as the overall tie complexity would still be quadratic.

2
Design of Algorithms L.EIC016
First Test Make-Up with Solutions Spring 2023

Question 3. [4 points]
Consider the knapsack problem where you wish to pack N items, xi of weights wi and values vi into a
knapsack with maximum weight capacity W. In this particular variant of the problem out of the N
items (with N > 3) there are 3 special items x1, x2 and x3. Two out of these three items you need to
have as “full” items they are considered critical. Their weights are w1, w2 and w3 and values v1, v2 and
v3. The remainder N-3 items you may or may not pack and can have a fractional part of them in the
knapsack.

For this problem variant develop an efficient (non-brute force) optimal algorithmic strategy to find
the optimal combination of items to include in the knapsack. Argue about the optimality of your
algorithm and determine its asymptotic time complexity.

Solution:
Generate choose(2,3) + 1 independent problems (7 problems in this case) each corresponding to the
combination of having at least 2 out of 3 of the critical items. The additional problem results for all
the 3 critical elements in the knapsack. For each of these generated problems adjust the available
capacity of the knapsack and them for the remainder N-3 items use the optimal greedy algorithm
discussed in class sorting the elements by descending order of v/w ratio. In the end, pick the best of
the 7 problem solutions. As you are in the end picking the best overall items for each problem, each
problem is itself optimal for the selection of the critical items, you will have the best overall solution.
Notice that for the critical items have to be either included or not so they cannot be partially included
in the solution.

The complexity of this approach is the same as the complexity of the algorithm developed in class as
the critical items and hence the 7 is a fixed constant multiplication factor and playing no role in the
big-O notation of the overall algorithm.

3
Design of Algorithms L.EIC016
First Test Make-Up with Solutions Spring 2023

Problem 4. [2 points]
Consider the weighted undirected graph below and the problem of determining the minimum cost
spanning tree (MST).

1
2 3
1 1
10 10
0 1 4 7

1 1
6 5
1

For this particular graph determine the following:


a) Using the Prim’s greedy algorithm determine the corresponding MST and its cost assuming
that in case of ties, the algorithm picks the node with the lowest numeric index.
b) How many MSTs do you have for this graph with the same cost? Explain.

Solution:
a) Prim’s MST algorithm picks the nodes in the following order: 0, 1, 2, 3, 4, 5, 6 and then 7 for
a total cost of 25.
1
2 3
1 1
10 10
0 1 4 7

1
6 5
1
b) There are 6 MSTs as there 6 possibilities to include one out of 6 of the lightest edges in the
graph and still making it a MST with the same cost of 25. Two of these MSTs are shown
below.

1 1
2 3 2 3
1 1 1
10 10 10 10
0 1 4 7 0 1 4 7

1 1 1
6 5 6 5
1 1

4
Design of Algorithms L.EIC016
First Test Make-Up with Solutions Spring 2023

Problem 5. [3 points]
Consider the network flow graph below where X, Y and Z are non-negative integer values and the
source of sink nodes are, respectively, s and t.

b
X Y
20 20
s a d t

X Z
c

For this network flow graph answer the following questions:

a) What is the analytical expression for the maximum flow between s and t?
b) Determine 3 cuts between s and t indicating their capacity (whenever applicable as a function
of the values of X, Y and Z)
c) How many augmenting paths does the Edmonds-Karp algorithm use? Indicate their capacity.
d) What would be the values of X, Y and Z so that all edges would be saturated for maximum
flow between s and t?
e) Does it make sense of increase the value of Y beyond the value of X? Why, or why not?

Solution:
a) Max Flow = min(20, min(X,Y),min(X,Z)).
b) Shown below are 3 simples cuts and the corresponding capacities.

2X
Y+Z

b X+Y
X Y
20 20
s a d t

X Z
c

c) The Edmonds-Karp algorithm would only use two augmenting paths, namely:

s – a – b – d – t with capacity min(20,X,Y)


s – a – c – d – t with capacity min(20,X,Z)

d) X = Y = Z = 10
e) No, since the capacity of the path is limited by X.

5
Design of Algorithms L.EIC016
First Test Make-Up with Solutions Spring 2023

Problem 6. [3 points]
Consider the directed and weighted graph below with source node s and where the weights represent
the distance between nodes.

1
7 b c
8
s 2 3 4 5

2
a d
5

For this graph answer the following questions:

a) Using Dijkstra’s shortest-path greedy algorithm, determine the sequence of nodes the algorithm
selected from its min-heap data structure. Indicate the final result in terms of the distance of every
node from the source node s.
b) Could the weight of the edge (b,c) be negative? If so, by how much could it be negative so that
Dijkstra’s algorithm would still work correctly?

Solution:
a) The order of selection is a, b, c and d resulting in the minimum distance tree shown below.

dist=5 dist=6
1
b c

s 3
dist=0
2
a d
5
dist=2 dist=7

b) Yes, it could be negative, but it could only be at most -2 as below that it would change the shortest
path to d from going directly through a to going through b and c.

6
Design of Algorithms L.EIC016
First Test Make-Up with Solutions Spring 2023

Problem 7. [3 points]
Consider the following sum-of-subset problem W = {1, 3, 4, 5}, M=8. Using backtracking show the
complete tree of states and use the bounding functions described in class and determine which states
can be pruned.

Solution:
The tree of exploration states is as shown below. For convenience, the values were already sorted so
that the two bounding functions described in class below can be easily applied. The first bounding
function check if at a given state the summation is so close to the desired value so that adding an
additional value will exceed the value. In other words, the search can be abandoned if:
1

- 𝑤/ 𝑥/ + 𝑤143 > 𝑀
/23

The second bounding function determines if adding all the remainder elements of the set will yield a
summation that is less than the desired value M. In other words, the search can be abandoned if:
1 7

- 𝑤/ 𝑥/ + - 𝑤/ < 𝑀
/23 /2143

Combining these functions that a simple backtracking search we get the state space shown below
where each node is a 3-tuple (sum, lev, rem) where sum is the current summation (thus
corresponding to the choices of xi values, lev is the tree level and thus which element is being chosen,
and rem the remainder elements that can still be included in the summation, i.e., the value of
∑7/2143 𝑤/ .

0 , 1 , 13
x =1 x =0
1 1

1 , 2 , 12 0 , 2 , 12
x =1 x =0 x =1 x =0
2 2 2 2

4 ,3 , 9 1 ,3 , 9 3 ,3 , 9 0 ,3 , 9
x =1 x =0 x =1 x =0 x =1 x =0 x =1 x =0
3 3 3 3 3 3 3 3

8 ,4 , 5 4 ,4 , 5 5 ,4 , 5 1 ,4 , 5 7 ,4 , 5 3 ,4 , 5 4 ,4 , 5 0 ,4 , 5

x =1 x =0 x =1 x =0
4 4 4 4

12 , 5 , 0 7 ,5 , 0 8 ,5 , 0 3 ,5 , 0

You might also like