SlideShare a Scribd company logo
Advanced Algorithms
Exercise 1
Q1, Q8
Group 2
陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧
2013/10/20

Q1 Q8

1
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

Q1 Q8

2
Q1- the number of additions used
f(n) {
if(n <= 2)
return 1;
return f(n-1)+f(n-2);
}
• Definition :
F1 = F2 = 1, Fn = Fn-1 + Fn-2 ∀ n ≥ 3
• Definition :
Gn : the number of addition used by above
program for f(n).
2013/10/20

Q1 Q8

3
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

Q1 Q8

4
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

Q1 Q8

5
Q1-more efficient program(1)
Integer Array : F[]
F[1] = F[2] = 0
For i = 0 to MAX_QUERY
F[i] = F[i-1] + F[i-2]
• 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

Q1 Q8

6
Q1-more efficient program(2)
1 1  Fn1 Fn 
A   
1 0  Fn Fn-1 


1 1  Fn1 Fn   Fn1  Fn Fn  Fn-1   Fn2 Fn1 
  F
1 0   F F    F
Fn   n1 Fn 
n -1  
n 1
   n

A  A  A  A  ...
n

b1

b2

b4

• Companion Matrix and Divide and Conquer.
Time complexity : O(log n)
Space Complexity : O(1)
2013/10/20

Q1 Q8

7
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

Q1 Q8

8
Q8
• ∀ vertex v ∃ vertex w,
wv ∈ E and v.label > w.label
w

v

2013/10/20

Q1 Q8

9
Q8
• Lemma 1. For the smallest label of vertex v,
its indegree equals zero. (indeg(v) = 0)
This node has
the smallest label.

2013/10/20

Q1 Q8

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

2013/10/20

the smallest label.

Q1 Q8

11
Q8 – efficient algorithm
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);
}
}
2013/10/20

Q1 Q8

12
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

Q1 Q8

13
• ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2013/10/20

Q1 Q8

14
• ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2013/10/20

Q1 Q8

15
• ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2013/10/20

Q1 Q8

16
Q2
• Determine the space complexity of
the quicksort algorithm.

2013/10/20

Q1 Q8

17
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

Q1 Q8

18
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

Q1 Q8

19
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

Q1 Q8

20
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

Q1 Q8

21
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)
s

n

1

s 1

  ( A  xi )   ( xi  A)
t
n
 t
  t

   ( A  M  M  xi )   ( A  xi )     ( xi  A)   ( xi  M  M  A) 
s 1
t 1
 1
  s 1


2013/10/20

Q1 Q8

22
Q4 – Proof. (1.1)
t
n
 t
  t

   ( A  M  M  xi )   ( A  xi )     ( xi  A)   ( xi  M  M  A) 
s 1
t 1
 1
  s 1

t

t

n

1

s 1

t 1

 t  ( A  M )   ( M  xi )  2 ( xi  A)   ( xi  M )  ( n  t )  ( M  A)
t

t

n

  ( M  xi )  2 ( xi  A)   ( xi  M )  ( n  2t )  ( M  A)

______ ______ ______ __________
1
s 1
t 1
≥0
?
≥0
= 0 (t = n/2)
when A = M, f(A) has minimum value.

2013/10/20

Q1 Q8

23
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)
t

n

1

t 1

  ( A  xi )   ( xi  A)
t
n
 s
  t

   ( A  M  M  xi )   ( A  xi )     ( xi  A)   ( xi  M  M  A) 
s 1
t 1
 1
  s 1


2013/10/20

Q1 Q8

24
Q4 – Proof. (1.2)
t
n
 s
  t

   ( A  M  M  xi )   ( A  xi )     ( xi  A)   ( xi  M  M  A) 
s 1
t 1
 1
  s 1

s

n

1

t 1

 s  ( A  M )   ( M  xi )   ( xi  M )  ( n  t )  ( M  A)
t

n

  ( M  xi )   ( xi  M )  ( n  t  s )  ( M  A)

______ ______ ___________
1
t 1
≥0
≥0
?
when A = M, f(A) has minimum value.

2013/10/20

Q1 Q8

25
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

Q1 Q8

26
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

Q1 Q8

27
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

Q1 Q8

28
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

2013/10/20

⇒

Q1 Q8

jk ∈ {2, 3}

29
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

Q1 Q8

30
Q5 – Observe
• 盡可能使用 3。

2013/10/20

Q1 Q8

31
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

Q1 Q8

32
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

