. - PowerPoint PPT Presentation

About This Presentation
Title:

.

Description:

Title: Single Source Shortest Path Author: xuying Last modified by: Prof. WANG Lusheng Created Date: 9/2/2003 5:10:41 AM Document presentation format – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 40
Provided by: xuying
Category:

less

Transcript and Presenter's Notes

Title: .


1
  • .  

2
 
3
New Algorithm
  • Algorithm
  • Input e1lte2lte3lten and f(e1), f(e2), , f(en)
  • for (p0 pltn p)
  • for (i1 iltn i)
  • if (ipltn)
  • compute c(i, ip) using the formula
    (in O(n) time)
  • Total running time O(n3)

4
Algorithm (including backtracking)
  • Algorithm
  • Input e1lte2lte3lten and f(e1), f(e2), , f(en)
  • for (p0 pltn p)
  • for (i1 iltn i)
  • if (ipltn)
  • compute c(i, ip) using the formula
    (in O(n) time)
  • b(i, ip)k, where e_k is the root
    to min c(i, j).

5
New Algorithm
  • // backtracking Let Q be a Queue.
  • Qempty
  • print b(1, n) as the root
  • Q.add (1, n)
  • while (Q ! empty)
  • (i,j)Q.dequeue()
  • if (iltb(i,j)-1) then
  • attach b(i, b(i,j)-1) as the left
    child of b(i, j)
  • Q.add(i, b(i,j)-1)
  • if (b(i,j)1ltj) then
  • attach b(b(i,j)1, j) as the
    right child of b(i, j).
  • Q.add(b(i,j)1, j)

6
  • Best binary search tree is
  • e1-gte2-gte3-gte4

7
Using array
  • C(1, 1), C(2, 2), C(3,3), C(4,4) --size 1
  • C(1, 2), C(2, 3), C(3, 4) --size 2
  • C(1, 3), C(2, 4) -size
    3
  • C(1, 4) final result
    --size 4.
  • Need to compute O(n2) C(i,j),each takes O(n)
    time.
  • This is a new approach dynamic programming
    approach

8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
(No Transcript)
12

Index

v12
p(1)0
1
v24
p(2)0
2
v34
p(3)1
3
v47
p(4)0
4
v52
p(5)3
5
v61
p(6)3
6
13
  • .  

14
  • .  

