# alex1 group2

Post on 12-Jul-2015

314 views

Category:

## Technology

Embed Size (px)

TRANSCRIPT

Exercise 1

Q1, Q8

Group 2

BINAYA KAR

2013/10/20 1 Q1 Q8

• Q1

Recall the recursive program (discussed in the class) that computes the n-th Fibonacci

number. Compute the number of additions

used by the program and design a more

efficient program.

f(n) {

if(n

• Q1- the number of additions used

Definition : F1 = F2 = 1, Fn = Fn-1 + Fn-2 n 3

Definition : Gn : the number of addition used by above

program for f(n).

f(n) {

if(n

• Q1- the number of additions used

G1 = G2 = 0, Gn = Gn-1 + Gn-2 + 1, n 3

Observe the program, f(n) calls f(n-1) and f(n-2) express that it used

Gn-1 + Gn-2 times of addition, and finally used

f(n) {

if(n

• Q1- the number of additions used

G1 = G2 = 0, Gn = Gn-1 + Gn-2 + 1, n 3

Goal : n 1, Gn = Fn 1

Basis : G1 = F1 1 = 0, G2 = F2 1 = 0

Assume that Gn = Fn 1 Gn = Gn-1 + Gn-2 + 1 = (Fn-1 1)+(Fn-2 1)+1 = (Fn-1 + Fn-2) 1

= Fn 1

2013/10/20 5 Q1 Q8

• Q1-more efficient program(1)

This way depends on what MAX_QUERY you known.

Time Complexity for building Table : O(n)

Space Complexity : O(n)

Query : O(1) 2013/10/20 6 Q1 Q8

Integer Array : F[]

F[1] = F[2] = 0

For i = 0 to MAX_QUERY

F[i] = F[i-1] + F[i-2]

• Q1-more efficient program(2)

Companion Matrix and Divide and Conquer. Time complexity : O(log n)

Space Complexity : O(1)

2013/10/20 7 Q1 Q8

1-nn

n1n

FF

FF A

01

11

...AAA A b b b n 421

n1n

1nn

n1n

1-nnn1n

1-nn

n1n

FF

FF

FF

FFFF

FF

FF

2

01

11

• Q8

Let G=(V, E) be a directed graph.

Design an efficient algorithm to label the vertices of the graph with distinct labels from 1

to |V| such that the label of each vertex v is

greater than the label of at least one of vs

predecessors, or determine that no such

labeling is possible. (A predecessor of v is a

vertex w such that wv E.).

2013/10/20 8 Q1 Q8

• Q8

vertex v vertex w, wv E and v.label > w.label

2013/10/20 9 Q1 Q8

v

w

• Q8

Lemma 1. For the smallest label of vertex v, its indegree equals zero. (indeg(v) = 0)

2013/10/20 10 Q1 Q8

This node has

the smallest label.

• Q8

Lemma 2. The neighbor of the smallest label vertex mark 2-nd smallest label.

2013/10/20 11 Q1 Q8

the smallest label.

2-nd smallest label.

2-nd smallest label.

• Q8 efficient algorithm

2013/10/20 12 Q1 Q8

visited[]:initialize false.

indeg[]:indegree of vertex

label[]:label of vertex

foundIdx = 1

for(each indeg[v] = 0)

dfs(v);

dfs(v) {

visited[v] = true;

label[v] = foundIdx++;

for(neighbor u, vu in E) {

if(!visited[u])

dfs(u);

}

}

• Q8 efficient algorithm

Finally, check whether all vertice have marked. return possible or impossible.

Time complexity : O(V+E)

Space complexity : O(V)

2013/10/20 13 Q1 Q8

• ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2013/10/20 14 Q1 Q8

• ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2013/10/20 15 Q1 Q8

• ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2013/10/20 16 Q1 Q8

• Q2

Determine the space complexity of the quicksort algorithm.

2013/10/20 17 Q1 Q8

Iterative Quicksort still used stack structure.

Don't care about that memory of input used.

Quicksort used 3 variable in split function, O(1) space complexity.

Discuss about stack memory each stack frame use O(1) space.

Maximum recursion depth log(N)

Space complexity : O(logN)

2013/10/20 18 Q1 Q8

• Q3

Derive a closed formula of T(n) that satisfies the following recurrence relation:

T(n) = T(n/2) + 1, T(1) = 0.

2013/10/20 19 Q1 Q8

T(n) = T(n/2) + 1, T(1) = 0.

Let n = 2k, T(k) = T(k-1) + 1, T(0) = 0 T(k) = k T(n) = log2(n)

2013/10/20 20 Q1 Q8

• Q4

Given n 1 numbers x1, x2, , xn, show that the function f(x) = sigma(|x-xi|) takes its

minimum value at the median of these n

number.

2013/10/20 21 Q1 Q8

• Q4 Proof.(1.1)

M is the median number.

(1) if n is even, A M and t = n/2

x1 x2 ... xs A xs+1 ... xt M xt+1 ... xn

f(A)

2013/10/20 22 Q1 Q8

t

s

n

t

ii

t t

s

ii

n

s

i

s

i

AMMxAx)xA(xMMA

AxxA

1 11 1

11

)()()(

)()(

• Q4 Proof. (1.1)

______ ______ ______ __________

0 ? 0 = 0 (t = n/2)

when A = M, f(A) has minimum value.

2013/10/20 23 Q1 Q8

)()2()()(2)(

)()()()(2)()(

)()()(

111

111

1 11 1

AMtnMxAxxM

AMtnMxAxxMMAt

AMMxAx)xA(xMMA

n

t

i

t

s

i

t

i

n

t

i

t

s

i

t

i

t

s

n

t

ii

t t

s

ii

• Q4 Proof.(1.2)

M is the median number.

(1) if n is even, A M and s = n/2

x1 x2 ... xs M xs+1 ... xt A xt+1 ... xn

f(A)

2013/10/20 24 Q1 Q8

t

s

n

t

ii

s t

s

ii

n

t

i

t

i

AMMxAx)xA(xMMA

AxxA

1 11 1

11

)()()(

)()(

• Q4 Proof. (1.2)

______ ______ ___________

0 0 ?

when A = M, f(A) has minimum value.

2013/10/20 25 Q1 Q8

)()()()(

)()()()()(

)()()(

11

11

1 11 1

AMstnMxxM

AMtnMxxMMAs

AMMxAx)xA(xMMA

n

t

i

t

i

n

t

i

s

i

t

s

n

t

ii

s t

s

ii

• Q4 Proof.(2.1)

(2) if n is odd, same as proof(1.1)&proof(1.2)

f(x) = sigma(|x-xi|) takes its minimum value at the median of these n number.

2013/10/20 26 Q1 Q8

• Q5

Given a positive integer n, find a way to partition n into one or more positive integers

j1, j2, , jk (i.e. j1 + j2 + + jk = n) such that

the product of these k integers is maximized.

2013/10/20 27 Q1 Q8

• Q5 Observe

f(n): maximum product of these k integers.

Observe these, f(2) = 2,

f(3) = 3,

f(4) = 4 = 2*2,

f(5) = 6 = 2*3,

f(6) = 9 = 3*3,

2013/10/20 28 Q1 Q8

• Q5 Observe

Get recusion function : f(2) = 2, f(3) = 3,

f(n) = max( f(a) * f(b) ) n 4 where a + b = n, a&b are positive integer.

Draw call tree jk {2, 3}

2013/10/20 29 Q1 Q8

• Q5 Observe

Let x is numbers of 2, y is numbers of 3 x 0, y 0, 2x + 3y = n,

Goal : 2x 3y has maximize value. f(n) = 2x 3y

log(f(n)) = x log2 + y log3

= x log2 [(n - 2x)/3] log3

= x(log2 - log(3(2/3))) + n/3 log3

Because (log2 - log(3(2/3))) < 0, get x 0

2013/10/20 30 Q1 Q8

• Q5 Observe

3

2013/10/20 31 Q1 Q8

• Q6

Determine the correct closed formula for An (see slide 5 in unit 2 ) and prove its

correctness.

Problem: Maximal number of regions obtained by joining n points around a circle by straight

lines.

2013/10/20 32 Q1 Q8

• Q6 Observe

f(n) = f(n-1) + C(n-1, 3) + n

Recommended