Q1 Q8

33
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

Q1 Q8

34
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

Q1 Q8

35
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

Q1 Q8

36
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

Q1 Q8

37
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
⇒s
n-th and (n+1)-th vertex.

2013/10/20

Q1 Q8

38
Q7 – efficient algorithm
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);
}

• O(|V|)
2013/10/20

Q1 Q8

39
Q9
• For an undirected graph G=(V, E) and a vertex v in V
let Gv denote the subgraph of G obtained by
removing v and all the edges incident to v from G. If
G is connected, then Gv 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
Gv 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

Q1 Q8

40
Q9
• Problem :
Give an undirected graph G, find a non-cut
vertex v satisfy G & Gv 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

Q1 Q8

41
Q9 – efficient algorithm
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.
}

• Worst case O(|V|+|E|) without cut operation.
• Worst case O(|V|) with cut operation.
2013/10/20

Q1 Q8

42
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

Q1 Q8

43
Q10 – Proof ?
• Lemma.
If exists a perfect matching in a rooted
tree, each leaf node must match its
parent.

2013/10/20

Q1 Q8

44
Q10 – Proof ?
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.
2013/10/20

Q1 Q8

45
2013/10/20

Q1 Q8

46

More Related Content

What's hot

Elliptic Curve Cryptography and Zero Knowledge Proof
Elliptic Curve Cryptography and Zero Knowledge ProofElliptic Curve Cryptography and Zero Knowledge Proof
Elliptic Curve Cryptography and Zero Knowledge Proof
Arunanand Ta
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
Ayesha Tahir
 
Elliptic Curve Cryptography
Elliptic Curve CryptographyElliptic Curve Cryptography
Elliptic Curve Cryptography
JorgeVillamarin5
 
Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...
Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...
Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...
Alexander Litvinenko
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
RAJIV GANDHI INSTITUTE OF TECHNOLOGY
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
AJAL A J
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
Joepang2015
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
QMC: Operator Splitting Workshop, A Splitting Method for Nonsmooth Nonconvex ...
QMC: Operator Splitting Workshop, A Splitting Method for Nonsmooth Nonconvex ...QMC: Operator Splitting Workshop, A Splitting Method for Nonsmooth Nonconvex ...
QMC: Operator Splitting Workshop, A Splitting Method for Nonsmooth Nonconvex ...
The Statistical and Applied Mathematical Sciences Institute
 
04 search heuristic
04 search heuristic04 search heuristic
04 search heuristic
Nour Zeineddine
 
Reed Solomon encoder and decoder \ ريد سلمون
Reed Solomon encoder and decoder \ ريد سلمونReed Solomon encoder and decoder \ ريد سلمون
Reed Solomon encoder and decoder \ ريد سلمون
Muhammed Abdulmahdi
 
Number theory
Number theoryNumber theory
Number theory
dhivyakesavan3
 
Efficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
Efficient Hill Climber for Multi-Objective Pseudo-Boolean OptimizationEfficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
Efficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
jfrchicanog
 
1524 elliptic curve cryptography
1524 elliptic curve cryptography1524 elliptic curve cryptography
1524 elliptic curve cryptography
Dr Fereidoun Dejahang
 
3 grechnikov
3 grechnikov3 grechnikov
3 grechnikov
Yandex
 
Introduction to Elliptic Curve Cryptography
Introduction to Elliptic Curve CryptographyIntroduction to Elliptic Curve Cryptography
Introduction to Elliptic Curve Cryptography
David Evans
 
Bch and reed solomon codes generation in frequency domain
Bch and reed solomon codes generation in frequency domainBch and reed solomon codes generation in frequency domain
Bch and reed solomon codes generation in frequency domain
Madhumita Tamhane
 
Solucionario teoria-electromagnetica-hayt-2001
Solucionario teoria-electromagnetica-hayt-2001Solucionario teoria-electromagnetica-hayt-2001
Solucionario teoria-electromagnetica-hayt-2001
Rene Mauricio Cartagena Alvarez
 
2020 preTEST4A
2020 preTEST4A2020 preTEST4A
2020 preTEST4A
A Jorge Garcia
 

What's hot (20)

Elliptic Curve Cryptography and Zero Knowledge Proof
Elliptic Curve Cryptography and Zero Knowledge ProofElliptic Curve Cryptography and Zero Knowledge Proof
Elliptic Curve Cryptography and Zero Knowledge Proof
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
 
