Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c)2001-2004 Michael P. Frank Modified by (c) 2004-2005 Haluk Bingöl 1/18 Module.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

College of Information Technology & Design
MATH 224 – Discrete Mathematics
8 Algorithms Foundations of Computer Science ã Cengage Learning.
CompSci 102 Discrete Math for Computer Science
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Introduction to Analysis of Algorithms
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Program Design and Development
Chapter 2: Algorithm Discovery and Design
The Fundamentals: Algorithms, the Integers & Matrices.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
High-Level Programming Languages: C++
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Invitation to Computer Science, Java Version, Second Edition.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
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.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/21 Module.
Discrete Mathematics 1 Kemal Akkaya DISCRETE MATHEMATICS Lecture 10 Dr. Kemal Akkaya Department of Computer Science.
CS 103 Discrete Structures Lecture 12
Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/18 Module.
Dividing Decimals # ÷ 3.5 next Taking the Fear out of Math
Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/18 Module.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
The Fundamentals. Algorithms What is an algorithm? An algorithm is “a finite set of precise instructions for performing a computation or for solving.
4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections Reading: Sections.
ALGORITHMS.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Module #5 - Algorithms 1 Module #5: Algorithms Rosen 5 th ed., §2.1 ~31 slides, ~1 lecture.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
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.
Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/18 Module.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Searching and Sorting Searching algorithms with simple arrays
CSE15 Discrete Mathematics 03/06/17
Applied Discrete Mathematics Week 2: Functions and Sequences
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
GC211Data Structure Lecture2 Sara Alhajjam.
COT 3100, Spring 2001 Applications of Discrete Structures
Today’s topics Algorithms Reading: Sections Upcoming
Enough Mathematical Appetizers!
Algorithms Chapter 3 With Question/Answer Animations
Algorithm design and Analysis
Rosen 5th ed., §2.1 ~31 slides, ~1 lecture
Applied Discrete Mathematics Week 6: Computation
Discrete Mathematics and its Applications
Rosen 5th ed., §2.1 ~31 slides, ~1 lecture
Discrete Mathematics and its Applications
Discrete Mathematics and its Applications
Discrete Mathematics CS 2610
Presentation transcript:

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/18 Module #5 - Algorithms Bogazici University Department of Computer Engineering CmpE 220 Discrete Mathematics 05. Algorithms Haluk Bingöl

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 2/18 Module #5 - Algorithms Module #5: Algorithms Rosen 5 th ed., §2.1 ~31 slides, ~1 lecture Abu al-Khowarizmi (ca )

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 3/38 Module #5 - Algorithms Algorithms Abu al-Khowarizmi (ca )

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 4/38 Module #5 - Algorithms Chapter 2: More Fundamentals §2.1: Algorithms Formal procedures §2.2: Orders of Growth §2.3: Complexity of algorithms Analysis using order-of-growth notation. §2.4: The Integers & Division Some basic number theory. §2.5: Integers & Algorithms Alternate bases, algorithms for basic arithmetic §2.6: Number theory applications Public-Key Cryptography §2.7: Matrices Some basic linear algebra.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 5/38 Module #5 - Algorithms §2.1: Algorithms The foundation of computer programming. Most generally, an algorithm just means a definite procedure for performing some sort of task. A computer program is simply a description of an algorithm, in a language precise enough for a computer to understand, requiring only operations that the computer already knows how to do. We say that a program implements (or “is an implementation of”) its algorithm.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 6/38 Module #5 - Algorithms Algorithms You Already Know Grade-school arithmetic algorithms: How to add any two natural numbers written in decimal on paper, using carries. Similar: Subtraction using borrowing. Multiplication & long division. Your favorite cooking recipe. How to register for classes at BU.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 7/38 Module #5 - Algorithms Programming Languages Some common programming languages: Newer: Java, C, C++, C#, Visual Basic, JavaScript, Perl, Tcl, Pascal, many others… Older: Fortran, Cobol, Lisp, Basic Assembly languages, for low-level coding. In this class we will use an informal, Pascal-like “pseudo-code” language. You should know at least 1 real language!

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 8/38 Module #5 - Algorithms Algorithm Example (English) Task: Given a sequence {a i }=a 1,…,a n, a i  ℕ, say what its largest element is. One algorithm for doing this, in English: –Set the value of a temporary variable v (largest element seen so far) to a 1 ’s value. –Look at the next element a i in the sequence. –If a i >v, then re-assign v to the number a i. –Repeat then previous 2 steps until there are no more elements in the sequence, & return v.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 9/38 Module #5 - Algorithms Executing an Algorithm When you start up a piece of software, we say the program or its algorithm are being run or executed by the computer. Given a description of an algorithm, you can also execute it by hand, by working through all of its steps with pencil & paper. Before ~1940, “computer” meant a person whose job was to execute algorithms!

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 10/38 Module #5 - Algorithms Executing the Max algorithm Let {a i }=7,12,3,15,8. Find its maximum…Let {a i }=7,12,3,15,8. Find its maximum… Set v = a 1 = 7.Set v = a 1 = 7. Look at next element: a 2 = 12.Look at next element: a 2 = 12. Is a 2 >v? Yes, so change v to 12.Is a 2 >v? Yes, so change v to 12. Look at next element: a 2 = 3.Look at next element: a 2 = 3. Is 3>12? No, leave v alone….Is 3>12? No, leave v alone…. Is 15>12? Yes, v=15…Is 15>12? Yes, v=15…

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 11/38 Module #5 - Algorithms Algorithm Characteristics Some important general features of algorithms: Input. Information or data that comes in. Output. Information or data that goes out. Definiteness. Algorithm is precisely defined. Correctness. Outputs correctly relate to inputs. Finiteness. Won’t take forever to describe or run. Effectiveness. Individual steps are all do-able. Generality. Works for many possible inputs. Efficiency. Takes little time & memory to run.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 12/38 Module #5 - Algorithms Our Pseudocode Language: §A2 procedure name(argument: type) variable := expression informal statement begin statements end {comment} if condition then statement [else statement] for variable := initial value to final value statement while condition statement procname(arguments) Not defined in book: Not defined in book: return expression

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 13/38 Module #5 - Algorithms procedure procname(arg: type) Declares that the following text defines a procedure named procname that takes inputs (arguments) named arg which are data objects of the type type. Example: procedure maximum(L: list of integers) [statements defining maximum…]

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 14/38 Module #5 - Algorithms variable := expression An assignment statement evaluates the expression expression, then reassigns the variable variable to the value that results. Example assignment statement: v := 3x+7 (If x is 2, changes v to 13.) In pseudocode (but not real code), the expression might be informally stated: x := the largest integer in the list L

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 15/38 Module #5 - Algorithms Informal statement Sometimes we may write a statement as an informal English imperative, if the meaning is still clear and precise: e.g.,“swap x and y” Keep in mind that real programming languages never allow this. When we ask for an algorithm to do so-and-so, writing “Do so-and-so” isn’t enough! Break down algorithm into detailed steps.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 16/38 Module #5 - Algorithms begin statements end Groups a sequence of statements together: begin statement 1 statement 2 … statement n end Allows the sequence to be used just like a single statement. Might be used: After a procedure declaration. In an if statement after then or else. In the body of a for or while loop. Curly braces {} are used instead in many languages.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 17/38 Module #5 - Algorithms {comment} Not executed (does nothing). Natural-language text explaining some aspect of the procedure to human readers. Also called a remark in some real programming languages, e.g. BASIC. Example, might appear in a max program: {Note that v is the largest integer seen so far.}

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 18/38 Module #5 - Algorithms if condition then statement Evaluate the propositional expression condition. If the resulting truth value is True, then execute the statement statement; otherwise, just skip on ahead to the next statement after the if statement. Variant: if cond then stmt1 else stmt2 Like before, but iff truth value is False, executes stmt2.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 19/38 Module #5 - Algorithms while condition statement Evaluate the propositional (Boolean) expression condition. If the resulting value is True, then execute statement. Continue repeating the above two actions over and over until finally the condition evaluates to False; then proceed to the next statement.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 20/38 Module #5 - Algorithms while condition statement Also equivalent to infinite nested ifs, like so: if condition begin statement if condition begin statement …(continue infinite nested if’s) end end

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 21/38 Module #5 - Algorithms for var := initial to final stmt Initial is an integer expression. Final is another integer expression. Semantics: Repeatedly execute stmt, first with variable var := initial, then with var := initial+1, then with var := initial+2, etc., then finally with var := final. Question: What happens if stmt changes the value of var, or the value that initial or final evaluates to?

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 22/38 Module #5 - Algorithms for var := initial to final stmt For can be exactly defined in terms of while, like so: begin var := initial while var  final begin stmt var := var + 1 end end

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 23/38 Module #5 - Algorithms procedure(argument) A procedure call statement invokes the named procedure, giving it as its input the value of the argument expression. Various real programming languages refer to procedures as functions (since the procedure call notation works similarly to function application f(x)), or as subroutines, subprograms, or methods.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 24/38 Module #5 - Algorithms Max procedure in pseudocode procedure max(a 1, a 2, …, a n : integers) v := a 1 {largest element so far} v := a 1 {largest element so far} for i := 2 to n {go thru rest of elems} for i := 2 to n {go thru rest of elems} if a i > v then v := a i {found bigger?} if a i > v then v := a i {found bigger?} {at this point v’s value is the same as the largest integer in the list} {at this point v’s value is the same as the largest integer in the list} return v return v

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 25/38 Module #5 - Algorithms Inventing an Algorithm Requires a lot of creativity and intuition Like writing proofs. Unfortunately, we can’t give you an algorithm for inventing algorithms. Just look at lots of examples… And practice (preferably, on a computer) And look at more examples… And practice some more… etc., etc.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 26/38 Module #5 - Algorithms Algorithm-Inventing Example Suppose we ask you to write an algorithm to compute the predicate: IsPrime:N → {T,F} Computes whether a given natural number is a prime number. First, start with a correct predicate-logic definition of the desired function:  n: IsPrime(n)  ¬  1<d<n: d|n Means d divides n evenly (without remainder)

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 27/38 Module #5 - Algorithms IsPrime example, cont. Notice that the negated exponential can be rewritten as a universal: ¬  1<d<n: d|n   1<d<n: d | n   2≤ d ≤ n−1: d | n This universal can then be translated directly into a corresponding for loop: for d := 2 to n−1 { Try all potential divisors >1 & 1 & <n } if d|n then return F { n has divisor d; not prime } return T { no divisors were found; n must be prime} Means d does not divide n evenly (the remainder is ≠0)

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 28/38 Module #5 - Algorithms Optimizing IsPrime The IsPrime algorithm can be further optimized: for d := 2 to  n 1/2  if d|n then return F return T This works because of this theorem: If n has any (integer) divisors, it must have one less than n 1/2. Proof: Suppose n’s smallest divisor >1 is a, and let b : ≡ n/a. Then n = ab, but if a > n 1/2 then b > n 1/2 (since a is n’s smallest divisor) and so n = ab > (n 1/2 ) 2 = n, an absurdity. Further optimizations are possible: - E.g., only try divisors that are primes less than n 1/2. Note smaller range of search.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 29/38 Module #5 - Algorithms Another example task Problem of searching an ordered list. Given a list L of n elements that are sorted into a definite order (e.g., numeric, alphabetical), And given a particular element x, Determine whether x appears in the list, and if so, return its index (position) in the list. Problem occurs often in many contexts. Let’s find an efficient algorithm!

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 30/38 Module #5 - Algorithms Search alg. #1: Linear Search procedure linear search (x: integer, a 1, a 2, …, a n : distinct integers) i := 1 {start at beginning of list} while (i  n  x  a i ) {not done, not found} i := i + 1 {go to the next position} if i  n then location := i {it was found} else location := 0 {it wasn’t found} return location {index or 0 if not found}

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 31/38 Module #5 - Algorithms Search alg. #2: Binary Search Basic idea: On each step, look at the middle element of the remaining list to eliminate half of it, and quickly zero in on the desired element. <x<x>x>x<x<x<x<x

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 32/38 Module #5 - Algorithms Search alg. #2: Binary Search procedure binary search (x:integer, a 1, a 2, …, a n : distinct integers) i := 1 {left endpoint of search interval} j := n {right endpoint of search interval} while i 1 item} m :=  (i+j)/2  {midpoint} if x>a m then i := m+1 else j := m end if x = a i then location := i else location := 0 return location

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 33/38 Module #5 - Algorithms Practice exercises 2.1.3: Devise an algorithm that finds the sum of all the integers in a list. [2 min] procedure sum(a 1, a 2, …, a n : integers) s := 0 {sum of elems so far} for i := 1 to n {go thru all elems} s := s + a i {add current item} {at this point s is the sum of all items} return s

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 34/38 Module #5 - Algorithms Sorting Algorithms Sorting is a common operation in many applications. E.g. spreadsheets and databases It is also widely used as a subroutine in other data-processing algorithms. Two sorting algorithms shown in textbook: Bubble sort Insertion sort However, these are not very efficient, and you should not use them on large data sets! We’ll see some more efficient algorithms later in the course.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 35/38 Module #5 - Algorithms Smallest elements “float” up to the top of the list, like bubbles in a container of liquid. Bubble Sort

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 36/38 Module #5 - Algorithms Insertion Sort Algorithm English description of algorithm: For each item in the input list, “Insert” it into the correct place in the sorted output list generated so far. Like so: Use linear or binary search to find the location where the new item should be inserted. Then, shift the items from that position onwards down by one position. Put the new item in the hole remaining.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 37/38 Module #5 - Algorithms Review §2.1: Algorithms Characteristics of algorithms. Pseudocode. Examples: Max algorithm, primality-testing, linear search & binary search algorithms. Sorting. Intuitively we see that binary search is much faster than linear search, but how do we analyze the efficiency of algorithms formally? Use methods of algorithmic complexity, which utilize the order-of-growth concepts from §1.8.

Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 38/38 Module #5 - Algorithms References Rosen Discrete Mathematics and its Applications, 5e Mc GrawHill, 2003Rosen Discrete Mathematics and its Applications, 5e Mc GrawHill, 2003