15
Running time for Compute-Opt(n)
  • f(n) f(n-1)f(p(n))O(1)
  • f(n-1)f(n-2) O(1) (assume that
    p(n)n-2)
  • gt2f(n-2) (ignoring O(!) and use f(n-2) to
    replace f(n-1)
  • 4f(n-4)
  • 8f(n-6)
  • .
  • gtO(2 n/2).
  • This is exponential in terms of n.

16
Weighted Interval Scheduling Bottom-Up
Input n, s1, s2, , sn, f1, f2, , fn, v1, v2,
, vn Sort jobs by finish times so that f1?f2?
?fn. Compute p(1), p(2) , , p(n)
M00 for j 1 to n do Mj
max vjmp(j), mj-1 if (Mj
Mj-1) then Bj0 else Bj1 /for
backtracking mn / Backtracking
while ( m ?0) if (Bm1) then
print job m mp(m) else
mm-1
Bj0 indicating job j is not selected. Bj1
indicating job j is selected.
17

0 1 2 3 4 5 6
Index
0
2
M

w12
p(1)0
1
w24
p(2)0
0
2
4
2
w34
p(3)1
3
0
2
4
6
p(4)0
w47
4
w52
p(5)3
0
2
4
6
7
5
w61
p(6)3
6
0
2
4
6
7
8
Mj max vjmp(j), mj-1
0
2
4
6
8
7
8
M2w2M040
M3w3M142
M4W4M070
M5W5M326
M6w6M316lt8
j 0 1 2 3 4 5 6 B 0 1 1 1 1 1 0
Backtracking job1, job 3, job 5
18
Backtracking and time complexity
  • Backtracking is used to get the schedule.
  • P()s can be computed in O(n) time after sorting
    all the jobs based on the starting times.
  • Time complexity
  • O(n) if the jobs are sorted and p() is computed.
  • Total time O(n log n) including sorting.

19
Computing p()s in O(n) time
P()s can be computed in O(n) time using two
sorted lists, one sorted by finish time (if two
jobs have the same finish time, sort them based
on starting time) and the other sorted by start
time. Start time b(0, 5), a(1, 3), e(3, 8), c(5,
6), d(6, 8) Finish time a(1, 3), b(0,5), c(5,6),
d(6,8), e(3,8) P(d)c, p(c )b, p(e) a,
p(a)0, p(b)0. (See demo7)
20
Example 2
Start time b(0, 5), a(1, 3), e(3, 8), c(5, 6),
d(6, 8) Finish time a(1, 3), b(0,5), c(5,6),
d(6,8), e(3,8) P(d)c, p(c )b, p(e) a, p(a)0,
p(b)0. v(a)2, v(b)3, v(c )5, v(d) 6,
v(e)8.8. Solution M00, Ma2. Mbmax2,
3Mp(b)3. Mcmax3, 5Mp(c
)5Mb8. Mdmax8, 6Mp(d)6Mc6814
. Memax14, 8.8Mp(e)max14, 8.8Mamax
14, 10.814. Backtracking b, c, d.
Job a b c d e

B 1 1 1 1 0
21
Summary of Dynamic programming
  • Define an array, e.g., c(i, j) or M(i), etc
  • Find equations to compute c(i, j) or d(i), etc
  • Give the order to computer all cells.
  • Remark The solution for big size is obtained
    from solution(s) of smaller size.

22
Longest common subsequence
  • Definition 1 Given a sequence Xx1x2...xm,
    another sequence Zz1z2...zk is a subsequence of
    X if there exists a strictly increasing sequence
    i1i2...ik of indices of X such that for all
    j1,2,...k, we have xijzj.
  • Example 1 If Xabcdefg, Zabdg is a subsequence
    of X. Xabcdefg,Zab d g

23
  • Definition 2 Given two sequences X and Y, a
    sequence Z is a common subsequence of X and Y if
    Z is a subsequence of both X and Y.
  • Example 2 Xabcdefg and Yaaadgfd. Zadf is a
    common subsequence of X and Y.
  • Xabc defg
  • Yaaaadgfd
  • Za d f

24
  • Definition 3 A longest common subsequence of X
    and Y is a common subsequence of X and Y with the
    longest length. (The length of a sequence is the
    number of letters in the sequence.)
  • Longest common subsequence may not be unique.
  • Example abcd
  • acbd
  • Both acd and abd are LCS.

25
Longest common subsequence problem
  • Input Two sequences Xx1x2...xm, and
  • Yy1y2...yn.
  • Output a longest common subsequence of X and Y.
  • Applications
  • Similarity of two lists
  • Given two lists L1 1, 2, 3, 4, 5 , L21, 3, 2,
    4, 5,
  • Length of LCS4 indicating the similarity of the
    two lists.
  • Unix command diff.

26
Longest common subsequence problem
  • Input Two sequences Xx1x2...xm, and
  • Yy1y2...yn.
  • Output a longest common subsequence of X and Y.
  • A brute-force approach
  • Suppose that m?n. Try all subsequence of X
    (There are 2m subsequence of X), test if such a
    subsequence is also a subsequence of Y, and
    select the one with the longest length.

27
Charactering a longest common subsequence
  • Theorem (Optimal substructure of an LCS)
  • Let Xx1x2...xi, and Yy1y2...yj be two
    sequences, and
  • Zz1z2...zk be any LCS of X and Y.
  • 1. If xiyj, then zkxiyj and Z1..k-1 is an
    LCS of X1..m-1 and Y1..n-1.
  • 2. If xi ?yj, then zk?xi implies that Z is an LCS
    of X1..i-1 and Y.
  • 2. If xi ?yj, then zk?yjimplies that Z is an LCS
    of X and Y1..j-1.

28
The recursive equation
  • Let ci,j be the length of an LCS of X1...i
    and Y1...j.
  • ci,j can be computed as follows
  • 0
    if i0 or j0,
  • ci,j ci-1,j-11 if
    i,jgt0 and xiyj,
  • maxci,j-1,ci-1,j if i,jgt0
    and xi?yj.
  • Computing the length of an LCS
  • There are n?m ci,js. So we can compute them in
    a specific order.

29
The algorithm to compute an LCS
  • 1. for i1 to m do
  • 2. ci,00
  • 3. for j0 to n do
  • 4. c0,j0
  • 5. for i1 to m do
  • 6. for j1 to n do
  • 7.
  • 8. if xi yj then
  • 9. ci,jci-1,j-11
  • 10 bi,j1
  • 11. else if ci-1,jgtci,j-1 then
  • 12. ci,jci-1,j
  • 13. bi,j2
  • 14. else ci,jci,j-1
  • 15. bi,j3
  • 14

30
  • Example 3 XBDCABA and YABCBDAB.

yi B D C A B A
xi 0 0 0 0 0 0 0
A 0 0 0 0 1 1 1
B 0 1 1 1 1 2 2
C 0 1 1 2 2 2 2
B 0 1 1 2 2 3 3
D 0 1 2 2 2 3 3
A 0 1 2 2 3 3 4
B 0 1 2 2 3 4 4
31
Constructing an LCS (back-tracking)
  • We can find an LCS using bi,js.
  • We start with bn,m and track back to some cell
    b0,i or bi,0.
  • The algorithm to construct an LCS (backtracking)
  • 1. im
  • 2. jn
  • 3. if i0 or j0 then exit
  • 4. if bi,j1 then
  • ii-1
  • jj-1
  • print xi
  • 5. if bi,j2 ii-1
  • 6. if bi,j3 jj-1
  • 7. Goto Step 3.
  • The time complexity O(nm).

32
Remarks on weighted interval scheduling
  • it takes long time to explain. (5013 minutes)
  • Do not mention exponent time etc.
  • For the first example, use the format of example
    2 to show the computation process (more clearly).

33
Shortest common supersequence
  • Definition Let X and Y be two sequences. A
    sequence Z is a supersequence of X and Y if both
    X and Y are subsequences of Z.
  • Shortest common supersequence problem
  • Input Two sequences X and Y.
  • Output a shortest common supersequence of X and
    Y.
  • Example Xabc and Yabb. Both abbc and abcb are
    the shortest common supersequences for X and Y.

34
  • Recursive Equation
  • Let ci,j be the length of an SCS of X1...i
    and Y1...j.
  • ci,j can be computed as follows
  • j
    if i0
  • i
    if j0,
  • ci,j ci-1,j-11 if
    i,jgt0 and xiyj,
  • minci,j-11,ci-1,j1 if
    i,jgt0 and xi?yj.

35
(No Transcript)
36
The pseudo-codes
  • for i0 to n do
  • ci, 0i
  • for j0 to m do
  • c0,jj
  • for i1 to n do
  • for j1 to m do
  • if (xi yj) ci ,j ci-1, j-11
    bi.j1
  • else
  • ci,jminci-1,j1,
    ci,j-11.
  • if (cI,jci-1,j1 then
    bI,j2
  • else bI,j3
  • pn, qm / backtracking
  • while (p?0 or q?0)
  • if (bp,q1) then print xp pp-1
    qq-1
  • if (bp,q2) then print xp pp-1
  • if (bp,q3) then print yq qq-1

37
  • Example SCS for XBDCABA and YABCBDAB.

yi B D C A B A
xi 0 1 2 3 4 5 6
A 1 2 3 4 4 5 6
B 2 2 3 4 5 5 6
C 3 3 4 4 5 6 7
B 4 4 5 5 6 6 7
D 5 5 5 6 7 7 8
A 6 6 6 6 7 8 8
B 7 7 7 7 8 8 9
76-4 (LCS)9 (SCS) see slide 23
38
Assignment 2 Due Week 11, Friday
  • Question 1 For the weighted interval scheduling
    problem, there are eight jobs with starting time
    and finish time as follows j1(0, 6), j2(2, 3),
    j3(3, 5), j4(5, 9), j5(8, 12), j6(9, 11),
    j7(10, 13) and j8(11, 16). The weight for each
    job is as follows v13.8, v22.0, v33.0,
    v43.0, v56.5, v62.5, v713.0, and v86.0.
  • Find a maximum weight subset of mutually
    compatible jobs. (Backtracking process is
    required.) (You have to compute p()s. The
    process of computing p()s is NOT required.)
  • Question 2 Let Xaabbacab and Ybaabcbb. Find
    the longest common subsequence for X and Y.
    (Backtracking process is required.)
  • Question 3. Let Xaabbacab and Ybaabcbb. Find
    the shortest common supsequence for X and Y.
    (Backtracking process is required.)

39
Summary of Week 7
  • Understand the algorithms for the weighted
    Interval Scheduling problem, LCS and SCS.
  • .  
Write a Comment
User Comments (0)
About PowerShow.com