0% found this document useful (0 votes)
2 views5 pages

Lab 07

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Lab 07

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Code: TT/P.

KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

SUBJECT: DESIGN AND ANALYSIS OF ALGORITHMS

CODE: 503040

Duration: 150 minutes

Allowed to use materials.

LAB 07: Dynamic Programming (Part 2)

I. Objectives
Understand the properties of Dynamic Programming algorithm design technique
Be able to design, implement, and analyze Dynamic Programming algorithms solving common
problems.
II. Idea
- set up a recurrence relating a solution to a larger instance to solutions of some smaller
instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
1. An example of a Dynamic Programming algorithm
Implement and analyze a dynamic programming algorithm to compute n-th Fibonacci number

The implementation in Python is presented as follows


1 def fibonacci(n):
2 """
3 calculates n-th Fibonacci numbers
4 input:
5 n(int) - the ordinal number >= 0
6 output:
7 F (int) - n-th Fibonacci number
8 """
9 F = [0 for _ in range(n+1)]
10 F[0] = 0
11 F[1] = 1
12 for i in range(2,n+1):
13 F[i] = F[i-1] + F[i-2]
14 return F[n]
15
16 print(fibonacci(4))

Thien Nguyen PAGE 1


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

Analysis:
1/ Basic operation: addition on line 13
2/ Worst case: as average case
3/Counting the number of basic operations in the worst case:

Time efficiency
T(n) = n-1  Θ(n)

(***) How to draw running time graphics


def fac(n):
"""
...
"""
if n == 0:
return 1
result = 1
for i in range(1, n):
result *= i
return result
import time
def measure_time(func, N):
"""
...
"""
runtime = []
for n in N:
start = time.time()
f = func(n)
stop = time.time()
runtime.append(stop-start)
return runtime

import pylab

N = list(range(100))
rtime = measure_time(fac, N)
rtime2 = [t*1.5 for t in rtime]
pylab.plot(N, rtime, N, rtime2)
pylab.legend(['1', '2'])

Thien Nguyen PAGE 2


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

III. Exercises
Warm up
1. Computing Longest Common Subsequence (LCS).
a) Implement (in Python) and analyze a dynamic programming (DP) algorithm to
solve the problem.
Hint:
Let A=a1a2…am and B=b1b2…bn .
len(i, j): the length of an LCS between
a1a2…ai and b1b2…bj
With proper initializations, len(i, j) can be computed as follows.

Thien Nguyen PAGE 3


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

Intermediate exercises
2. Computing the transitive closure of a relation (Alternatively: existence of all nontrivial
paths in a digraph).
a) Implement (in Python) and analyze a dynamic programming (DP) algorithm to
solve the problem.
b) In the same axes, draw the graphics of the actual running time of the DP program
and a divide-and-conquer solution.

Rk [i , j]= { R k−1 [i , j]∨¿ R k−1 [i , k ]∧R k−1 [k , j] }

Thien Nguyen PAGE 4


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

3. All pairs shortest path distances.


a) Implement (in Python) and analyze a dynamic programming (DP) algorithm to
solve the problem.
b) In the same axes, draw the graphics of the actual running time of the DP program
and a divide-and-conquer solution.

Upper-Intermediate exercise

4. Revising Floyd algorithm to print out the shortest path which starts at a vertex v s and
ends at another vertex v e. Implement (in Python) a dynamic programming (DP) algorithm
to solve the problem.
Hint: create matrix P to store shortest paths, where the value of the element P[i][j] is the
index of a next vertex in the shortest path which starts at vertex i and ends at vertex j.

Thien Nguyen PAGE 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy