DOC PREVIEW
UT Dallas CS 6363 - lec8

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 8 0 0 1 Dynamic Programming Chapter 15 We now take up the concept of dynamic programming again using examples Example 1 Chapter 15 2 Matrix Chain Multiplication INPUT An Ordered set of Matrices A1 A2 A3 An with Ai of size pi 1 pi given vector p p0 p1 pn OUTPUT The product of these matrices taken in this order MATRIX CHAIN ORDER p n length p 1 for i 1 to n do m i i 0 for l 2 to n do for i 1 to n l 1 do j i l 1 m i j for k i to j 1 do q m i k m k 1 j pi 1 pk pj if q m i j then m i j q s i j k return m and s Given a parenthesization for a matrix chain we have a corresponding binary tree whose leaves correspond to the matrices and conversely For example if we have A1 A2 A3 A4 A5 A6 also written as A1 A2 A3 A4 A5 A6 1 we have the following tree A1 A6 A2 A3 A4 A5 Example 2 Chapter 15 4 Longest Common Subsequence INPUT Two sequences X hx1 x2 xn i and Y hy1 y2 ym i OUTPUT Matrix C and pointer matrix B LCS Length X Y m length X n length Y for i 1 to m do c i 0 0 for j 1 to n do c 0 j 0 for i 1 to m do for j 1 to n do if xi yj then c i j c i 1 j 1 1 b i j else if c i 1 j c i j 1 then c i j c i 1 j b i j else c i j c i j 1 b i j return c and b Example 3 First Edition Optimal Polygonal Triangulation 2 INPUT An ordered set of vertices of a convex polygon vi 0 i n given vector v v0 v1 vn and w vi vk vj OUTPUT Triangulation with minimum cost POLYGONAL TRIANGULATION v n length v 1 for i 1 to n do m i i 0 for l 2 to n do for i 1 to n l 1 do j i l 1 m i j for k i to j 1 do q m i k m k 1 j w vi 1 vk vj if q m i j then m i j q s i j k return m and s 3 Consider the following convex polygon and a possible triangulation v0 A1 v1 v6 A2 A6 v2 v5 A3 A5 v3 A4 v4 This triangulation corresponds to the same tree as in matrix chain multiplication Indeed there is a one to one correspondence between the triangulation and a parenthesization It is this that is used in the above algorithm Example 4 Chapter 15 5 Binary Search Trees We are given an ordered sequence K k1 k2 kn of n distinct keys in sorted order so that ki ki 1 We wish to build a binary search tree from these keys For each key ki we have a probability pi that a search will be for ki Some searches may be for values not in K So we have dummy keys d0 d1 dn representing values not in K In the sorted order these would look like d0 k1 d1 k2 kn dn meaning that di represents values in between ki and ki 1 The probability qi associated with di is the probability that the search will ask for values in between ki and ki 1 q0 is the probability that values less than k1 will be asked for and qn is the probability that values greater than kn will be requested 4 Recall that in a binary search tree if y is a node in the left sub tree of x and z is a node in right sub tree of x then key y key x key z If the frequencies with which these keys occur is uniform a balanced tree is the best to minimize the average search time If however they are not uniform then it may be advantageous to have tree that is not balanced in order to minimize the expected number of comparisons For example consider i 0 1 2 3 4 5 pi 0 15 0 10 0 05 0 10 0 20 qi 0 05 0 10 0 05 0 05 0 05 0 10 Two binary search trees are shown below for this example k2 k2 k1 d0 k4 k1 d1 d0 k3 d2 k5 d1 d5 k5 d3 d4 k4 d5 d4 k3 d2 Expected time is given by 2 40 and 2 35 E T n X i 1 n X i 1 pi dT ki 1 n X qi dT di i 0 pi n X i 1 pi dT ki n X qi dT di i 0 where dT ki and dT di denote the depth of these nodes in the tree from the root Time here is measured by the number of queries We now take up the problem of designing a binary search tree by using dynamic programming Problem 5 Given an ordered set k1 k2 k3 kn of n distinct keys The frequency of occurrence of requests to key ki is pi and the 5 d3 frequency for dummy key di is qi We want a binary search tree that minimizes BT where BT n X pi i 1 n X pi dT ki i 1 n X qi dT di i 0 where dT ki is the depth of key ki and that of di is dT di in the tree T What is the di erence between this and the Hu man trees If we select kj for the root then d0 k1 kj 1 dj 1 form the left sub tree and dj kj 1 kn dn form the right sub tree This gives the recursion for dynamic programming Let c i j denote the minimum cost for a search tree containing keys di 1 ki kj dj Then for i j c i j min pr c i r 1 i r j j X k i pk j X k i 1 r 1 X pk k i r 1 X qk c r 1 j k i 1 j X pk k r 1 qk min c i r 1 c r 1 j i r j w i j min c i r 1 c r 1 j i r j P P where w i j jk i pk jk i 1 qk For j i c i j 0 Please note that this is slightly di erent from that in the book This is because our function BT is di erent This can be done in O n3 time as shown below OPTIMAL BST p q n 1 2 3 4 5 6 7 8 9 10 11 for i 1 to n do c i i qi 1 pi qi wi i qi 1 pi qi for …


View Full Document

UT Dallas CS 6363 - lec8

Documents in this Course
Exam #1

Exam #1

5 pages

lec4

lec4

5 pages

lec3

lec3

5 pages

lec1

lec1

3 pages

lec14

lec14

11 pages

lec13

lec13

22 pages

lec12

lec12

8 pages

lec11

lec11

3 pages

lec10new

lec10new

11 pages

lec9

lec9

13 pages

lec8

lec8

9 pages

lec7

lec7

10 pages

lec6

lec6

8 pages

lec7

lec7

10 pages

lec6

lec6

8 pages

lec4

lec4

5 pages

lec3

lec3

5 pages

lec1

lec1

3 pages

lec14

lec14

11 pages

lec13

lec13

22 pages

lec12

lec12

8 pages

lec11

lec11

3 pages

lec10new

lec10new

11 pages

lec9

lec9

13 pages

lec4

lec4

5 pages

lec3

lec3

5 pages

lec1

lec1

3 pages

lec14

lec14

11 pages

lec13

lec13

22 pages

lec12

lec12

8 pages

lec11

lec11

3 pages

lec10new

lec10new

11 pages

lec9

lec9

13 pages

lec8

lec8

9 pages

lec7

lec7

10 pages

lec6

lec6

8 pages

lec14

lec14

11 pages

lec13

lec13

22 pages

lec12

lec12

8 pages

lec11

lec11

3 pages

lec10new

lec10new

11 pages

lec9

lec9

13 pages

lec8

lec8

9 pages

lec7

lec7

10 pages

lec6

lec6

8 pages

lec4

lec4

5 pages

lec3

lec3

5 pages

lec1

lec1

3 pages

greedy

greedy

4 pages

siphon

siphon

18 pages

hwk5

hwk5

3 pages

Load more
Download lec8
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lec8 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lec8 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?