Recursive Maximum Itera0ve version Have a array or vector - - PowerPoint PPT Presentation

recursive maximum itera0ve version
SMART_READER_LITE
LIVE PREVIEW

Recursive Maximum Itera0ve version Have a array or vector - - PowerPoint PPT Presentation

Recursive Maximum Itera0ve version Have a array or vector called A. Want to find the maximum element: biggest = A[0] for (size_t p = 0; p < A.size(); p++) if


slide-1
SLIDE 1

Recursive ¡Maximum ¡

slide-2
SLIDE 2

Itera0ve ¡version ¡

  • Have ¡a ¡array ¡or ¡vector ¡called ¡A. ¡ ¡Want ¡to ¡find ¡

the ¡maximum ¡element: ¡

biggest = A[0] for (size_t p = 0; p < A.size(); p++) if (A[p] > biggest) biggest = A[p]; /* After the loop, we know biggest is the maximum element in A. */

slide-3
SLIDE 3

Recursive ¡version ¡

  • Base ¡case: ¡What ¡is ¡the ¡smallest ¡size ¡array ¡I ¡would ¡

ever ¡want ¡to ¡find ¡the ¡maximum ¡element ¡in? ¡

  • Recursive ¡case: ¡

– Suppose ¡you ¡have ¡an ¡array ¡A ¡(with ¡>1 ¡element). ¡ ¡ ¡ – How ¡can ¡I ¡describe ¡finding ¡the ¡maximum ¡element ¡as ¡ involving ¡finding ¡the ¡maximum ¡element ¡of ¡a ¡smaller ¡sized ¡ array? ¡

– Hint: ¡Suppose ¡my ¡array ¡has ¡5 ¡elements. ¡ ¡My ¡best ¡ friend ¡knows ¡how ¡to ¡find ¡the ¡largest ¡value ¡in ¡an ¡ array, ¡but ¡only ¡for ¡4 ¡elements. ¡ ¡How ¡can ¡I ¡use ¡him ¡ to ¡solve ¡my ¡problem? ¡

slide-4
SLIDE 4

Recursive ¡version ¡

  • max(A) ¡
  • Base ¡case: ¡If ¡A.size() ¡== ¡1, ¡return ¡A[0] ¡
  • Recursive ¡case: ¡If ¡A.size() ¡> ¡1: ¡

– Find ¡the ¡maximum ¡element ¡in ¡A[1:] ¡ ¡(whole ¡array ¡except ¡A[0]) ¡

  • call ¡it ¡M ¡

– If ¡M ¡> ¡A[0]: ¡return ¡M ¡ – Else: ¡return ¡A[0] ¡

slide-5
SLIDE 5

A ¡= ¡[7, ¡9, ¡8] ¡

Call ¡max([7, ¡9, ¡8]) ¡ ¡ A ¡= ¡[7, ¡9, ¡8] ¡ M ¡= ¡(recursive ¡call) ¡ Call ¡max([9, ¡8]) ¡ ¡ A ¡= ¡[9, ¡8] ¡ M ¡= ¡(recursive ¡call) ¡ Call ¡max([8]) ¡ ¡ A ¡= ¡[8] ¡ Base ¡case! ¡

  • max(A) ¡
  • Base ¡case: ¡If ¡A.size() ¡== ¡1, ¡

return ¡A[0] ¡

  • Recursive ¡case: ¡If ¡A.size() ¡> ¡1: ¡

– Find ¡the ¡maximum ¡element ¡in ¡ A[1:] ¡ ¡(whole ¡array ¡except ¡A[0]) ¡

  • call ¡it ¡M ¡

– If ¡M ¡> ¡A[0]: ¡return ¡M ¡ – Else: ¡return ¡A[0] ¡

slide-6
SLIDE 6

A ¡= ¡[7, ¡9, ¡8] ¡

Call ¡max([7, ¡9, ¡8]) ¡ ¡ A ¡= ¡[7, ¡9, ¡8] ¡ M ¡= ¡(recursive ¡call) ¡ Call ¡max([9, ¡8]) ¡ ¡ A ¡= ¡[9, ¡8] ¡ M ¡= ¡(recursive ¡call) ¡ Call ¡max([8]) ¡ ¡ A ¡= ¡[8] ¡ Base ¡case! ¡

  • max(A) ¡
  • Base ¡case: ¡If ¡A.size() ¡== ¡1, ¡

return ¡A[0] ¡

  • Recursive ¡case: ¡If ¡A.size() ¡> ¡1: ¡

– Find ¡the ¡maximum ¡element ¡in ¡ A[1:] ¡ ¡(whole ¡array ¡except ¡A[0]) ¡

  • call ¡it ¡M ¡

– If ¡M ¡> ¡A[0]: ¡return ¡M ¡ – Else: ¡return ¡A[0] ¡

Returns ¡ 8 ¡

slide-7
SLIDE 7

A ¡= ¡[7, ¡9, ¡8] ¡

