Algorithms Step-by-step instructions that tell a computing agent how to solve some problem using only finite resources Resources Memory CPU cycles Time/Space.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

MATH 224 – Discrete Mathematics
CSE Lecture 3 – Algorithms I
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Efficiency of Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Efficiency of Algorithms
CS107 Introduction to Computer Science
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
1 Algorithms and Analysis CS 2308 Foundations of CS II.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
BIT Presentation 4.  An algorithm is a method for solving a class of problems.  While computer scientists think a lot about algorithms, the term.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
1 Chapter 3: Efficiency of Algorithms Quality attributes for algorithms Correctness: It should do things right No flaws in design of the algorithm Maintainability.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Analysis of Algorithms
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
ALGORITHMS.
Searching Topics Sequential Search Binary Search.
Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Copyright Prentice Hall Modified by Sana odeh, NYU
Complexity Analysis (Part I)
Growth of Functions & Algorithms
Definition of Computer Science
Searching and Sorting Arrays
Applied Discrete Mathematics Week 2: Functions and Sequences
CSC 427: Data Structures and Algorithm Analysis
Week 13: Searching and Sorting
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Lecture – 2 on Data structures
Chapter 4 Divide-and-Conquer
Teach A level Computing: Algorithms and Data Structures
Enough Mathematical Appetizers!
Computation.
Quicksort 1.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Algorithms Chapter 3 With Question/Answer Animations
Algorithm design and Analysis
Analysis Algorithms.
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Algorithm Efficiency Chapter 10.
Data Structures Review Session
Comparing Algorithms Unit 1.2.
Algorithm Discovery and Design
Applied Discrete Mathematics Week 6: Computation
Searching CLRS, Sections 9.1 – 9.3.
Quicksort.
Algorithms Step-by-step instructions that tell a computing agent how to solve some problem using only finite resources Resources Memory CPU cycles Time/Space.
CSC 427: Data Structures and Algorithm Analysis
Data Structures Introduction
Enough Mathematical Appetizers!
Quicksort.
Analysis of Algorithms
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Algorithms.
Discrete Mathematics CS 2610
Complexity Analysis (Part I)
Quicksort.
Invitation to Computer Science 5th Edition
Presentation transcript:

Algorithms Step-by-step instructions that tell a computing agent how to solve some problem using only finite resources Resources Memory CPU cycles Time/Space Types of instructions Sequential Conditional If statements Iterative Loops

Pseudocode: The Interlingua for Algorithms an English-like description of the sequential, conditional, and iterative operations of an algorithm no rigid syntax. As with an essay, clarity and organization are key. So is completeness.

Pseudocode Example Find Largest Number Input: A list of positive numbers Output: The largest number in the list Procedure: 1. Set Largest to zero 2. Set Current-Number to the first in the list 3. While there are more numbers in the list 3.1 if (the Current-Number > Largest) then 3.1.1 Set Largest to the Current-Number 3.2 Set Current-Number to the next one in the list 4. Output Largest

Pseudocode Example Find Largest Number Input: A list of positive numbers Output: The largest number in the list Procedure: 1. Set Largest to zero 2. Set Current-Number to the first in the list 3. While there are more numbers in the list 3.1 if (the Current-Number > Largest) then 3.1.1 Set Largest to the Current-Number 3.2 Set Current-Number to the next one in the list 4. Output Largest conditional operation

Pseudocode Example Find Largest Number Input: A list of positive numbers Output: The largest number in the list Procedure: 1. Set Largest to zero 2. Set Current-Number to the first in the list 3. While there are more numbers in the list 3.1 if (the Current-Number > Largest) then 3.1.1 Set Largest to the Current-Number 3.2 Set Current-Number to the next one in the list 4. Output Largest iterative operation

Pseudocode Example Find Largest Number Input: A list of positive numbers Output: The largest number in the list Procedure: 1. Set Largest to zero 2. Set Current-Number to the first in the list 3. While there are more numbers in the list 3.1 if (the Current-Number > Largest) then 3.1.1 Set Largest to the Current-Number 3.2 Set Current-Number to the next one in the list 4. Output Largest Let’s “play computer” to review this algorithm…

