• Non ci sono risultati.

Test code: 00

N/A
N/A
Protected

Academic year: 2021

Condividi "Test code: 00"

Copied!
22
0
0

Testo completo

(1)

Prof. Roberto Sebastiani February 17

th

, 2011

Test code: 00

Name: Roberto

Surname: Sebastiani Matriculation #: 00000

[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

(2)

This page is willingly left empty.

(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)

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 last 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 = a.length-1;

while (i >=0 && a[i] != v) i--;

if(i >= 0 ) return i+1;

else

return -1;

}

(5)

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;

}

(6)

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 last 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 = a.length-1;

while (i >=0 && a[i] != v) i--;

if(i >= 0 ) return i+1;

else

return -1;

}

(7)

Ex.2 Consider the recurrence

T (n) = 8 · T (n/4) + 1024 · n 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 = 8, b = 4 and f(n) = 1024 · n n;

hence log

b

(a) =

loglog2(a)

2(b)

= 3/2, s.t. n

logb(a)

= n

3/2

= n n.

2. Since f (n) = Θ(n

logb(a)

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

logb(a)

· log(n)) = Θ(n

n · log(n))

]

(8)

Consider the recurrence

T (n) = 27 · T (n/9) + 18 · n 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 = 27, b = 9 and f(n) = 18 · n n;

hence log

b

(a) =

loglog3(a)

3(b)

= 3/2, s.t. n

logb(a)

= n

3/2

= n n.

2. Since f (n) = Θ(n

logb(a)

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

logb(a)

· log(n)) = Θ(n

n · log(n))

]

(9)

Consider the recurrence

T (n) = 2

5

· T (n/4) + 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 = 2

5

, b = 4 and f (n) = 4 · n

2

n;

hence log

b

(a) =

loglog2(a)

2(b)

= 5/2, s.t. n

logb(a)

= n

5/2

= n

2

n.

2. Since f (n) = Θ(n

logb(a)

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

logb(a)

· log(n)) = Θ(n

2

n · log(n))

]

(10)

Consider the recurrence

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) =

loglog2(a)

2(b)

= 5/2, s.t. n

logb(a)

= n

5/2

= n

2

n.

2. Since f (n) = Θ(n

logb(a)

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

logb(a)

· log(n)) = Θ(n

2

n · log(n))

]

(11)

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 increasing 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;

}

(12)

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;

}

(13)

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 increasing 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;

}

(14)

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;

}

(15)

Ex.4

A

B

D

E H

C F

G

I

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

[ Solution:

AB, AC, BD, BE, CD, CF, DC, EG, FD, FE, FI, GH, GI, IH, HI ]

(16)

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 ]

(17)

A

B

D

E H

C F

G

I

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

[ Solution:

AB, AC, BD, BE, CD, CF, DC, EG, FD, FE, FI, GH, GI, IH, HI ]

(18)

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 ]

(19)

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

(20)

Consider the following undirected weighted graph:

2

1 3 2

7

7 4 2

8 5

8 10 8

1

10 2

4 6

4 3

I H

G F

E

D B

C

A

Compute the shortest-path tree from node I 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 I.

[ Solution:

• Tree: IG, GF, FH, GD, DA, HE, EB, BC

N ode Dist. f rom I

I 0

G 2

F 3

H 5

D 6

A 8

E 9

B 11

C 12

(21)

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

(22)

Consider the following undirected weighted graph:

2 1 3 2

7

7

4 2

8 5

8 10 8

1

10 2

4 6 4

3

I

H G

F

E D

B

C A

Compute the shortest-path tree from node I 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 I.

[ Solution:

• Tree: IG, GF, FH, GD, DA, HE, EB, BC

N ode Dist. f rom I

I 0

G 2

F 3

H 5

D 6

A 8

E 9

B 11

C 12

Riferimenti

Documenti correlati

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,

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

A distanza di tre anni dalla pubblicazione di quel testo si presentano in que- sta sede  documenti dell’instrumentum pertinenti a due aree della Sardhnía: da un lato due

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

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.

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