Call ¡max([7, ¡9, ¡8]) ¡ ¡ A ¡= ¡[7, ¡9, ¡8] ¡ M ¡= ¡(recursive ¡call) ¡ Call ¡max([9, ¡8]) ¡ ¡ A ¡= ¡[9, ¡8] ¡ M ¡= ¡8 ¡ Call ¡max([8]) ¡ ¡ A ¡= ¡[8] ¡ Base ¡case! ¡

  • max(A) ¡
  • Base ¡case: ¡If ¡A.size() ¡== ¡1, ¡

return ¡A[0] ¡

  • Recursive ¡case: ¡If ¡A.size() ¡> ¡1: ¡

– Find ¡the ¡maximum ¡element ¡in ¡ A[1:] ¡ ¡(whole ¡array ¡except ¡A[0]) ¡

  • call ¡it ¡M ¡

– If ¡M ¡> ¡A[0]: ¡return ¡M ¡ – Else: ¡return ¡A[0] ¡

Returns ¡ 8 ¡

slide-8
SLIDE 8

A ¡= ¡[7, ¡9, ¡8] ¡

Call ¡max([7, ¡9, ¡8]) ¡ ¡ A ¡= ¡[7, ¡9, ¡8] ¡ M ¡= ¡(recursive ¡call) ¡ Call ¡max([9, ¡8]) ¡ ¡ A ¡= ¡[9, ¡8] ¡ M ¡= ¡8 ¡ Call ¡max([8]) ¡ ¡ A ¡= ¡[8] ¡ Base ¡case! ¡

  • max(A) ¡
  • Base ¡case: ¡If ¡A.size() ¡== ¡1, ¡

return ¡A[0] ¡

  • Recursive ¡case: ¡If ¡A.size() ¡> ¡1: ¡

– Find ¡the ¡maximum ¡element ¡in ¡ A[1:] ¡ ¡(whole ¡array ¡except ¡A[0]) ¡

  • call ¡it ¡M ¡

– If ¡M ¡> ¡A[0]: ¡return ¡M ¡ – Else: ¡return ¡A[0] ¡

Returns ¡ 8 ¡ Returns ¡ 9 ¡

slide-9
SLIDE 9

A ¡= ¡[7, ¡9, ¡8] ¡

Call ¡max([7, ¡9, ¡8]) ¡ ¡ A ¡= ¡[7, ¡9, ¡8] ¡ M ¡= ¡9 ¡ Call ¡max([9, ¡8]) ¡ ¡ A ¡= ¡[9, ¡8] ¡ M ¡= ¡8 ¡ Call ¡max([8]) ¡ ¡ A ¡= ¡[8] ¡ Base ¡case! ¡

  • max(A) ¡
  • Base ¡case: ¡If ¡A.size() ¡== ¡1, ¡

return ¡A[0] ¡

  • Recursive ¡case: ¡If ¡A.size() ¡> ¡1: ¡

– Find ¡the ¡maximum ¡element ¡in ¡ A[1:] ¡ ¡(whole ¡array ¡except ¡A[0]) ¡

  • call ¡it ¡M ¡

– If ¡M ¡> ¡A[0]: ¡return ¡M ¡ – Else: ¡return ¡A[0] ¡

Returns ¡ 8 ¡ Returns ¡ 9 ¡

slide-10
SLIDE 10

A ¡= ¡[7, ¡9, ¡8] ¡

Call ¡max([7, ¡9, ¡8]) ¡ ¡ A ¡= ¡[7, ¡9, ¡8] ¡ M ¡= ¡9 ¡ Call ¡max([9, ¡8]) ¡ ¡ A ¡= ¡[9, ¡8] ¡ M ¡= ¡8 ¡ Call ¡max([8]) ¡ ¡ A ¡= ¡[8] ¡ Base ¡case! ¡

  • max(A) ¡
  • Base ¡case: ¡If ¡A.size() ¡== ¡1, ¡

return ¡A[0] ¡

  • Recursive ¡case: ¡If ¡A.size() ¡> ¡1: ¡

– Find ¡the ¡maximum ¡element ¡in ¡ A[1:] ¡ ¡(whole ¡array ¡except ¡A[0]) ¡

  • call ¡it ¡M ¡

– If ¡M ¡> ¡A[0]: ¡return ¡M ¡ – Else: ¡return ¡A[0] ¡

Returns ¡ 8 ¡ Returns ¡ 9 ¡ Returns ¡ 9 ¡

slide-11
SLIDE 11

C++ ¡recursive ¡version ¡

  • C++ ¡doesn't ¡let ¡you ¡take ¡slices ¡of ¡arrays ¡(also ¡inefficient). ¡
  • No0ce ¡that ¡our ¡slices ¡always ¡involving ¡chopping ¡off ¡the ¡

first ¡element ¡in ¡the ¡array; ¡i.e, ¡A[0] ¡

– [7, ¡9, ¡8] ¡-­‑> ¡[9, ¡8] ¡-­‑> ¡[8] ¡

  • How ¡can ¡we ¡simulate ¡an ¡array ¡slice ¡without ¡actually ¡doing ¡

