1 / 12

An example

An example. An example. Evaluation of arithmetic expressions. If we evaluate an expression tree bottom-up , it will take O( n ) time for a long and skinny tree. Hence we apply tree contraction. Evaluation of arithmetic expressions.

ranger
Download Presentation

An example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An example Advanced Topics in Algorithms and Data Structures

  2. An example Advanced Topics in Algorithms and Data Structures

  3. Evaluation of arithmetic expressions • If we evaluate an expression tree bottom-up, it will take O(n) time for a long and skinny tree. • Hence we apply tree contraction. Advanced Topics in Algorithms and Data Structures

  4. Evaluation of arithmetic expressions • We do not completely evaluate each internal node. We evaluate the internal nodes partially. • For each internal node v, we associate a label (av, bv). av and bv are constants. • The value of the expression at node is: (av X + bv), where X is an unknown value for the expression of the subtree rooted at v. Advanced Topics in Algorithms and Data Structures

  5. Keeping an invariant Invariant: • Let u be an internal node which holds the operation  , . • Let v and w are the children of u with labels (av, bv) and (aw, bw). • Then the value at u is: val(u) = (avval(v) + bv)  (awval(w) + bw) Advanced Topics in Algorithms and Data Structures

  6. Keeping an invariant Advanced Topics in Algorithms and Data Structures

  7. Applying the rake operation • The value at node u is: val(u) = (avcv + bv)  (aw X + bw) • X is the unknown value at node w. Advanced Topics in Algorithms and Data Structures

  8. Applying the rake operation • The contribution of val(u) to the value of node p(u) is: au val(u) + bu = au[(avcv + bv)  (aw X + bw)] + bu • We can adjust the labels of node w to (a’w ,b’w) • a’w = au(avcv + bv) aw • b’w = au(avcv + bv) bw + bu Advanced Topics in Algorithms and Data Structures

  9. Complexity of expression evaluation • The correctness of the expression evaluation depends on correctly maintaining the invariants. • We start with a label (1, 0) for each leaf and correctly maintain the invariant at each rake operation. • We have already proved the correctness of the rake operation. • Hence, evaluation of an expression given as a binary tree takes O(n) work and O(log n) time. Advanced Topics in Algorithms and Data Structures

  10. An example Advanced Topics in Algorithms and Data Structures

  11. An example Advanced Topics in Algorithms and Data Structures

  12. An example Advanced Topics in Algorithms and Data Structures

More Related