170 likes | 348 Views
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.
E N D
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 • 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.
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
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
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.
Example: division trees • In a division tree the primes are the roots of the tree. • There is an implied ordering in a rooted tree.
Expression trees • An (algebraic) expression tree shows the order of operations in a particular algebraic expression. For example:a / b + c-de meaning: ((a/b) + c) - (de) or a / (b + (c- (de)))
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)
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
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 -
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.
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.