Growth of Functions & Algorithms

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

College of Information Technology & Design
MATH 224 – Discrete Mathematics
Algorithms (continued)
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
The Fundamentals: Algorithms, the Integers & Matrices.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Chapter Complexity of Algorithms –Time Complexity –Understanding the complexity of Algorithms 1.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
1 Growth of Functions CS 202 Epp, section ??? Aaron Bloomfield.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Chapter 19: Searching and Sorting Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
1 Algorithms CS/APMA 202 Rosen section 2.1 Aaron Bloomfield.
Data Structure Introduction.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
Section 3.1. Section Summary Properties of Algorithms Algorithms for Searching and Sorting Greedy Algorithms Halting Problem.
1 Algorithms CS 202 Epp section ??? Aaron Bloomfield.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
The Fundamentals. Algorithms What is an algorithm? An algorithm is “a finite set of precise instructions for performing a computation or for solving.
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
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.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
CSE15 Discrete Mathematics 03/06/17
Chapter 9: Sorting and Searching Arrays
16 Searching and Sorting.
19 Searching and Sorting.
Applied Discrete Mathematics Week 2: Functions and Sequences
CSE15 Discrete Mathematics 03/13/17
COP 3503 FALL 2012 Shayan Javed Lecture 15
COMP108 Algorithmic Foundations Algorithm efficiency
Enough Mathematical Appetizers!
Computation.
CS 3343: Analysis of Algorithms
Algorithms Chapter 3 With Question/Answer Animations
Analysis Algorithms.
Analysis of Bubble Sort and Loop Invariant
MSIS 655 Advanced Business Applications Programming
Applied Discrete Mathematics Week 6: Computation
Sorting … and Insertion Sort.
Searching and Sorting 1-D Arrays
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Searching and Sorting Topics Sequential Search on an Unordered File
24 Searching and Sorting.
CMSC 203, Section 0401 Discrete Structures Fall 2004 Matt Gaston
Enough Mathematical Appetizers!
Enough Mathematical Appetizers!
Algorithms.
Discrete Mathematics CS 2610
Presentation transcript:

Growth of Functions & Algorithms CS 202 Epp section 9.1, 9.2

Constant Growth time k Number of inputs

Linear Growth 2n n time n/2 Number of inputs

Logarithmic Growth time Problem size

Quadratic Growth time Problem size

Exponential Growth time Problem size

What is an algorithm? An algorithm is “a finite set of precise instructions for performing a computation or for solving a problem” A program is one type of algorithm All programs are algorithms Not all algorithms are programs!

Some algorithms are harder than others Some algorithms are easy Finding the largest (or smallest) value in a list Finding a specific value in a list Determining if a number is even or odd Some algorithms are a bit harder Sorting a list Some algorithms are very hard Finding the shortest path between Miami and Seattle Some algorithms are essentially impossible Factoring large composite numbers

Algorithm 1: Maximum element Given a list, how do we find the maximum element in the list? To express the algorithm, we’ll use pseudocode

Algorithm 1: Maximum element Algorithm for finding the maximum element in a list: procedure max (a1, a2, …, an: integers) max := a1 for i := 2 to n if max < ai then max := ai {max is the largest element}

Algorithm 1: Maximum element max := a1 for i := 2 to n if max < ai then max := ai procedure max (a1, a2, …, an: integers) max := a1 for i := 2 to n if max < ai then max := ai max 4 7 9 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 4 1 7 5 2 9 3 6 8 i 9 10 8 4 2 3 5 6 7

Maximum element running time How long does this take? If the list has n elements, worst case scenario is that it takes n “steps” Here, a step is considered a single step through the list

We didn’t cover any of the rest of these slides, but they may be useful for review.

Properties of algorithms Algorithms generally share a set of properties: Input: what the algorithm takes in as input Output: what the algorithm produces as output Definiteness: the steps are defined precisely Correctness: should produce the correct output Finiteness: the steps required should be finite Effectiveness: each step must be able to be performed in a finite amount of time Generality: the algorithm should be applicable to all problems of a similar form

Searching algorithms Given a list, find a specific element in the list We will see two types Linear search a.k.a. sequential search Binary search

Algorithm 2: Linear search Given a list, find a specific element in the list List does NOT have to be sorted! procedure linear_search (x: integer; a1, a2, …, an: integers) i := 1 while ( i ≤ n and x ≠ ai ) i := i + 1 if i ≤ n then location := i else location := 0 {location is the subscript of the term that equals x, or it is 0 if x is not found}

Algorithm 2: Linear search, take 1 while ( i ≤ n and x ≠ ai ) i := i + 1 if i ≤ n then location := i else location := 0 procedure linear_search (x: integer; a1, a2, …, an: integers) i := 1 while ( i ≤ n and x ≠ ai ) i := i + 1 if i ≤ n then location := i else location := 0 x 3 location 8 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 4 1 7 5 2 9 3 6 8 i 8 1 7 3 2 4 5 6

