discrete maths n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Discrete Maths PowerPoint Presentation
Download Presentation
Discrete Maths

play fullscreen
1 / 94

Discrete Maths

175 Views Download Presentation
Download Presentation

Discrete Maths

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Discrete Maths 241-303, Semester 1 2014-2015 • Objective • introduce more unusual tree algorithms and techniques • you already know about binary (search) trees 6. Trees

  2. Overview 1. Uses of Trees 2. Huffman Codes 3. (Rooted) Tree Terminology 4. Spanning Trees 5. Minimal Spanning Trees 6. Game Trees 7. More Information

  3. 1. Uses of Trees Davenport S. Williams S. Williams S. Williams Bartoli V. Williams V. Williams Wimbledon Womens Tennis

  4. Organizational Chart President Vice-President for Academics Vice-President for Admin. Dean of Engineering Dean of Business Planning Officer Purchases Officer . . . . . . . . Head of CoE Head of EE Head of AC. . . . .

  5. Saturated Hydrocarbons H H H H • Non-rooted (free) trees • a free tree is a graph with no cycles C H H C C C H H C H H H C H H H C H H Isobutane H C H H Butane H

  6. A Computer File System / usr bin tmp bin ad spool ls mail who junk ed vi exs opr uucp printer

  7. 2. Huffman Codes • A Huffman code represents characters by variable-length bit strings • there are many different Huffman codes • By comparison, ASCII uses fixed-length codes. • Character ASCII code A 1000001 B 1000010 : : S 1010011 : :

  8. A Huffman Code in Tree Form Root • Character Code A 1 O 00 R 010 S 0110 T 0111 1 0 A 1 0 O 1 0 1 0 R T S where do these come from?

  9. Huffman uses Less Bits useful for network comms, text compression, etc. • ASCII coding of "RATS": • 1010010 1000001 1010100 1010011 • 4*7 = 28 bits • Huffman Coding (as in the tree): • 010 1 0111 0110 • 12 bits

  10. Constructing a Huffman Code • A Huffman code is based on a table giving the frequency of the characters in the 'language'. • The 'language' can be a subset of a full language • e.g. business English, computer language keywords, the months of the year, a language made up of only {A, O, R, S, T}

  11. Letter Frequencies for English • The most common letters, in full English, in order of frequency, are usually ETAOINSHRDLU. • This varies depending on the data source • e.g. novels, textbooks, cartoons

  12. Example: The Haddock Language • The Haddock language contains only 5 characters: • !, @, #, $, % • we can make words like: • !!!@@ $%$# @@@@@@@@@ • We must create a frequency table by analysing existing examples of Haddock writing. continued

  13. The frequency table we create: • Character Frequency ! 2/32 @ 3/32 # 7/32 $ 8/32 % 12/32 • We work with the numerators of the fractions.

  14. Informal Algorithm • 1. Make a sequence of the frequencies: { 2, 3, 7, 8, 12} • 2. Repeatedly replace the smallest two frequencies by a sum until only 2 elements are left: • {2,3,7,8,12} ==> {2+3, 7, 8, 12}{5,7,8,12} ==> {5+7, 8, 12}{8,12,12} ==> {8+12, 12}{12,20} continued

  15. 3. Build a tree by expanding the sequence back to its full size. • Start with the 2 elements as a 2 branch tree. 1 1 0 1 0 0 ==> ==> ==> 1 0 1 0 1 0 12 20 12 8 12 5 7 8 12 continued

  16. 1 0 ==> 1 0 1 0 1 0 7 8 12 • 4. Replace each frequency by the character having that frequency: 2 3 1 0 1 0 1 0 1 0 # $ % ! @ continued

  17. Huffman Code for Haddock • Character Code ! 111 @ 110 # 10 $ 01 % 00 • The higher frequency letters have shorter Huffman codes.

  18. Another Possible Answer 1 0 • Why? There are two "12" nodes (see slide 12), either of which can be expanded back to "5" and "7". 12 1 0 1 8 0 1 0 7 2 3 continued

  19. Huffman Code for Haddock v.2 • Character Code ! 0011 @ 0010 # 000 $ 01 % 1 • The higher frequency letters have shorter Huffman codes.

  20. Huffman Algorithm • Tree huffman(FreqSeq f, int size){ if (size == 2){ let f1, f2 be the frequencies in f return baseTree; else { let fi, fj be the smallest freqs in f; replace fi and fj in f with fi+fj; Tree t1 = huffman(f, size-1); Tree t = replace "fi+fj" vertex in t1 with extTree return t; }} 1 0 f1 f2 baseTree 1 0 fi fj extTree call: Tree t = huffman( {2,3,7,8,12}, 5);

  21. 3. (Rooted) Tree Terminology • e.g. Part of the ancient Greek god family: levels 0 Uranus 1 Aphrodite Kronos Atlas Prometheus 2 Eros Zeus Poseidon Hades Ares 3 Apollo Athena Hermes Heracles : :

  22. Some Definitions • Let T be a tree with root v0. • Suppose that x, y, z are verticies in T. • (v0, v1,..., vn) is a simple path in T (no loops). • a) vn-1 is the parent of vn. • b) v0, ..., vn-1 are ancestors of vn • c) vn is a child of vn-1 continued

  23. d) If x is an ancestor of y, then y is a descendant of x. • e) If x and y are children of z, then x and y are siblings. • f) If x has no children, then x is a terminal vertex (or a leaf). • g) If x is not a terminal vertex, then x is an internal (or branch) vertex. continued

  24. h) The subtree of T rooted at x is the graph with vertex set V and edge set E • V contains x and all the descendents of x • E = {e | e is an edge on a simple path from x to some vertex in V} • i) The length of a path is the number of edges it uses, not verticies. continued

  25. j) The level of a vertex x is the length of the simple path from the root to x. • k) The height of a vertex x is the length of the simple path from x to the farthest leaf • the height of a tree is the height of its root • l) A tree where every internal vertex has exactly m children is called a full m-ary tree.

  26. Applied to the Example • The root is Uranus. • A simple path is {Uranus, Aphrodite, Eros} • The parent of Eros is Aphrodite. • The ancestors of Hermes are Zeus, Kronos, and Uranus. • The children of Zeus are Apollo, Athena, Hermes, and Heracles. continued

  27. The descendants of Kronos are Zeus, Poseidon, Hades, Ares, Apollo, Athena, Hermes, and Heracles. • The leaves (terminal verticies) are Eros, Apollo, Athena, Hermes, Heracles, Poseidon, Hades, Ares, Atlas, and Prometheus. • The branches (internal verticies) are Uranus, Aphrodite, Kronos, and Zeus. continued

  28. The subtree rooted at Kronos: Kronos Zeus Poseidon Hades Ares Apollo Athena Hermes Heracles continued

  29. The length of the path {Uranus, Aphrodite, Eros} is 2 (not 3). • The level of Ares is 2. • The height of the tree is 3.

  30. Some Properties of Trees • 1) A tree with n verticies has n-1 edges. • e.g. • Proof: • ignore the root; • pair each vertex with the edge above it; • so n-1 verticies use n-1 edges (all of them)

  31. 2) A full m-ary tree with i internal verticies has a total of m*i + 1 verticies. • e.g. A full 2-ary tree (binary tree) 4 internal verticies A total of 9 verticies continued

  32. Proof • ignore the root; • every remaining vertex is the child of an internal vertex; • each internal vertex has m children; • there are i internal verticies, so there is a total of m*i verticies in the tree; • add back the root, to get m*i + 1

  33. 4. Spanning Trees • A spanning tree T is a subgraph of a graph G which contains all the verticies of G. • Example graph G: a b c d e f h g continued

  34. One possible spanning tree: a b the tree is drawn with thick lines c d e f h g

  35. 4.1. Example: IP Multicasting • A network of computers and routers: source computer router continued

  36. How can a packet (message) be sent from the source computer to every other computer? • The inefficient way is to use broadcasting • send a copy along every link, and have each router do the same • each router and computer will receive many copies of the same packet • loops may mean the packet never disappears! continued

  37. IP multicasting is an efficient solution • send a single packet to one router • have the router send it to 1 or more routers in such a way that a computer never receives the packet more than once • This behaviour can be represented by a spanning tree. continued

  38. The spanning tree for the network: source computer the tree is drawn with thick lines router

  39. 4.2. Finding a Spanning Tree • There are two main types of algorithms: • breadth-first search • depth-first search

  40. 4.3. Breadth-first Search • Process all the verticies at a given level before moving to the next level. • Example graph G (again): a b c d e f h g

  41. Informal Algorithm • 1) Put the verticies into an ordering • e.g. {a, b, c, d, e, f, g, h} • 2) Select a vertex, add it to the spanning tree T: e.g. a • 3) Add to T all edges (a,X) and X verticies that do not create a cycle in T • i.e. (a,b), (a,c), (a,g) T = {a, b, c, g} a g b c continued

  42. a level 1 g b c • Repeat step 3 on the verticies just added, these are on level 1 • i.e. b: add (b,d) c: add (c,e) g: nothing T = {a,b,c,d,e} • Repeat step 3 on the verticies just added, these are on level 2 • i.e. d: add (d,f) e: nothing T = {a,b,c,d,e,f} d e a g b c level 2 d e f continued

  43. a g • Repeat step 3 on the verticies just added, these are on level 3 • i.e. f: add (f,h) T = {a,b,c,d,e,f,h} • Repeat step 3 on the verticies just added, these are on level 4 • i.e. h: nothing, so stop b c d e level 3 f h continued

  44. Resulting spanning tree: a b a different spanning tree from the earlier solution c d e f h g

  45. Breadth-first Algorithm • Tree bfs(Verts vsG, Edges edsG)// input ordered verts and edges of graph G{ Vert v1 = firstVert(VsG); // first vert in G Seq levelVs = ( v1 ); // verts at curr level Verts vsT = { v1 }; // verts in tree Edges edsT = {}; // edges in tree while (1) { for each x in levelVs for each y in vsG - vsT if (x,y) is an edge in edsG add (x,y) to edsT; add y to vsT; if no edges added to vsT return tree made from vsT and edsT; levelVs = childrenOf( levelVs ); }} continued

  46. Typical call: Verts vsG = {a, b, c, d, e, f, g, h};Edges edsG = { (a,b), (a,c), (a,g), (b,d), (b,g), (c,d), (c,e), (d,f), (e,g), (f,h) };Tree spanTree = bfs( vsG, edsG ); :

  47. 4.4. Depth-first Search • Process all the verticies down one path, then backtrack (go back) to verticies along other paths. • Example graph G (again): a b c d e f h g

  48. Informal Algorithm • 1) Put the verticies into an ordering • e.g. {a, b, c, d, e, f, g, h} • 2) Select a vertex, add it to the spanning tree T: e.g. a • 3) Add the edge (a,X) where X is the smallest vertex in the ordering, and does not make a cycle in T • i.e. (a,b), T = {a, b} a b continued

  49. a b d • 4) Repeat step 3 with the new vertex, until there is no possible new vertex • i.e. add the edges (b,d) (d,c) (c,e) (e,f) (f,h) T = {a,b,d,c,e,f,h} • 5) At this point, there is no (h,X), so backtrack to a vertex that does have another edge: • parent of h == fbut there is no new (f,X) to add, so backtrack c e f h continued

  50. a b • parent of f == e • there is an (e,g) to add, so repeat step 3 with e • 6) After g is added, there are no further verticies to add, so stop. d c e g f h continued