09 DP3 and Heaps

09 DP3 and Heaps - Priority Queues and Heaps (of Fun)...

Info iconThis preview shows pages 1–21. Sign up to view the full content.

View Full Document Right Arrow Icon
Priority Queues and Heaps (of Fun) 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 11 February 2010 Reading for today: Chapter 21
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Announcements HW 2 - Snake Written due now! Programming due tonight. HW 3 – Dynamic Programming Should be available soon. Readings: Chapters 6.9, 21.1-21.4
Background image of page 2
3 Last time . . Optimal BSTs Suppose we know how often we will access each word in a dictionary Goal: Build a static BST with the lowest overall search cost e.g., and : of : the : of and the of and the
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A recursive algorithm: 4 Optimal Binary Search Trees Cost for subtree containing words a i …a j : Total frequency of words a i …a j : The minimum cost of the optimal created subtrees, for every possible root a k f(i,j) C(i,j)
Background image of page 4
5 Optimal BST Example f 1 =8, f 2 =2, f 3 =5, f 4 =3, f 5 =2 C 0 1 2 3 4 6 C(i,j) = min (C(i,k-1) + C(k+1,j)) + f(i,j) if i<j
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Optimal BST Example f 1 =8, f 2 =2, f 3 =5, f 4 =3, f 5 =2 C 0 1 2 3 4 5 8 C(i,i-1) = 0 C(i,i) = f(i,i)
Background image of page 6
7 Optimal BST Example f 1 =8, f 2 =2, f 3 =5, f 4 =3, f 5 =2 C 0 1 2 3 4 5 8 12 9 11 7 6 C(i,i+1) = min (C(i+1,i+1) + C(i,i)) + f(i,i+1)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Optimal BST Example f 1 =8, f 2 =2, f 3 =5, f 4 =3, f 5 =2 C(i,j) = min (C(i,k-1) + C(k+1,j)) + f(i,j) if i<j C 0 1 2 3 4 5 8 12 24 9 16 5 11 17 7 6
Background image of page 8
9 Optimal BST Example f 1 =8, f 2 =2, f 3 =5, f 4 =3, f 5 =2 root C 0 1 2 3 4 5 8 12 24 33 2 9 15 21 3 5 11 17 7 6 C(i,j) = min (C(i,k-1) + C(k+1,j)) + f(i,j) if i<j
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Optimal BST Example f 1 =8, f 2 =2, f 3 =5, f 4 =3, f 5 =2 C 0 1 2 3 4 5 1 0 8 12 24 33 39 2 9 15 21 3 5 11 17 4 7 5 2 6 0 C(i,j) = min (C(i,k-1) + C(k+1,j)) + f(i,j) if i<j
Background image of page 10
11 Optimal BST Example f 1 =8, f 2 =2, f 3 =5, f 4 =3, f 5 =2 3 1 4 2 5 5 6 6 16 6 Total access cost: 39 (Costs shown next to the node)
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Today’s outline One more DP Convex polygon triangulation Priority queues Binary heaps
Background image of page 12
Polygon Triangulations
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 Polygon Triangulations Triangulations are a very important concept in computational geometry. Here we consider triangulations of a convex polygon. The problem is to find which interior lines to add so that the total length is minimal.
Background image of page 14
15 Triangulations n n-1 2 1 Number the points from 1 to n counterclockwise as shown below Let l(i,k) be the length of diagonal (i,k). k
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
16 Triangulations n n-1 2 1 k In the optimal solution there must be a third vertex for the triangle whose base is (n,1) . We consider all such possible 3rd vertices, and recurse on the remaining two polygons.
Background image of page 16
17 Triangulations n n-1 2 1 k What are the subproblems? i We consider all such possible 3rd vertices, and recurse on the remaining two polygons.
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
18 Triagulation subproblems k k-1 i+1 i j Let C(i, k) be the minimum cost of triangulating the polygon consisting of vertices [i, i+1, … k-1, k, i]
Background image of page 18
k k-1 i+1 i j C(i,k) = if k-i 2 (j = i+1) (j = k-1) if i+2 j k-2 C(i,k) = min Exercise:
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
20
Background image of page 20
Image of page 21
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 57

09 DP3 and Heaps - Priority Queues and Heaps (of Fun)...

This preview shows document pages 1 - 21. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online