09 DP3 and Heaps

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

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

Priority Queues and Heaps (of Fun) 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 11 February 2010 Reading for today: Chapter 21

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

View Full Document
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
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

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

View Full Document
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)
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

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

View Full Document
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)
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)

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

View Full Document
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
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

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

View Full Document
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
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)

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

View Full Document
12 Today’s outline One more DP Convex polygon triangulation Priority queues Binary heaps
Polygon Triangulations

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

View Full Document
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.
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

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

View Full Document
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.
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.

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

View Full Document
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]
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:

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

View Full Document
20
This is the end of the preview. Sign up to access the rest of the document.

## 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
Ask a homework question - tutors are online