Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mathematical Induction II

Similar presentations


Presentation on theme: "Mathematical Induction II"— Presentation transcript:

1 Mathematical Induction II
Lecture 21 Section 4.3 Thu, Feb 17, 2005

2 Example: Binary Search Trees
In a complete binary search tree of depth n - 1, (n rows) what is the average number of comparisons required to locate an element? (Assuming all positions are equally likely.) Analysis Elements in row 1 require 1 comparison. Elements in row 2 require 2 comparisons. Elements in row 3 require 3 comparisons. In general, elements in row k require k comparisons.

3 Example: Binary Search Trees
Further analysis Row 1 contains 1 element. Row 2 contains 2 elements. Row 3 contains 4 elements. In general, row k contains 2k - 1 elements. Fact The average number of comparisons is the total number of comparisons required for all elements divided by the number of elements.

4 Example: Binary Search Trees
The total number of elements is … + 2n – 1. The total number of comparisons is 11 + 22 + 34 + 48 + … + n2n – 1. Therefore, the average is (11 + 22 + … + n2n – 1)/( … + 2n – 1) Yuck!

5 Example: Binary Search Trees
Prove by mathematical induction that … + 2n – 1 = 2n – 1. 11 + 22 + 34 + 48 + … + n2n – 1 = (n – 1)2n + 1. Then, the average is [(n – 1)2n + 1]/(2n – 1). This is approximately equal to n – 1 (for large n).

6 Binary Search Trees In reality, it often requires two comparisons at each node. Is the value less than the node? If not, is the value greater than the node? If not, then the value equals the node. In other words, we must make a three-way decision at each node.

7 Binary Search Trees So, exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60

8 Binary Search Trees So, exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60 1 2 2

9 Binary Search Trees So, exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60 1 2 2 1 2 3

10 Binary Search Trees So, exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60 1 2 2 1 2 3 4 5

11 Binary Search Trees So, exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60 1 2 2 1 2 1 2 3 4 4 5

12 Binary Search Trees So, exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60 1 2 2 1 2 1 2 3 4 4 5 5 6

13 Binary Search Trees It takes an average of 1.5 comparisons to move down one level. Then it takes 2 comparisons to match a value at that level. Level 1: 2 comparisons. Level 2: = 3.5 comparisons. Level 3: 2(1.5) + 2 = 5 comparisons. Level n: (n – 1)(1.5) + 2 = 1.5n

14 Binary Search Trees To find the average, we must compute
12 + 2 5 + … + 2n – 1(1.5n + 0.5). We could work this our from scratch… Or use what we have already worked out:  2n – 1(1.5n + 0.5) = (1.5  2n – 1n) + (0.5  2n – 1) = (1.5((n – 1)2n + 1)) + (0.5(2n – 1)) = (1.5n – 1)2n + 1.

15 Binary Search Trees Therefore, the average is
((1.5n – 1)2n + 1)/(2n – 1). For large n, this is approximately 1.5n – 1.

16 Binary Search Trees What if we changed the order in which we did the comparisons at each node? Is the value equal to the node? If not, is the value less than the node? If not, then the value is greater than the node. Will that be more or less efficient than the previous order?

17 Binary Search Trees Exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60

18 Binary Search Trees Exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60 1 2

19 Binary Search Trees Exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60 1 2 3

20 Binary Search Trees Exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60 1 2 5 3

21 Binary Search Trees Exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60 1 2 5 3

22 Binary Search Trees Exactly how many comparisons does it take to locate an element? 40 20 70 50 30 10 60 1 2 5 3

23 Binary Search Trees To find the average, we must compute
11 + 23 + 45 + … + 2n – 1(2n – 1). Again, we can use what we have already worked out:  2n – 1(2n – 1) = (2  2n – 1n) – ( 2n – 1) = (2((n – 1)2n + 1)) – (2n – 1) = (2n – 3)2n + 3

24 Binary Search Trees Therefore, the average is
((2n – 3)2n + 3)/(2n – 1). For large n, this is approximately 2n – 3. This is larger (worse) than 1.5n – 1. Therefore, it is better not to check for equality first when searching a binary tree.

25 Binary Search Trees Would it be more efficient to store all the data at the leaves, but store reference values at the nodes for navigating the tree? If value < node, move left, else move right. When we reach a leaf node, we should verify that we have a match.

26 Binary Search Trees At each node we would make only a two-way decision (more efficient), But the tree would be larger (less efficient). On balance, would this scheme be more efficient or less efficient, in terms of the number of comparisons?

27 Binary Search Trees Consider the case of 7 values 10, 20, 30, 40, 50, 60, 70, in a balanced tree. 40 20 70 50 30 10 60

28 Binary Search Trees Consider such a tree with 7 values. 40 20 70 50 30
10 60

29 Binary Search Trees Consider such a tree with 7 values. 40 20 70 50 30
10 60 1

30 Binary Search Trees Consider such a tree with 7 values. 40 20 70 50 30
10 60 1