Elliptic Curve Cryptography
Elliptic Curve CryptographyElliptic Curve Cryptography
Elliptic Curve Cryptography
 
Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...
Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...
Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
QMC: Operator Splitting Workshop, A Splitting Method for Nonsmooth Nonconvex ...
QMC: Operator Splitting Workshop, A Splitting Method for Nonsmooth Nonconvex ...QMC: Operator Splitting Workshop, A Splitting Method for Nonsmooth Nonconvex ...
QMC: Operator Splitting Workshop, A Splitting Method for Nonsmooth Nonconvex ...
 
04 search heuristic
04 search heuristic04 search heuristic
04 search heuristic
 
Reed Solomon encoder and decoder \ ريد سلمون
Reed Solomon encoder and decoder \ ريد سلمونReed Solomon encoder and decoder \ ريد سلمون
Reed Solomon encoder and decoder \ ريد سلمون
 
Number theory
Number theoryNumber theory
Number theory
 
Efficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
Efficient Hill Climber for Multi-Objective Pseudo-Boolean OptimizationEfficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
Efficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
 
1524 elliptic curve cryptography
1524 elliptic curve cryptography1524 elliptic curve cryptography
1524 elliptic curve cryptography
 
3 grechnikov
3 grechnikov3 grechnikov
3 grechnikov
 
Introduction to Elliptic Curve Cryptography
Introduction to Elliptic Curve CryptographyIntroduction to Elliptic Curve Cryptography
Introduction to Elliptic Curve Cryptography
 
Bch and reed solomon codes generation in frequency domain
Bch and reed solomon codes generation in frequency domainBch and reed solomon codes generation in frequency domain
Bch and reed solomon codes generation in frequency domain
 
Solucionario teoria-electromagnetica-hayt-2001
Solucionario teoria-electromagnetica-hayt-2001Solucionario teoria-electromagnetica-hayt-2001
Solucionario teoria-electromagnetica-hayt-2001
 
2020 preTEST4A
2020 preTEST4A2020 preTEST4A
2020 preTEST4A
 

Viewers also liked

Aaex3 group2
Aaex3 group2Aaex3 group2
Aaex3 group2
Shiang-Yun Yang
 
Platonic Solids -2
Platonic Solids -2Platonic Solids -2
Platonic Solids -2
Gisélia Piteira
 
Platonicsolids
PlatonicsolidsPlatonicsolids
Platonicsolids
Lisa Tweedie
 
Propuesta ml reconstrucción andenes
Propuesta ml   reconstrucción andenesPropuesta ml   reconstrucción andenes
Propuesta ml reconstrucción andenes
hugurigue
 
Platonic Solids
Platonic SolidsPlatonic Solids
Platonic Solids
Kristen T
 
Do you want to be an architect?
Do you want to be an architect?Do you want to be an architect?
Do you want to be an architect?
Jose Luis Mollá
 

Viewers also liked (7)

User interface
User interfaceUser interface
User interface
 
Aaex3 group2
Aaex3 group2Aaex3 group2
Aaex3 group2
 
Platonic Solids -2
Platonic Solids -2Platonic Solids -2
Platonic Solids -2
 
Platonicsolids
PlatonicsolidsPlatonicsolids
Platonicsolids
 
Propuesta ml reconstrucción andenes
Propuesta ml   reconstrucción andenesPropuesta ml   reconstrucción andenes
Propuesta ml reconstrucción andenes
 
Platonic Solids
Platonic SolidsPlatonic Solids
Platonic Solids
 
Do you want to be an architect?
Do you want to be an architect?Do you want to be an architect?
Do you want to be an architect?
 

Similar to Alex1 group2

Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
sohelranasweet
 
DAA_LECT_2.pdf
DAA_LECT_2.pdfDAA_LECT_2.pdf
DAA_LECT_2.pdf
AryanSaini69
 
Phase diagram at finite T & Mu in strong coupling limit of lattice QCD
Phase diagram at finite T & Mu in strong coupling limit of lattice QCDPhase diagram at finite T & Mu in strong coupling limit of lattice QCD
Phase diagram at finite T & Mu in strong coupling limit of lattice QCD
Benjamin Jaedon Choi
 
2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...
2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...
2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...
The Statistical and Applied Mathematical Sciences Institute
 
Trial kedah 2014 spm add math k2 skema
Trial kedah 2014 spm add math k2 skemaTrial kedah 2014 spm add math k2 skema
Trial kedah 2014 spm add math k2 skema
Cikgu Pejal
 
Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)
Shiang-Yun Yang
 