Algorithms vary in efficiency example: sum the numbers from 1 to n 1. Set sum to 0 2. Set currNum to 1 3. Repeat until currNum > n 4. Set sum to sum + currNum 5. Set currNum to currNum + 1 Algorithm I efficiency space= 3 memory cells time = t(step1) + t(step 2) + n t(step 4) + n t(step 5) space requirement is constant (i.e. independent of n) time requirement is linear (i.e. grows linearly with n). This is written “O(n)” to see this graphically... 8

Algorithm Is time requirements size of the problem time y = mx + b time = m n + b The exact equation for the line is unknown because we lack precise values for the constants m and b. But, we can say: time is a linear function of the size of the problem time = O(n) 9

Algorithm II for summation First, consider a specific case: n = 100. The “key insight”, due to Gauss: the numbers can be grouped into 50 pairs of the form: 1 + 100 = 101 2 + 99 = 101 . . . 50 + 51 = 101 } sum = 50 x 101 This algorithm requires a single multiplication! Second, generalize the formula for any (even) n : sum = (n / 2) (n + 1) Time requirement is constant. time = O(1) 10

Sequential Search: A Commonly used Algorithm Suppose you want to a find a student in the UT directory. It contains EIDs, names, phone numbers, lots of other information. You want a computer application for searching the directory: given an EID, return the student’s phone number. You want more, too, but this is a good start…

Sequential Search of a student database . name EID major credit hrs. John Smith Paula Jones Led Belly Chuck Bin JS456 PJ123 LEB900 CB1235 physics history music math 36 125 72 89 1 2 n 3 algorithm to search database by EID : 1. ask user to enter EID to search for 2. set i to 1 3. set found to ‘no’ 4. while i <= n and found = ‘no’ do if EID = EIDi then set found to ‘yes’ else increment i by 1 7. if found = ‘no’ then print “no such student” else < student found at array index i > 12

Time requirements for sequential search amount of work best case (minimum amount of work): EID found in student1 one loop iteration worst case (maximum amount of work): EID found in studentn n loop iterations average case (expected amount of work): EID found in studentn/2 n/2 loop iterations because the amount of work is a constant multiple of n, the time requirement is O(n) in the worst case and the average case. 13

O(n) searching is too slow Consider searching UT’s student database using sequential search on a computer capable of 20,000 integer comparisons per second: n = 150,000 (students registered during past 10 years) average case 150,000 comparisons 1 seconds = 3.75 seconds 2 20,000 comparisons x worst case 150,000 comparisons x 1 seconds = 7.5 seconds 20,000 comparisons Bad news for searching NYC phone book, IRS database, ... 14

Searching an ordered list is faster: an example of binary search . name student major credit hrs. John Smith Paula Jones Led Belly Chuck Bin 24576 36794 42356 93687 physics history music math 36 125 72 89 1 2 n 3 student 38453 41200 43756 45987 47865 49277 51243 58925 59845 60011 60367 64596 86756 4 5 6 7 8 9 10 11 12 13 14 15 16 note: the student array is sorted in increasing order how would you search for 58925 ? 38453 ? 46589 ? Probe 1 Probe 3 Probe 2 15

The binary search algorithm assuming that the entries in student are sorted in increasing order, 1. ask user to input studentNum to search for 2. set found to ‘no’ 3. while not done searching and found = ‘no’ 4. set middle to the index counter at the middle of the student list 5. if studentNum = studentmiddle then set found to ‘yes’ 6. if studentNum < studentmiddle then chop off the last half of the student list 7. If studentNum > studentmiddle then chop off the first half 8. if found = ‘no’ then print “no such student” else <studentNum found at array index middle> What does this mean? How? How? 16

The binary search algorithm assuming that the entries in student are sorted in increasing order, 1. ask user to input studentNum to search for 2. set beginning to 1 3. set end to n 4. set found to ‘no’ 5. while beginning <= end and found = ‘no’ 6. set middle to (beginning + end) / 2 {round down to nearest integer} 7. if studentNum = studentmiddle then set found to ‘yes’ 8. if studentNum < studentmiddle then set end to middle - 1 9. if studentNum > studentmiddle then set beginning to middle + 1 10.if found = ‘no’ then print “no such student” else <studentNum found at array index middle> 17

