Final Exam EPIS2

You might also like

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

Spring 2022 01.06.

2022
Name: Matriculation number:
21-737-762
Oleksandr Krupchenko

Hamelirainstrasse 3 8302 29.03.2003


Address: DateofBirth:

About Exam

Submit your solution in one of the following ways:

1. You can use a tablet and pen (iPad, Surface, etc) to fill in your solution directly into the pdf file and upload the
completed pdf file to EPIS.

2. You can print the pdf file, use the available whitespace to fill in your solution, scan your solution, and upload the pdf
file to EPIS.

3. You can use blank white paper for your solutions, scan the sheets, and upload the pdf file to EPIS. Put your name and
matriculation number on every sheet. State all task numbers clearly.

Exercise 1 2 3 4 5 Total

Points
Achieved

1
Maximum
29 14 14 14 21 92
Points
4. You can use a text editor to answer the questions and submit the document as pdf.

Notes:

• If you do not have scanner it is possible to take pictures of your solution with your phone. We recommend Microsoft
Office Lens or Camscanner. Create a pdf file that includes all pictures and submit a single pdf file.

• We suggest that you submit your solutions several minutes earlier than the deadline.

• You bear the risk for your last-minute submission.

Signature:

Correction slot Please do not fill out the part below

Exercise 2 14 Points

Task 2.1 [10 points]:

Provide pseudocode for a function reverseEven that takes a queue Q of integers as a


parameter, and modifies Q by reversing the order of the even integers in the queue,
while keeping the odd integers in place.

For example given the queue:


Q = [14, 17, 16, 18, 21, 7, 28, 40]
The function reverseEven modifies the queue to:
Q = [40, 17, 28, 18, 21, 7, 16, 14]

You are only allowed to use the following abstract data types and functions:
Queue:
Q = initQueue(): initializes a Queue enQueue(Q,x): inserts value
x to queue Q (in the end) deQueue(Q): removes value from
queue Q (in the beginning) queueSize(Q): returns the number of
elements in queue Q

Stack:

2
Name: Matriculation number:

S = initStack(): initializes a stack push(S,x): pushes value x onto


stack S pop(S): removes value from stack S stackSize(S):
returns the number of elements in stack S
ReverseEven reverse(ReverseEven head)
{
if(head == NULL || head.next == NULL)
return head
ReverseEven restPart = reverse(head.next)
head.next.next = head
head.next = NULL
return restPart
}
ReverseEven reverseFromMToN(ReverseEven head, int m, int n)
{
if(m == n)
return head
int count = 1
ReverseEven curr = head
ReverseEven revPrev = NULL
while(count < m)
{
revPrev = curr
curr = curr.next
count = count + 1
}
ReverseEven revStart = curr
while(count < n)
{
curr = curr.next
count = count + 1
}
ReverseEven revEnd = curr
ReverseEven revNext = curr.next
curr.next = NULL
ReverseEven revPart = reverse(revStart)
if(revPrev)
{
revPrev.next.next = revNext
revPrev.next = revPart
}
else
{
if(revNext)
{
head.next = revNext
}
head = revPart
}

return head
}

3
Task 2.2 [4 points]:

Analyze and explain the asymptotic complexity of function reverseEven for the
following cases:

1. No assumption about integers in queue Q.

2. All integers in a queue Q are even integers.

3. All integers in a queue Q are odd integers.

4
Name: Matriculation number:

Exercise 3 14 Points

Consider a binary search tree T1 with n elements and root r1, and a binary search tree
T2 with m elements and root r2.

The structure of a tree node is defined as follows:

1 struct TreeNode {
2 int val;
3 struct TreeNode* left;
4 struct TreeNode* right;
5 };

Task 3.1 [9 points]:

Give the relevant parts of a C program that prints the elements of T1 and T2 sorted
in ascending order. As auxiliary data structure you are allowed to use an array that
is large enough to hold the combined elements of both trees. Your solution may not
use a sorting algorithm.

5
6
Name: Matriculation number:

Task 3.2 [5 points]:

Determine and explain the asymptotic runtime complexity of your solution.

7
Exercise 4 14 Points

Consider an undirected graph G = (V,E). Let n = |V |. Each vertex of V has a unique


