College of Computer and Information Sciences Design and Analysis of Algorithms Lecturer: Dr. Bandar Alshammari https://sites.google.com/site/drbandaralshammari/ 1 Fall 2013 Course References 1. Required Text Book Computer Algorithms, Ellis Horowitz, Startaj Sahni, & Sanguthevar Rajasekaran, 1998 Computer Science U.S.A 2. Another Recommended Text Book Introduction to Algorithms, Thomas Cormen et al’s, Second Edition, 2001, MIT Printing Press Lecture Notes 3. Available @ https://sites.google.com/site/drbandaralshammari/ 4. 2 Notes taken during lectures CSC413 Fall - 2013 Dr. Bandar Alshammari Course Assessments 1. Five (5) Homeworks worth: 10% 2. Two (2) Midterms Exams: 15% each. 3. Comprehensive Final Exam: 60% 3 CSC413 Fall - 2013 Dr. Bandar Alshammari Course Motivation Need to run computer programs efficiently! INPUT Computer program: Accepts Input (Data) Performs a Sequence of action with the input COMPUTER PROGRAM Generates Output (Data) Efficient Sequence of Actions OUTPUT Algorithms 4 CSC413 Fall - 2013 Dr. Bandar Alshammari Course Contents Lecture Topic Lecture Contents Introduction What is an algorithm? & algorithm specification Performance Analysis Space Complexity, Time Complexity and Asymptotic Notations Trees, Queues, Stack and Heaps Data Structure Searching and Sorting Optimization in Algorithms design 5 CSC413 Binary & Linear Searches, Selection, Insertion, Quick and Merge Sorts Graphs & Greedy Algorithms. Fall - 2013 Dr. Bandar Alshammari Week Date Hijri Date Lecture 3 9/11/1434 15/09/2013 Lecture 1 & 2 4 16/11/1434 22/09/2013 Lecture 3 & 4 5 23/11/1434 29/09/2013 Lecture 5 6 1/12/1434 6/10/2013 Lecture 6 Assessment Due Homework 1 Mid Semester Break 6 7 22/12/1434 27/10/2013 Lecture 7 + Revision Homework 2 8 29/12/1434 3/11/2013 Midterm Exam1 Lectures 1 - 6 9 6/1/1435 10/11/2013 Lecture 8 & 9 10 13/1/1435 17/11/2013 Lecture 10 11 20/1/1435 24/11/2013 Lecture 11 12 27/1/1435 1/12/2013 Lecture 12 + Revision Homework 4 13 5/2/1435 8/12/2013 Midterm Exam 2 Lectures 7 - 12 14 12/2/1435 15/12/2013 Lecture 13 15 19/2/1435 22/12/2013 Revision 16 26/2/1435 29/12/2013 Revision CSC413 Fall - 2013 Homework 3 Homework 5 Dr. Bandar Alshammari Lecture (2) Algorithms Basic Concepts Introduction II. Algorithm specification I. 7 A. Pseudocode Conventions B. Recursive Algorithm CSC413 Fall - 2013 Dr. Bandar Alshammari 1.1 Introduction An algorithm, named after the ninth century scholar Abu Jafar Muhammad Ibn Musu AlKhowarizmi, An algorithm is “a finite set of instructions that accomplishes a particular task”. Of course, there can be many algorithms for a single problem, i.e., A single problem can be solved by many techniques e.g., iteration and/or 8 recursion. CSC413 Fall - 2013 Dr. Bandar Alshammari 1.1 Introduction All algorithms must satisfy the following criteria: input: zero or more quantities that are externally supplied output: at least one quantity is produced definiteness: clear and unambiguous finiteness: terminate after a finite number of steps effectiveness: instruction is basic enough to be carried out A Computer Program is “a clearly specified series of computer instructions implementing the algorithm”. A program does not have to satisfy the finiteness criteria. 9 CSC413 Fall - 2013 Dr. Bandar Alshammari 1.1 Introduction (More Definitions) Algorithm validation: Is the process which shows that a certain algorithm can compute the correct answer for all possible legal inputs. Analysis of Algorithms (performance Analysis): Is the task of determining how much computing time and storage an algorithm 10 requires. CSC413 Fall - 2013 Dr. Bandar Alshammari 1.2 Algorithm Specification Representation A natural language, like English or Chinese. A graphic, like flowcharts. A computer language, like C and JAVA. 11 CSC413 Fall - 2013 Dr. Bandar Alshammari 1.2.1 Pseudocode (Assignment) 12 CSC413 Fall - 2013 Dr. Bandar Alshammari 1.2.1 Pseudocode (Conditional) Solution 13 CSC413 Fall - 2013 Dr. Bandar Alshammari 1.2.1 Pseudocode (Loops) Solution 14 CSC413 Fall - 2013 Dr. Bandar Alshammari 1.2.1 Pseudocode (Selection Sort) Example 1.1 [Selection sort]: From those integers that are currently unsorted, find the smallest and place it next in the sorted list. i 0 1 2 3 15 CSC413 [0] 30 10 10 10 10 [1] 10 30 20 20 20 [2] 50 50 50 30 30 Fall - 2013 [3] 40 40 40 40 40 [4] 20 20 30 50 50 Dr. Bandar Alshammari 1.2.1 Java Code(Selection Sort) Think about sorting an array in a decreasing order (i.e., 10,9,8,.....,2,1,0)???? 16 CSC413 Fall - 2013 Dr. Bandar Alshammari 1.2.2 Recursion A Recursive function executes its code and then returns control to the calling function. Similarly, an algorithm is said to be recursive, if the same algorithm is invoked in the body. 17 CSC413 Fall - 2013 Dr. Bandar Alshammari 1.2.2 Recursion This perspective ignores the fact that functions can call themselves (direct recursion). They may call other functions that invoke the calling function again (indirect recursion). extremely powerful frequently allow us to express an otherwise complex process in very clear term We should express a recursive algorithm when the problem itself is defined recursively. 18 CSC413 Fall - 2013 Dr. Bandar Alshammari 1.2.2 Recursion Example Consider the factorial function, N!, defined as the product of all integers between 1 and N. 3! = 3 × 2 × 1 = 6 N! = N × (N − 1) × (N − 2) × . . . × 1 • What is the base case? Look for the case that’s not defined in terms of N: e.g. 1! = 1 or 0! = 1. • What is the recursive case? Define the problem in terms of itself, while reducing the problem size (i.e., reduce N! to (N − 1)!) • The factorial function can be expressed recursively as: 1! = 1 (base case) N! = N × (N − 1)! for N > 1 (recursive case) • 19 The base case is 1!. All other values of N! are defined recursively as N × (N − 1)! CSC413 Fall - 2013 Dr. Bandar Alshammari 1.2.2 Recursion Example Given here in pseudocode: Algorithm factorial(n) { if (n<=1) then return 1; else return n * factorial(n-1); } Eventually the base case will be reached: 4! = 4 × 3! = 4 × 3 × 2! = 4 × 3 × 2 × 1! (base case reached) = 4 × 3 × 2 × 1 (recursion is terminated) 20 CSC413 Fall - 2013 Dr. Bandar Alshammari Tower of Hanoi The Towers of Hanoi puzzle was invented in the 1880s by Douard Lucas, a French mathematician. The puzzle consists of three poles on which a set of disks, each with a different diameter, may be placed. Initially the disks are stacked on the leftmost pole, in order of size, with the largest disk on the bottom. 21 CSC413 Fall - 2013 Dr. Bandar Alshammari Rules for Tower of Hanoi The goal of the puzzle is to move all the disks from the leftmost pole to the rightmost pole, adhering to the following rules: 1. Move only one disk at a time. 2. A larger disk may not be placed on top of a smaller disk. 3. All disks, except the one being moved, must be on a pole. 22 CSC413 Fall - 2013 Dr. Bandar Alshammari Three-disk solution to the Tower of Hanoi puzzle 23 CSC413 Fall - 2013 Dr. Bandar Alshammari Tower of Hanoi Solution (Code) // Move the top n disks from tower A to tower C Algorithm TowerOfHanoi(n,a,b,c) { if (n>=1) then { TowerOfHanoi(n-1,a,c,b); Write(“move top disk from tower”, a,”to top of tower”, c); TowerOfHanoi(n-1,b,a,c); } } 24 CSC413 Fall - 2013 Dr. Bandar Alshammari Tower of Hanoi Solution Property Takes 2N- 1 steps to solve N disc problem. • For example for 3 discs = 23- 1 = 7 moves. • Another example for 4 discs = 24- 1 = 15 moves • This is an example of exponential complexity. • As the number of disks increases, the number of required moves increases exponentially. 25 CSC413 Fall - 2013 Dr. Bandar Alshammari