Escola naval 2015
Escola naval 2015Escola naval 2015
Escola naval 2015
KalculosOnline
 
AsymptoticAnalysis.ppt
AsymptoticAnalysis.pptAsymptoticAnalysis.ppt
AsymptoticAnalysis.ppt
SiddheshUpadhyay3
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
 
Positive and negative solutions of a boundary value problem for a fractional ...
Positive and negative solutions of a boundary value problem for a fractional ...Positive and negative solutions of a boundary value problem for a fractional ...
Positive and negative solutions of a boundary value problem for a fractional ...
journal ijrtem
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
Reetesh Gupta
 
m.tech final
m.tech finalm.tech final
Integration techniques
Integration techniquesIntegration techniques
Integration techniques
Krishna Gali
 
Introduction
IntroductionIntroduction
Introduction
pilavare
 
Design and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt pptDesign and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt ppt
srushtiivp
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
 
M112rev
M112revM112rev
2 funda.ppt
2 funda.ppt2 funda.ppt
2 funda.ppt
02LabiqaIslam
 
mathematics question bank for engineering students
mathematics question bank for engineering studentsmathematics question bank for engineering students
mathematics question bank for engineering students
MrMRubanVelsUniversi
 
On Cubic Graceful Labeling
On Cubic Graceful LabelingOn Cubic Graceful Labeling
On Cubic Graceful Labeling
rahulmonikasharma
 

Similar to Alex1 group2 (20)

Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
DAA_LECT_2.pdf
DAA_LECT_2.pdfDAA_LECT_2.pdf
DAA_LECT_2.pdf
 
Phase diagram at finite T & Mu in strong coupling limit of lattice QCD
Phase diagram at finite T & Mu in strong coupling limit of lattice QCDPhase diagram at finite T & Mu in strong coupling limit of lattice QCD
Phase diagram at finite T & Mu in strong coupling limit of lattice QCD
 
2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...
2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...
2018 MUMS Fall Course - Statistical and Mathematical Techniques for Sensitivi...
 
Trial kedah 2014 spm add math k2 skema
Trial kedah 2014 spm add math k2 skemaTrial kedah 2014 spm add math k2 skema
Trial kedah 2014 spm add math k2 skema
 
Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)
 
Escola naval 2015
Escola naval 2015Escola naval 2015
Escola naval 2015
 
AsymptoticAnalysis.ppt
AsymptoticAnalysis.pptAsymptoticAnalysis.ppt
AsymptoticAnalysis.ppt
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Positive and negative solutions of a boundary value problem for a fractional ...
Positive and negative solutions of a boundary value problem for a fractional ...Positive and negative solutions of a boundary value problem for a fractional ...
Positive and negative solutions of a boundary value problem for a fractional ...
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
m.tech final
m.tech finalm.tech final
m.tech final
 
Integration techniques
Integration techniquesIntegration techniques
Integration techniques
 
Introduction
IntroductionIntroduction
Introduction
 
Design and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt pptDesign and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt ppt
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
M112rev
M112revM112rev
M112rev
 
2 funda.ppt
2 funda.ppt2 funda.ppt
2 funda.ppt
 
mathematics question bank for engineering students
mathematics question bank for engineering studentsmathematics question bank for engineering students
mathematics question bank for engineering students
 
On Cubic Graceful Labeling
On Cubic Graceful LabelingOn Cubic Graceful Labeling
On Cubic Graceful Labeling
 

More from Shiang-Yun Yang

Polarity analysis for sentiment classification
Polarity analysis for sentiment classificationPolarity analysis for sentiment classification
Polarity analysis for sentiment classification
Shiang-Yun Yang
 
文明的進程第十組
文明的進程第十組文明的進程第十組
文明的進程第十組
Shiang-Yun Yang
 
計算幾何論文報告 Minimum local disk cover sets
計算幾何論文報告 Minimum local disk cover sets計算幾何論文報告 Minimum local disk cover sets
計算幾何論文報告 Minimum local disk cover sets
Shiang-Yun Yang
 
N grams as linguistic features
N grams as linguistic featuresN grams as linguistic features
N grams as linguistic features
Shiang-Yun Yang
 
軍事報告 電磁砲
軍事報告 電磁砲軍事報告 電磁砲
軍事報告 電磁砲
Shiang-Yun Yang
 
第二十組福斯汽車
第二十組福斯汽車第二十組福斯汽車
第二十組福斯汽車
Shiang-Yun Yang
 
