Download - Alex1 group2

Transcript
Page 1: Alex1 group2

Advanced Algorithms

Exercise 1

Q1, Q8

Group 2

陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧

2013/10/20 1 Q1 Q8

Page 2: Alex1 group2

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 <= 2) return 1;

return f(n-1)+f(n-2);

}

2013/10/20 2 Q1 Q8

Page 3: Alex1 group2

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 <= 2) return 1;

return f(n-1)+f(n-2);

}

2013/10/20 3 Q1 Q8

Page 4: Alex1 group2

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

one addition to calculate result.

f(n) {

if(n <= 2) return 1;

return f(n-1)+f(n-2);

}

2013/10/20 4 Q1 Q8

Page 5: Alex1 group2

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

Page 6: Alex1 group2

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]

Page 7: Alex1 group2

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

Page 8: Alex1 group2

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 v’s

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

Page 9: Alex1 group2

Q8

• ∀ vertex v ∃ vertex w,

wv ∈ E and v.label > w.label

2013/10/20 9 Q1 Q8

v

w

Page 10: Alex1 group2

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.

Page 11: Alex1 group2

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.

Page 12: Alex1 group2

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

}

}

Page 13: Alex1 group2

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

Page 14: Alex1 group2

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

2013/10/20 14 Q1 Q8

Page 15: Alex1 group2

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

2013/10/20 15 Q1 Q8

Page 16: Alex1 group2

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

2013/10/20 16 Q1 Q8

Page 17: Alex1 group2

Q2

• Determine the space complexity of

the quicksort algorithm.

2013/10/20 17 Q1 Q8

Page 18: Alex1 group2

Q2 – Answer.

• 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

Page 19: Alex1 group2

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

Page 20: Alex1 group2

Q3 – Answer.

• 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

Page 21: Alex1 group2

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

Page 22: Alex1 group2

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

)()()(

)()(

Page 23: Alex1 group2

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

Page 24: Alex1 group2

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

)()()(

)()(

Page 25: Alex1 group2

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

Page 26: Alex1 group2

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

Page 27: Alex1 group2

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

Page 28: Alex1 group2

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

Page 29: Alex1 group2

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

Page 30: Alex1 group2

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

Page 31: Alex1 group2

Q5 – Observe

• 盡可能使用 3。

2013/10/20 31 Q1 Q8

Page 32: Alex1 group2

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

Page 33: Alex1 group2

Q6 – Observe

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

⇒ f(n) = 1 + C(n,2) + C(n,4)

• f(2) = 2, f(3) = 4, f(4) = 8, f(5) = 16, ...

2013/10/20 33 Q1 Q8

Page 34: Alex1 group2

Q6 – Proof

• Euler’s Formula : V – E + F = 2

• V = n + C(n, 4)

• E = 4 * C(n,4)/2 + C(n, 2)

= 2 * C(n, 4) + C(n, 2)

• F = 2 + E – V = 2 + C(n, 2) + C(n, 4) - n

2013/10/20 34 Q1 Q8

Page 35: Alex1 group2

Q6 – Proof

• Euler’s Formula : V – E + F = 2

• V = n + C(n, 4)

• E = 4 * C(n,4)/2 + C(n, 2)

= 2 * C(n, 4) + C(n, 2)

• F = 2 + E – V = 2 + C(n, 2) + C(n, 4) - n

⇒ f(n) = F – 1 + n = 1 + C(n,2) + C(n,4)

2013/10/20 35 Q1 Q8

Page 36: Alex1 group2

Q7

• Let d1, d2, …, dn, n ≥ 2, be positive integers.

Prove that if d1 + d2 + ... + dn= 2n-2, then there

exists a tree with n vertices of degrees exactly

d1, d2, …, dn. Based on your proof, design an

efficient algorithm to construct such a tree.

2013/10/20 36 Q1 Q8

Page 37: Alex1 group2

Q7 – Proof

• Goal : if d1 + d2 + ... + dn= 2n-2, there exists a tree.

• Basis : n = 1 and n = 2 is true.

• Assume that

d1, d2, …, dn, dn+1 of n+1 vertices given,

with d1 + d2 + ... + dn+1 = 2(n+1) - 2

⇒ exists a degree > 1 (pigeonhole principle)

• Let dn+1 > 1,

⇒ d1, d2, …, dn + dn+1 - 2 satisfy the conditions.

2013/10/20 37 Q1 Q8

Page 38: Alex1 group2

Q7 – Proof

• For |V| = n and d1, d2, …, dn + dn+1 – 2,

Add a (n+1)-th vertex, remove dn+1 – 1 from

n-th vertex, then add these in neighbors of

(n+1)-th vertex. Finally, add an edge between

n-th and (n+1)-th vertex.

2013/10/20 38 Q1 Q8

⇒s

Page 39: Alex1 group2

Q7 – efficient algorithm

• O(|V|)

2013/10/20 39 Q1 Q8

Build Queue Q0, Q1

Q0 : if d[i] != 1, Q0.push(i)

Q1 : if d[i] == 1, Q1.push(i)

while(!Q1.empty()) {

x = Q1.front(), Q1.pop();

y = Q0.front(), Q0.pop();

show have edge between x and y.

if(--d[y] == 1) Q1.push(y);

else Q0.push(y);

}

Page 40: Alex1 group2

Q9

• For an undirected graph G=(V, E) and a vertex v in V

let G\v denote the subgraph of G obtained by

removing v and all the edges incident to v from G. If

G is connected, then G\v can be disconnected or

connected. As a matter of fact, for any connected

graph G, we can always find a vertex v in G such that

G\v is connected. Prove this claim by mathematical

induction on the numbers of vertices and edges,

respectively. Discuss whether these proving

procedures imply algorithms for finding such a

vertex? 2013/10/20 40 Q1 Q8

Page 41: Alex1 group2

Q9

• Problem :

Give an undirected graph G, find a non-cut

vertex v satisfy G & G\v must be a connected

graph.

• If G is a tree, each leaf node is non-cut vertex.

Else ⇒ non-cut vertex must be in a cycle.

2013/10/20 41 Q1 Q8

Page 42: Alex1 group2

Q9 – efficient algorithm

• Worst case O(|V|+|E|) without cut operation.

• Worst case O(|V|) with cut operation.

2013/10/20 42 Q1 Q8

dfs(node) {

visited[node] = true;

for i in node's neighbors

if(visited[i])

cut-inverseEdge --- O(1)

dfs(i);

else

i is a non-cut vertex.

end process.

}

Page 43: Alex1 group2

Q10

• Give a linear-time algorithm that takes

as input a tree and determines whether it

has a perfect matching: a set of edges

that touches each node exactly once.

2013/10/20 43 Q1 Q8

Page 44: Alex1 group2

Q10 – Proof ?

• Lemma.

If exists a perfect matching in a rooted

tree, each leaf node must match its

parent.

2013/10/20 44 Q1 Q8

Page 45: Alex1 group2

Q10 – Proof ?

2013/10/20 45 Q1 Q8

implement by depth-first search,

mx[] : label of node with matched.

dfs(node, parent) {

visited[node] = true;

for i in node's neighbors

if(visited[i] == false)

dfs(i, node);

if(mx[node] == null&&mx[parent] == null)

mx[node] = parent

mx[parent] = node

}

check all nodes have matched.

Page 46: Alex1 group2

2013/10/20 Q1 Q8 46


Top Related