the ¡slicing? ¡

– Hint: ¡Imagine ¡reading ¡a ¡textbook ¡(lol). ¡ ¡When ¡you ¡finish ¡reading ¡ a ¡page, ¡you ¡don't ¡rip ¡it ¡out ¡of ¡the ¡book, ¡yet ¡you ¡want ¡to ¡be ¡able ¡ to ¡return ¡to ¡that ¡place ¡in ¡the ¡book ¡later ¡to ¡study ¡more ¡(rofl). ¡ ¡ How ¡do ¡you ¡solve ¡this ¡conundrum? ¡

slide-12
SLIDE 12
slide-13
SLIDE 13

C++ ¡recursive ¡version ¡

  • Use ¡a ¡integer ¡variable ¡"bookmark" ¡to ¡save ¡your ¡spot ¡in ¡

the ¡array. ¡ ¡ ¡

  • When ¡we ¡make ¡a ¡recursive ¡call, ¡instead ¡of ¡passing ¡an ¡

updated ¡array ¡A ¡(like ¡the ¡Python ¡version), ¡we ¡will ¡pass ¡an ¡ updated ¡bookmark. ¡

  • Our ¡func0on ¡will ¡now ¡be ¡max(A, ¡low) ¡

– low ¡(the ¡bookmark) ¡represents ¡the ¡index ¡of ¡the ¡bookmark ¡in ¡the ¡ array: ¡everything ¡before ¡the ¡bookmark ¡is ¡already ¡read, ¡ everything ¡aeerwards ¡is ¡unread. ¡

slide-14
SLIDE 14

Recursive ¡C++ ¡version ¡

  • max(A, ¡low) ¡
  • Base ¡case: ¡??? ¡
  • Recursive ¡case: ¡ ¡

– Find ¡the ¡maximum ¡element ¡in ¡??? ¡ ¡

  • call ¡it ¡M ¡

– if ¡???: ¡return ¡M ¡ – else: ¡return ¡??? ¡ ¡

  • Where ¡does ¡the ¡bookmark ¡start? ¡
slide-15
SLIDE 15

Recursive ¡C++ ¡version ¡

  • max(A, ¡low) ¡
  • Base ¡case: ¡if ¡low ¡== ¡A.size() ¡-­‑ ¡1 ¡
  • Recursive ¡case: ¡

– Find ¡the ¡maximum ¡element ¡in ¡everything ¡aeer ¡A[low] ¡

  • M ¡= ¡max(A, ¡low ¡+ ¡1) ¡

– if ¡M ¡> ¡A[low]: ¡return ¡M ¡ – else: ¡return ¡A[low] ¡ ¡

  • Ini0al ¡call ¡should ¡be ¡max(A, ¡0) ¡
slide-16
SLIDE 16

Binary ¡Search ¡

slide-17
SLIDE 17

Phon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¡is ¡found ¡(stop). ¡
  • If ¡array[mid] ¡> ¡key, ¡repeat ¡algorithm ¡with ¡only ¡the ¡

le7 ¡half ¡of ¡the ¡array. ¡

– How ¡do ¡we ¡change ¡low ¡& ¡high? ¡

  • If ¡array[mid] ¡< ¡key, ¡repeat ¡algorithm ¡with ¡only ¡the ¡

right ¡half ¡of ¡the ¡array. ¡

– How ¡do ¡we ¡change ¡low ¡& ¡high? ¡ ¡

slide-44
SLIDE 44
  • If ¡low ¡> ¡high, ¡we ¡know ¡the ¡item ¡is ¡not ¡found ¡(stop). ¡
  • If ¡array[mid] ¡== ¡key, ¡item ¡is ¡found ¡(stop). ¡
  • If ¡array[mid] ¡> ¡key, ¡repeat ¡algorithm ¡with ¡only ¡the ¡

le7 ¡half ¡of ¡the ¡array. ¡

– How ¡do ¡we ¡change ¡low ¡& ¡high? ¡ – high ¡= ¡mid ¡-­‑ ¡1 ¡

  • If ¡array[mid] ¡< ¡key, ¡repeat ¡algorithm ¡with ¡only ¡the ¡

right ¡half ¡of ¡the ¡array. ¡

– How ¡do ¡we ¡change ¡low ¡& ¡high? ¡ – low ¡= ¡mid ¡+ ¡1 ¡

slide-45
SLIDE 45

Recursive ¡formula0on ¡

  • Func0on: ¡binary_search(A, ¡key, ¡low, ¡high) ¡
  • Base ¡cases: ¡

– Found ¡key: ¡Return ¡posi0on ¡found. ¡ – low ¡> ¡high: ¡Return ¡-­‑1 ¡(indica0ng ¡not ¡found). ¡

  • Recursive ¡cases: ¡

– array[mid] ¡> ¡key: ¡binary_search(A, ¡key, ¡low, ¡mid ¡– ¡1) ¡ – array[mid] ¡< ¡key: ¡binary_search(A, ¡key, ¡mid ¡+ ¡1, ¡high) ¡