Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion CSC 172 SPRING 2002 LECTURE 8 Visual Proof 123.n0 1 2 3 n.

Similar presentations


Presentation on theme: "Recursion CSC 172 SPRING 2002 LECTURE 8 Visual Proof 123.n0 1 2 3 n."— Presentation transcript:

1

2 Recursion CSC 172 SPRING 2002 LECTURE 8

3 Visual Proof 123.n0 1 2 3 n

4 1..(n+1)/2..n0 1 2 3 n

5 Visual Proof 1..(n+1)/2..n0 1 2 3 n

6 Visual Proof 1..(n+1)/2..n0 1 2 3 n

7 Visual Proof 1..(n+1)/2..n0 1 2 3 n

8 Visual Proof 1..(n+1)/2..n0 1 2 3 n+1 n

9 Visual Proof 1..(n+1)/2..n0 1 2 3 n+1 n n/2

10 Visual Proof 1..(n+1)/2..n0 1 2 3 n+1 n

11 Visual Proof 1..(n+1)/2..n0 1 2 3 n+1 n

12 Visual Proof 1..(n+1)/2..n0 1 2 3 n+1 n

13 Recursion: Basis & Induction Usually thought of as a programming technique Can also serve as a method of definition

14 Recursive Definition of Expressions Example: expressions with binary operators Basis: An operand (variable or constant) is an expression - x1, y, 3, 57, 21.3

15 Recursive Definition of Expressions Induction: 1. If E is an expression, then (E) is an expression 2. If E 1 and E 2 are expressions, and ° is a binary operator (e.g. +, *), then E 1 ° E 2 is an expression Thus we can build, x, y, 3, x+y, (x+y), (x+y)*3

16 An Interesting Proof S(n): An expression E with binary operators of length n has one more operand than operators. Proof is by complete induction on the length (number of operators, operands, and parentheses) of the expression

17 S(n): An expression E with binary operators of length n has one more operand than operators. Basis: n=1 E must be a single operand Zero operators

18 S(n): An expression E with binary operators of length n has one more operand than operators. Induction (complete): Assume S(1), S(2),... S(n). Let E have length n+1 > 1 How was E constructed? Rule 1, or Rule 2?  If by rule 1, then E = (E 1 )  E1 has length n-1  BTIH: S(n-1) has one more operand than operators  E and E1 have the same number of operands & operators  So, S(n+1) will hold under construction by rule 1

19 S(n): An expression E with binary operators of length n has one more operand than operators. If by rule 2, then E = E 1 ° E 2 Both E 1 and E 2 have length <= n, because ° is one symbol length(E 1 ) + length(E 2 ) = n Let E 1 and E 2 have a and b operators BTIH E 1 and E 2 have a+1 and b+1 operands Thus, E has (a+1)+(b+1) = a+b+2 operands E has a+b+1 operators (the “+1” is for the “°”)

20 S(n): An expression E with binary operators of length n has one more operand than operators. E has (a+1)+(b+1) = a+b+2 operands E has a+b+1 operators Note: we used all of S(1),S(2),...,S(n) in the inductive step The fact that “expression” was defined recursively let us break expressions apart. We don’t know how it broke up, but we have all the cases covered.

21 Recursion: an organization of process A style of programming and problem-solving where we express a solution in terms of smaller instances of itself. Uses basis/induction just like inductive proofs Basis: needs no smaller instances Induction: solution in terms of smaller instances

22 Sorting Lists with MergeSort Note: a list of length 1 is sorted If two lists are sorted, how can you combine them into one sorted list? How can you divide one list into two?

23 Recursive Mergesort Basis: A list of length 1 is sorted Induction: for lists of >= 1 element 1. Split the list into two equal (as possible) parts 2. Recursively sort each part 3. Merge the result of the two recursive sorts One at a time, select the smaller element from the two fronts of each list Physical demo

24 Simplified MergeSort A linked list is a list of nodes with Node getNext(), setNext() methods. and getData(), setData methods.

