Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.

Slides:



Advertisements
Similar presentations
Algorithms.
Advertisements

MATH 224 – Discrete Mathematics
The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 5 Algorithms. 2 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Chapter 2: Algorithm Discovery and Design
Chapter 2 The Algorithmic Foundations of Computer Science
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Chapter 2: Algorithm Discovery and Design
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Chapter 2: Algorithm Discovery and Design
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Chapter 2: Algorithm Discovery and Design
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Eleventh Edition by J.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Copyright © 2015 Pearson Education, Inc. Chapter 5: Algorithms.
Introduction to Programming. What is a Program  Set of Instructions that tells the computer what to Do.  Driving force behind the computer  Without.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science 6th Edition
Invitation to Computer Science, Java Version, Second Edition.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Chapter 12 Recursion, Complexity, and Searching and Sorting
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
“The study of algorithms is the cornerstone of computer science.” Algorithms Fall 2011.
Chapter 5 Algorithms Introduction to computer, 2nd semester, 2010/2011 Mr.Nael Aburas Faculty of Information Technology.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Algorithm: definition An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.
Chapter 5 Algorithms. © 2005 Pearson Addison-Wesley. All rights reserved 5-2 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation.
Chapter 5: 演算法 Algorithms 陳以德助理教授 : 高醫大舊二棟 轉 2586
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.
Computer Science: An Overview Eleventh Edition
ALGORITHMS.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Algorithms
Algorithms and Problem Solving
Q & A.
Computer Science: An Overview Eleventh Edition
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Chapter 5: Algorithms 5.1 The Concept of an Algorithm
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.
Chapter 5: Algorithms Computer Science: An Overview Tenth Edition
Computer Science — An Overview J. Glenn Brookshear
Algorithm Efficiency and Sorting
Algorithm Discovery and Design
Algorithms and Problem Solving
Introduction to Algorithms
The structure of programming
Thinking procedurally
Computer Science: An Overview Tenth Edition
Presentation transcript:

Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved

© 2007 Pearson Addison-Wesley. All rights reserved 0-2 The central role of algorithms in computer science: examples

© 2007 Pearson Addison-Wesley. All rights reserved 0-3 Layering: physical communication application transport network link application transport network link application transport network link application transport network link network link data

© 2007 Pearson Addison-Wesley. All rights reserved 0-4 The central role of algorithms in computer science

© 2007 Pearson Addison-Wesley. All rights reserved 0-5 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures 5.5 Recursive Structures 5.6 Efficiency and Correctness

© 2007 Pearson Addison-Wesley. All rights reserved 0-6 Definition of Algorithm An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

© 2007 Pearson Addison-Wesley. All rights reserved 0-7 In-Class Exercise In what sense do the steps described by the following list of instructions fail to constitute an algorithm? –Step1. Take a coin out of your pocket and put it on the table; –Step2. Return to step 1 Return to step 1 Return to step 1 if there is a coin in the pocket.

© 2007 Pearson Addison-Wesley. All rights reserved 0-8 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures 5.5 Recursive Structures 5.6 Efficiency and Correctness

© 2007 Pearson Addison-Wesley. All rights reserved 0-9 Figure 5.2 Folding a bird from a square piece of paper

© 2007 Pearson Addison-Wesley. All rights reserved 0-10 Pseudocode Primitives Assignment name  expression Conditional selection if condition then action Repeated execution while condition do activity »repeat activity until condition Procedure procedure name (generic names)

© 2007 Pearson Addison-Wesley. All rights reserved 0-11 Figure 5.3 Origami primitives

© 2007 Pearson Addison-Wesley. All rights reserved 0-12 Figure 5.4 The procedure Greetings in pseudocode

© 2007 Pearson Addison-Wesley. All rights reserved 0-13 In-class Exercise 1. Does the following program represent an algorithm in the strict sense? Why or why not? Count  0; While (Count not 5) do (Count  Count + 2)

© 2007 Pearson Addison-Wesley. All rights reserved 0-14 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures 5.5 Recursive Structures 5.6 Efficiency and Correctness

© 2007 Pearson Addison-Wesley. All rights reserved 0-15 Polya’s Problem Solving Steps 1. Understand the problem. 2. Devise a plan for solving the problem. 3. Carry out the plan. 4. Evaluate the solution for accuracy and its potential as a tool for solving other problems.

© 2007 Pearson Addison-Wesley. All rights reserved 0-16 Getting a Foot in the Door Try working the problem backwards Solve an easier related problem –Relax some of the problem constraints –Solve pieces of the problem first (bottom up methodology) Stepwise refinement: Divide the problem into smaller problems (top-down methodology)

© 2007 Pearson Addison-Wesley. All rights reserved 0-17 Ages of Children Problem Person A is charged with the task of determining the ages of B’s three children. –B tells A that the product of the children’s ages is 36. –A replies that another clue is required. –B tells A the sum of the children’s ages. –A replies that another clue is needed. –B tells A that the oldest child plays the piano. –A tells B the ages of the three children. How old are the three children?

