MA/CSSE 473 Day 7 Dyamc Programmg Bomal Coeffcets Warshall's algorthm (Optmal BSTs) Studet questos? Dyamc programmg Used for problems wth recursve solutos ad overlappg subproblems Typcally, we save (memoze) solutos to the subproblems, to avod recomputg them. Prevously see example: Fb()
Dyamc Programmg Example Bomal Coeffcets: C(, k) s the coeffcet of x k the expaso of (+x) C(,0) = C(, ) =. If 0 < k <, C(, k) = C(, k) + C(, k ) Ca show by ducto that the "usual" factoral formula for C(, k) follows from ths recursve defto. A upcomg homework problem. If we do't cache values as we compute them, ths ca take a lot of tme, because of duplcate (overlappg) computato. Computg a bomal coeffcet Bomal coeffcets are coeffcets of the bomal formula: (a + b) = C(,0)a b 0 +... + C(,k)a k b k +... + C(,)a 0 b Recurrece: C(,k) = C(,k) + C(,k ) for > k > 0 C(,0) =, C(,) = for 0 Value of C(,k) ca be computed by fllg a table: 0... k k 0... C(,k ) C(,k) C(,k)
Computg C(, k): Tme effcecy: Θ(k) Space effcecy: Θ(k) If we are computg C(, k) for may dfferet ad k values, we could cache the table betwee calls. Elemetary Dy. Prog. problems These are Secto 8. of Levt Smple ad straghtforward. I am gog to have you read them o your ow. Co row Chage makg Co Collecto 3
Trastve closure of a drected graph We ask ths questo for a gve drected graph G: for each of vertces, (A,B), s there a path from A to B G? Start wth the boolea adjacecy matrx A for the ode graph G. A[][j] s f ad oly f G has a drected edge from ode to ode j. The trastve closure of G s the boolea matrx T such that T[][j] s ff there s a otrval drected path from ode to ode j G. If we use boolea adjacecy matrces, what does M represet? M 3? I boolea matrx multplcato, + stads for or, ad * stads for ad Trastve closure va multplcato Aga, usg + for or, we get T =M + M + M 3 + Ca we lmt t to a fte operato? We ca stop at M. How do we kow ths? Number of umerc multplcatos for solvg the whole problem? 4
Warshall's Algorthm for Trastve Closure Smlar to bomal coeffcets algorthm Assume that the vertces have bee umbered v, v,, v, Graph represeted by a boolea adjacecy matrx M. Numberg s arbtrary, but s fxed throughout the algorthm. Defe the boolea matrx R (k) as follows: R (k) [][j] s ff there s a path from v to v j the drected graph that has the form v = w 0 w w s = v j, where s >=, ad for all t =,, s, the w t s v m for some m k.e, oe of the termedate vertces are umbered hgher tha k What s R (0)? Note that the trastve closure T s R () R (k) example R (k) [][j] s ff there s a path the drected graph v = w 0 w w s = v j, where s >, ad for all t =,, s, the w t s v m for some m k Example: assumg that the ode umberg s alphabetcal order, calculate R (0), R (), ad R () 5
Quckly Calculatg R (k) Back to the matrx multplcato approach: How much tme dd t take to compute A k [][j], oce we have A k? Ca we do better whe calculatg R (k) [][j] from R (k )? How ca R (k) [][j] be? ether R (k ) [][j] s, or there s a path from v to v k that uses o vertces umbered hgher tha v k, ad a smlar path from v k to v j. Thus R (k) [][j] s R (k ) [][j] or ( R (k ) [][k] ad R (k ) [k][j] ) Note that ths ca be calculated costat tme f we already have the three vales from the rght had sde. Tme for calculatg R (k) from R (k )? Total tme for Warshall's algorthm? Code ad example o ext sldes 6
Floyd's algorthm All pars shortest path A etwork s a graph whose edges are labeled by (usually) o egatve umbers. We store those edge umbers as the values the adjacecy matrx for the graph A shortest path from vertex u to vertex v s a path whose edge sum s smallest. Floyd's algorthm calculates the shortest path from u to v for each par (u, v) od vertces. It s so much lke Warshall's algorthm, that I am cofdet you ca quckly get the detals from the textbook after you uderstad Warshall's algorthm. 7
Dyamc Programmg Example OPTIMAL BINARY SEARCH TREES Warmup: Optmal lked lst order Suppose we have dstct data tems x, x,, x a lked lst. Also suppose that we kow the probabltes p, p,, p that each of these tems s the tem we'll be searchg for. Questos we'll attempt to aswer: What s the expected umber of probes before a successful search completes? How ca we mmze ths umber? What about a usuccessful search? 8
9 Examples p = / for each. What s the expected umber of probes? p = ½, p = ¼,, p = /, p = / expected umber of probes: What f the same tems are placed to the lst the opposte order? The ext slde shows the evaluato of the last two summatos Maple. Good practce for you? prove them by ducto Calculatos for prevous slde
What f we do't kow the probabltes?. Sort the lst so we ca at least mprove the average tme for usuccessful search. Self orgazg lst: Elemets accessed more frequetly move toward the frot of the lst; elemets accessed less frequetly toward the rear. Strateges: Move ahead oe posto (exchage wth prevous elemet) Exchage wth frst elemet Move to Frot (oly effcet f the lst s a lked lst) What we are actually lkely to kow s frequeces prevous searches. Our best estmate of the probabltes wll be proportoal to the frequeces, so we ca use frequeces stead of probabltes. Optmal Bary Search Trees Suppose we have dstct data keys K, K,, K ( creasg order) that we wsh to arrage to a Bary Search Tree Suppose we kow the probabltes that a successful search wll ed up at K ad the probabltes that the key a usuccessful search wll be larger tha K ad smaller tha K + Ths tme the expected umber of probes for a successful or usuccessful search depeds o the shape of the tree ad where the search eds up Geeral prcple? 0
Example For ow we cosder oly successful searches, wth probabltes A(0.), B(0.3), C(0.), D(0.4). How may dfferet ways to arrage these to a BST? Geeralze for N dstct values. What would be the worst case arragemet for the expected umber of probes? For smplcty, we'll multply all of the probabltes by 0 so we ca deal wth tegers. Try some other arragemets: Opposte, Greedy, Better, Best?