תרגול 3 - רקורסיה.

Slides:



Advertisements
Similar presentations
A simple example finding the maximum of a set S of n numbers.
Advertisements

More on Divide and Conquer. The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving.
Divide and Conquer Strategy
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
Administrative Sep. 20 (today) – HW1 due Sep. 21 8am – problem session Sep. 25 – HW3 (=QUIZ #1) due Sep. 27 – HW4 due Sep. 28 8am – problem session Oct.
Master Theorem Section 7.3 of Rosen Spring 2010 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions:
Lecture 4. kf(n) is O(f(n)) for any positive constant k n r is O(n p ) if r  p since lim n  n r /n p = 0, if r < p = 1 if r = p f(n) is O(g(n)), g(n)
CSE 421 Algorithms Richard Anderson Lecture 12 Recurrences.
Lecture 3. kf(n) is O(f(n)) for any positive constant k f(n) + g(n) is O(f(n)) if g(n) is O(f(n)) T 1 (n) is O(f(n)), T 2 (n) is O(g(n)) T 1 (n) T 2 (n)
CSC 3323 Notes – Recurrence Relations Algorithm Analysis.
Analysis of Recursive Algorithms
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
Recurrence Examples.
Analysis of Algorithms
CS2336: Computer Science II
CS 310 – Fall 2006 Pacific University CS310 Complexity Section 7.1 November 27, 2006.
Recurrence Relations Connection to recursive algorithms Techniques for solving them.
Lecture 2: Algorithm Complexity. Recursion A subroutine which calls itself, with different parameters. Need to evaluate factorial(n) factorial(n) = n.
8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method.
Arithmetic.
Asymptotic Growth Rates Themes –Analyzing the cost of programs –Ignoring constants and Big-Oh –Recurrence Relations & Sums –Divide and Conquer Examples.
1 Logarithms Definition If y = a x then x = log a y For log 10 x use the log button. For log e x use the ln button.
Mathematics Review and Asymptotic Notation
CS 3343: Analysis of Algorithms
Recurrence: Master Theorem T(n) = aT(n/b) + f (n) where f (n)=cn k 1.a < b k T(n) ~ n k 2.a = b k T(n) ~ n k log b n 3.a > b k T(n) ~ n log b a.
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull Strassen's Algorithm: Matrix Multiplication.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Continued Fractions, Euclidean Algorithm and Lehmer’s Algorithm Applied Symbolic Computation CS 567 Jeremy Johnson TexPoint fonts used in EMF. Read the.
CS223 Advanced Data Structures and Algorithms 1 Sorting and Master Method Neil Tang 01/21/2009.
Project 2 due … Project 2 due … Project 2 Project 2.
a) y = 3 x b) y = -3 x c) y = (1/2) x d) y = -(1/2) x.
CSED101 INTRODUCTION TO COMPUTING TREE 2 Hwanjo Yu.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Recursion Algorithm : Design & Analysis [3]. In the last class… Asymptotic growth rate The Sets ,  and  Complexity Class An Example: Maximum Subsequence.
Unit 7 Number Systems and Bases Presentation 1Binary and Base 10 Presentation 2Adding Binary Numbers Presentation 3Subtracting Binary Numbers Presentation.
7.3 Divide-and-Conquer Algorithms and Recurrence Relations If f(n) represents the number of operations required to solve the problem of size n, it follow.
Divide and Conquer Strategy
Algorithm Analysis (Time complexity). Software development cycle -Four phases: 1.Analysis 2.Design Algorithm Design an algorithm to solve the problem.
Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2/19/2016 Recurrences. 2/19/2016 Evaluating Run-time of Loops MergeSort(Array A) int n=A.length; int i=0; int t=1 for (t=1;t
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 4.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1.
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Chapter 2 Algorithm Analysis
CS 3343: Analysis of Algorithms
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis
Analysis of Algorithms
CS 3343: Analysis of Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Binary Code  
Data Structures and Algorithms
CS 3343: Analysis of Algorithms
יסודות מבני נתונים תרגול 3: נוסחאות נסיגה (recurrences)
CS 3343: Analysis of Algorithms
Lecture 6 More Divide-Conquer and Paradigm #4 Data Structure.
CSE 373 Data Structures and Algorithms
Recurrence Equation Masters Theorem
Time Complexity Lecture 14 Sec 10.4 Thu, Feb 22, 2007.
Divide and Conquer Neil Tang 4/24/2008
Big-O & Recursion.
Time Complexity Lecture 15 Mon, Feb 27, 2006.
At the end of this session, learner will be able to:
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

תרגול 3 - רקורסיה

נוסחה המתארת ירידה במורד עץ הרקורסיה מהי נוסחא רקורסיבית? נוסחה המתארת ירידה במורד עץ הרקורסיה 𝑇 𝑛 =𝑎𝑇 𝑛 𝑏 +𝑓 𝑛 𝑎 - מספר הקריאות הרקורסיביות בכל איטרציה 𝑏- פי כמה מקטינים את הקלט בכל קריאה רקורסיבית 𝑓 𝑛 - פונקציית זמן הריצה של הפעולות בכל קריאה רקורסיבית

שיטות לפתרון נוסחאות רקורסיביות שיטת הצבה: מנחשים את הפתרון ואז מוכיחים אותו באינדוקציה שיטת האיטרציה: מפתחים "עד הסוף" ומחשבים נוסחה סגורה לסכום שהתקבל. שיטת המאסטר

תרגיל 1 – שיטת ההצבה בסיס האינדוקציה 𝑛=1: 𝑇 1 =1≤𝑐 1 ,כלומר נצטרך 𝑐≥1 מנחשים את הפתרון ואז מוכיחים אותו באינדוקציה נתונה הנוסחא הבאה: 𝑇(1)=1 𝑇 𝑛 =𝑇 𝑛 2 + 𝑛 הראו ש- 𝑇 𝑛 =𝑂 𝑛 . פתרון: בשיטת ההצבה: 𝑇 𝑛 =𝑂 𝑛 ← לפי ההגדרה קיים 𝑐 כך ש -𝑇 𝑛 ≤𝑐 𝑛 בסיס האינדוקציה 𝑛=1: 𝑇 1 =1≤𝑐 1 ,כלומר נצטרך 𝑐≥1 צעד האינדוקציה: נניח 𝑇 𝑘 ≤𝑐 𝑘 לכל 𝑘<𝑛 הנחת האינדוקציה 𝑇 𝑛 =𝑇 𝑛 2 + 𝑛 ≤𝑐 𝑛 2 + 𝑛 = 𝑛 𝑐 2 +1 ≤𝑐 𝑛 כלומר, צריך למצוא c כך ש 𝑐 2 +1 ≤𝑐, נקח 𝑐=4 נרצה שיתקיים

תרגיל 2 – שיטת האיטרציה i=1 i=2 i=3 מפתחים "עד הסוף" ומנסים לחשב נוסחא סגורה לסכום שהתקבל מצאו את זמן הריצה של הנוסחה הרקורסיביות הבאה: 𝑇(𝑎)=𝜃(1) 𝑇(𝑛)=𝑇(𝑛−𝑎)+𝑇(𝑎)+𝑛 פתרון: נפתח בשיטת האיטרציה ופשוט נפתח את הנוסחא עד קבלת נוסחא כללית: 𝑇 𝑛 =𝑇 𝑛−𝑎 +𝑇 𝑎 +𝑛 = 𝑇 𝑛−2𝑎 +𝑇 𝑎 + 𝑛−𝑎 +𝑇 𝑎 +𝑛 = 𝑇 𝑛−3𝑎 +𝑇 𝑎 + 𝑛−2𝑎 +2𝑇 𝑎 +2𝑛−𝑎 =𝑇 𝑛−3𝑎 +3𝑇 𝑎 +3𝑛−2𝑎−𝑎=… =𝑇 𝑛−𝑖𝑎 +𝑖∗𝑇 𝑎 +𝑖∗𝑛− 𝑘=1 𝑖−1 𝑘∗𝑎 =𝑇 𝑛−𝑖𝑎 +𝑖∗𝑇 𝑎 +𝑖∗𝑛−𝑎∗ 𝑖−1 ∗𝑖 2 i=1 i=2 i=3

תרגיל 2 – שיטת האיטרציה 𝑇 𝑛 =𝑇 𝑛−𝑖∗𝑎 +𝑖∗𝑇 𝑎 +𝑖∗𝑛−𝑎∗ 𝑖∗ 𝑖−1 2 מפתחים "עד הסוף" ומנסים לחשב נוסחא סגורה לסכום שהתקבל מצאו את זמן הריצה של הנוסחה הרקורסיביות הבאה: 𝑇(𝑎)=𝜃(1) 𝑇(𝑛)=𝑇(𝑛−𝑎)+𝑇(𝑎)+𝑛 𝑇 𝑛 =𝑇 𝑛−𝑖∗𝑎 +𝑖∗𝑇 𝑎 +𝑖∗𝑛−𝑎∗ 𝑖∗ 𝑖−1 2 כמה איטרציות נבצע עד שנגיע למקרה בסיס 𝑇 𝑎 ? 𝑇 𝑛−𝑖∗𝑎 = 𝑇 𝑎 𝑛−𝑖∗𝑎=𝑎 𝑖= 𝑛−𝑎 𝑎 = 𝑛 𝑎 −1

תרגיל 2 – שיטת האיטרציה 𝑇(𝑎)=𝜃(1) 𝑇(𝑛)=𝑇(𝑛−𝑎)+𝑇(𝑎)+𝑛 מפתחים "עד הסוף" ומנסים לחשב נוסחא סגורה לסכום שהתקבל 𝑇(𝑎)=𝜃(1) 𝑇(𝑛)=𝑇(𝑛−𝑎)+𝑇(𝑎)+𝑛 נציב 𝑖= 𝑛 𝑎 −1 בנוסחה ונקבל 𝑇 𝑛 =𝑇 𝑛−𝑖∗𝑎 +𝑖∗𝑇 𝑎 +𝑖∗𝑛−𝑎∗ 𝑖∗ 𝑖−1 2 =𝑇 𝑛− 𝑛 𝑎 −1 𝑎 + 𝑛 𝑎 −1 𝑇 𝑎 + 𝑛 𝑎 −1 𝑛−𝑎 1 2 𝑛 𝑎 −1 𝑛 𝑎 −2 =𝑇 𝑎 + 𝑛 𝑎 𝑇 𝑎 −𝑇 𝑎 + 𝑛 2 𝑎 −𝑛− 𝑎 2 ∗ 𝑛 2 𝑎 2 − 3𝑛 𝑎 +2 = 𝑛 𝑎 𝑇 𝑎 + 𝑛 2 𝑎 −𝑛− 𝑛 2 2𝑎 + 3𝑛 2 −𝑎 = 𝑛 𝑎 𝑇 𝑎 + 𝑛 2 2𝑎 + 𝑛 2 −𝑎 =𝛩 𝑛 𝛩 1 +𝛩 𝑛 2 𝛩 1 +𝛩 𝑛 𝛩 1 −𝛩 1 =𝛩( 𝑛 2 ) ולכן 𝑻 𝒏 =𝜣( 𝒏 𝟐 )

Master Theorem נתון: 𝑇(𝑛)=𝑎𝑇( 𝑛 𝑏 )+𝑓(𝑛) נתון: 𝑇(𝑛)=𝑎𝑇( 𝑛 𝑏 )+𝑓(𝑛) רעיון (לא פורמלי): נשווה את 𝑓(𝑛) עם 𝑛 log 𝑏 𝑎 ונראה "מי יותר דומיננטי" כללים: אם 𝑓 𝑛 =𝑂( 𝑛 log 𝑏 𝑎 −𝜖 ) אז 𝑇 𝑛 =𝜃 𝑛 log 𝑏 𝑎 אם 𝑓 𝑛 =𝜃( 𝑛 log 𝑏 𝑎 ) אז 𝑇 𝑛 =𝜃 𝑛 log 𝑏 𝑎 log 𝑛 אם 𝑓 𝑛 =Ω( 𝑛 log 𝑏 𝑎 +𝜖 ) וגם קיים 𝑐<1 כך ש 𝑎𝑓 𝑛 𝑏 <𝑐𝑓 𝑛 לכל n מספיק גדול אז 𝑇 𝑛 =𝜃(𝑓 𝑛 ) הערה: מקרה 2 המוכלל - אם 𝑓 𝑛 =𝜃( 𝑛 log 𝑏 𝑎 log 𝑘 𝑛 ) אז 𝑇 𝑛 =𝜃 𝑛 log 𝑏 𝑎 log k+1 𝑛 בעצם f(n) קובע לנו מהי הפונקציה המתאימה

תרגיל 3 - שיטת המאסטר מצאו את זמני הריצה של הנוסחאות הרקורסיביות הבאות: 𝑇(𝑛) = 7𝑇(𝑛/2) + 𝑛2 𝑇(𝑛) = 4𝑇(𝑛/2) + 𝑛2 𝑇(𝑛) = 2𝑇(𝑛/3) + 𝑛3

תרגיל 3 𝑇(𝑛) = 7𝑇 𝑛 2 + 𝑛2 פתרון: 𝑎 = 7, 𝑏 = 2, 𝑓(𝑛) = 𝑛2 𝑇(𝑛) = 7𝑇 𝑛 2 + 𝑛2 פתרון: 𝑎 = 7, 𝑏 = 2, 𝑓(𝑛) = 𝑛2 𝑛 log 𝑏 𝑎 = 𝑛 2.803… 𝑓 𝑛 =𝑛 2 =𝑂 𝑛 2.8−𝜖 =𝑂 𝑛 log 𝑏 𝑎 −𝜖 ולכן: 𝑻(𝒏)=𝜽( 𝒏 𝐥𝐨𝐠 𝟐 𝟕 ) אם 𝑓 𝑛 =𝑂( 𝑛 log 𝑏 𝑎 −𝜖 ) אז 𝑇 𝑛 =𝜃 𝑛 log 𝑏 𝑎

תרגיל 3 𝑇(𝑛) = 4𝑇 𝑛 2 + 𝑛2 פתרון: a = 4, b = 2, f(n) = n2 𝑇(𝑛) = 4𝑇 𝑛 2 + 𝑛2 פתרון: a = 4, b = 2, f(n) = n2 𝑛 log 𝑏 𝑎 = 𝑛 log 2 4 = 𝑛 2 𝑓 𝑛 =𝑛 2 =𝜃 𝑛 2 =𝜃 𝑛 log 𝑏 𝑎 ולכן : 𝑇(𝑛)=𝜃( 𝑛 2 log 𝑛 ) אם 𝑓 𝑛 =𝜃( 𝑛 log 𝑏 𝑎 ) אז 𝑇 𝑛 =𝜃 𝑛 log 𝑏 𝑎 log 𝑛

תרגיל 3 𝑇(𝑛) = 2𝑇 𝑛 3 + 𝑛3 פתרון: a = 2, b = 3, f(n) = n3 𝑇(𝑛) = 2𝑇 𝑛 3 + 𝑛3 פתרון: a = 2, b = 3, f(n) = n3 𝑛 log 𝑏 𝑎 = 𝑛 0.63… 𝑓 𝑛 =𝑛 3 =Ω 𝑛 0.63...+𝜖 =Ω 𝑛 log 𝑏 𝑎 +𝜖 𝑐<1 𝑎𝑛𝑑 2⋅𝑓 𝑛 3 = 2 𝑛 3 3 3 <𝑐 𝑛 3 Take c = 0.5 𝑇(𝑛)=𝜃( 𝑛 3 ) ⇔ 2 27 <𝑐<1 אם 𝑓 𝑛 =Ω( 𝑛 log 𝑏 𝑎 +𝜖 ) וגם קיים 𝑐<1 כך ש 𝑎𝑓 𝑛 𝑏 <𝑐𝑓 𝑛 לכל n מספיק גדול אז 𝑇 𝑛 =𝜃(𝑓 𝑛 )

שאלה 6 ידוע שלכל קבוע t קיים 𝑛 0 כך שלכל 𝑛> 𝑛 0 , 𝑙𝑜𝑔𝑛≤ 𝑛 t השתמשו בשיטת המאסטר למצוא את 𝑇(𝑛): 𝑇(𝑛) = 3𝑇(𝑛/2) + 𝑛𝑙𝑜𝑔𝑛 פתרון: 𝑎 = 3, 𝑏 = 2, 𝑓(𝑛) = 𝑛𝑙𝑜𝑔𝑛 log 2 3 =1.585 נראה כי : 𝑓 𝑛 =𝑛𝑙𝑜𝑔𝑛=𝑂 𝑛 log 2 3 −𝜖 =𝑂 𝑛 1.585−𝜖 כלומר, לפי הגדרה, נמצא 𝑐>0 כך ש 𝑛𝑙𝑜𝑔𝑛≤𝑐 𝑛 1.585 −𝜖 נחלק ב 𝑛 : 𝑙𝑜𝑔𝑛≤𝑐 𝑛 1.585 −𝜖−1 =𝑐 𝑛 0.585−𝜖 ידוע שלכל קבוע t קיים 𝑛 0 כך שלכל 𝑛> 𝑛 0 , 𝑙𝑜𝑔𝑛≤ 𝑛 t נקח 𝑐=1 ו 𝜖<0.585 כלשהו. נקבל כי𝑓 𝑛 =𝑂 𝑛 log 2 3 −𝜖 , ולכן .𝑇(𝑛) = Θ( 𝑛 𝑙𝑜𝑔23 )

שאלה 6 השתמשו בשיטת המאסטר למצוא את 𝑇(𝑛): 𝑇(𝑛) = 3𝑇(𝑛/2) + 𝑛𝑙𝑜𝑔𝑛 השתמשו בשיטת המאסטר למצוא את 𝑇(𝑛): 𝑇(𝑛) = 3𝑇(𝑛/2) + 𝑛𝑙𝑜𝑔𝑛 פתרון: 𝑎 = 3, 𝑏 = 2, 𝑓(𝑛) = 𝑛𝑙𝑜𝑔𝑛 log 2 3 =1.585 נראה כי : 𝑓 𝑛 =𝑛𝑙𝑜𝑔𝑛=𝑂 𝑛 log 2 3 −𝜖 =𝑂 𝑛 1.585−𝜖 נראה כי 𝑛⋅𝑙𝑜𝑔𝑛=𝑂 𝑛 1.5 : lim 𝑛→∞ 𝑓 𝑛 𝑔 𝑛 = lim 𝑛→∞ 𝑛⋅ log 𝑛 𝑛 1.5 = lim 𝑛→∞ log 𝑛 𝑛 0.5 = ∞ ∞ נשתמש בלופיטל (נגזרת של המונה והמכנה) ונקבל : lim 𝑛→∞ log 𝑛 𝑛 0.5 = lim 𝑛→∞ 1 𝑛 ⋅ ln 2 0.5⋅ 𝑛 −0.5 = lim 𝑛→∞ ln 2 0.5⋅ 𝑛 0.5 =0<∞ lim 𝑛→∞ 𝑓 𝑛 𝑔 𝑛 =0<∞ כלומר 𝑓 𝑛 =𝑂 𝑛 1.5 =𝑂 𝑛 log 2 3 −𝜖 ולכן 𝑇(𝑛) = Θ( 𝑛 log 2 3 )

תרגיל 4 𝑓(0)=0 𝑓(1)=1 𝑓(𝑖)=𝑓(𝑖−1)+𝑓(𝑖−2), 𝑖>1 מצאו פונקציה איטרטיבית ופונקציה רקורסיבית למציאת האיבר ה- 𝑛 בסדרת פיבונצ'י 𝑓(𝑛), ונתחו את זמני הריצה. 𝑓(0)=0 𝑓(1)=1 𝑓(𝑖)=𝑓(𝑖−1)+𝑓(𝑖−2), 𝑖>1 שיטה רקורסיבית: זמן הריצה מוגדר ע"י הנוסחה הרקורסיבית T(n)=T(n-1)+T(n-2)+1

תרגיל 4 נחסום את הנוסחה הרקורסיבית מלמעלה ומלטה 𝑇(𝑛)=𝑇(𝑛−1)+𝑇(𝑛−2)+1 כדי לחסום מלמעלה נשתמש באי-שיוויון 𝑇(𝑛)=𝑇(𝑛−1)+𝑇(𝑛−2)+1 ≤ 2𝑇(𝑛−1)+1 כדי לחסום מלמטה נשתמש באי-שיוויון 𝑇(𝑛)=𝑇(𝑛−1)+𝑇(𝑛−2)+1 ≥ 2𝑇(𝑛−2)+1

סכום סדרה הנדסית: 𝑖=0 𝑛−1 2 𝑖 = 2 𝑛 −1 תרגיל 4 חסימה מלמעלה: T(n) ≤ 2T(n-1) +1 ≤ 2(2T(n-2) +1) +1 ≤2(2(2T(n-3) +1) +1) +1 ≤ …≤ ≤ 2𝑘𝑇(𝑛−𝑘)+ 𝑖=0 𝑘−1 2 𝑖 נציב 𝑘=𝑛−1 ונקבל 𝑇 𝑛 =𝑂( 2 𝑛−1 + 2 𝑛 )=𝑂( 2 𝑛 ) 𝑆 𝑛 = 𝑎 1 ⋅ 𝑞 𝑛 −1 𝑞−1 סכום סדרה הנדסית: 𝑖=0 𝑛−1 2 𝑖 = 2 𝑛 −1

במקרה הטוב נקבל את הזמן הזה... תרגיל 4 באופן דומה חסימה מלמטה: T(n) ≥ 2T(n-2) +1 ≥ 2(2T(n-4) +1) +1 ≥ 2(2(2T(n-6) +1) +1) +1 ≥ … ≥ ≥ 2kT(n-2k)+ 𝑖=0 𝑘−1 2 𝑖 נציב 𝑘= 𝑛−1 2 ונקבל 𝑇 𝑛 =Ω( 2 𝑛−1 2 + 2 𝑛−1 2 )=Ω( 2 𝑛 2 ) במקרה הטוב נקבל את הזמן הזה... זה שווה?

תרגיל 4 מצאו שיטה איטרטיבית ושיטה רקורסיבית למציאת האיבר ה 𝑛 בסדרת פיבונצ'י 𝑓(𝑛), ונתחו את זמני הריצה 𝑓(0)=0 𝑓(1)=1 𝑓(𝑖)=𝑓(𝑖−1)+𝑓(𝑖−2), 𝑖>1 שיטה איטרטיבית: זמן ריצה: 𝜃(𝑛)

תרגיל 5 הציעו שיטה רקורסיבית לפתרון בעיית מגדלי הנוי ומצאו את זמן הריצה שלה

תרגיל 5 מצאו שיטה רקורסיבית לפתרון בעיית מגדלי הנוי ומצאו את זמן הריצה שלה פתרון: זמן הריצה מתואר ע"י הנוסחה הרקורסיבית T(n)=2T(n-1)+1 Tower_Of_Hanoi: If N==1 : Move disk from source to destination return; Tower_Of_Hanoi (N-1, source, destination, helper) Move disk from source to destination Tower_Of_Hanoi (N-1, helper, destination, source)

תרגיל 5 T(n) = 2T(n-1) +1 = 2(2T(n-2) +1) +1 נפתור בשיטת האיטרציה: T(n) = 2T(n-1) +1 = 2(2T(n-2) +1) +1 =2(2(2T(n-3) +1) +1) +1 = …= = 2kT(n-k)+ 𝑖=1 𝑘−1 2 𝑖 נציב k=n-1 ונקבל 𝑇 𝑛 = 2 𝑛−1 𝑇 1 + 2 𝑛−1 −1=𝜃 2 𝑛

שאלה 8 יש ברשותכם שתי ביצים. אתם נמצאים בבניין בן N קומות. נרצה לדעת מהי הקומה הגבוהה ביותר M שניתן לזרוק ממנה את הביצה מבלי שתישבר? ניתן לזרוק את הביצים כמה פעמים שרוצים מהחלון של איזה קומה שרוצים (כל עוד היא לא נשברת). תארו אלגוריתם יעיל ככל הניתן למצוא את הקומה M.

פתרון 1 נזרוק את הביצה הראשונה בכל הקומות 𝑖∗ 𝑛 לכל 1≤𝑖≤ 𝑛 . ברגע שהיא נשברת (נניח ב𝑖=𝑘 ), נזרוק את הביצה השניה 𝑛 פעמים בין הקומות 𝑘−1 𝑛 ל 𝑘 𝑛 . סה"כ יש לכל היותר 2 𝑛 −1 זריקות. 81 = 9* 𝑛 … דוגמא: 72 = 8* 𝑛 𝑛 = 9 נקח n = 81 , קודם זורקים מקומה 9, אחר כך מקומה 18, אחר כך מקומה 27 וכך הלאה. נניח והביצה נשברה בקומה 27. אז נצטרך לעבור על הקטע [18,27] בצורה סדרתית מלמטה למעלה. 27 = 3* 𝑛 ניתוח זמן ריצה: במקרה הגרוע הביצה הראשונה נשברת בקומה n, כלומר בסה"כ נעשה 𝑛 זריקות. במקרה הגרוע הביצה השנייה נשברת בקומה אחרונה של הקטע [ 𝑛 -1* 𝑛 , 𝑛], כלומר נעשה 𝑛 -1 זריקות נוספות. בסה"כ ביצענו 𝟐 𝒏 −𝟏 זריקות 18 = 2* 𝑛 9 = 1* 𝑛

פתרון 2 - שיפור 1+2+3+…+𝑥≥𝑛 … 13+12+…+1 = 𝑖=𝑥 1 𝑖 13+12+…+2 = 𝑖=𝑥 2 𝑖 שיפור אפשרי - לאזן בין מספר הזריקות שביצענו כדי למצוא קטע, לבין גודל הקטע. נחפש 𝑥 המקיים: את הביצה הראשונה נזרוק מהקומות 𝑖=𝑥 1 𝑖 , כלומר בפעם הראשונה נזרוק מהקומה 𝑥 , אח"כ 𝑥+ 𝑥−1 וכך הלאה. 1+2+3+…+𝑥≥𝑛 13+12+…+1 = 𝑖=𝑥 1 𝑖 1+2+3+…+𝑥≥𝑛 ⇒ 𝑥 𝑥+1 2 ≥𝑛 ⇒ 𝑥 2 +𝑥−2𝑛≥0 ⇒𝑥= 2𝑛−1 13+12+…+2 = 𝑖=𝑥 2 𝑖 … 13+12+11=36 = 𝑥+ 𝑥−1 + 𝑥−2 דוגמא: 13+12=25 = 𝑥+ 𝑥−1 2𝑛−1 = 12.7 = 13 נקח n = 81 , קודם זורקים מקומה 13, אחר כך מקומה 25, אחר כך מקומה 36 וכך הלאה. 13 = 𝑥 נניח והביצה נשברה בקומה 36. אז נצטרך לעבור על הקטע [26,35] בצורה סדרתית מלמטה למעלה, כלומר נבצע עוד 10 זריקות לכל היותר ==> בסה"כ 3+10=13 זריקות. ניתוח זמן ריצה: על כל זריקה בשלב הראשון אנחנו מקטינים קטע חיפוש ב 1, לכן כמות הזריקות בכל מקרה תהיה שווה (ללא תלות באיזה קטע נחפש). בסה"כ נבצע 𝟐𝒏−𝟏 = 𝟏𝟐.𝟕 = 𝟏𝟑 זריקות

Analysis of algorithms for multiplication Given two binary numbers x, y with n-bits each, give an efficient algorithm to calculate the product z = x*y. Analyze the time complexity of this algorithm. Example: 10110110 (18210) x 10011101 (15710) = 110111110011110 (2857410)