敏捷簡報
敏捷簡報敏捷簡報
敏捷簡報
Shiang-Yun Yang
 
計算型智慧論文報告 Building optimal regression tree ...
計算型智慧論文報告 Building optimal regression tree ...計算型智慧論文報告 Building optimal regression tree ...
計算型智慧論文報告 Building optimal regression tree ...Shiang-Yun Yang
 
Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)
Shiang-Yun Yang
 
Rpg 角色扮演遊戲 – 初探
Rpg 角色扮演遊戲 – 初探Rpg 角色扮演遊戲 – 初探
Rpg 角色扮演遊戲 – 初探Shiang-Yun Yang
 
Aaex6 group2(中英夾雜)
Aaex6 group2(中英夾雜)Aaex6 group2(中英夾雜)
Aaex6 group2(中英夾雜)Shiang-Yun Yang
 
Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)
Shiang-Yun Yang
 
Aaex2 group2
Aaex2 group2Aaex2 group2
Aaex2 group2
Shiang-Yun Yang
 

More from Shiang-Yun Yang (14)

Polarity analysis for sentiment classification
Polarity analysis for sentiment classificationPolarity analysis for sentiment classification
Polarity analysis for sentiment classification
 
文明的進程第十組
文明的進程第十組文明的進程第十組
文明的進程第十組
 
計算幾何論文報告 Minimum local disk cover sets
計算幾何論文報告 Minimum local disk cover sets計算幾何論文報告 Minimum local disk cover sets
計算幾何論文報告 Minimum local disk cover sets
 
N grams as linguistic features
N grams as linguistic featuresN grams as linguistic features
N grams as linguistic features
 
軍事報告 電磁砲
軍事報告 電磁砲軍事報告 電磁砲
軍事報告 電磁砲
 
第二十組福斯汽車
第二十組福斯汽車第二十組福斯汽車
第二十組福斯汽車
 
敏捷簡報
敏捷簡報敏捷簡報
敏捷簡報
 
計算型智慧論文報告 Building optimal regression tree ...
計算型智慧論文報告 Building optimal regression tree ...計算型智慧論文報告 Building optimal regression tree ...
計算型智慧論文報告 Building optimal regression tree ...
 
Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)
 
Rpg 角色扮演遊戲 – 初探
Rpg 角色扮演遊戲 – 初探Rpg 角色扮演遊戲 – 初探
Rpg 角色扮演遊戲 – 初探
 
Aaex6 group2(中英夾雜)
Aaex6 group2(中英夾雜)Aaex6 group2(中英夾雜)
Aaex6 group2(中英夾雜)
 
Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)
 
通識報告
通識報告通識報告
通識報告
 
Aaex2 group2
Aaex2 group2Aaex2 group2
Aaex2 group2
 

Recently uploaded

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Things to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUUThings to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUU
FODUU
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 

Recently uploaded (20)

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Things to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUUThings to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUU
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 