31 Binary Search Trees Consider such a tree with 7 values. 40 20 70 50 30
10 60 1

32 Binary Search Trees Consider such a tree with 7 values. 40 20 70 50 30
10 60 1 5 5 5 5 5 5 5

33 Binary Search Trees The depth of this tree is n + 1.
For each value, n comparisons are needed to reach the leaf. Then 2 comparisons are needed to match. 1 to confirm that it is a leaf node. 1 to match the values.

34 Binary Search Trees Therefore, for every value, the number of comparisons is n + 2. Therefore, the average is n + 2. Is this an improvement over the previous methods?

35 Example: Recursive Sequences
Define a sequence a1 = 2, an = 2an – 1 – 1 for n  2. Find a non-recursive formula for an and prove that it is correct. Analysis {an} = {2, 3, 5, 9, 17, 33, …}. {an – 1} = {1, 2, 4, 8, 16, 32, …}. Conjecture that an = 2n –

36 Example: Recursive Sequences
Proof: Basic Step P(1) is true since a1 = 2 = Inductive Step Suppose that ak = 2k – for some k  1.

37 Example: Recursive Sequences
Then ak + 1 = 2ak – 1 (by def.) = 2(2k – 1 + 1) – 1 (by ind. hyp.) = 2k + 2 – 1 = 2k + 1. Therefore, P(k + 1) is true. Therefore, an = 2n + 1 for all n  1.

38 Example: Contest Problem
Let n be a positive integer. Suppose we have n red dots and n blue dots in the plane such that no three dots are collinear. Prove that it is possible to connect the red dots to the blue dots in distinct pairs such that none of the line segments intersect.

39 Example: Contest Problem

40 Example: Contest Problem

41 Example: Contest Problem
Proof: Basic Step P(1) is obviously true since there is only one pair of dots and only one segment.

42 Example: Contest Problem
Proof: Basic Step P(1) is obviously true since there is only one pair of dots and only one segment.

43 Example: Contest Problem
Inductive Step Suppose P(1), …, P(k) are true for some k  1. Now suppose we have a collection of k + 1 red dots and k + 1 blue dots. Consider the convex hull of this set.

44 Example: Contest Problem
There are two possibilities. Case 1: The dots on the convex hull are not all the same color. Case 2: The dots on the convex hull are all the same color.

45 Example: Contest Problem
There are two possibilities. Case 1: The dots on the convex hull are not all the same color. Case 2: The dots on the convex hull are all the same color.

46 Example: Contest Problem
Case 1: Suppose they are not all the same color. Choose two adjacent dots of different colors on the convex hull and connect them with a segment.

47 Example: Contest Problem

48 Example: Contest Problem

49 Example: Contest Problem

50 Example: Contest Problem
By induction, the remaining collection of k red dots and k blue dots may be connected so that no two segments intersect. Nor will any of those segments intersect the first segment because it is on the convex hull and no three dots are collinear. Therefore, the entire set of dots may be connected with non-intersecting segments.

51 Example: Contest Problem

52 Example: Contest Problem

53 Example: Contest Problem
Case 2: Suppose they are all the same color, say red. Choose a line that is not parallel to any segment connecting any two dots. Move the line across the set of dots, from right to left. Keep count of the number of red dots and the number of blue dots to the right of the line.

54 Example: Contest Problem
0 red dots, so far (r = 0). 0 blue dot, so far (b = 0).

55 Example: Contest Problem
1 red dots, so far (r = 1). 0 blue dot, so far (b = 0).

56 Example: Contest Problem
1 red dots, so far (r = 1). 1 blue dot, so far (b = 1).

57 Example: Contest Problem
2 red dots, so far (r = 2). 1 blue dot, so far (b = 1).

58 Example: Contest Problem
3 red dots, so far (r = 3). 1 blue dot, so far (b = 1).

59 Example: Contest Problem
Initially, r – b = 0. At the end, r – b = 0. Just after passing the first dot, r – b = 1. Just before passing the last dot, r – b = –1. The line meets only one dot at a time. Therefore, somewhere in between, r – b must be 0.

60 Example: Contest Problem
3 red dots (r = 3). 1 blue dot (b = 1). r – b = 2.

61 Example: Contest Problem
3 red dots (r = 3). 2 blue dot (b = 2). r – b = 1.

62 Example: Contest Problem
3 red dots (r = 3). 3 blue dot (b = 3). r – b = 0.

63 Example: Contest Problem
Apply the induction hypothesis. The dots on the right may be connected with non-intersecting segments. The dots on the left may be connected with non-intersecting segments. Therefore, the entire set of dots may be connected with non-intersecting segments.

64 Example: Contest Problem
Therefore, P(n) is true for all n  1.

65 Example: Contest Problem
Therefore, P(n) is true for all n  1.

66 Example: Contest Problem
Therefore, P(n) is true for all n  1.


Download ppt "Mathematical Induction II"

Similar presentations


Ads by Google