Upcoming SlideShare
×

# Introduction to Algorithms

18,205 views

Published on

The first things to look at in an algorithms course

Published in: Technology, Education
1 Comment
25 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• thanks

Are you sure you want to  Yes  No
Views
Total views
18,205
On SlideShare
0
From Embeds
0
Number of Embeds
399
Actions
Shares
0
910
1
Likes
25
Embeds 0
No embeds

No notes for slide
• ### Introduction to Algorithms

1. 1. Algorithm Analysis & Data Structures Jaideep Srivastava
2. 2. Schedule of topics <ul><li>Lecture 1: Algorithm analysis </li></ul><ul><ul><li>Concept – what is it? </li></ul></ul><ul><ul><li>Importance – why do it? </li></ul></ul><ul><ul><li>Examples – lots of it </li></ul></ul><ul><ul><li>Formalism </li></ul></ul><ul><li>Lecture 2: Recursion </li></ul><ul><ul><li>Concept </li></ul></ul><ul><ul><li>Examples </li></ul></ul><ul><li>Lecture 3: Trees </li></ul><ul><ul><li>Concept & properties </li></ul></ul><ul><ul><li>Tree algorithms </li></ul></ul>
3. 3. Introduction <ul><li>A famous quote: Program = Algorithm + Data Structure. </li></ul><ul><li>All of you have programmed; thus have already been exposed to algorithms and data structure. </li></ul><ul><li>Perhaps you didn't see them as separate entities; </li></ul><ul><li>Perhaps you saw data structures as simple programming constructs (provided by STL--standard template library). </li></ul><ul><li>However, data structures are quite distinct from algorithms, and very important in their own right. </li></ul>
4. 4. Lecture 1 – Algorithm Analysis & Complexity
5. 5. Objectives <ul><li>The main focus of is to introduce you to a systematic study of algorithms and data structures. </li></ul><ul><li>The two guiding principles of the course are: abstraction and formal analysis. </li></ul><ul><li>Abstraction: We focus on topics that are broadly applicable to a variety of problems. </li></ul><ul><li>Analysis: We want a formal way to compare two objects (data structures or algorithms). </li></ul><ul><li>In particular, we will worry about &quot;always correct&quot;-ness, and worst-case bounds on time and memory (space). </li></ul>
6. 6. What is Algorithm Analysis For <ul><li>Foundations of Algorithm Analysis and Data Structures. </li></ul><ul><li>Analysis: </li></ul><ul><ul><li>How to predict an algorithm’s performance </li></ul></ul><ul><ul><li>How well an algorithm scales up </li></ul></ul><ul><ul><li>How to compare different algorithms for a problem </li></ul></ul><ul><li>Data Structures </li></ul><ul><ul><li>How to efficiently store, access, manage data </li></ul></ul><ul><ul><li>Data structures effect algorithm’s performance </li></ul></ul>
7. 7. Example Algorithms <ul><li>Two algorithms for computing the Factorial </li></ul><ul><li>Which one is better? </li></ul><ul><li>int factorial (int n) { </li></ul><ul><li>if (n <= 1) return 1; </li></ul><ul><li>else return n * factorial(n-1); </li></ul><ul><li>} </li></ul><ul><li>int factorial (int n) { </li></ul><ul><li>if (n<=1) return 1; </li></ul><ul><li>else { </li></ul><ul><li>fact = 1; </li></ul><ul><li>for (k=2; k<=n; k++) </li></ul><ul><li>fact *= k; </li></ul><ul><li>return fact; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
8. 8. Examples of famous algorithms <ul><li>Constructions of Euclid </li></ul><ul><li>Newton's root finding </li></ul><ul><li>Fast Fourier Transform ( signal processing ) </li></ul><ul><li>Compression (Huffman, Lempel-Ziv, GIF, MPEG) </li></ul><ul><li>DES, RSA encryption ( network security ) </li></ul><ul><li>Simplex algorithm for linear programming ( optimization ) </li></ul><ul><li>Shortest Path Algorithms (Dijkstra, Bellman-Ford) </li></ul><ul><li>Error correcting codes (CDs, DVDs) </li></ul><ul><li>TCP congestion control, IP routing ( computer networks ) </li></ul><ul><li>Pattern matching ( Genomics ) </li></ul><ul><li>Search Engines ( www ) </li></ul>
9. 9. Role of Algorithms in Modern World <ul><li>Enormous amount of data </li></ul><ul><ul><li>Network traffic (telecom billing, monitoring) </li></ul></ul><ul><ul><li>Database transactions (Sales, inventory) </li></ul></ul><ul><ul><li>Scientific measurements (astrophysics, geology) </li></ul></ul><ul><ul><li>Sensor networks. RFID tags </li></ul></ul><ul><ul><ul><li>Radio frequency identification ( RFID ) is a method of remotely storing and retrieving data using devices called RFID tags. </li></ul></ul></ul><ul><ul><li>Bioinformatics (genome, protein bank) </li></ul></ul>
10. 10. A real-world Problem <ul><li>Communication in the Internet </li></ul><ul><li>Message (email, ftp) broken down into IP packets. </li></ul><ul><li>Sender/receiver identified by IP address. </li></ul><ul><li>The packets are routed through the Internet by special computers called Routers. </li></ul><ul><li>Each packet is stamped with its destination address, but not the route. </li></ul><ul><li>Because the Internet topology and network load is constantly changing, routers must discover routes dynamically. </li></ul><ul><li>What should the Routing Table look like? </li></ul>
11. 11. IP Prefixes and Routing <ul><li>Each router is really a switch: it receives packets at several input ports, and appropriately sends them out to output ports. </li></ul><ul><li>Thus, for each packet, the router needs to transfer the packet to that output port that gets it closer to its destination . </li></ul><ul><li>Should each router keep a table: IP address x Output Port? </li></ul><ul><li>How big is this table? </li></ul><ul><li>When a link or router fails, how much information would need to be modified? </li></ul><ul><li>A router typically forwards several million packets/sec! </li></ul>
12. 12. Data Structures <ul><li>The IP packet forwarding is a Data Structure problem! </li></ul><ul><li>Efficiency, scalability is very important. </li></ul><ul><li>Similarly, how does Google find the documents matching your query so fast? </li></ul><ul><li>Uses sophisticated algorithms to create index structures, which are just data structures. </li></ul><ul><li>Algorithms and data structures are ubiquitous. </li></ul><ul><li>With the data glut created by the new technologies, the need to organize, search, and update MASSIVE amounts of information FAST is more severe than ever before. </li></ul>
13. 13. Algorithms to Process these Data <ul><li>Which are the top K sellers? </li></ul><ul><li>Correlation between time spent at a web site and purchase amount? </li></ul><ul><li>Which flows at a router account for > 1% traffic? </li></ul><ul><li>Did source S send a packet in last s seconds? </li></ul><ul><li>Send an alarm if any international arrival matches a profile in the database </li></ul><ul><li>Similarity matches against genome databases </li></ul><ul><li>Etc. </li></ul>
14. 14. Max Subsequence Problem <ul><li>Given a sequence of integers A1, A2, …, An, find the maximum possible value of a subsequence Ai, …, Aj. </li></ul><ul><li>Numbers can be negative. </li></ul><ul><li>You want a contiguous chunk with largest sum. </li></ul><ul><li>Example: -2, 11, -4, 13, -5, -2 </li></ul><ul><li>The answer is 20 (subseq. A2 through A4). </li></ul><ul><li>We will discuss 4 different algorithms , with time complexities O(n 3 ), O(n 2 ), O(n log n), and O(n). </li></ul><ul><li>With n = 10 6 , algorithm 1 may take > 10 years; algorithm 4 will take a fraction of a second! </li></ul>
15. 15. Algorithm 1 for Max Subsequence Sum <ul><li>Given A 1 ,…,A n , find the maximum value of A i +A i+ 1 + ··· +A j </li></ul><ul><li>0 if the max value is negative </li></ul>int maxSum = 0; for( int i = 0; i < a.size( ); i++ ) for( int j = i; j < a.size( ); j++ ) { int thisSum = 0; for( int k = i; k <= j; k++ ) thisSum += a[ k ]; if( thisSum > maxSum ) maxSum = thisSum; } return maxSum; <ul><li>Time complexity: O  n 3  </li></ul>
16. 16. Algorithm 2 <ul><li>Idea: Given sum from i to j-1, we can compute the sum from i to j in constant time. </li></ul><ul><li>This eliminates one nested loop, and reduces the running time to O(n 2 ). </li></ul>into maxSum = 0; for( int i = 0; i < a.size( ); i++ ) int thisSum = 0; for( int j = i; j < a.size( ); j++ ) { thisSum += a[ j ]; if( thisSum > maxSum ) maxSum = thisSum; } return maxSum;
17. 17. Algorithm 3 <ul><li>This algorithm uses divide-and-conquer paradigm. </li></ul><ul><li>Suppose we split the input sequence at midpoint. </li></ul><ul><li>The max subsequence is entirely in the left half , entirely in the right half , or it straddles the midpoint . </li></ul><ul><li>Example: </li></ul><ul><li>left half | right half </li></ul><ul><li>4 -3 5 -2 | -1 2 6 -2 </li></ul><ul><li>Max in left is 6 (A1 through A3); max in right is 8 (A6 through A7). But straddling max is 11 (A1 thru A7). </li></ul>
18. 18. Algorithm 3 (cont.) <ul><li>Example: </li></ul><ul><li>left half | right half </li></ul><ul><li>4 -3 5 -2 | -1 2 6 -2 </li></ul><ul><li>Max subsequences in each half found by recursion. </li></ul><ul><li>How do we find the straddling max subsequence? </li></ul><ul><li>Key Observation : </li></ul><ul><ul><li>Left half of the straddling sequence is the max subsequence ending with -2. </li></ul></ul><ul><ul><li>Right half is the max subsequence beginning with -1. </li></ul></ul><ul><li>A linear scan lets us compute these in O(n) time. </li></ul>
19. 19. Algorithm 3: Analysis <ul><li>The divide and conquer is best analyzed through recurrence: </li></ul><ul><li>T(1) = 1 </li></ul><ul><li>T(n) = 2T(n/2) + O(n) </li></ul><ul><li>This recurrence solves to T(n) = O(n log n). </li></ul>
20. 20. Algorithm 4 <ul><li>Time complexity clearly O ( n ) </li></ul><ul><li>But why does it work? I.e. proof of correctness. </li></ul>2, 3, -2, 1, -5, 4, 1, -3, 4, -1, 2 int maxSum = 0, thisSum = 0; for( int j = 0; j < a.size( ); j++ ) { thisSum += a[ j ]; if ( thisSum > maxSum ) maxSum = thisSum; else if ( thisSum < 0 ) thisSum = 0; } return maxSum; }
21. 21. Proof of Correctness <ul><li>Max subsequence cannot start or end at a negative Ai. </li></ul><ul><li>More generally, the max subsequence cannot have a prefix with a negative sum. </li></ul><ul><li>Ex: -2 11 -4 13 -5 -2 </li></ul><ul><li>Thus, if we ever find that Ai through Aj sums to < 0, then we can advance i to j+1 </li></ul><ul><ul><li>Proof. Suppose j is the first index after i when the sum becomes < 0 </li></ul></ul><ul><ul><li>The max subsequence cannot start at any p between i and j. Because A i through A p-1 is positive, so starting at i would have been even better. </li></ul></ul>
22. 22. Algorithm 4 <ul><li>int maxSum = 0, thisSum = 0; </li></ul><ul><li>for( int j = 0; j < a.size( ); j++ ) </li></ul><ul><li>{ </li></ul><ul><li>thisSum += a[ j ]; </li></ul><ul><li>if ( thisSum > maxSum ) </li></ul><ul><li>maxSum = thisSum; </li></ul><ul><li>else if ( thisSum < 0 ) </li></ul><ul><li>thisSum = 0; </li></ul><ul><li>} </li></ul><ul><li>return maxSum </li></ul><ul><li>The algorithm resets whenever prefix is < 0. Otherwise, it forms new sums and updates maxSum in one pass. </li></ul>
23. 23. Why Efficient Algorithms Matter <ul><li>Suppose N = 10 6 </li></ul><ul><li>A PC can read/process N records in 1 sec. </li></ul><ul><li>But if some algorithm does N*N computation, then it takes 1M seconds = 11 days!!! </li></ul><ul><li>100 City Traveling Salesman Problem . </li></ul><ul><ul><li>A supercomputer checking 100 billion tours/sec still requires 10 100 years! </li></ul></ul><ul><li>Fast factoring algorithms can break encryption schemes. Algorithms research determines what is safe code length. (> 100 digits) </li></ul>
24. 24. How to Measure Algorithm Performance <ul><li>What metric should be used to judge algorithms? </li></ul><ul><ul><li>Length of the program (lines of code) </li></ul></ul><ul><ul><li>Ease of programming (bugs, maintenance) </li></ul></ul><ul><ul><li>Memory required </li></ul></ul><ul><ul><li>Running time </li></ul></ul><ul><li>Running time is the dominant standard. </li></ul><ul><ul><li>Quantifiable and easy to compare </li></ul></ul><ul><ul><li>Often the critical bottleneck </li></ul></ul>
25. 25. Abstraction <ul><li>An algorithm may run differently depending on: </li></ul><ul><ul><li>the hardware platform (PC, Cray, Sun) </li></ul></ul><ul><ul><li>the programming language (C, Java, C++) </li></ul></ul><ul><ul><li>the programmer (you, me, Bill Joy) </li></ul></ul><ul><li>While different in detail, all hardware and programming models are equivalent in some sense: Turing machines . </li></ul><ul><li>It suffices to count basic operations. </li></ul><ul><li>Crude but valuable measure of algorithm’s performance as a function of input size . </li></ul>
26. 26. Average, Best, and Worst-Case <ul><li>On which input instances should the algorithm’s performance be judged? </li></ul><ul><li>Average case: </li></ul><ul><ul><li>Real world distributions difficult to predict </li></ul></ul><ul><li>Best case: </li></ul><ul><ul><li>Seems unrealistic </li></ul></ul><ul><li>Worst case: </li></ul><ul><ul><li>Gives an absolute guarantee </li></ul></ul><ul><ul><li>We will use the worst-case measure. </li></ul></ul>
27. 27. Examples <ul><li>Vector addition Z = A+B </li></ul><ul><ul><li>for (int i=0; i<n; i++) </li></ul></ul><ul><ul><ul><li>Z[i] = A[i] + B[i]; </li></ul></ul></ul><ul><ul><li>T(n) = c n </li></ul></ul><ul><li>Vector (inner) multiplication z =A*B </li></ul><ul><ul><li>z = 0; </li></ul></ul><ul><ul><li>for (int i=0; i<n; i++) </li></ul></ul><ul><ul><ul><li>z = z + A[i]*B[i]; </li></ul></ul></ul><ul><ul><li>T(n) = c’ + c 1 n </li></ul></ul>
28. 28. Examples <ul><li>Vector (outer) multiplication Z = A*B T </li></ul><ul><ul><li>for (int i=0; i<n; i++) </li></ul></ul><ul><ul><li>for (int j=0; j<n; j++) </li></ul></ul><ul><ul><li>Z[i,j] = A[i] * B[j]; </li></ul></ul><ul><ul><li>T(n) = c 2 n 2 ; </li></ul></ul><ul><li>A program does all the above </li></ul><ul><ul><li>T(n) = c 0 + c 1 n + c 2 n 2 ; </li></ul></ul>
29. 29. Simplifying the Bound <ul><li>T(n) = c k n k + c k-1 n k-1 + c k-2 n k-2 + … + c 1 n + c o </li></ul><ul><ul><li>too complicated </li></ul></ul><ul><ul><li>too many terms </li></ul></ul><ul><ul><li>Difficult to compare two expressions, each with 10 or 20 terms </li></ul></ul><ul><li>Do we really need that many terms? </li></ul>
30. 30. Simplifications <ul><li>Keep just one term! </li></ul><ul><ul><li>the fastest growing term (dominates the runtime) </li></ul></ul><ul><li>No constant coefficients are kept </li></ul><ul><ul><li>Constant coefficients affected by machines, languages, etc. </li></ul></ul><ul><li>Asymtotic behavior (as n gets large) is determined entirely by the leading term. </li></ul><ul><ul><li>Example . T(n) = 10 n 3 + n 2 + 40n + 800 </li></ul></ul><ul><ul><ul><li>If n = 1,000, then T(n) = 10,001,040,800 </li></ul></ul></ul><ul><ul><ul><li>error is 0.01% if we drop all but the n 3 term </li></ul></ul></ul><ul><ul><li>In an assembly line the slowest worker determines the throughput rate </li></ul></ul>
31. 31. Simplification <ul><li>Drop the constant coefficient </li></ul><ul><ul><li>Does not effect the relative order </li></ul></ul>
32. 32. Simplification <ul><li>The faster growing term (such as 2 n ) eventually will outgrow the slower growing terms (e.g., 1000 n) no matter what their coefficients! </li></ul><ul><li>Put another way, given a certain increase in allocated time, a higher order algorithm will not reap the benefit by solving much larger problem </li></ul>
33. 33. Complexity and Tractability Assume the computer does 1 billion ops per sec.
34. 34. 2 n n 2 n log n n log n log n n n log n n 2 n 3 n 3 2 n
35. 35. Another View <ul><li>More resources (time and/or processing power) translate into large problems solved if complexity is low </li></ul>1.3 13 10 2 n 2.2 22 10 N 3 3.2 45 14 5n 2 10 10 1 1000n 10 100 10 100n Increase in Problem size Problem size solved in 10 4 sec Problem size solved in 10 3 sec T(n)
36. 36. Asymptotics <ul><li>They all have the same “growth” rate </li></ul>
37. 37. Caveats <ul><li>Follow the spirit, not the letter </li></ul><ul><ul><li>a 100n algorithm is more expensive than n 2 algorithm when n < 100 </li></ul></ul><ul><li>Other considerations: </li></ul><ul><ul><li>a program used only a few times </li></ul></ul><ul><ul><li>a program run on small data sets </li></ul></ul><ul><ul><li>ease of coding, porting, maintenance </li></ul></ul><ul><ul><li>memory requirements </li></ul></ul>
38. 38. Asymptotic Notations <ul><li>Big-O, “bounded above by”: T(n) = O(f(n)) </li></ul><ul><ul><li>For some c and N, T(n)  c·f(n) whenever n > N. </li></ul></ul><ul><li>Big-Omega, “bounded below by”: T(n) =  (f(n)) </li></ul><ul><ul><li>For some c>0 and N, T(n)  c·f(n) whenever n > N. </li></ul></ul><ul><ul><li>Same as f(n) = O(T(n)). </li></ul></ul><ul><li>Big-Theta, “bounded above and below”: T(n) =  (f(n)) </li></ul><ul><ul><li>T(n) = O(f(n)) and also T(n) =  (f(n)) </li></ul></ul><ul><li>Little-o, “strictly bounded above”: T(n) = o(f(n)) </li></ul><ul><ul><li>T(n)/f(n)  0 as n   </li></ul></ul>
39. 39. By Pictures <ul><li>Big-Oh (most commonly used) </li></ul><ul><ul><li>bounded above </li></ul></ul><ul><li>Big-Omega </li></ul><ul><ul><li>bounded below </li></ul></ul><ul><li>Big-Theta </li></ul><ul><ul><li>exactly </li></ul></ul><ul><li>Small-o </li></ul><ul><ul><li>not as expensive as ... </li></ul></ul>
40. 40. Example
41. 41. Examples
42. 42. Summary (Why O(n)?) <ul><li>T(n) = c k n k + c k-1 n k-1 + c k-2 n k-2 + … + c 1 n + c o </li></ul><ul><li>Too complicated </li></ul><ul><li>O(n k ) </li></ul><ul><ul><li>a single term with constant coefficient dropped </li></ul></ul><ul><li>Much simpler, extra terms and coefficients do not matter asymptotically </li></ul><ul><li>Other criteria hard to quantify </li></ul>
43. 43. Runtime Analysis <ul><li>Useful rules </li></ul><ul><ul><li>simple statements (read, write, assign) </li></ul></ul><ul><ul><ul><li>O(1) (constant) </li></ul></ul></ul><ul><ul><li>simple operations (+ - * / == > >= < <= </li></ul></ul><ul><ul><ul><li>O(1) </li></ul></ul></ul><ul><ul><li>sequence of simple statements/operations </li></ul></ul><ul><ul><ul><li>rule of sums </li></ul></ul></ul><ul><ul><li>for, do, while loops </li></ul></ul><ul><ul><ul><li>rules of products </li></ul></ul></ul>
44. 44. Runtime Analysis (cont.) <ul><li>Two important rules </li></ul><ul><ul><li>Rule of sums </li></ul></ul><ul><ul><ul><li>if you do a number of operations in sequence, the runtime is dominated by the most expensive operation </li></ul></ul></ul><ul><ul><li>Rule of products </li></ul></ul><ul><ul><ul><li>if you repeat an operation a number of times, the total runtime is the runtime of the operation multiplied by the iteration count </li></ul></ul></ul>
45. 45. Runtime Analysis (cont.) <ul><li>if (cond) then O(1) </li></ul><ul><ul><li>body 1 T 1 (n) </li></ul></ul><ul><li>else </li></ul><ul><ul><li>body 2 T 2 (n) </li></ul></ul><ul><li>endif </li></ul><ul><li>T(n) = O(max (T 1 (n), T 2 (n)) </li></ul>
46. 46. Runtime Analysis (cont.) <ul><li>Method calls </li></ul><ul><ul><li>A calls B </li></ul></ul><ul><ul><li>B calls C </li></ul></ul><ul><ul><li>etc. </li></ul></ul><ul><li>A sequence of operations when call sequences are flattened </li></ul><ul><ul><li>T(n) = max(T A (n), T B (n), T C (n)) </li></ul></ul>
47. 47. Example <ul><ul><li>for (i=1; i<n; i++) </li></ul></ul><ul><ul><ul><li>if A(i) > maxVal then </li></ul></ul></ul><ul><ul><ul><ul><li>maxVal= A(i); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>maxPos= i; </li></ul></ul></ul></ul><ul><li>Asymptotic Complexity: O(n) </li></ul>
48. 48. Example <ul><ul><li>for (i=1; i<n-1; i++) </li></ul></ul><ul><ul><ul><li>for (j=n; j>= i+1; j--) </li></ul></ul></ul><ul><ul><ul><ul><li>if (A(j-1) > A(j)) then </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>temp = A(j-1); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>A(j-1) = A(j); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>A(j) = tmp; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>endif </li></ul></ul></ul></ul><ul><ul><ul><li>endfor </li></ul></ul></ul><ul><ul><li>endfor </li></ul></ul><ul><li>Asymptotic Complexity is O(n 2 ) </li></ul>
49. 49. Run Time for Recursive Programs <ul><li>T(n) is defined recursively in terms of T(k), k<n </li></ul><ul><li>The recurrence relations allow T(n) to be “unwound” recursively into some base cases (e.g., T(0) or T(1)). </li></ul><ul><li>Examples: </li></ul><ul><ul><li>Factorial </li></ul></ul><ul><ul><li>Hanoi towers </li></ul></ul>
50. 50. Example: Factorial <ul><li>int factorial (int n) { </li></ul><ul><li>if (n<=1) return 1; </li></ul><ul><li>else return n * factorial(n-1); </li></ul><ul><li>} </li></ul><ul><li>factorial (n) = n*n-1*n-2* … *1 </li></ul><ul><li>n * factorial(n-1) </li></ul><ul><li>n-1 * factorial(n-2) </li></ul><ul><li>n-2 * factorial(n-3) </li></ul><ul><li>… </li></ul><ul><li>2 *factorial(1) </li></ul>T(n) T(n-1) T(n-2) T(1)
51. 51. Example: Factorial (cont.) <ul><li>int factorial1(int n) { </li></ul><ul><li>if (n<=1) return 1; </li></ul><ul><li>else { </li></ul><ul><li>fact = 1; </li></ul><ul><li>for (k=2;k<=n;k++) </li></ul><ul><li>fact *= k; </li></ul><ul><li>return fact; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Both algorithms are O(n). </li></ul>
52. 52. Example: Hanoi Towers <ul><li>Hanoi(n,A,B,C) = </li></ul><ul><li>Hanoi(n-1,A,C,B)+Hanoi(1,A,B,C)+Hanoi(n-1,C,B,A) </li></ul>
53. 53. // Early-terminating version of selection sort bool sorted = false; !sorted && sorted = true; else sorted = false; // out of order <ul><li>Worst Case </li></ul><ul><li>Best Case </li></ul>template<class T> void SelectionSort(T a[], int n) { for (int size=n; (size>1); size--) { int pos = 0; // find largest for (int i = 1; i < size; i++) if (a[pos] <= a[i]) pos = i; Swap(a[pos], a[size - 1]); } } Worst Case, Best Case, and Average Case
54. 54. <ul><li>T(N)=6N+4 : n0=4 and c=7, f(N)=N </li></ul><ul><li>T(N)=6N+4 <= c f(N) = 7N for N>=4 </li></ul><ul><li>7N+4 = O(N) </li></ul><ul><li>15N+20 = O(N) </li></ul><ul><li>N 2 =O(N)? </li></ul><ul><li>N log N = O(N)? </li></ul><ul><li>N log N = O(N 2 )? </li></ul>T(N) f(N) c f(N) n 0 T(N)=O(f(N)) <ul><li>N 2 = O(N log N)? </li></ul><ul><li>N 10 = O(2 N )? </li></ul><ul><li>6N + 4 = W(N) ? 7N? </li></ul><ul><li>N+4 ? N 2 ? N log N? </li></ul><ul><li>N log N = W(N 2 )? </li></ul><ul><li>3 = O(1) </li></ul><ul><li>1000000=O(1) </li></ul><ul><li>Sum i = O(N)? </li></ul>
55. 55. An Analogy: Cooking Recipes <ul><li>Algorithms are detailed and precise instructions. </li></ul><ul><li>Example: bake a chocolate mousse cake. </li></ul><ul><ul><li>Convert raw ingredients into processed output. </li></ul></ul><ul><ul><li>Hardware (PC, supercomputer vs. oven, stove) </li></ul></ul><ul><ul><li>Pots, pans, pantry are data structures. </li></ul></ul><ul><li>Interplay of hardware and algorithms </li></ul><ul><ul><li>Different recipes for oven, stove, microwave etc. </li></ul></ul><ul><li>New advances. </li></ul><ul><ul><li>New models: clusters, Internet, workstations </li></ul></ul><ul><ul><li>Microwave cooking, 5-minute recipes, refrigeration </li></ul></ul>
56. 56. Lecture 2 - Recursion
57. 57. What is Recursion? <ul><li>Recursion is when a function either directly or indirectly makes a call to itself. </li></ul><ul><li>Recursion is a powerful problem solving tool. </li></ul><ul><li>Many mathematical functions and algorithms are most easily expressed using recursion. </li></ul>
58. 58. How does it work? <ul><li>Functions are implemented using an internal stack of activation records . </li></ul><ul><li>Each time a function is called a new activation record is pushed on the stack. </li></ul><ul><li>When a function returns, the stack is popped and the activation record of the calling method is on top of the stack. </li></ul>
59. 59. How does it work? (cont.) <ul><li>The function being called, and whose activation record is being pushed on the stack, can be different from the calling function (e.g., when main calls a function). </li></ul><ul><li>The function being called can be a different instance of the calling subprogram. </li></ul><ul><li>Each instance of the function has its own parameters and local variables. </li></ul>
60. 60. Example <ul><li>Many mathematical functions are defined recursively. For example, the factorial function: </li></ul>N! = N * (N-1)! for N>0 0! = 1 <ul><li>We have defined factorial in terms of a smaller (or simpler) instance of itself. </li></ul><ul><li>We must also define a base case or stopping condition. </li></ul>
61. 61. Recursive Function Call <ul><li>A recursive call is a function call in which the called function is the same as the one making the call. </li></ul><ul><li>We must avoid making an infinite sequence of function calls (infinite recursion). </li></ul>
62. 62. Finding a Recursive Solution <ul><li>Each successive recursive call should bring you closer to a situation in which the answer is known. general (recursive) case </li></ul><ul><li>A case for which the answer is known (and can be expressed without recursion) is called a base case . </li></ul>
63. 63. General format for many recursive functions <ul><li>if (some conditions for which answer is known) </li></ul><ul><li> // base case </li></ul><ul><li> solution statement </li></ul><ul><li>else // general case </li></ul><ul><li> recursive function call </li></ul>
64. 64. Tail Recursion <ul><li>Use only one recursive call at the end of a function </li></ul><ul><li>void tail (int i) { </li></ul><ul><li>if (i > 0) { </li></ul><ul><li>System.out.print( i+ “ ”); </li></ul><ul><li>tail(i – 1); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>void iterativetail(int i) { for ( ; i > 0; i--) System.out.print( i+ “ ”); }
65. 65. NonTail Recursion <ul><li>void nonTail (int i) { </li></ul><ul><li>if (i > 0) { </li></ul><ul><li> nonTail(i – 1); </li></ul><ul><li>System.out.print( i+ “ ”); </li></ul><ul><li>nonTail(i – 1); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
66. 66. Indirect recursion <ul><li>Receive( buffer ) </li></ul><ul><li>while buffer is not filled up </li></ul><ul><li>If information is still incoming </li></ul><ul><li>get a char and store it in buffer; </li></ul><ul><li>else exit(); </li></ul><ul><li>decode(buffer); </li></ul><ul><li>Decode(buffer) </li></ul><ul><li>decode information in buffer; </li></ul><ul><li>store(buffer); </li></ul><ul><li>Store(buffer) </li></ul><ul><li>transfer information from buffer to file; </li></ul><ul><li>receive(buffer); </li></ul>
67. 67. Nested recursion <ul><li>h(n) { 0 if n=0 </li></ul><ul><li>h(n) ={ n if n>4 </li></ul><ul><li>h(n) { h(2+h(2n)) if n <=4 </li></ul>
68. 68. Writing a recursive function to find n factorial <ul><li>DISCUSSION </li></ul><ul><li>The function call Factorial(4) should have value 24, because that is 4 * 3 * 2 * 1 . </li></ul><ul><li>For a situation in which the answer is known, the value of 0! is 1. </li></ul><ul><li>So our base case could be along the lines of </li></ul><ul><li>if ( number == 0 ) </li></ul><ul><li>return 1; </li></ul>
69. 69. Writing a recursive function to find Factorial(n) <ul><li>Now for the general case . . . </li></ul><ul><li>The value of Factorial(n) can be written as </li></ul><ul><li>n * the product of the numbers from (n - 1) to 1, </li></ul><ul><li>that is, </li></ul><ul><li> n * (n - 1) * . . . * 1 </li></ul><ul><li>or, n * Factorial(n - 1) </li></ul><ul><li>And notice that the recursive call Factorial(n - 1) gets us “closer” to the base case of Factorial(0). </li></ul>
70. 70. Recursive Function Example: Factorial <ul><li>Problem: calculate n! (n factorial) </li></ul><ul><li>n! = 1 if n = 0 </li></ul><ul><li>n! = 1 * 2 * 3 *...* n if n > 0 </li></ul><ul><li>Recursively: </li></ul><ul><li>if n = 0 , then n! = 1 </li></ul><ul><li>if n > 0, then n! = n * ( n-1 ) ! </li></ul>
71. 71. Factorial Function <ul><li>int RecFactorial( /* in */ int n) </li></ul><ul><li>// Calculates n factorial, n! </li></ul><ul><li>// Precondition: n is a non-negative </li></ul><ul><li>// integer </li></ul><ul><li>{ </li></ul><ul><li>if ( n <= 0 ) then </li></ul><ul><li>return 1 </li></ul><ul><li>else </li></ul><ul><li>return n * RecFa ctorial ( n-1 ) </li></ul><ul><li>} </li></ul>
72. 72. <ul><li>1 int fact(int n) </li></ul><ul><li>2 { </li></ul><ul><li>3 if ( n <= 0 ) then </li></ul><ul><li>4 return 1; </li></ul><ul><li>5 else </li></ul><ul><li>6 return n*fact(n-1); </li></ul><ul><li>7 } </li></ul><ul><li>main(...) { </li></ul><ul><li>... </li></ul><ul><li>20 System.out.print( (fact(3)); </li></ul><ul><li>... </li></ul>returns 6 to main()
73. 73. Exponentiation <ul><li>base exponent </li></ul><ul><li>e.g. 5 3 </li></ul><ul><li>Could be written as a function </li></ul><ul><li>Power(base, exp) </li></ul>
74. 74. Can we write it recursively? <ul><li>b e = b * b (e-1) </li></ul><ul><li>What’s the limiting case? </li></ul><ul><li>When e = 0 we have b 0 which always equals? </li></ul><ul><li>1 </li></ul>
75. 75. Another Recursive Function <ul><li>1 function Power returnsa Num(base, exp) </li></ul><ul><li>2 // Computes the value of Base Exp </li></ul><ul><li>3 // Pre: exp is a non-negative integer </li></ul><ul><li>4 if ( exp = 0 ) then </li></ul><ul><li>5 returns 1 </li></ul><ul><li>6 else </li></ul><ul><li>7 returns base * Power (base, exp-1 ) </li></ul><ul><li>8 endif </li></ul><ul><li>9 endfunction </li></ul>Power x N = x * x N -1 for N >0 x 0 = 1 main(...) { ... 20 cout << (Power(2,3)); ...
76. 76. <ul><li>A man has an infant male-female pair of rabbits in a hutch entirely surrounded by a wall. We wish to know how many rabbits can be bred from this pair in one year, if the nature of the rabbits is such that every month they breed one other male-female pair which begins to breed in the second month after their birth. Assume that no rabbits die during the year. </li></ul>The Puzzle
77. 77. A Tree Diagram for Fibonacci’s Puzzle
78. 78. Observations <ul><li>The number of rabbits at the beginning of any month equals the number of rabbits of the previous month plus the number of new pairs . </li></ul><ul><li>The number of new pairs at the beginning of a month equals the number of pairs two months ago . </li></ul><ul><li>One gets the sequence: </li></ul><ul><li>1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … 233 </li></ul>
79. 79. Fibonacci sequence <ul><li>Recursive definition </li></ul><ul><ul><li>f(1) = 1 </li></ul></ul><ul><ul><li>f(2) = 1 </li></ul></ul><ul><ul><li>f(n) = f(n-1) + f(n-2) </li></ul></ul>
80. 80. <ul><li>Fibonacci Number Sequence </li></ul><ul><ul><li>if n = 1, then Fib(n) = 1 </li></ul></ul><ul><ul><li>if n = 2, then Fib(n) = 1 </li></ul></ul><ul><ul><li>if n > 2, then Fib(n) = Fib(n-2) + Fib(n-1) </li></ul></ul><ul><li>Numbers in the series: </li></ul><ul><ul><li>1, 1, 2, 3, 5, 8, 13, 21, 34, ... </li></ul></ul>A More Complex Recursive Function
81. 81. Fibonacci Sequence Function <ul><li>function Fib returnsaNum (n) </li></ul><ul><li>// Calculates the nth Fibonacci number </li></ul><ul><li>// Precondition: N is a positive integer </li></ul><ul><li>if ( (n = 1) OR (n = 2) ) then </li></ul><ul><li>returns 1 </li></ul><ul><li>else </li></ul><ul><li>returns Fib ( n-2 ) + Fib ( n-1 ) </li></ul><ul><li>endif </li></ul><ul><li>endfunction //Fibonacci </li></ul>
82. 82. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5)
83. 83. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4)
84. 84. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4) Fib(3): Fib returns Fib(1) + Fib(2)
85. 85. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4) Fib(3): Fib returns Fib(1) + Fib(2) Fib(1): Fib returns 1
86. 86. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4) Fib(3): Fib returns 1 + Fib(2)
87. 87. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4) Fib(3): Fib returns 1 + Fib(2) Fib(2): Fib returns 1
88. 88. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns Fib(3) + Fib(4) Fib(3): Fib returns 1 + 1
89. 89. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4)
90. 90. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns Fib(2) + Fib(3)
91. 91. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns Fib(2) + Fib(3) Fib(2): Fib returns 1
92. 92. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3)
93. 93. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3) Fib(3): Fib returns Fib(1) + Fib(2)
94. 94. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3) Fib(3): Fib returns Fib(1) + Fib(2) Fib(1): Fib returns 1
95. 95. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3) Fib(3): Fib returns 1 + Fib(2)
96. 96. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3) Fib(3): Fib returns 1 + Fib(2) Fib(2): Fib returns 1
97. 97. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + Fib(3) Fib(3): Fib returns 1 + 1
98. 98. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + Fib(4) Fib(4): Fib returns 1 + 2
99. 99. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5) Fib(5): Fib returns 2 + 3
100. 100. Tracing with Multiple Recursive Calls Main Algorithm: answer <- 5
101. 101. Fib(5) 15 calls to Fib to find the 5th Fibonacci number!!! Fib(3) Fib(4) Fib(3) Fib(2) Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0) Fib(2) Fib(1) Fib(1) Fib(0)
102. 102. Excessive Recursion-Fibonacci recursion <ul><li>Int iterativeFib ( int n ) </li></ul><ul><li>if ( n < 2) </li></ul><ul><li>return n; </li></ul><ul><li>else { </li></ul><ul><li>int i=2, tmp,current = 1, last=0; </li></ul><ul><li>for ( ; i<=n; ++i) { </li></ul><ul><li>tmp=current; </li></ul><ul><li>current+=last; </li></ul><ul><li>last=tmp; </li></ul><ul><li>} </li></ul><ul><li>return current; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
103. 105. Rules of Recursion <ul><li>First two fundamental rules of recursion: </li></ul><ul><ul><li>Base cases: Always have at least one case that can be solved without using recursion. </li></ul></ul><ul><ul><li>Make progress: Any recursive call must make progress towards the base case. </li></ul></ul>
104. 106. Rules of Recursion <ul><li>Third fundamental rule of recursion: </li></ul><ul><ul><li>“ You gotta believe”: Always assume that the recursive call works. </li></ul></ul>
105. 107. Rules of Recursion <ul><li>Fourth fundamental rule of recursion: </li></ul><ul><ul><li>Compound interest rule: Never duplicate work by solving the same instance of a problem in separate recursive calls. </li></ul></ul>
106. 108. Towers of Hanoi <ul><li>The puzzle consisted of N disks and three poles: A (the source), B (the destination), and C (the spare) </li></ul>
107. 109. Towers of Hanoi B C A
108. 110. Towers of Hanoi B C A
109. 111. Towers of Hanoi B C A
110. 112. Towers of Hanoi B C A
111. 113. Towers of Hanoi B C A
112. 114. Towers of Hanoi B C A
113. 115. Towers of Hanoi B C A
114. 116. Towers of Hanoi B C A
115. 117. Towers of Hanoi A pseudocode description of the solution is: Towers(Count, Source, Dest, Spare) if (Count is 1) Move the disk directly from Source to Dest else { Solve Towers(Count-1, Source, Spare, Dest) Solve Towers(1, Source, Dest, Spare) Solve Towers(Count-1, Spare, Dest, Source) }
116. 118. Towers of Hanoi void solveTowers( int count, char source, char dest, char spare){ if (count == 1) cout<<“Move disk from pole “ << source << &quot; to pole &quot; << destination <<endl; else { towers(count-1, source, spare, destination); towers(1, source, destination, spare); towers(count-1, spare, destination, source); }//end if }//end solveTowers
117. 119. Recall that . . . <ul><li>Recursion occurs when a function calls itself (directly or indirectly). </li></ul><ul><li>Recursion can be used in place of iteration (looping). </li></ul><ul><li>Some functions can be written more easily using recursion. </li></ul>
118. 120. Pascal Triangle (Is this recursive?)
119. 121. Pascal Triangle <ul><li>The combinations of n items taken r at a time . For example: three items:    a    b    c </li></ul><ul><li>taken 2 at a time:    ab    ac    bc </li></ul><ul><li>Thus there are three combinations of 3 items taken 2 at a time. </li></ul><ul><li>In General:    C(n,r) = n!/(r!(n-r)!)     Obviously you can calculate C(n,r) using factorials. </li></ul>
120. 122. Pascal Triangle <ul><li>This leads to Pascal Triangle: </li></ul>n     0                             1              1                       1       1              2                    1     2      1              3                1     3      3     1              4            1     4      6      4    1              5        1     5      10     10    5    1 This can also be written:             r     0    1    2    3     4    5         n  0     1             1     1     1              2     1     2    1              3     1     3    3    1             4     1     4    6    4    1              5     1     5   10  10    5    1
121. 123. Pascal Triangle <ul><li>Note from Pascal's Triangle that:     C(n,r) = C(n-1, r-1) + C(n-1,r) </li></ul><ul><li>This leads to the recurrence for nonnegative r and n, C(n,r) =               1             if r = 0 or r = n,                            0             if r > n,                             C(n-1, r-1) + C(n-1,r)     otherwise. </li></ul>
122. 124. Pascal Triangle <ul><li>This immediately leads to the recursive function for combinations: </li></ul><ul><li>  int C(int n, int r) </li></ul><ul><li>{   if((r == 0) || (r == n))      return 1;   else if(r > n)       return 0;   else        return C(n-1, r-1) + C(n-1, r);     } </li></ul>
123. 125. What is the value of rose(25) ? <ul><li>int rose (int n) </li></ul><ul><li>{ </li></ul><ul><li>if ( n == 1 ) // base case </li></ul><ul><li>return 0; </li></ul><ul><li>else // general case </li></ul><ul><li>return ( 1 + rose ( n / 2 ) ); </li></ul><ul><li>} </li></ul>
124. 126. Finding the value of rose(25) <ul><li>rose(25) the original call </li></ul><ul><li>= 1 + rose(12) first recursive call </li></ul><ul><li>= 1 + ( 1 + rose(6) ) second recursive call </li></ul><ul><li>= 1 + ( 1 + ( 1 + rose(3) ) ) third recursive call </li></ul><ul><li>= 1 + ( 1 + ( 1 + (1 + rose(1) ) ) ) fourth recursive call </li></ul><ul><li>= 1 + 1 + 1 + 1 + 0 </li></ul><ul><li>= 4 </li></ul>
125. 127. Writing recursive functions <ul><li>There must be at least one base case, and at least one general (recursive) case. The general case should bring you “closer” to the base case. </li></ul><ul><li>The parameter(s) in the recursive call cannot all be the same as the formal parameters in the heading. Otherwise, infinite recursion would occur. </li></ul><ul><li>In function rose( ) , the base case occurred when (n == 1) was true. The general case brought us a step closer to the base case, because in the general case the call was to rose(n/2) , and the argument n/2 was closer to 1 (than n was). </li></ul>
126. 128. Three-Question Method of verifying recursive functions <ul><li>Base-Case Question: Is there a nonrecursive way out of the function? </li></ul><ul><li>Smaller-Caller Question: Does each recursive function call involve a smaller case of the original problem leading to the base case? </li></ul><ul><li>General-Case Question: Assuming each recursive call works correctly, does the whole function work correctly? </li></ul>
127. 129. Guidelines for writing recursive functions <ul><li>1. Get an exact definition of the problem to be solved. </li></ul><ul><li>2. Determine the size of the problem to be solved on this call to the function. On the initial call, the size of the whole problem is expressed by the actual parameter(s). </li></ul><ul><li>3. Identify and solve the base case(s) which have non-recursive solutions. </li></ul><ul><li>4. Identify and solve the general case(s) in terms of smaller (recursive) cases of the same problem. </li></ul>
128. 130. <ul><li>struct ListType </li></ul><ul><li>{ </li></ul><ul><li>int length ; // number of elements in the list </li></ul><ul><li>int info[ MAX_ITEMS ] ; </li></ul><ul><li>} ; </li></ul><ul><li>ListType list ; </li></ul>struct ListType
129. 131. Recursive function to determine if value is in list <ul><li>PROTOTYPE </li></ul><ul><li>bool ValueInList( ListType list , int value , int startIndex ) </li></ul><ul><li> </li></ul><ul><li>Already searched Needs to be searched </li></ul>index of current element to examine 74 36 . . . 95 list[0] [1] [startIndex] 75 29 47 . . . [length -1]
130. 132. <ul><li>bool ValueInList ( ListType list , int value , int startIndex ) </li></ul><ul><li>// Searches list for value between positions startIndex </li></ul><ul><li>// and list.length-1 </li></ul><ul><li>{ </li></ul><ul><li>if ( list.info[startIndex] == value ) // one base case return true ; </li></ul><ul><li>else if (startIndex == list.length -1 ) // another base case </li></ul><ul><li> return false ; </li></ul><ul><li> else // general case </li></ul><ul><li> return ValueInList( list, value, startIndex + 1 ) ; </li></ul><ul><li>} </li></ul>
131. 133. “ Why use recursion?” Many solutions could have been written without recursion, by using iteration instead. The iterative solution uses a loop, and the recursive solution uses an if statement. However, for certain problems the recursive solution is the most natural solution. This often occurs when pointer variables are used.
132. 134. When to Use Recursion <ul><ul><li>If the problem is recursive in nature therefore it is likely the a recursive algorithm will be preferable and will be less complex </li></ul></ul><ul><ul><li>If running times of recursive and non-recursive algorithms are hardly perceivable, recursive version is better </li></ul></ul><ul><ul><li>If both recursive and non-recursive algorithms have the same development complexity, a non-recursive version should be preferred </li></ul></ul><ul><ul><li>A third alternative in some problems is to use table-driven techniques </li></ul></ul><ul><ul><ul><ul><li>Sometimes we know that we will not use the only a few values of a particular function </li></ul></ul></ul></ul><ul><ul><ul><ul><li>If this is the case an implementation using a table would probably suffice and the performance will be better </li></ul></ul></ul></ul>int factorial[8] = {1, 1, 2, 6, 24, 120, 720, 5040};
133. 135. Use a recursive solution when: <ul><li>The depth of recursive calls is relatively “shallow” compared to the size of the problem. </li></ul><ul><li>The recursive version does about the same amount of work as the nonrecursive version. </li></ul><ul><li>The recursive version is shorter and simpler than the nonrecursive solution. </li></ul>SHALLOW DEPTH EFFICIENCY CLARITY
134. 136. Lecture 3 – Binary Trees
135. 137. Why Trees? <ul><li>Limitations of </li></ul><ul><ul><li>Arrays </li></ul></ul><ul><ul><li>Linked lists </li></ul></ul><ul><ul><li>Stacks </li></ul></ul><ul><ul><li>Queues </li></ul></ul>
136. 138. Trees: Recursive Definition <ul><li>A tree is a collection of nodes. </li></ul><ul><li>The collection can be empty, or consist of a “root” node R. </li></ul><ul><li>There is a “directed edge” from R to the root of each subtree. The root of each subtree is a “child” of R . R is the “parent” of each subtree root. </li></ul>
137. 139. Trees: Recursive Definition (cont.) ROOT OF TREE T T1 T2 T3 T4 T5 SUBTREES
138. 140. Trees: An Example A B C D E F G H I
139. 141. Trees: More Definitions <ul><li>Nodes with no children are leaves : (C,E,F,H,I). </li></ul><ul><li>Nodes with the same parents are siblings : (B,C,D,E) and (G,H). </li></ul><ul><li>A path from node n to node m is the sequence of directed edges from n to m . </li></ul><ul><li>A length of a path is the number of edges in the path </li></ul>
140. 142. Trees: More Definitions (cont.) <ul><li>The level/depth of node n is the length of the path from the root to n . The level of the root is 0. </li></ul><ul><li>The height/depth of a tree is equal to the maximum level of a node in the tree. </li></ul><ul><li>The height of a node n is the length of the longest path from n to a leaf. The height of a leaf node is 0. </li></ul><ul><li>The height of a tree is equal to the height of the root node. </li></ul>
141. 143. Binary Trees – A Informal Definition <ul><li>A binary tree is a tree in which no node can have more than two children. </li></ul><ul><li>Each node has 0, 1, or 2 children </li></ul><ul><ul><li>In this case we can keep direct links to the children: </li></ul></ul>struct TreeNode { Object element; TreeNode *left_child; TreeNode *right_child; };
142. 144. Binary Trees – Formal Definition <ul><li>A binary tree is a structure that </li></ul><ul><ul><li>contains no nodes, or </li></ul></ul><ul><ul><li>is comprised of three disjoint sets of nodes: </li></ul></ul><ul><ul><ul><li>a root </li></ul></ul></ul><ul><ul><ul><li>a binary tree called its left subtree </li></ul></ul></ul><ul><ul><ul><li>a binary tree called its right subtree </li></ul></ul></ul><ul><li>A binary tree that contains no nodes is called empty </li></ul>
143. 145. Binary Trees: Recursive Definition ROOT OF TREE T T1 T2 SUBTREES *left_child *right_child
144. 146. Differences Between A Tree & A Binary Tree <ul><li>No node in a binary tree may have more than 2 children, whereas there is no limit on the number of children of a node in a tree. </li></ul><ul><li>The subtrees of a binary tree are ordered; those of a tree are not ordered. </li></ul>
145. 147. Differences Between A Tree & A Binary Tree (cont.) <ul><li>The subtrees of a binary tree are ordered; those of a tree are not ordered </li></ul><ul><li>Are different when viewed as binary trees </li></ul><ul><li>Are the same when viewed as trees </li></ul>a b a b
146. 148. Internal and External Nodes <ul><li>Because in a binary tree all the nodes must have the same number of children we are forced to change the concepts slightly </li></ul><ul><ul><li>We say that all internal nodes have two children </li></ul></ul><ul><ul><li>External nodes have no children </li></ul></ul>internal node external node
147. 149. Recursive definition of a Binary Tree <ul><li>Most of concepts related to binary trees can be explained recursive </li></ul><ul><li>For instance, A binary tree is: </li></ul><ul><ul><li>An external node , or </li></ul></ul><ul><ul><li>An internal node connected to a left binary tree and a right binary tree (called left and right subtrees) </li></ul></ul><ul><li>In programming terms we can see that our definition for a linked list (singly) can be modified to have two links from each node instead of one. </li></ul>
148. 150. <ul><li>Property2 : a unique path exists from the root to every other node </li></ul>What is a binary tree? (cont.)
149. 151. Mathematical Properties of Binary Trees <ul><li>Let's us look at some important mathematical properties of binary trees </li></ul><ul><li>A good understanding of these properties will help the understanding of the performance of algorithms that process trees </li></ul><ul><li>Some of the properties we'll describe relate also the structural properties of these trees. This is the case because performance characteristics of many algorithms depend on these structural properties and not only the number of nodes. </li></ul>
150. 152. Minimum Number Of Nodes <ul><li>Minimum number of nodes in a binary tree whose height is h . </li></ul><ul><li>At least one node at each level. </li></ul>minimum number of nodes is h + 1
151. 153. Maximum Number Of Nodes <ul><li>All possible nodes at first h levels are present </li></ul><ul><li>Maximum number of nodes </li></ul><ul><li>= 1 + 2 + 4 + 8 + … + 2 h = 2 h+1 - 1 </li></ul>
152. 154. Number of Nodes & Height <ul><li>Let n be the number of nodes in a binary tree whose height is h . </li></ul><ul><li>h + 1 <= n <= 2 h+1 – 1 </li></ul><ul><li>log 2 (n+1)-1 <= h <= n -1 </li></ul><ul><li>The max height of a tree with N nodes is N - 1 (same as a linked list) </li></ul><ul><li>The min height of a tree with N nodes is log(N+1)-1 </li></ul>
153. 155. Relationship Between Number of Nodes (Internal - External) <ul><li>A binary tree with N internal nodes has N+1 external nodes </li></ul><ul><li>Let's try to prove this using induction... </li></ul>
154. 156. Number of edges <ul><li>A binary tree with N internal nodes has 2N edges </li></ul>Let's try to prove this using induction...
155. 157. Number of edges <ul><li>A binary tree with N nodes (internal and external) has N-1 edges </li></ul>Let's try to prove this using induction...
156. 158. Binary Tree Representation <ul><li>Array representation </li></ul><ul><li>Linked representation </li></ul>
157. 159. Binary Trees <ul><li>Full binary tree : </li></ul><ul><ul><li>All internal nodes have two children. </li></ul></ul><ul><li>Complete binary tree : </li></ul><ul><ul><li>All leaves have the same level </li></ul></ul><ul><ul><li>All internal nodes have two children </li></ul></ul>
158. 160. Node Number Properties <ul><li>Parent of node i is node i/2 </li></ul><ul><ul><li>But node 1 is the root and has no parent </li></ul></ul><ul><li>Left child of node i is node 2i </li></ul><ul><ul><li>But if 2i > n , node i has no left child </li></ul></ul><ul><li>Right child of node i is node 2i+1 </li></ul><ul><ul><li>But if 2i+1 > n , node i has no right child </li></ul></ul>Complete binary tree 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
159. 161. Full Binary Tree With n Nodes <ul><li>Start with a complete binary tree that has at least n nodes. </li></ul><ul><li>Number the nodes as described earlier. </li></ul><ul><li>The binary tree defined by the nodes numbered 1 through n is the unique n node full binary tree. </li></ul>Full binary tree with 11 nodes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
160. 162. Array Representation <ul><li>Number the nodes using the numbering scheme for a full binary tree </li></ul><ul><li>Store the node numbered i in tree[i] </li></ul>a b c d e f g h i j 1 2 3 4 6 7 8 9 b a c d e f g h i j 1 2 3 4 5 6 7 8 9 10 tree[] 0 5 10
161. 163. Right-Skewed Binary Tree <ul><li>An n node binary tree needs an array whose length is between n+1 and 2 n </li></ul><ul><li>If h = n-1 then skewed binary tree </li></ul>a b 1 3 c 7 d 15 tree[] 0 5 10 a - b - - - c - - - - - - - 15 d
162. 164. Array Representation (cont.) <ul><li>Each tree node is represented as a struct </li></ul><ul><li>Struct TreeNode { </li></ul><ul><li>object element; </li></ul><ul><li>int child1; </li></ul><ul><li>int child2; </li></ul><ul><li>… </li></ul><ul><li>int childn; </li></ul><ul><li>}; </li></ul><ul><li>Struct TreeNode tree[100]; </li></ul>
163. 165. Linked Representation <ul><li>Each tree node is represented as an object whose data type is TreeNode </li></ul><ul><li>The space required by an n node binary tree is n * (space required by one node) </li></ul>
164. 166. Trees: Linked representation Implementation 1 struct TreeNode { Object element; TreeNode *child1; TreeNode *child2; . . . TreeNode *childn; }; <ul><li>Each node contains a link to all of its children. </li></ul><ul><li>This isn’t a good idea, because a node can have an arbitrary number of children! </li></ul>
165. 167. struct TreeNode { Object element; TreeNode *child1; TreeNode *sibling; }; Each node contain a link to its first child and a link to its next sibling. This is a better idea. Trees: Linked representation Implementation 2
166. 168. Implementation 2: Example / The downward links are to the first child; the horizontal links are to the next sibling. / / / / A / B F / C D E / G H / I /
167. 169. Binary Trees <ul><li>A binary tree is a tree whose nodes have at most two offspring </li></ul><ul><li>Example </li></ul><ul><li>struct nodeType { </li></ul><ul><li>object element; </li></ul><ul><li>struct nodeType *left, *right; </li></ul><ul><li>}; </li></ul><ul><li>struct nodeType *tree; </li></ul>
168. 170. Linked Representation Example a c b d f e g h leftChild element rightChild root
169. 171. Some Binary Tree Operations <ul><li>• Determine the height. </li></ul><ul><li>• Determine the number of nodes. </li></ul><ul><li>• Make a clone. </li></ul><ul><li>• Determine if two binary trees are clones. </li></ul><ul><li>• Display the binary tree. </li></ul><ul><li>• Evaluate the arithmetic expression </li></ul><ul><li>represented by a binary tree. </li></ul><ul><li>• Obtain the infix form of an expression. </li></ul><ul><li>• Obtain the prefix form of an expression. </li></ul><ul><li>• Obtain the postfix form of an expression. </li></ul>
170. 172. CS122 Algorithms and Data Structures Week 7: Binary Search Trees Binary Expression Trees
171. 173. Uses for Binary Trees… -- Binary Search Trees <ul><li>Use for storing and retrieving information </li></ul><ul><li>Insert, delete, and search faster than with a linked list </li></ul><ul><li>Idea: Store information in an ordered way (keys) </li></ul>
172. 174. A Property of Binary Search Trees <ul><li>The key of the root is larger than any key in the left subtree </li></ul><ul><li>The key of the root is smaller than any key in the right subtree </li></ul><ul><li>Note: Duplicated keys are not allowed </li></ul>
173. 175. A Property of Binary Search Tree ROOT OF TREE T T1 T2 SUBTREES *left_child *right_child X All nodes in T1 have keys < X. All nodes in T2 have keys > X.
174. 176. Binary Search Trees in C++ <ul><li>We will use two classes: </li></ul><ul><ul><li>The class BinaryNode simply constructs individual nodes in the tree. </li></ul></ul><ul><ul><li>The class BinarySearchTree maintains a pointer to the root of the binary search tree and includes methods for inserting and removing nodes. </li></ul></ul>
175. 177. Search Operation BinaryNode *search (const int &x, BinaryNode *t) { if ( t == NULL ) return NULL; if (x == t->key) return t; // Match if ( x < t->key ) return search( x, t->left ); else // t ->key < x return search( x, t->right ); }
176. 178. FindMin Operation BinaryNode* findMin (BinaryNode *t) { if ( t == NULL ) return NULL; if ( t -> left == NULL ) return t; return findMin (t -> left); } This method returns a pointer to the node containing the smallest element in the tree.
177. 179. FindMax Operation BinaryNode* findMax (BinaryNode *t) { if ( t == NULL ) return NULL; if ( t -> right == NULL ) return t; return findMax (t -> right); } This function returns a pointer to the node containing the largest element in the tree.
178. 180. Insert Operation <ul><li>To insert X into a tree T, proceed down the tree as you would with a find. If X is found, do nothing. Otherwise insert X at the last spot on the path that has been traversed. </li></ul>
179. 181. Insert Operation (cont.) void BinarySearchTree insert (const int &x, BinaryNode *&t) const { if (t == NULL) t = new BinaryNode (x, NULL, NULL); else if (x < t->key) insert(x, t->left); else if( t->key < x) insert(x, t->right); else ; // Duplicate entry; do nothing } Note the pointer t is passed using call by reference.
180. 182. Removal Operation <ul><li>If the node to be removed is a leaf, it can be deleted immediately. </li></ul><ul><li>If the node has one child , the node can be deleted after its parent adjusts a link to bypass the deleted node. </li></ul>
181. 183. <ul><li>If the node to be removed has two children , the general strategy is to replace the data of this node with the smallest key of the right subtree . </li></ul><ul><li>Then the node with the smallest data is now removed (this case is easy since this node cannot have two children). </li></ul>Removal Operation (cont.)
182. 184. Removal Operation (cont.) void remove (const int &x, BinaryNode* &t) const { if ( t == NULL ) return; // key is not found; do nothing if ( t->key == x) { if( t->left != NULL && t->right != NULL ) { // Two children t->key = findMin( t->right )->key; remove( t->key, t->right ); } else { // One child BinaryNode *oldNode = t; t = ( t->left != NULL ) ? t->left : t->right; delete oldNode; } } else { // Two recursive calls if ( x < t->key ) remove( x, t->left ); else if( t->key < x ) remove( x, t->right ); } }
183. 185. Deleting by merging
184. 186. Deleting by merging
185. 187. Deleting by copying
186. 188. Balancing a binary tree <ul><li>A binary tree is height-balanced or simply balanced if the difference in height of both the subtrees is either zero or one </li></ul><ul><li>Perfectly balanced if all leaves are to be found on one level or two levels. </li></ul>
187. 189. Balancing a binary tree
188. 190. Analysis <ul><li>The running time of these operations is O(lv) , where lv is the level of the node containing the accessed item. </li></ul><ul><li>What is the average level of the nodes in a binary search tree? It depends on how well balanced the tree is. </li></ul>
189. 191. Average Level of Nodes 10 5 20 1 8 13 34 Consider this very well-balanced binary search tree. What is the level of its leaf nodes? N=7 Data Order: 10, 5, 1, 8, 20, 13, 34
190. 192. A Better Analysis <ul><li>The analysis on the previous slide was for a particularly well-balanced binary search tree. However, not all binary search trees will be this well balanced. </li></ul><ul><li>In particular, binary search trees are created via insertions of data. Depending on the order of the data, various trees will emerge. </li></ul>
191. 193. Effect of Data Order Obtained if data is 4, 3, 2 1 Obtained if data is 1, 2, 3, 4 Note in these cases the average depth of nodes is about N/2 , not log(N)!
192. 194. Depth of Nodes <ul><li>In the best case the depth will be about O(log N). </li></ul><ul><li>In the worst case, if the data are already ordered, the depth will be about O(N). </li></ul>
193. 195. Effects of Data Order… <ul><li>So, if the input data are randomly ordered, what is the average depth of the nodes? </li></ul><ul><li>The analysis is beyond the scope of this course, but it can be shown that the average depth is O(log N), which is a very nice result. </li></ul>
194. 196. Summary <ul><li>In this lecture we showed that, for an average binary search tree, the average depth of the nodes is O(log N). This is quite amazing, indicating that the bad situations, which are O(N), don’t occur very often. </li></ul><ul><li>However, for those who are still concerned about the very bad situations, we can try to “balance” the trees. </li></ul>
195. 197. Uses for Binary Trees… -- Binary Expression Trees <ul><li>Binary trees are a good way to express arithmetic expressions. </li></ul><ul><ul><li>The leaves are operands and the other nodes are operators. </li></ul></ul><ul><ul><li>The left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator at the root of the subtree. </li></ul></ul>
196. 198. Binary Expression Trees: Examples <ul><li>a + b </li></ul>- a (a + b) * (c – d) / (e + f) + a b - a / + a b - c d + e f * /
197. 199. Merits of Binary Tree Form <ul><li>Left and right operands are easy to visualize </li></ul><ul><li>Code optimization algorithms work with the binary tree form of an expression </li></ul><ul><li>Simple recursive evaluation of expression </li></ul>+ a b - c d + e f * /
198. 200. Levels Indicate Precedence <ul><li>The levels of the nodes in the tree indicate their relative precedence of evaluation (we do not need parentheses to indicate precedence). </li></ul><ul><li>Operations at lower levels of the tree are evaluated later than those at higher levels. </li></ul><ul><li>The operation at the root is always the last operation performed. </li></ul>
199. 201. A Binary Expression Tree What value does it have? ( 4 + 2 ) * 3 = 18 ‘ *’ ‘ +’ ‘ 4’ ‘ 3’ ‘ 2’
200. 202. Inorder Traversal: (A + H) / (M - Y) ‘ +’ ‘ A’ ‘ H’ ‘ -’ ‘ M’ ‘ Y’ tree Print left subtree first Print right subtree last Print second ‘ /’
201. 203. Inorder Traversal (cont.) a + * b c + * + g * d e f Inorder traversal yields: (a + (b * c)) + (((d * e) + f) * g)
202. 204. Preorder Traversal: / + A H - M Y ‘ +’ ‘ A’ ‘ H’ ‘ -’ ‘ M’ ‘ Y’ tree Print left subtree second Print right subtree last Print first ‘ /’
203. 205. Preorder Traversal (cont.) a + * b c + * + g * d e f Preorder traversal yields: (+ (+ a (* b c)) (* (+ (* d e) f) g))
204. 206. ‘ +’ ‘ A’ ‘ H’ ‘ -’ ‘ M’ ‘ Y’ tree Print left subtree first Print right subtree second Print last Postorder Traversal: A H + M Y - / ‘ /’
205. 207. Postorder Traversal (cont.) a + * b c + * + g * d e f Postorder traversal yields: a b c * + d e * f + g * +
206. 208. Traversals and Expressions <ul><li>Note that the postorder traversal produces the postfix representation of the expression. </li></ul><ul><li>Inorder traversal produces the infix representation of the expression. </li></ul><ul><li>Preorder traversal produces a representation that is the same as the way that the programming language Lisp processes arithmetic expressions! </li></ul>
207. 209. Constructing an Expression Tree <ul><li>There is a simple O( N ) stack-based algorithm to convert a postfix expression into an expression tree. </li></ul><ul><li>Recall we also have an algorithm to convert an infix expression into postfix, so we can also convert an infix expression into an expression tree without difficulty (in O( N ) time). </li></ul>
208. 210. Expression Tree Algorithm <ul><li>Read the postfix expression one symbol at at time: </li></ul><ul><ul><li>If the symbol is an operand, create a one-node tree and push a pointer to it onto the stack. </li></ul></ul><ul><ul><li>If the symbol is an operator, pop two tree pointers T1 and T2 from the stack, and form a new tree whose root is the operator, and whose children are T1 and T2. </li></ul></ul><ul><ul><li>Push the new tree pointer on the stack. </li></ul></ul>
209. 211. Example a b + : Note: These stacks are depicted horizontally. a b + b a
210. 212. Example a b + c d e + : + b a c d e + b a c d e +
211. 213. Example a b + c d e + * : + b a c d e + *
212. 214. Example a b + c d e + * * : + b a c d e + * *