integer label between 1 and n. Beam search is a modified breadth first search (BFS)
with an additional bandwidth constraint. It works as follows: When extending level
i then, instead of extending the search to all vertices that are adjacent to vertices on
level i, beam search selects k of these vertices and places them on level i+1 of the
search tree. Any k vertices adjacent to vertices on level i can be chosen, non-chosen
vertices are ignored when extending level i to level i+1, and during the entire search
each vertex is visited only once.

Example. A possible beam search of G1 with bandwidth k = 2 starts at vertex 1 at level


0 and visits the nodes in the following sequence: 1; 4, 5; 7, 6; 3. Beam search starts
at level 0 with only vertex 1. The vertices on level 0 have 4 adjacent vertices. For
extending the search from level 0 to level 1, vertices 4 and 5 are chosen whereas
vertices 2 and 3 are ignored.
3 7
4 6
1 5
2

G1 = (V,E)

Task 4.1 [2 points]:

Determine a possible beam search with bandwidth k = 2 that starts at vertex 5.

Task 4.2 [2 points]:

Determine a possible beam search with bandwidth k = 1 that starts at vertex 1.

Task 4.3 [10 points]:

8
Name: Matriculation number:

Write a pseducode algorithm beam(x, k) that prints a possible sequence of vertices


that can be visited during a beam search starting at vertex x with bandwidth k. You
can use the following vertex properties and functions:

• v.adj: list of adjacent vertices of v

• v.visited = 1: v is/was visited by beam search

• v.visited = 0: v has not been visited by beam search

• InitQueue(Q): initialize a new queue

• enQueue(Q, x): inserts vertex x to queue Q (in the end)

• deQueue(Q): removes one vertex from queue Q (in the beginning)

// Adjascency List representation in C++

#include <bits/stdc++.h>

using namespace std;

// Add edge

void addEdge(vector<int> adj[], int s, int d) {

adj[s].push_back(d);

adj[d].push_back(s);

// Print the graph

void printGraph(vector<int> adj[], int V) {

for (int d = 0; d < V; ++d) {

cout << "\n Vertex "

9
<< d << ":";

for (auto x : adj[d])

cout << "-> " << x;

printf("\n");

int main() {

int V = 5;

// Create a graph

vector<int> adj[V];

// Add edges

addEdge(adj, 0, 1);

addEdge(adj, 0, 2);

addEdge(adj, 0, 3);

addEdge(adj, 1, 2);

printGraph(adj, V);

10
Name: Matriculation number:

11
Exercise 5 21 Points

Consider an array A with n positive integers. Let S be the sum over all integers of A.
In the Minus process, we repeat the following steps until only zero or one element is
left:

• we select any two elements, a and b, and remove these two elements from A

• if a 6= b, we add |a − b| back to A otherwise nothing is added to A

The M value is equal to the last element. If no element is left, the M value is 0. The task
is to compute the smallest possible M value that a Minus process can compute.

Task 5.1 [2 points]:

Determine the smallest possible M value after the Minus process on A1.
1 3 1 2
Array A1

12
Name: Matriculation number:

We compute the smallest possible M value by finding a subsequence of A whose sum


is as close to bS/2c as possible. The subsequence of A is derived by selecting/not
selecting elements from A.

Specifically, we maintain a 2-D matrix dp with dimension ( + 1). Let


dp[i][j] be the maximum sum of a subsequence of subarray A[0..i − 1], such that this
maximum sum dp[i][j] is not larger than j.

For example, to calculate dp1[2][3], with i = 2 and j = 3, we consider the subsequences


of subarray A1[0..1]. To maximize dp1[2][3] we proceed according to the following
cases:

• If A1[1] = 3, the 2nd element, can be part of the subsequence, we use the value
of dp1[1][0] and dp1[1][3] to compute dp1[2][3].

• If A1[1] = 3, the 2nd element, cannot be part of the subsequence, we use the
value of dp1[1][3] to compute dp1[2][3].

Task 5.2 [3 points]: Fill in the following dp1 array for A1.

j=0 j=1 j=2 j=3

i=0 0 0 0 0
i=1
0
i=2

i=3 0
i=4
0

Figure 1: dp1 0

Task 5.3 [4 points]:

Write down the recursive problem solution for dp[i][j].

Task 5.4 [12 points]:

Write a C function calculate(A, n) that returns the smallest possible M value that is the
result of the Minus process.

13
14

You might also like