Upload
jack-wilcox
View
215
Download
1
Embed Size (px)
Citation preview
Second Midterm Exam• 1st Lecture in December (same time as the
lecture)
• 75 minute duration
• Will cover all lectures delivered before the exam date
• Will consist of MCQ’s, fill-in-the-blanks, questions with short answers, writing of proofs, and drawing of diagrams
• If you miss this exam for any reason, you will have to appear for a makeup exam on the Thursday of the last week of teaching. That exam will cover all lectures delivered in the semester. It will consist of writing of proofs, drawing of diagrams and answering questions having 0.5-1 page answers.
Trees
A tree is an undirected, connected graph with no simple circuits
They are called trees because they look like trees. For example, family trees are graphs that represent genealogical chart
Trees as Models: ExamplesChemistry: Trees were introduced for counting the number of isomers of saturated hydrocarbons. The two isomers of butane are shown at the right
Computer Science: The organization of a computer file system into directories,subdirectories, and files is naturally represented as a tree
Business: Trees are usedto represent the structureof organizations
Which Graphs are Trees?Theorem: An undirected graph is a tree iff
there is a unique, simple path between any two of its vertices
G1 is a tree as it is a connected graphs with no
simple circuits
G2 is a tree as it is a connected graphs with no
simple circuits
G3 is not a tree as e, b, a, d, e is a simple circuit in
this graph
G4 is not a tree as it is not connected
Which Graphs are Trees?A connected graph that contains no simple circuits is a tree
Because a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops. Therefore any tree must be a simple graph
What if there are no simple circuits but the graph is not connected? Each of theconnected componentsis a tree and the collectionis called a forest
Rooted TreesA rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root
We usually draw a rooted tree with its root at the top of the graph
We can change an unrooted tree into a rooted tree by choosing any vertex as the root
Different choices of the root produce different rooted trees
Parent, Child & Sibling VerticesIf v is a vertex of rooted tree T other than the root, the parent of v is the unique vertex u such that there is a directed edge from u to v• The parent of a vertex is the vertex connected to
it on the path to the root• Every vertex except the root has a unique parent
When u is the parent of v, v is called the child of u• A child of a vertex v is a vertex of which v is the
parent
Vertices sharing a parent are called siblings
Parent, Child & Sibling Vertices
a
b c d
e f g h
i j k l m
Siblings
a is the parent of b, c, and d
e is the child of b
Ancestor & Descendent Vertices
The ancestors of a vertex (other than the root) are the vertices in the path from the root to this vertex, excluding the vertex itself and including the root
The descendants of a vertex v are those vertices that have v as an ancestor
LeavesA vertex with no children is called a leaf
a
b c d
e f g h
i j k l m
c, g, i, j, k, l and m are leaves
Internal VerticesVertices with children are called internal
vertices a
b c d
e f g h
i j k l m
a, b, d, e, f and h are internal vertices
Subtrees
If a is a vertex in a tree, the subtree with a as its root is the subgraph of the tree consisting of:
• a and its descendants, and
• all edges incident to these descendants
m-ary and Full m-ary Trees
A rooted tree is called an m-ary tree if every internal vertex has no more than m children
A tree is called a full m-ary tree if every internal vertex has exactly m children
An m-ary tree with m 2 is called a binary tree
Full Binary Trees: ExampleWhat is the arity of this tree?
This is a binary (i.e.2-ary) tree
Is this a full m-ary tree?
Yes, this is a full binary tree since every internal vertex has exactly 2 children
Full 3-ary Trees: ExampleWhat is the arity of this tree?
This is a 3-ary tree
Is this a full m-ary tree?
Yes, this is a full 3-ary tree since every internal vertex has exactly 3 children
Full 5-ary Trees: ExampleWhat is the arity of this tree?
This is a 5-ary tree
Is this a full m-ary tree?
Yes, this is a full 5-ary tree since every internal vertex has exactly 5 children
3-ary Trees: ExampleWhat is the arity of this tree?
Some internal nodes have 2 children, but some have 3, so this is a 3-ary tree
Is this a full m-ary tree?
No. It is not a full-3-arytree, since oneinternal node hasonly 2 children
Ordered Rooted Tree
An ordered rooted tree is onewhere the children of eachinternal vertex are ordered
Ordered Rooted Tree: Left ChildIn an ordered binary tree, if an internal vertex has two children, then the left one is called the left child
f is the leftchild of d
a
b c
d e h i
f g j k l
m
a
b c
d e h i
f g j k l
mg is the rightchild of d
Ordered Rooted Tree: Right ChildIn an ordered binary tree, if an internal vertex has two children, then the right one is called the right child
Ordered Rooted Tree: Left Subtree
a
b c
d e h i
f g j k l
mLeft subtree of c
The subtree rooted at the left child of a vertex is called the left subtree
Ordered Rooted Tree: Right Subtree
a
b c
d e h i
f g j k l
m Right subtree of c
The subtree rooted at the right child of a vertex is called the right subtree
Level of a Vertex
Levels
a
b c d
e f g h
i j k l m
0
1
2
3
The level of a vertex in a rooted tree is the length of the unique path from the root to this vertex. The level of the root vertex is defined to be 0
Height of a TreeThe height of a rooted tree is the maximumof the levels of the vertices
a
b c d
e f g h
i j k l m
0
1
2
3
Height = 3
Levels
Level of a Vertex & Height of a Tree: Example
a is at level 0b, j, k are at level 1c, e, f, l are at level 2d, g, i, m, n are at level 3h is at level 4
Because the largestlevel of any vertex is 4,this tree’s height is 4
h
a
Properties of Trees
1. A tree with n vertices has n - 1 edges
2. A full m-ary tree with i internal vertices contains m · i + 1 vertices
3. There are at most mh leaves in an m-ary tree of height h
Section 11.1: Exercises1.a)Which vertex is the root?b)Which vertices are internal?c) Which vertices are leaves?d)Which vertices are
children of j?e)Which vertex is
the parent of h?f) Which vertices
are siblings of o?g)Which vertices are ancestors of m?h)Which vertices are descendants of b?i) What is the arity of this tree? Is it a full m-
ary tree?j) What is the level of each vertex?k)What is the height of the tree?l) Draw the subtrees rooted at a and c.
Section 11.1: Exercises17.How many edges does a tree with 200 vertices
have?
18.How many edges does a full binary tree with 500 internal vertices have?
19. Draw a tree having 6 vertices.
20. Draw a graph which has 6 vertices but is not a tree.
21. Draw an unbalanced tree having 6 vertices.
22. Draw a forest having 6 vertices.
Second Midterm Exam• 1st Lecture in December (same time as the
lecture)
• 75 minute duration
• Will cover all lectures delivered before the exam date
• Will consist of MCQ’s, fill-in-the-blanks, questions with short answers, writing of proofs, and drawing of diagrams
• If you miss this exam for any reason, you will have to appear for a makeup exam on the Thursday of the last week of teaching. That exam will cover all lectures delivered in the semester. It will consist of writing of proofs, drawing of diagrams and answering questions having 0.5-1 page answers.
Binary Search TreesSearching for items in a list is an important task
The goal of search is to efficiently find the required item in an ordered collection
A binary search tree is a good tool for this purpose
A binary search tree is binary tree in which each child of a vertex is designated as the left or right child, and each vertex is labeled with a key, which is one of the search itemsVertices are assigned keys so that key of a vertex is both
larger than the keys of all vertices on its left subtree and smaller than the keys of all vertices in the right subtree
Binary Search Trees: ExampleFind a binary search tree for the words mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry using alphabetical ordering
Decision TreesRooted trees can be used to model a
decision process in which a sequence of decision leads to a solution
A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision treeThe possible solutions of the problem
correspond to the paths to the leaves of this rooted tree
Decision Trees: ExampleSuppose there are 7 identical coins, and a counterfeit coin, that weighs less than the others. How many weighings are necessary using a balance scale to determine which of the 8 coins is the counterfeit one?
Using Bit-Strings to Encode Text
Consider the problem of data compression. Suppose that we have a 100,000-character file that contains only 6 different characters, a, b, c, d, e, and f. Some of these characters occur more frequently than others
Currently, the file is stored using a fixed-length code of 3 bits per character, where:
a = 000 , b = 001, c = 010, d = 011, e = 100, f = 101
This requires 300,000 bits to store the file
Prefix CodesWe can use Huffman coding which uses a variable-length code to compress the file
We can use a 0 to represent the most frequently-occurring letter in the file, which will save us two bits per occurrence
Huffman codes are prefix codes, which means that all bit patterns are unambiguous, i.e., no bit-string corresponds to more than one sequence of letters. This means that bit-string of one letter never occurs as the first part of the bit-string for another letter
Using Huffman coding, we can store the file in 224,000 bits
Prefix Codes: Examples10
1
1
1
1
0
0
0
0
0 = a
10 = b
110 = c
1110 = d11110
= e11111
= f
10
1
10
1
10
0 = a
101 = b
100 = c
111 = d
1101 = e
1100 = f
0 0
Huffman Codes: Codewordsa b c d e f
Frequency of occurrence (in thousands)
45 13 12 16 9 5
Codeword (fixed-length)
000
001
010
011 110 101
Codeword(variable length)
0 101
100
111
1101
1100
Huffman Codes are Unambiguous
Since we are using a prefix code, we can simply concatenate our codewords together to produce our bit-string
Example: The string abc can be represented as 0101100
This is unambiguous. That is, this character-string and bit-string pairing is unique
Huffman Code DecodingOnly one character can begin with 0; that is a. So a must be the first character in our string
This leaves 101100. The next bit is a 1; five characters can begin with 1, so we look at the second bit. Two characters can begin with 10, so we look at the third bit. Only one character can begin with 101; that is b
This leaves 100. Again, looking at all three bits, we see that this character must be c
Game TreesTrees can be used to model and analyze certain2-player games like tic-tack-toe
In game trees, vertices represent the position that a game can be in as it progresses
The edges represent legal moves between these positions
The root represents the starting point
When the game is at position represented by a vertex at an even level, it is the first player’s move. Otherwise, it is the second player’s move
The leaves of the game tree represent the concluding positions of a game
Tree Traversal
Procedures for systematically visiting every vertex of an ordered tree are called traversals
The three most commonly used traversals are:1. Preorder traversal2. Inorder traversal3. Postorder traversal
Preorder TraversalLet T be an ordered rooted tree with root r:1.If T consists only of r, then r is the preorder
traversal of T2.Otherwise, suppose that T1, T2, …, Tn are the
subtrees of r from left to right in T. The preorder traversal begins by visiting r, andcontinues by traversingT1 in preorder, then T2
in preorder, andso on, until Tn istraversed inpreorder
Preorder Traversal (contd.)
procedure preorder (T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to right T(c) := subtree with c as root preorder(T(c))
Inorder TraversalLet T be an ordered rooted tree with root r:1.If T consists only of r, then r is the inorder
traversal of T2.Otherwise, suppose that T1, T2, …, Tn are the
subtrees of r from left to right in T. The inorder traversal begins by traversing T1
in inorder, then visiting r,and continues bytraversing T2
in inorder, andso on, until Tn
is traversedin inorder
Inorder Traversal (contd.)
procedure inorder (T: ordered rooted tree)r := root of Tif r is a leaf then list relse l := first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list(r) for each child c of r from left to right T(c) := subtree with c as root inorder(T(c))
Postorder TraversalLet T be an ordered rooted tree with root r:1. If T consists only of r, then r is the postorder
traversal of T2.Otherwise, suppose that T1, T2, …, Tn are the
subtrees of r from left to right in T. The postorder traversal begins by traversing T1
in postorder, then T2
in postorder, andso on, after Tn
is traversedin postorder,r is visited
Postorder Traversal (contd.)
Procedure postordered (T: ordered rooted tree)r := root of Tfor each child c of r from left to right T(c) := subtree with c as root postorder(T(c))list r
Expression TreesComplex expressions (e.g. compound propositions, set combinations, arithmetic expressions) can be represented using ordered rooted tree
Example:A binary tree representing((x + y) ↑ 2 ) + ((x − 4)/3)
Expression Forms: Infix, Prefix, PostfixInfix expression form has operators written between operands. Parentheses are sometimes added to infix expressions to remove ambiguity. Example: x + y
Prefix expression form (or Polish notation form) has operators written before the operands. Parentheses are never needed for expressions in the prefix form. Example: + x y
Postfix expression form (or reverse Polish notation form) has operators written after the operands. Parentheses are never needed for expressions in the postfix form. Example: x y +
Note: All three example expressions shown above lead to the exact same result, i.e. the sum of x and y
Infix NotationAn inorder traversal of the tree representing an expression produces the original expression when parentheses are included
Trees shown bellow all lead to the same infix expression x + y/x + 3
To make such expressions unambiguous, it is necessary to include parentheses in the inorder traversal whenever we encounter an operation. The fully parenthesized expression obtained this way is said to be in the infix form
(x + (y/x)) + 3 x + (y/(x + 3))(x + y)/(x + 3)
Infix NotationWe obtain the infix form of an expression by
traversing its binary tree in inorder
x + y/x + 3 x + y/x + 3x + y/x + 3 (x + (y/x)) + 3 x + (y/(x + 3))(x + y)/(x + 3)
Trees shown bellow all lead to the same infix expressionx + y/x + 3. To make infix expressions unambiguous, it is necessary to include parentheses in the inorder traversal whenever we encounter an operation
Prefix NotationWe obtain the prefix form of an
expression by traversing its binary tree in preorder
Prefix form of ((x + y) ↑ 2 ) + ((x − 4)/3) is + ↑ + x y 2 / − x 4 3
Prefix expressions are evaluated by working from right to left. When we encounter an operator, we perform the corresponding operation with the two operations to the right
Example: We show the steps used to evaluate a particular prefix expression
Postfix NotationWe obtain the postfix form of an
expression by traversing its binary tree in postorder
Postfix form of ((x + y) ↑ 2 ) + ((x − 4)/3) is x y + 2 ↑ x 4 − 3 / +
A binary operator follows its two operands. So, to evaluate an expression one works from left to right, carryingout an operation represented by an operator on its preceding operands
Example: We show the steps used to evaluate a particular postfix expression