25 Node split public static Node split(Node head){ Node secondNode; if (head == null) return null; else if (head.getNext() == null) return null; else { secondNode = head.getNext(); head.setNext(secondNode.getNext()); secondNode.setNext(split(secondNode.getNext()); return secondNode; }

26 A linked List 6 1 3 2 5 9 

27 A linked List in Split 6 1 3 2 5 9  Split head

28 A linked List in Split 6 1 3 2 5 9  Split 0 head secondNode secondNode = head.getNext();

29 Split 0 head secondNode A linked List in Split 6 1 3 2 5 9  head.setNext(secondNode.getNext());

30 Split 0 head secondNode First recursive call in Split 6 1 3 2 5 9  secondNode.setNext(split(secondNode.getNext());

31 Split 1 head secondNode New Split 6 1 3 2 5 9  split(secondNode.getNext());

32 Split 1 head secondNode New Split 6 1 3 2 5 9  head.setNext(secondNode.getNext());

33 Split 1 head secondNode Second recursive Split 6 1 3 2 5 9  secondNode.setNext(split(secondNode.getNext());

34 Split 2 head secondNode Second recursive Split 6 1 3 2 5 9  secondNode.setNext(split(secondNode.getNext());

35 Split 2 head secondNode Second recursive Split 6 1 3 2 5  9  head.setNext(secondNode.getNext()); secondNode.setNext(split(secondNode.getNext()); ? What gets returned? return secondNode;

36 Split 1 head secondNode Backing up 6 1 3 2 5  9  secondNode.setNext(split(secondNode.getNext());

37 Split 1 head secondNode Backing up 6 1 3 2 5  9  secondNode.setNext(split(secondNode.getNext()); What gets returned? return secondNode;

38 Split 1 head secondNode Backing up 6 1 3 2 5  9  secondNode.setNext(split(secondNode.getNext()); What gets returned? return secondNode;

39 Split 1 head secondNode Backing up 6 1 3 2 5  9  secondNode.setNext(split(secondNode.getNext()); What gets returned? return secondNode;

40 Node Merge public static Node merge(Node list1, Node list2){ if (list1 == null) return list2; else if (list2 == null) return list1; else if (list1.getData.compareTo(list2.getData()) < 1) { list1.setNext(merge(list1.getNext(),list2); return list1; } else { list2.setNext(merge(list1,list2.getNext()); return list2; }

41 Two linked Lists 1 3 7 2 8 9  

42 Two linked Lists in merge 1 3 7 2 8 9   list1 merge 0 list2

43 Two linked Lists in merge 1 3 7 2 8 9   list1 merge 1 list2 0 th

44 Two linked Lists in merge 1 3 7 2 8 9   list1 merge 2 list2 0 th 1 st

45 Two linked Lists in merge 1 3 7 2 8 9   list1 merge 3 list2 0 th 1 st 2 nd

46 Two linked Lists in merge 1 3 7 2 8 9   list1 merge 4 list2 0 th 1 st 2 nd  3 rd

47 Two linked Lists in merge 1 3 7 2 8 9  list1 merge 3 list2 0 th 1 st 2 nd

48 Two linked Lists in merge 1 3 7 2 8 9  list1 merge 2 list2 0 th 1 st 2 nd

49 Two linked Lists in merge 1 3 7 2 8 9  list1 merge 2 list2 0 th 1 st 2 nd

50 Two linked Lists in merge 1 3 7 2 8 9  list1 merge 0 list2 0 th 1 st 2 nd

51 Node MergeSort public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList)); }

52 Mergesort 31415926

53 Mergesort: Split 3141592 6

54 314159 2 6

55 31415 9 2 6

56 3141 5 9 2 6

57 314 1 5 9 2 6

58 31 4 1 5 9 2 6

59 3 1 4 1 5 9 2 6

60 3 1 4 1 5 9 2 6

61 3 1 4 1 5 9 2 6

62 3 1 4 1 5 9 2 6

63 3 1 4 1 5 9 2 6

64 3 1 4 1 5 9 2 6

65 3 1 4 1 5 9 2 6

66 3 1 4 1 5 9 2 6

67 Mergesort: Merge 3 1 4 1 5 9 2 6

68 3 1 4 1 5 9 2 6

69 3 1 4 1 5 9 2 6

70 Mergesort: Split 3 1 4 1 5 9 2 6

71 3 1 4 1 5 9 2 6

72 3 1 4 1 5 9 2 6

73 Mergesort: Merge 3 1 4 1 5 9 2 6

74 3 1 4 1 5 9 2 6

75 3 1 4 1 5 9 2 6

76 3 1 4 1 5 9 2 6

77 3 1 4 1 5 9 2 6

78 3 1 4 1 5 9 2 6

79 3 1 4 1 5 9 2 6

80 Mergesort: Split 3 1 4 1 5 9 2 6

81 3 1 4 1 5 9 2 6

82 3 1 4 1 5 9 2 6

83 3 1 4 1 5 9 2 6

84 3 1 4 1 5 9 2 6

85 3 1 4 1 5 9 2 6

86 3 1 4 1 5 9 2 6

87 Mergesort: Merge 3 1 4 1 5 9 2 6

88 3 1 4 1 5 9 2 6

89 3 1 4 1 5 9 2 6

90 Mergesort: Split 3 1 4 1 5 9 2 6

91 3 1 4 1 5 9 2 6

92 3 1 4 1 5 9 2 6

93 Mergesort: Merge 3 1 4 1 5 9 2 6

94 3 1 4 1 5 9 2 6

95 3 1 4 1 5 9 2 6

96 3 1 4 1 5 9 2 6

97 3 1 4 1 5 9 2 6

98 3 1 4 1 5 9 2 6

99 3 1 4 1 5 9 2 6

100 3 1 4 1 5 9 2 6

101 3 1 4 1 5 9 2 6

102 3 1 4 1 5 9 2 6

103 31 4 1 5 9 2 6

104 3141 5 9 2 6

105 31415 9 2 6

106 31415926


Download ppt "Recursion CSC 172 SPRING 2002 LECTURE 8 Visual Proof 123.n0 1 2 3 n."

Similar presentations


Ads by Google