• Non ci sono risultati.

Prof. Roberto Sebastiani February 17 th , 2011

N/A
N/A
Protected

Academic year: 2021

Condividi "Prof. Roberto Sebastiani February 17 th , 2011"

Copied!
7
0
0

Testo completo

(1)

Prof. Roberto Sebastiani February 17 th , 2011

Test code: 01

Name: Patric

Surname: Amatulli Matriculation #: 1843

[COPY WITH SOLUTIONS]

Instructions: for each question, insert your answer below the text of the questions itself. You can use auxiliary empty paper, but these will not be corrected.

The text of the answers returned by the student must be clear, understandable and unambiguous. The handwriting must be readable. Ambiguities, lack of clearness and understandability affect negatively the evaluation of the answers. The examiners’

interpretation of the handwritten text is indisputable.

Score: Each exercise EXi has a given weight w(EXi), s.t. P

i w(EXi) ≥ 30:

EXi: Ex.1 Ex.2 Ex.3 Ex.4 Ex.5

w(EXi): 7 5 7 7 7

During the correction it will be assigned a given percentage p(EXi) of correctness, from 0% (wrong) to 100% (perfect). No penalties are given for wrong answers. Then the score, expressed in 30’s, is computed as:

score = round( X

i

w(EXi) · p(EXi)/100).

The part is passed if score ≥ 18.

Examples:

• solving 100% of all exercises: 33 (30L) =⇒ passed

• solving 100% of first 3 exercises, 0% the others: 19 =⇒ passed

• solving 100% of first 2 exercises, 50% of the third, 0% of the others: 16 =⇒ failed

i

(2)
(3)

Ex.1 Write a method in java:

public static int linearSearch(int a[], int v)

which, taking as input an array of integers and an integer value v, returns the position of the first occurrence of the element v in a, if present, -1 otherwise. The method must implement an iterative version of the linear search algorithm. It should be as efficient as possible.

NOTE: “by “position” it must return the user-perspective notion of position: e.g., if v is the 3rd element of the array, it must return 3, not 2.

[ Solution: One possible implementation is the following:

public static int linearSearch(int a[], int v) { int i = 0;

while (i < a.length && a[i] != v) i++;

if(i < a.length) return i+1;

else

return -1;

}

]

(4)

T (n) = 3 5 · T (n/9) + 4 · n 2 n.

1. Is it possible to apply Master Method?

2. If yes, use one condition of Master theorem to compute the asymptotic behavior of T (n). If not, explain why in detail.

[ Solution:

1. Yes. In fact, T (n) is in the form a · T (n/b) + f(n) s.t. a = 3 5 , b = 9 and f (n) = 4 · n 2 n;

hence log b (a) = log log

2

(a)

2

(b) = 5/2, s.t. n log

b

(a) = n 5/2 = n 2 n.

2. Since f (n) = Θ(n log

b

(a) ), it is possible to apply the second condition of Master theorem, from which we can conclude that T (n) = Θ(n log

b

(a) · log(n)) = Θ(n 2

n · log(n))

]

(5)

Ex.3 Write a method in java:

public static int[] merge(int a[], int b[])

which, taking as input two arrays of integers which are sorted in decreasing order, returns the array resulting from the merging of the two arrays.

[ Solution: One possible implementation is the following:

public static int[] merge(int a[], int b[]) { int[] c = new int[a.length+b.length];

for (int i=0,j=0,k=0;k<a.length+b.length;k++) { if (j==b.length || (i<a.length && a[i]>b[j])) {

c[k]=a[i];

i++;

} else {

c[k]=b[j];

j++;

} }

return c;

}

]

(6)

I

H F

E B

G D

C A

Given the graph in the figure, draw the sequence of arcs explored by a breadth-first search (BFS) starting from the node I. (Notation: use e.g., “AB” to denote the arc “A −→ B”.)

[ Solution:

IH, IG, HF, HE, GF, GD, FG, EC, DF, DE, DA, CB, CA, AB, BA ]

(7)

Ex.5 Consider the following undirected weighted graph:

2 3 1

2

7 7

4 2

8 5 8

10

8

1 2 10

4 6

4 3

A

B C

D

E F

H G

I

Compute the shortest-path tree from node A using Dijkstra’s algorithm. In particular:

• list the arcs of the final shortest-path tree, in the order they have been added to the tree.

• for each node, indicate its distance from A.

[ Solution:

• Tree: AC, CD, DB, CF, FI, BE, EH, HG

N ode Dist. f rom A

A 0

C 2

D 3

B 5

F 6

I 8

E 9

H 11

G 12

]

Riferimenti

Documenti correlati

With a focus on the renowned Italian–Armenian novelist Antonia Arslan’s Genocide narrative La masseria delle allodole (2004; English translation Skylark Farm, Arslan 2006), I’ll

Finally in the Chapter 5, a technique, based on a combination of the Mode Matching-Finite Element Method, Spectral Decomposition (MM-FEM-SD) and Method of Moments (MoM), to

If correctly utilized, the river - a barrier that has to be crossed - is also an element of urban composition that is shaped depending on the needs of city dwellers.. As with

Kprowpc j , K prowkh j , K prowkp j – value of the repaid bank commission in the j-th year of operation during the loan repayment period of N sbpc years, taken to cover

In the static regime, density-functional perturbation theory (DFPT) allows one to calculate response functions of systems as large as currently dealt with in ground-state

Solution proposed by Roberto Tauraso, Dipartimento di Matematica, Universit`a di Roma “Tor Vergata”, via della Ricerca Scientifica, 00133 Roma,

Write a program which given two lists L1 and L2 of integers decides if all elements in the first list are present in the second one.

Write a Prolog program which returns the element of a list L1 which are not included in a second list L2.. Write a Prolog program which decides if two binary trees are equivalent