Time requirements for binary search At each iteration of the loop, the algorithm cuts the list (i.e. the list called student) in half. In the worst case (i.e. when studentNum is not in the list called student) how many times will this happen? n = 16 1st iteration 16/2 = 8 2nd iteration 8/2 = 4 3rd iteration 4/2 = 2 4th iteration 2/2 = 1 the number of times a number n can be cut in half and not go below 1 is log2 n. Said another way: log2 n = m is equivalent to 2m = n In the average case and the worst case, binary search is O(log2 n) 18

This is a major improvement n sequential search binary search O(n) O(log2 n) 100 100 7 150,000 150,000 18 20,000,000 20,000,000 25 27=128 218=262,144 225 is about 33,000,000 number of comparisons needed in the worst case 150,000 comparisons x 1 second = 7.5 seconds 20,000 comparisons in terms of seconds... 18 comparisons x 1 second > .001 seconds sequential search: binary search: vs. 19

Sorting a list First, an algorithm that’s easy to write, but is badly inefficient... unsorted sorted 35467 67854 46781 13528 87341 1 2 3 4 5 initially unsorted sorted 35467 67854 46781 13528 87341 1 2 3 4 5 1st iteration unsorted sorted 35467 67854 46781 13528 87341 1 2 3 4 5 2nd iteration unsorted sorted 35467 67854 46781 13528 87341 1 2 3 4 5 etc. 3rd iteration 20

The “Simple Sort” Algorithm given a list of positive numbers, unsorted1, …, unsortedn and another list, sorted1, …, sortedn, with all values initially set to zero 1. set i to 1 2. repeat until i > n set indexForSmallest to the index of the smallest positive value in unsorted 4. set sortedi to unsortedindexForSmallest 5. set unsortedindexForSmallest to 0 6. increment i

This algorithm is expensive! Time requirement total time = n iterations x time per iteration time per iteration = time to find smallest value in a list of length n = O(n) total time = n x O(n) = O(n2) Space requirement total space = space for unsorted + space for sorted = O(2n) 22

Creating Algorithms is the Challenge of Computer Science It’s not easy; try this one: The Traveling Salesperson problem: A salesperson wants to visit 25 cities while minimizing the total number of miles driven, visiting each city exactly once, and returning home again. Which route is best?

Simplify the Problem to get an intuition about it C D four cities connected by roads Q: Starting at A, what’s the shortest route that meets the requirements? A: Obvious to anyone who looks at the entire map. Not so obvious to an algorithm that “sees”, at any one time, only one city and its roads One algorithm to answer the question: 1. generate all possible routes of length 5 2. check each path to determine whether it meets the requirement start at A, visit B, C, and D in some order, then return to A How much time does this algorithm require? 24

All Paths from A of length 5 A D A D A D A D A D A D A D A D B C B C B C B C A D A D B C A B C D Number of paths to generate and check is 24 = 16. 25

Can you Improve the Algorithm? Prune bad routes as soon as possible. What’s a “bad route?” Look for good solutions, even if they’re sub-optimal. What’s a “good solution?” Bad routes have cycles. Also a route is bad if its opening subroute is more costly than the best known complete route.

This gets real bad, real fast! In general, the algorithm’s time requirement is: (the number of roads in&out of a city) number of cities Assuming the number of roads is only 2, the time requirement is 2number of cities , given by the powers of 2 table. Assuming a computer could evaluate 10 million routes per second, finding the best route for 25 cities would require about 3.5 seconds. No problem! However, finding the best route for 64 cities would require about seconds, or hours! 20 million 5000

Comparing the time requirements work 2n n2 order 10 50 100 1,000 35 30 25 20 15 10 5 log2n .0003 .0006 .0007 .001 n .0001 .005 .01 .1 n2 .01 .25 1 1.67 min 2n .1024 3570 4x1016 forget it! years centuries n log2n time requirements for algorithms of various orders of magnitude. Time is in seconds, unless stated otherwise n 0 5 10 15 28

Conclusions Algorithms are the key to creating computing solutions. The design of an algorithm can make the difference between useful and impractical. Algorithms often have a trade-off between space (memory) and time.