Alex1 group2

  • 1. Advanced Algorithms Exercise 1 Q1, Q8 Group 2 陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧 2013/10/20 Q1 Q8 1
  • 2. 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 Q1 Q8 2
  • 3. Q1- the number of additions used f(n) { if(n <= 2) return 1; return f(n-1)+f(n-2); } • Definition : F1 = F2 = 1, Fn = Fn-1 + Fn-2 ∀ n ≥ 3 • Definition : Gn : the number of addition used by above program for f(n). 2013/10/20 Q1 Q8 3
  • 4. 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 Q1 Q8 4
  • 5. 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 Q1 Q8 5
  • 6. Q1-more efficient program(1) Integer Array : F[] F[1] = F[2] = 0 For i = 0 to MAX_QUERY F[i] = F[i-1] + F[i-2] • 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 Q1 Q8 6
  • 7. Q1-more efficient program(2) 1 1  Fn1 Fn  A    1 0  Fn Fn-1    1 1  Fn1 Fn   Fn1  Fn Fn  Fn-1   Fn2 Fn1    F 1 0   F F    F Fn   n1 Fn  n -1   n 1    n  A  A  A  A  ... n b1 b2 b4 • Companion Matrix and Divide and Conquer. Time complexity : O(log n) Space Complexity : O(1) 2013/10/20 Q1 Q8 7
  • 8. 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 Q1 Q8 8
  • 9. Q8 • ∀ vertex v ∃ vertex w, wv ∈ E and v.label > w.label w v 2013/10/20 Q1 Q8 9
  • 10. Q8 • Lemma 1. For the smallest label of vertex v, its indegree equals zero. (indeg(v) = 0) This node has the smallest label. 2013/10/20 Q1 Q8 10
  • 11. Q8 • Lemma 2. The neighbor of the smallest label vertex mark 2-nd smallest label. 2-nd smallest label. 2-nd smallest label. 2013/10/20 the smallest label. Q1 Q8 11
  • 12. Q8 – efficient algorithm 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); } } 2013/10/20 Q1 Q8 12
  • 13. 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 Q1 Q8 13
  • 17. Q2 • Determine the space complexity of the quicksort algorithm. 2013/10/20 Q1 Q8 17
  • 18. 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 Q1 Q8 18
  • 19. 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 Q1 Q8 19
  • 20. 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 Q1 Q8 20
  • 21. 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 Q1 Q8 21
  • 22. 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) s n 1 s 1   ( A  xi )   ( xi  A) t n  t   t     ( A  M  M  xi )   ( A  xi )     ( xi  A)   ( xi  M  M  A)  s 1 t 1  1   s 1  2013/10/20 Q1 Q8 22
  • 23. Q4 – Proof. (1.1) t n  t   t     ( A  M  M  xi )   ( A  xi )     ( xi  A)   ( xi  M  M  A)  s 1 t 1  1   s 1  t t n 1 s 1 t 1  t  ( A  M )   ( M  xi )  2 ( xi  A)   ( xi  M )  ( n  t )  ( M  A) t t n   ( M  xi )  2 ( xi  A)   ( xi  M )  ( n  2t )  ( M  A) ______ ______ ______ __________ 1 s 1 t 1 ≥0 ? ≥0 = 0 (t = n/2) when A = M, f(A) has minimum value. 2013/10/20 Q1 Q8 23
  • 24. 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) t n 1 t 1   ( A  xi )   ( xi  A) t n  s   t     ( A  M  M  xi )   ( A  xi )     ( xi  A)   ( xi  M  M  A)  s 1 t 1  1   s 1  2013/10/20 Q1 Q8 24
  • 25. Q4 – Proof. (1.2) t n  s   t     ( A  M  M  xi )   ( A  xi )     ( xi  A)   ( xi  M  M  A)  s 1 t 1  1   s 1  s n 1 t 1  s  ( A  M )   ( M  xi )   ( xi  M )  ( n  t )  ( M  A) t n   ( M  xi )   ( xi  M )  ( n  t  s )  ( M  A) ______ ______ ___________ 1 t 1 ≥0 ≥0 ? when A = M, f(A) has minimum value. 2013/10/20 Q1 Q8 25
  • 26. 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 Q1 Q8 26
  • 27. 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 Q1 Q8 27
  • 28. 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 Q1 Q8 28
  • 29. 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 2013/10/20 ⇒ Q1 Q8 jk ∈ {2, 3} 29
  • 30. 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 Q1 Q8 30
  • 31. Q5 – Observe • 盡可能使用 3。 2013/10/20 Q1 Q8 31
  • 32. 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 Q1 Q8 32
  • 33. 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 Q1 Q8 33
  • 34. 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 Q1 Q8 34
  • 35. 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 Q1 Q8 35
  • 36. 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 Q1 Q8 36
  • 37. 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 Q1 Q8 37
  • 38. 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 ⇒s n-th and (n+1)-th vertex. 2013/10/20 Q1 Q8 38
  • 39. Q7 – efficient algorithm 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); } • O(|V|) 2013/10/20 Q1 Q8 39
  • 40. Q9 • For an undirected graph G=(V, E) and a vertex v in V let Gv denote the subgraph of G obtained by removing v and all the edges incident to v from G. If G is connected, then Gv 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 Gv 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 Q1 Q8 40
  • 41. Q9 • Problem : Give an undirected graph G, find a non-cut vertex v satisfy G & Gv 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 Q1 Q8 41
  • 42. Q9 – efficient algorithm 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. } • Worst case O(|V|+|E|) without cut operation. • Worst case O(|V|) with cut operation. 2013/10/20 Q1 Q8 42
  • 43. 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 Q1 Q8 43
  • 44. Q10 – Proof ? • Lemma. If exists a perfect matching in a rooted tree, each leaf node must match its parent. 2013/10/20 Q1 Q8 44
  • 45. Q10 – Proof ? 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. 2013/10/20 Q1 Q8 45