Algorithm 2: Linear search, take 2 procedure linear_search (x: integer; a1, a2, …, an: integers) i := 1 while ( i ≤ n and x ≠ ai ) i := i + 1 if i ≤ n then location := i else location := 0 i := 1 while ( i ≤ n and x ≠ ai ) i := i + 1 if i ≤ n then location := i else location := 0 x 11 location a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 4 1 7 5 2 9 3 6 8 i 10 1 9 11 5 3 2 4 6 7 8

Linear search running time How long does this take? If the list has n elements, worst case scenario is that it takes n “steps” Here, a step is considered a single step through the list

Algorithm 3: Binary search Given a list, find a specific element in the list List MUST be sorted! Each time it iterates through, it cuts the list in half procedure binary_search (x: integer; a1, a2, …, an: increasing integers) i := 1 { i is left endpoint of search interval } j := n { j is right endpoint of search interval } while i < j begin m := (i+j)/2 { m is the point in the middle } if x > am then i := m+1 else j := m end if x = ai then location := i else location := 0 {location is the subscript of the term that equals x, or it is 0 if x is not found}

Algorithm 3: Binary search, take 1 j := n procedure binary_search (x: integer; a1, a2, …, an: increasing integers) while i < j begin m := (i+j)/2 if x > am then i := m+1 else j := m end if x = ai then location := i else location := 0 i := 1 j := n while i < j begin m := (i+j)/2 if x > am then i := m+1 else j := m end if x = ai then location := i x 14 location 7 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 2 4 6 8 10 12 14 16 18 20 i 7 6 1 m 6 5 7 8 j 10 8 7

Algorithm 3: Binary search, take 2 j := n procedure binary_search (x: integer; a1, a2, …, an: increasing integers) while i < j begin m := (i+j)/2 if x > am then i := m+1 else j := m end if x = ai then location := i else location := 0 i := 1 j := n while i < j begin m := (i+j)/2 if x > am then i := m+1 else j := m end if x = ai then location := I else location := 0 x 15 location a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 2 4 6 8 10 12 14 16 18 20 i 8 1 6 m 7 8 5 j 10 8

Binary search running time How long does this take (worst case)? If the list has 8 elements It takes 3 steps If the list has 16 elements It takes 4 steps If the list has 64 elements It takes 6 steps If the list has n elements It takes log2 n steps

Sorting algorithms Given a list, put it into some order Numerical, lexicographic, etc. We will see two types Bubble sort Insertion sort

Algorithm 4: Bubble sort One of the most simple sorting algorithms Also one of the least efficient It takes successive elements and “bubbles” them up the list procedure bubble_sort (a1, a2, …, an) for i := 1 to n-1 for j := 1 to n-i if aj > aj+1 then interchange aj and aj+1 { a1, …, an are in increasing order }

Algorithm 4: Bubble sort 1 7 5 2 9 3 6 8 Algorithm 4: Bubble sort 1 4 7 7 5 for i := 1 to n-1 for j := 1 to n-i if aj > aj+1 then interchange aj and aj+1 procedure bubble_sort (a1, a2, …, an) for i := 1 to n-1 for j := 1 to n-i if aj > aj+1 then interchange aj and aj+1 1 2 3 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 4 5 7 6 8 9 i j 10

Bubble sort running time Bubble sort algorithm: for i := 1 to n-1 for j := 1 to n-i if aj > aj+1 then interchange aj and aj+1 Outer for loop does n-1 iterations Inner for loop does n-1 iterations the first time n-2 iterations the second time … 1 iteration the last time Total: (n-1) + (n-2) + (n-3) + … + 2 + 1 = (n2-n)/2 We can say that’s “about” n2 time

Algorithm 5: Insertion sort Another simple (and inefficient) algorithm It starts with a list with one element, and inserts new elements into their proper place in the sorted part of the list procedure insertion_sort (a1, a2, …, an) for j := 2 to n begin i := 1 while aj > ai i := i +1 m := aj for k := 0 to j-i-1 aj-k := aj-k-1 ai := m end { a1, a2, …, an are sorted } take successive elements in the list find where that element should be in the sorted portion of the list move all elements in the sorted portion of the list that are greater than the current element up by one put the current element into it’s proper place in the sorted portion of the list

Insertion sort running time for j := 2 to n begin i := 1 while aj > ai i := i +1 m := aj for k := 0 to j-i-1 aj-k := aj-k-1 ai := m end { a1, a2, …, an are sorted } Outer for loop runs n-1 times In the inner for loop: Worst case is when the while keeps i at 1, and the for loop runs lots of times If i is 1, the inner for loop runs 1 time (k goes from 0 to 0) on the first iteration, 1 time on the second, up to n-2 times on the last iteration Total is 1 + 2 + … + n-2 = (n-1)(n-2)/2 We can say that’s “about” n2 time

Comparison of running times Searches Linear: n steps Binary: log2 n steps Binary search is about as fast as you can get Sorts Bubble: n2 steps Insertion: n2 steps There are other, more efficient, sorting techniques In principle, the fastest are heap sort, quick sort, and merge sort These each take take n * log2 n steps In practice, quick sort is the fastest, followed by merge sort