Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

MATH 224 – Discrete Mathematics
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
CompSci 102 Discrete Math for Computer Science
CSE115/ENGR160 Discrete Mathematics 02/28/12
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
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.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 03/10/11
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching and Sorting Arrays
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
The Fundamentals: Algorithms, the Integers & Matrices.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
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.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Sorting HKOI Training Team (Advanced)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Jessie Zhao Course page: 1.
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
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.
Example { 1 – –1} Use Bubble Sort (sort in increasing order} After first pass { –1 50} After Second Pass { } After.
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.
CS 103 Discrete Structures Lecture 12
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
ALGORITHMS.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Copyright © Curt Hill Sorting Ordering an array.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of 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.
Chapter 3 With Question/Answer Animations 1. Chapter Summary Algorithm and Growth of Function (This slide) Algorithms - Sec 3.1 – Lecture 13 Example Algorithms.
CSE15 Discrete Mathematics 03/06/17
Growth of Functions & Algorithms
Applied Discrete Mathematics Week 2: Functions and Sequences
GC211Data Structure Lecture2 Sara Alhajjam.
Enough Mathematical Appetizers!
Computation.
Algorithms Chapter 3 With Question/Answer Animations
Algorithm design and Analysis
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.
Applied Discrete Mathematics Week 6: Computation
Sorting … and Insertion Sort.
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
A Robust Data Structure
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
CSCE 222 Discrete Structures for Computing
Algorithms.
Discrete Mathematics CS 2610
Presentation transcript:

Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective

Copyright © Curt Hill What is an algorithm? Algorithm: A prescribed set of well defined rules and processes for solving a problem in a finite number of steps Prescribed: known in advance Well defined: unambiguous, clear Finite: always ends Not every program implements an algorithm, but most do

Properties An algorithm should demonstrate several properties: Input – they take some type of input data Output – they produce some answer Definiteness – each step is unambiguously defined Correctness – a correct algorithm produces correct results for each set of input values Copyright © 2014 Curt Hill

More Properties Finiteness – always finishes in a finite number of steps –Finite may be large –Recall the halting problem Effectiveness - it must be possible to perform each step exactly in a finite amount of time Generality – it should solve the problem for any set of inputs, not just a particular set –We are allowed to limit input domains Copyright © 2014 Curt Hill

Expression How do we represent an algorithm? Almost every program implements an algorithm –We do not necessarily want to learn each programming language Choose a standard language –ACM chose ALGOL as their standard –Before that FORTRAN was common Other possibilities exist Copyright © 2014 Curt Hill

Pseudo Code The book, among others, has chosen pseudo code This has greater precision than natural language yet we do not worry about the syntactical details of a real programming language The book’s choice looks much like Pascal, which is also a descendent of ALGOL Copyright © 2014 Curt Hill

Rules(?) of Pseudo code –No need to declare variables, except in procedure headers –No need for ; to terminate or separate a statement –Usually one statement per line –Indentation is significant –Assignment statement uses := –Reserved words are bold –An if statement has a then or then and else –Simplified for and while syntax –Comments in braces Copyright © 2014 Curt Hill

Example Find maximum element in sequence: procedure max(a 1, a 2, …a n : integer) max := a 1 {Assignment on name} for i=2 to n if max < a i then max := a i return max {All done} Copyright © 2014 Curt Hill

Searching Something we do a lot of in Computer Science We have a collection of items We want to find whether a given one exists in the collection Sometimes we search for a key and want the associated data –Account number and account information Other times just to see if the value exists – such as set membership Copyright © 2014 Curt Hill

Two Searches A linear search looks sequentially through a sequence of values –The search length is proportional to the number of items A binary search looks through a sorted sequence of values –This works much faster Copyright © 2014 Curt Hill

Linear Copyright © 2014 Curt Hill procedure linear search(x:integer, a 1, a 2, …,a n : distinct integers) i := 1 while (i ≤ n and x ≠ a i ) i := i + 1 if i ≤ n then location := i else location := 0 return location{location is the subscript of the term that equals x, or is 0 if x is not found}

Binary Copyright © 2014 Curt Hill procedure binary search(x: integer, a 1,a 2,…, a n : increasing integers) i := 1 {i is the left endpoint of interval} j := n {j is right endpoint of interval} while i < j m := ⌊ (i + j)/2 ⌋ if x > a m then i := m + 1 else j := m if x = a i then location := i else location := 0 return location{location is the subscript i of the term a i equal to x, or 0 if x is not found}

Sorting Another extremely important operation Put a sequence into ascending or descending order Look at two here Bubble Insertion Copyright © 2014 Curt Hill

Copyright © Curt Hill Bubble Sort Basic idea Start at top Compare adjacent elements Exchange if out of order Repeat until a pass has no exchanges See the text for pseudo code

Copyright © Curt Hill First Pass Small items bubble up slowly –One element per pass Large items sink quickly –Keep descending until they find a larger item or hit bottom

Commentary The book uses two for loops This is not the best approach However, the bubble sort is perhaps the worst sort in existence The only thing it should be ever considered for is a sequence where there are only slight disorderings Copyright © 2014 Curt Hill

Copyright © Curt Hill Insertion Sort Partition the array into two pieces The first one and all the rest The first part of the array is already sorted Remove the first unsorted item Insert into the correct location of the sorted part

Copyright © Curt Hill How it works Sorted part of array Unsorted part of array Remove Insert

Greedy Algorithms There are a number of situations where we want to find an optimal solution –Usually a maximum of minimum There are typically several parameters, that is values that can be changed Exhaustively searching every combination of every parameter is typically prohibitive Copyright © 2014 Curt Hill

How do they work? Start at some point –All parameters to function have an arbitrary or given value Look one step in every direction –Vary each parameter by a small increment Choose the one that gives the best results and return to the first step with new values Quit when no more improvement may be made Copyright © 2014 Curt Hill

Discussion There must be a readily available function that produces a measure of the goodness of the solution The results may be optimal or not depending on the type of problem As the algorithm homes in on the problem the step size may be adjusted Next we will consider the change problem giving the fewest coins Copyright © 2014 Curt Hill

Change Suppose we have to give change The amount required is in the range 1 to 99 cents We wish to give the fewest coins We have the normal coins: quarters, dimes, nickels and pennies Our function takes the count of each coin and the original change amount and returns how much change is left to be given Copyright © 2014 Curt Hill

Greed is good The starting point is no coins are given – fours counts of zero We next look at four steps: incrementing each coin We choose the one that gives us the smallest value that not negative This is our best step – next go to the beginning with adjusted parameters Copyright © 2014 Curt Hill

Example Suppose that the amount is 78 cents We try one of each coin and find that the quarter gets us to 53 We try one of each again and find that the quarter gets us to 28 We try one of each again and find that the quarter gets us to 3 Now the quarter, dime and nicket sends us negative, but 1 penny gets us to 2, which we repeat twice more Copyright © 2014 Curt Hill

Commentary This is a simple with an easy solution A similar program was shown in 127/160 that solves this optimally but is not a classis greedy However, greedy solutions work in much more complicated environments where the correct path is not know in advance The book also observes that if nickels are not available this algorithm will not necessarily be optimal Copyright © 2014 Curt Hill

The Halting Problem This is important because of the finite number of steps requirement of an algorithm That it is unsolvable shows absolute limitations to static analysis –What can be seen without running a program –It also proves the existence of unsolvable problems We saw the proof in chapter 1 presentations Copyright © 2014 Curt Hill

Exercises 3, 7, 19, 35, 53, 55 Copyright © 2014 Curt Hill