1 / 17

More Trees

More Trees. In this session we will discuss Dijkstra’s algorithm for finding distances between vertices in a weighted graph Expression trees Trees for Huffman codes. Weight tables.

brendy
Download Presentation

More Trees

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. More Trees In this session we will discuss Dijkstra’s algorithm for finding distances between vertices in a weighted graph Expression trees Trees for Huffman codes.

  2. Weight tables • Adjacency Weight0 1 0 1 0 5 _ 4 1 0 1 1 5 0 2 3 0 1 0 1 _ 2 0 2 1 1 1 0 4 3 2 0 • A graph that is not simple requires a matrix of weights in which every entry is a vector of weights with every element the weight for a different path. • For a maximization or minimization problem we can discard the less favourable paths. The entry for C-A could be either 0 or _ (infinite) depending on whether we want to maximize or minimize total weight.

  3. Length and distance in a weighted graph • The length of a path is the sum of the weights of its edges. • The distance between two vertices is the length of the shortest path. l(ABGE) = 15 = l(AFGE) l(ACE) = 17 l(AGE) = 10 l(AGDE) = 9 d(A,E) = 9

  4. Dijkstra's algorithmto determine distance • First AB,AC,AG, AF • Currently min. paths are 5, 7, -, -, 3, 2. • Ranging through B, F adds nothing; after C: 5, 7, -, 17, 3, 2 and after G : 4, 5, 7, 10, 3, 2 • Only edge DE has not been covered and it reduces a distance, so finally we have the distances: 4, 5, 7, 9, 3, 2

  5. Rooted Trees • A rooted tree is one with a distinguished vertex -- the first of a chain of decisions, the ancestor of one line of a family (there is an implied direction). Rooted trees are described in terms of parents, children, ancestors, descendants. • A binary rooted trees has two leaves at every vertex. i.e. --left and right children.

  6. Binary decision trees

  7. Example: division trees • In a division tree the primes are the roots of the tree. • There is an implied ordering in a rooted tree.

  8. A Directory Tree has a root drive

  9. Expression trees • An (algebraic) expression tree shows the order of operations in a particular algebraic expression. For example:a / b + c-de meaning: ((a/b) + c) - (de) or a / (b + (c- (de)))

  10. In-order traversal • In-order traversal reads the operations in infix function notation. [0] y =InOrder(T ) [0.1] NB. Input T is an expression tree [1] IfT is a leaf (ie. data) then [1.1] y T else [1.2] y  InOrder(leftchild) RootOperation InOrder(rightchild)

  11. Pre-order traversal (Polish prefix notation) • ((a / b) + c) - (d  e) • First output the root then the arguments:- ((a / b) + c) (d  e)- (+ (a / b) c) ( de)- (+ (/ ab) c) ( de) • Notice that this can be unambiguously interpreted without the parentheses: • - + / abc de

  12. Post-order traversalReverse Polish notation • ((a / b) + c) - (d  e) • First the arguments and then the root operation: ((a / b) + c) (d  e) -((a / b) c +) (d e ) -((a b /) c +) (d e ) -Again it may be interpreted without the parentheses:a b / c + d e -

  13. Huffman Codes • Huffman codes represent characters with bit strings of variable length. For efficiency, shorter codes are used for more frequently occurring characters. • Huffman codes are used in fax machines and some data compression programs • ARE is 01001111using the tree shown.What is 0110101000 ?How many bits would it take up in ASCII? • Exercise: Construct a Huffman code and write your name in this code.

  14. Letter frequencies from two different sources

  15. General text word frequencies

  16. Big ideas? • Representing text in a Huffman code and decoding a binary message written in a given code. • Djikstra's algorithm to find the shortest distance between two vertices of a weighted graph. • Expression trees for algebraic expressions in in-order, Polish and reverse Polish notation.

More Related