Presentation is loading. Please wait.

Presentation is loading. Please wait.

IST311 Recursion Sorting Searching

Similar presentations


Presentation on theme: "IST311 Recursion Sorting Searching"— Presentation transcript:

1 IST311 Recursion Sorting Searching
IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition by Y. Daniel Liang

2 Motivation – Solving Complex Problems
The Eight Queens puzzle consists on placing eight queens on a chessboard such that no two queens are on the same row, same column, or same diagonal, as shown in figure below. Your turn: Try placing n Queens on an n*n board

3 3. Motivation – Solving Complex Problems
8-Queens Strategy Try to collocate each queen in the next unchecked column, choosing the first available row. Mark the corresponding row, column, and diagonals as checked. If the current queen cannot be collocated in any unchecked cell, try to collocate the previous queen in another row. Do the same with the remaining queens, stop when all queens are placed or no more placement opportunities arise.

4 3. Using Recursion Recursion is one of the techniques used when applying the Divide-and- Conquer problem solving strategy. Two important aspects of Recursion It is a way of defining some problems (regardless of their implementation) It is a way of solving problems.

5 Example1 - Exponentiation
Problem Calculate the value of bn where n is an integer, and n≥0. Observation: bn = { 1 if n=0; b*bn-1 if n ˃ 0 Example: Compute the value of 24 = 2 * 23 = 2 * 22 = 2 * 21 = 2 * 20 = 1

6 Example2 - Computing Factorial
Notation n ! = n * (n-1)! factorial( n ) = n * factorial( n-1 ); Example: Compute the value of 4! factorial(4) = 4 * factorial( 3 ) = 4 * ( 3 * factorial(2) ) = 4 * ( 3 * (2 * factorial(1)) ) = 4 * ( 3 * ( 2 * (1 * factorial(0)))) = 4 * (3 * ( 2 * ( 1 * 1 ))) = 4 * (3 * ( 2 * 1)) = 4 * (3 * 2 ) = 4 * 6 = 24 6

7 Tracing Recursive Solution
Fact(4) = 4 x 6 = 24 Fact(4) = 4 x Fact(3) Fact(3) = 3 x 2 = 6 Fact(3) = 3 x Fact(2) Backtrack Recursive descend Fact(2) = 2 x Fact(1) Fact(2) = 2 x 1 = 2 Fact(1) = 1 x Fact(0) Fact(1) = 1 x 1 =1 Fact(0) = 1 ⟵ Base case 7

8 Computing Factorial private static int fact(int n) { if ( n == 0 )
return 1; else return n*fact(n-1); }

9 factorial(4) Stack Trace

10 Other Example – Recursive Summation
Recursive sum of first n numbers f(0) = 0; f(n) = n + f(n-1); Example: f (12) = Observe that f(12) = 12 + f(11), and f (11) = Which in turn can be computed as f(11) = 11 + f(10) Likewise, f(10) = 10 + f(9), and so on, until f(0) = 0 is discovered f(11) f(10)

11 Recursive Summation Recursively calculate the sum:
private static double SumPowerTwo(int n) { if ( n == 1 ) return 1.0/2; else return 1.0 / Math.pow(2, n) + SumPowerTwo( n-1 ); }

12 Fibonacci Numbers fib(0) = 1; fib(1) = 1;
Fibonacci series: indices: fib(0) = 1; fib(1) = 1; fib(index) = fib(index -1) + fib(index -2); index >=2 fib(3) = fib(2) + fib(1) = (fib(1) + fib(0)) + fib(1) = (1 + 0) +fib(1) = 1 + fib(1) = = 2

13 Fibonacci Numbers private static int Fib(int n) {
if ((n == 1) || (n == 0)) return 1; else return Fib(n - 2) + Fib(n - 1); }

14 Fibonnaci Numbers, cont.

15 Fibonnaci Numbers. Fibonacci Spiral
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … Known as “The fingerprint of God” See an interesting video at (watch at your own risk!) Images from: mathacademy.com,

16 Characteristics of Recursion
All recursive methods have the following characteristics: One or more base cases (the simplest case) are used to stop recursion (boundary conditions). Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. In general, to solve a problem using recursion, you break it into sub-problems. If a sub-problem resembles the original problem, you can apply the same approach to solve the sub-problem recursively. This sub-problem is almost the same as the original problem in nature with a smaller size.

17 Problem Solving Using Recursion
Let us consider a simple problem of printing a message for n times. You can break the problem into two subproblems: One is to print the message one time and the other is to print the message for n-1 times. The second problem is the same as the original problem with a smaller size. The base case for the problem is n==0. private static void PrintMsg(string msg, int times) { // printing a message a number of times if (times == 0) return; } Console.WriteLine(times + " " + msg); PrintMsg(msg, times - 1);

18 Version1: Palindrome Examples: 100a001, MADAMIMADAM, abccba are palindromes. private static bool IsPalindrome(string str) { if (str.Length == 0 || str.Length == 1) return true; } char first = str[0]; char last = str[str.Length - 1]; if (first != last) return false; else str = str.Substring(1, (str.Length - 1) - 1); return IsPalindrome(str);

19 Version2: Recursive Overridden Method
Examples: 100a001, MADAMIMADAM, abccba are palindromes. public static bool isPalindrome(string s) { return isPalindrome(s, 0, s.Length - 1); } public static bool isPalindrome(string s, int low, int high) if (high <= low) // Base case return true; else if (s[low] != s[high]) // Base case return false; else return isPalindrome(s, low + 1, high - 1);

20 Triangular Numbers Pythagoreans though there was a mystical message in the sequence 1, 3, 6, 10, 15, 21, 28, … those values were called triangular numbers because of their connection to the graphical construction of straight triangles. Current 1 2 3 4 5 6 7 Sum of previous 10 15 21 Result 28

21 Triangular Numbers & Right Triangles
How many bricks are needed to build a right pyramid of height (or base) n ?

22 Triangular Numbers private static int Triangular(int n) { if ( n == 1)
return 1; else return n + Triangular(n-1); }

23 Recursive Selection Sort
Find the largest number in the list and swap it with the last number. Ignore the last number and sort the remaining smaller list recursively. 55 66 44 11 22 33 largest 55 33 44 11 22 66 Do the same here with this smaller list 22 33 44 11 55 66 Repeat . . .

24 Recursive Selection Sort
public class SelectionSort // Recursive method for sorting the numbers { // find/place minimum number in each iteration public static void Main(string[] args) internal static void RecursiveSelectionSort( double[] list, int low, int high) // Initialize the list double[] myList = new double[] {5.0, 4.4, 1.9, 2.9, 3.4, 3.5}; if (low < high) // Print the original list int indexOfMin = 0; Console.WriteLine("My list before sorting is: "); double min = list[low]; PrintList(myList); for (int i = low + 1; i < high; i++) // Sort the list if (list[i] < min) RecursiveSelectionSort(myList,0,myList.Length - 1); min = list[i]; // Print the sorted list indexOfMin = i; Console.WriteLine(“\n My list after sorting is: "); } list[indexOfMin] = list[low]; // method for printing numbers list[low] = min; internal static void PrintList(double[] list) RecursiveSelectionSort(list, low + 1, high); for (int i = 0; i < list.Length; i++) } //recursiveSelectionSort Console.Write(list[i] + " "); } //class Console.WriteLine();

25 Recursive Binary Search
Case 1: If the key is less than the middle element, recursively search the key in the first half of the array. Case 2: If the key is equal to the middle element, the search ends with a match. Case 3: If the key is greater than the middle element, recursively search the key in the second half of the array. 7 11 55 44 77 99 first middle last

26 Recursive Implementation
// Use binary search to find the given key in the sample list public static int RecursiveBinarySearch(int[] list, int key) { int low = 0; int high = list.Length - 1; return RecursiveBinarySearch(list, key, low, high); } // Use binary search to find the key between list[low] & list[high] public static int RecursiveBinarySearch(int[] list, int key, int low, int high) if (low > high) // The list has been exhausted without a match return -low - 1; int mid = (low + high) / 2; if (key < list[mid]) return RecursiveBinarySearch(list, key, low, mid - 1); else if (key == list[mid]) return mid; else return RecursiveBinarySearch(list, key, mid + 1, high); }

27 Recursive Bubble Sort k temp
K elements are in their ‘correct’ position. temp k list[0] list[1] list[i] list[i+1] list[n-2] list[n-1] if ( list[i] > list[i+1] ) swap(list, i, i+1)

28 Recursive Bubble Sort static void Main(String[] args) {
int[] list = { 22, 33, 11, 55, 77, 66, 44 }; int n = list.Length; RecursiveBubble(list, n-1); ShowList(list); } private static void RecursiveBubble(int[] list, int n) if (n <= 1) return; for ( int i=0; i< n; i++) if (list[i] > list[i+1]) int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; RecursiveBubble(list, n - 1);

29 Merge Sort Algorithm The merge sort algorithm can be described recursively as follows: The algorithm divides the array into two halves and applies merge sort on each half recursively. After the two halves are sorted, merge them. 29

30 Merge Sort Algorithm (1 of 2)
/** Divide & Conquer method for sorting the numbers in given array */ public static void MergeSort(int[] list) { if (list.Length > 1) // Merge sort the first half int[] firstHalf = new int[list.Length / 2]; Array.Copy(list, 0, firstHalf, 0, list.Length / 2); MergeSort(firstHalf); // Merge sort the second half int secondHalfLength = list.Length - list.Length / 2; int[] secondHalf = new int[secondHalfLength]; Array.Copy(list, list.Length / 2, secondHalf, 0, secondHalfLength); MergeSort(secondHalf); // Merge firstHalf with secondHalf int[] temp = Merge(firstHalf, secondHalf); Array.Copy(temp, 0, list, 0, temp.Length); } 30

31 Merge Sort Algorithm (2 of 2)
/** Merge two sorted lists */ private static int[] Merge(int[] list1, int[] list2) { int[] temp = new int[list1.Length + list2.Length]; int current1 = 0; // Current index in list1 int current2 = 0; // Current index in list2 int current3 = 0; // Current index in temp while (current1 < list1.Length && current2 < list2.Length) if (list1[current1] < list2[current2]) temp[current3++] = list1[current1++]; else temp[current3++] = list2[current2++]; } while (current1 < list1.Length) while (current2 < list2.Length) return temp; }//merge 31

32 Merge Sort

33 Merge Two Sorted Lists

34 Heaps Heap is a useful data structure for designing efficient sorting algorithms and priority queues. A heap is a binary tree with the following properties: It is a complete binary tree. Each node is greater than or equal to any of its children. a > b a > c either (b≤ c) or (c ≤ b)

35 Complete Binary Tree A binary tree is complete if every level of the tree is full except that the last level may not be full and all the leaves on the last level are placed in left-most mode. For example, in the figure below, the binary trees in (a) and (b) are complete, but the binary trees in (c) and (d) are not complete. Further, the binary tree in (a) is a heap, but the binary tree in (b) is not a heap, because the root (39) is less than its right child (42). (a) (b) (c) (d)

36 Representing a Heap Using an Array
Node position Parent Left Child Right Child i ( i – 1) / 2 2 i + 1 2 i + 2 Example. The node for element 39 is at position 4, so its left child (element 14) is at ….. 9 (2*4+1), its right child (element 33) is at … 10 (2*4+2), and its parent (element 42) is at ……… 1 ((4-1)/2).

37 Adding Elements to a Heap
Adding 3, 5, 1, 19, 11, and 22 to a heap, initially empty

38 Adding Elements to a Heap
Add new node to the end of the heap Rebuild heap as follows Let last node be the current node; While (the current node is greater than its parent) { Swap the current node with its parent }

39 Rebuild the heap after adding a new node
Adding 88 to the heap

40 Removing the Root and Rebuild the Tree
Remove the root (maximum value in the heap) Rebuild heap as follows: Move the last node to replace the root; Let the new root be the current node; While(the current node has children and the current node is smaller than one of its children) { Swap the current node with the larger children; }

41 Removing the Root and Rebuild the Tree
Removing root 62 from the heap

42 Removing the Root and Rebuild the Tree
Move 9 to root

43 Removing the Root and Rebuild the Tree
Swap 9 with 59

44 Removing the Root and Rebuild the Tree
Swap 9 with 44 59

45 Removing the Root and Rebuild the Tree
Swap 9 with 30 59

46 The Heap Class Note: This is NOT a class in the Collection Framework.
Note: Heap is not a primitive type, you must provide your own version. The Heap Class Note: This is NOT a class in the Collection Framework. You need to implement your own (Hint: use an ArrayList to represent a complete binary tree)

47 Heap Sort /** Heap sort method */
static void Main(string[] args) { int[] data = new int[] {3, 5, 1, 19, 11, 22}; HeapSort(data); ShowList(data); } /** Heap sort method */ public static void HeapSort<E> (E[] list) where E : IComparable<E> { // Create a Heap of integers Heap<E> heap = new Heap<E>(); // Add elements to the heap for (int i = 0; i < list.Length; i++) heap.Add(list[i]); // Remove root element from the heap for (int i = list.Length - 1; i >= 0; i--) list[i] = heap.Remove(); return list; }

48 Heap Sort Time Let h denote the height for a heap of n elements.
Since a heap is a complete binary tree, the first level has 1 node, the second level has 2 nodes, the kth level has 2(k-1) nodes, the (h-1)th level has 2(h-2) nodes, and the hth level has at least one node and at most 2(h-1) nodes. Therefore, the height of a Heap is log(n). Since the add method traces a path from a leaf to a root, it takes at most h steps to add a new element to the heap. Then it takes O(nlogn) to create a heap holding n elements. Similar reasoning for the remove method, yielding O(nlogn). Therefore Heap-Sort requires O(nlogn) time.

49 Bucket Sort and Radix Sort
The lower bound for general sorting algorithms is O(nlogn). So, no sorting algorithms based on comparisons can perform better than O(nlogn). However, if the keys are small integers, you can use bucket sort without having to compare the keys. In general Bucket Sort complexity is the order of O(Kn) where K depends on the number of buckets and the size of the key

50 Radix Sort Uses 10 buckets and a list of input keys.
The ith digit of a key indicates the host bucket. Begin with least significant key digit and place list items in corresponding buckets. Remove data from buckets to reconstruct list. Repeat with next least significant digit until all key positions have been used. The cost of sorting is T(n) = kc n where k is the number of digits in the largest of the n key values, and c is the card’s processing time (read, compare, move to pocket). Therefore the complexity of Radix Sort is O(n) 50

51 Radix Sort IBM 83 Card Sorting Machine
51

52 Radix Sort Example: List = { 331, 454, 230, 034, 343, 045, 345, 059, 453, 345, 231, 009 } Bucket 1 2 3 4 5 6 7 8 9 Last radix 230 331 231 343 453 454 034 045 345 059 009 Remove 230, 331, 231, 343, 453, 454, 034, 045, 345, 059, 009 Second radix 009, 230, 331, 231, 034, 343, 045, 345, 453, 454, 059 Third radix 009, 034, 045, 059, 230, 231, 331, 343, 345, 453, 454 52

53 External Sort - Phase I Repeatedly bring data from the file to an array, sort the array using an internal sorting algorithm, and output the data from the array to a temporary file.

54 Phase II Merge a pair of sorted segments (e.g., S1 with S2, S3 with S4, ..., and so on) into a larger sorted segment and save the new segment into a new temporary file. Continue the same process until one sorted segment results.

55 Implementing Phase II Each merge step merges two sorted segments to form a new larger segment. The new segment doubles the number elements. The number of segments is reduced by half after each merge step. A segment is too large to be brought to an array in memory. To implement a merge step, Copy half number of segments from file f1.dat to a temporary file f2.dat. Then merge the first remaining segment in f1.dat with the first segment in f2.dat into a temporary file named f3.dat.

56 Implementing Phase II

57 Greatest Common Divisor - GCD
gcd(2, 3) = 1 gcd(2, 10) = 2 gcd(25, 35) = 5 gcd(14, 28) = 14 Approach 1: Brute-force, start from min(n, m) down to 1, to check if a number is common divisor for both m and n, if so, it is the greatest common divisor. Approach 2: Euclid’s algorithm Approach 3: Recursive method GCD is the largest positive integer that divides The numbers without a remainder

58 Greatest Common Divisor - GCD
Example: Find GCD for 35, 25 Approach 1: Brute-force, start from min(n, m) down to 1, to check if a number is common divisor for both m and n, if so, it is the greatest common divisor. candidateSolution Is (35 % candidateSolution==0) && (25 % candidateSolution==0) ? 25 false 24 23 22 21 . . . 5 true min(35, 25)

59 Approach 2: Euclid’s algorithm
// Get absolute value of numb1 and numb2; n1 = Math.abs(numb1); n2 = Math.abs(numb2); // r is the remainder of n1 divided by n2; r = n1 % n2; while (r != 0) { n1 = n2; n2 = r; } // When r is 0, n2 is the greatest common // divisor between n1 and n2 return n2;

60 Approach 2: Euclid’s algorithm
Example: Find GCD for 35, 25 n1 n2 r = n1 % n2 35 25 10 5 GCD(35,25) is 5

61 Approach 3: Recursive Method
numb2 if ( numb1 % numb2 = 0 ); gcd(numb1, numb2) = gcd(numb2, numb1 % numb2) otherwise; private static int gcd(int numb1, int numb2) { if (numb1 % numb2 == 0) return numb2; } else return gcd(numb2, numb1 % numb2);

62 Fractals? A fractal is a geometrical figure just like triangles, circles, and rectangles, but fractals can be divided into parts, each of which is a reduced-size copy of the whole.

63 Sierpiński Triangle It begins with an equilateral triangle, which is considered to be the Sierpinski fractal of order (or level) 0, as shown in Figure (a). Connect the MidPoints of the sides of the triangle of order 0 to create a Sierpinski triangle of order 1, as shown in Figure (b). Leave the center triangle intact. Connect the MidPoints of the sides of the three other triangles to create a Sierpinski of order 2, as shown in Figure (c). You can repeat the same process recursively to create a Sierpinski triangle of order 3, 4, ..., and so on, as shown in Figure (d).

64 Sierpinski Triangle Solution

65 Sierpinski Triangle Solution
public void DrawTriangle (int order, Point p1, ponit p2, Point p3) if (order == 0) return; else { // calculate MidPoints of triangle p1-p2-p3 p12 = MidPoint (p1, p2); p13 = MidPoint(p1, p3); p23 = MidPoint(p2, p3); // draw inner Sierpinski triangles of smaller order DrawTriangle(order-1, p1, p12, p13); DrawTriangle(order-1, p12, p2, p23); DrawTriangle(order-1, p13, p23, p3); }

66 Eight Queens

67 Eight Queens

68 Eight Queens // start by calling: search(0);
// search for a solution starting on specified row private static boolean search(int row){ if (row == SIZE) return true; for (int column=0; column < SIZE; column++){ queens[row] = column; //place a queen at row,column if (isValid(row, column) && search(row+1)){ System.out.println("Queen at: " + row + " : " + column); } return false; }//search // check if a queen can be placed at row i and column j private static boolean isValid(int row, int column){ for(int i=1; i<= row; i++){ if ( queens[row-i] == column //check column || queens[row-i] == column - i //check upleft diagonal || queens[row-i] == column + i )//check upright diagonal }//isValid


Download ppt "IST311 Recursion Sorting Searching"

Similar presentations


Ads by Google