© 2007 Pearson Addison-Wesley. All rights reserved 0-18 Figure 5.5

© 2007 Pearson Addison-Wesley. All rights reserved 0-19 Exercise Homework 1, 2, 4 in Page 213

© 2007 Pearson Addison-Wesley. All rights reserved 0-20 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures 5.5 Recursive Structures 5.6 Efficiency and Correctness

© 2007 Pearson Addison-Wesley. All rights reserved 0-21 Iterative Structures Pretest loop: while (condition) do (loop body) Posttest loop: repeat (loop body) until(condition)

© 2007 Pearson Addison-Wesley. All rights reserved 0-22 Figure 5.6 The sequential search algorithm in pseudocode

© 2007 Pearson Addison-Wesley. All rights reserved 0-23 Figure 5.7 Components of repetitive control

© 2007 Pearson Addison-Wesley. All rights reserved 0-24 Figure 5.8 The while loop structure

© 2007 Pearson Addison-Wesley. All rights reserved 0-25 Figure 5.9 The repeat loop structure

© 2007 Pearson Addison-Wesley. All rights reserved 0-26 In-class Exercise Convert the pseudocode routin Z Z 0 X X 1 while (X<6) do (Z  Z+X; X X X+1) to an equivalent routine using a repeat statement. answer: Z Z 0 X X 1 repeat (Z  Z+X; X X X+1) until (X=6) (Extra Point Question) Page Selection sort

© 2007 Pearson Addison-Wesley. All rights reserved 0-27 Figure 5.10 Sorting the list Fred, Alex, Diana, Byron, and Carol alphabetically

© 2007 Pearson Addison-Wesley. All rights reserved 0-28 Figure 5.11 The insertion sort algorithm expressed in pseudocode

© 2007 Pearson Addison-Wesley. All rights reserved 0-29 Recursion The execution of a procedure leads to another execution of the procedure. Multiple activations of the procedure are formed, all but one of which are waiting for other activations to complete.

© 2007 Pearson Addison-Wesley. All rights reserved 0-30 Figure 5.12 Applying our strategy to search a list for the entry John

© 2007 Pearson Addison-Wesley. All rights reserved 0-31 Figure 5.13 A first draft of the binary search technique

© 2007 Pearson Addison-Wesley. All rights reserved 0-32 Figure 5.14 The binary search algorithm in pseudocode

© 2007 Pearson Addison-Wesley. All rights reserved 0-33 Figure 5.15

© 2007 Pearson Addison-Wesley. All rights reserved 0-34 Figure 5.16

© 2007 Pearson Addison-Wesley. All rights reserved 0-35 Figure 5.17

© 2007 Pearson Addison-Wesley. All rights reserved 0-36 In-class Exercise What names are interrogated by the binary search when searching for the name Evelyn in the list Alice, Bill, Carol, David, Evelyn, Fred and George? Answer: David, Fred, and Evelyn What is the maximum number of entries that must be interrogated when applying the binary search to a list of 200 entries? What about a list of 100,000 entries? Answer:

© 2007 Pearson Addison-Wesley. All rights reserved 0-37 In-class Exercise What sequence of numbers would be printed by the following recursive procedure if we started it with N assigned the value 1? Procedure Exercise(N) print the value of N; if (N<3) then (apply the procedure Exercise to the value N+1) print the value of N; Answer: What is the termination condition in the recursive procedure of the above question?

© 2007 Pearson Addison-Wesley. All rights reserved 0-38 Algorithm Efficiency Measured as number of instructions executed Big theta notation: Used to represent efficiency classes –Example: Insertion sort is in Θ(n 2 ) Best, worst, and average case analysis

© 2007 Pearson Addison-Wesley. All rights reserved 0-39 Figure 5.18 Applying the insertion sort in a worst-case situation

© 2007 Pearson Addison-Wesley. All rights reserved 0-40 Figure 5.19 Graph of the worst-case analysis of the insertion sort algorithm

© 2007 Pearson Addison-Wesley. All rights reserved 0-41 Figure 5.20 Graph of the worst-case analysis of the binary search algorithm

© 2007 Pearson Addison-Wesley. All rights reserved 0-42 Software Verification Proof of correctness –Assertions Preconditions Loop invariants Testing

© 2007 Pearson Addison-Wesley. All rights reserved 0-43 Chain Separating Problem A traveler has a gold chain of seven links. He must stay at an isolated hotel for seven nights. The rent each night consists of one link from the chain. What is the fewest number of links that must be cut so that the traveler can pay the hotel one link of the chain each morning without paying for lodging in advance?

© 2007 Pearson Addison-Wesley. All rights reserved 0-44 Figure 5.21 Separating the chain using only three cuts

© 2007 Pearson Addison-Wesley. All rights reserved 0-45 Figure 5.22 Solving the problem with only one cut

© 2007 Pearson Addison-Wesley. All rights reserved 0-46 Figure 5.23 The assertions associated with a typical while structure