Copyright © 2015 Pearson Education, Inc. Chapter 5: Algorithms.

Slides:



Advertisements
Similar presentations
Algorithms.
Advertisements

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.
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Faster Sorting Methods Chapter 9 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 19: Searching and Sorting Algorithms
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
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 2: Algorithm Discovery and Design
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.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
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.
Introduction to Computer Systems
Introduction to Programming. What is a Program  Set of Instructions that tells the computer what to Do.  Driving force behind the computer  Without.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Problem Solving and Algorithms
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.
“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.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
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
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
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.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Chapter 16: Searching, Sorting, and the vector Type.
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.
Chapter 7 JavaScript: Control Statements, Part 1
Algorithms and Problem Solving
Q & A.
Computer Science: An Overview Eleventh Edition
An Introduction to Sorting
Chapter 5: Algorithms 5.1 The Concept of an Algorithm
Chapter 5: Algorithms Computer Science: An Overview Tenth Edition
Computer Science — An Overview J. Glenn Brookshear
Algorithm Discovery and Design
Algorithms: the big picture
Computer Science: An Overview Tenth Edition
Presentation transcript:

Copyright © 2015 Pearson Education, Inc. Chapter 5: Algorithms

Copyright © 2015 Pearson Education, Inc. 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 Chapter 5: Algorithms 5-2

Copyright © 2015 Pearson Education, Inc. An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process. Definition of Algorithm 5-3

Copyright © 2015 Pearson Education, Inc. Requires well-defined primitives A collection of primitives constitutes a programming language. Algorithm Representation 5-4

Copyright © 2015 Pearson Education, Inc. Figure 5.2 Folding a bird from a square piece of paper 5-5

Copyright © 2015 Pearson Education, Inc. Figure 5.3 Origami primitives 5-6

Copyright © 2015 Pearson Education, Inc. Assignment name = expression Example RemainingFunds = CheckingBalance + SavingsBalance Pseudocode Primitives 5-7

Copyright © 2015 Pearson Education, Inc. Conditional selection if (condition): activity Example if (sales have decreased): lower the price by 5% Pseudocode Primitives (continued) 5-8

Copyright © 2015 Pearson Education, Inc. Conditional selection if (condition): activity else: activity Example if (year is leap year): daily total = total / 366 else: daily total = total / 365 Pseudocode Primitives (continued) 5-9

Copyright © 2015 Pearson Education, Inc. Repeated execution while (condition): body Example while (tickets remain to be sold): sell a ticket Pseudocode Primitives (continued) 5-10

Copyright © 2015 Pearson Education, Inc. Indentation shows nested conditions if (not raining): if (temperature == hot): go swimming else: play golf else: watch television Pseudocode Primitives (continued) 5-11

Copyright © 2015 Pearson Education, Inc. Define a function def name(): Example def ProcessLoan(): Executing a function if (...): ProcessLoan() else: RejectApplication() Pseudocode Primitives (continued) 5-12

Copyright © 2015 Pearson Education, Inc. Figure 5.4 The procedure Greetings in pseudocode 5-13 def Greetings(): Count = 3 while (Count > 0): print('Hello') Count = Count - 1

Copyright © 2015 Pearson Education, Inc. Using parameters def Sort(List):. Executing Sort on different lists Sort(the membership list) Sort(the wedding guest list) Pseudocode Primitives (continued) 5-14

Copyright © 2015 Pearson Education, Inc. 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. Polya’s Problem Solving Steps 5-15

Copyright © 2015 Pearson Education, Inc. 1. Understand the problem. 2. Get an idea of how an algorithmic function might solve the problem. 3. Formulate the algorithm and represent it as a program. 4. Evaluate the solution for accuracy and its potential as a tool for solving other problems. Polya’s Steps in the Context of Program Development 5-16

Copyright © 2015 Pearson Education, Inc. 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) Getting a Foot in the Door 5-17

Copyright © 2015 Pearson Education, Inc. 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? Ages of Children Problem 5-18

Copyright © 2015 Pearson Education, Inc. Figure

Copyright © 2015 Pearson Education, Inc. Figure 5.6 The sequential search algorithm in pseudocode 5-20 def Search (List, TargetValue): if (List is empty): Declare search a failure else: Select the first entry in List to be TestEntry while (TargetValue > TestEntry and entries remain): Select the next entry in List as TestEntry if (TargetValue == TestEntry): Declare search a success else: Declare search a failure

Copyright © 2015 Pearson Education, Inc. Figure 5.7 Components of repetitive control 5-21

Copyright © 2015 Pearson Education, Inc. Pretest loop: while (condition): body Posttest loop: repeat: body until(condition) Iterative Structures 5-22

Copyright © 2015 Pearson Education, Inc. Figure 5.8 The while loop structure 5-23

Copyright © 2015 Pearson Education, Inc. Figure 5.9 The repeat loop structure 5-24

Copyright © 2015 Pearson Education, Inc. Figure 5.10 Sorting the list Fred, Alex, Diana, Byron, and Carol alphabetically 5-25

Copyright © 2015 Pearson Education, Inc. Figure 5.11 The insertion sort algorithm expressed in pseudocode 5-26 def Sort(List): N = 2 while (N <= length of List): Pivot = Nth entry in List Remove Nth entry leaving a hole in List while (there is an Entry above the hole and Entry > Pivot): Move Entry down into the hole leaving a hole in the list above the Entry Move Pivot into the hole N = N + 1

Copyright © 2015 Pearson Education, Inc. 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. Recursion 5-27

Copyright © 2015 Pearson Education, Inc. Figure 5.12 Applying our strategy to search a list for the entry John 5-28

Copyright © 2015 Pearson Education, Inc. Figure 5.13 A first draft of the binary search technique 5-29 if (List is empty): Report that the search failed else: TestEntry = middle entry in the List if (TargetValue == TestEntry): Report that the search succeeded if (TargetValue < TestEntry): Search the portion of List preceding TestEntry for TargetValue, and report the result of that search if (TargetValue > TestEntry): Search the portion of List following TestEntry for TargetValue, and report the result of that search

Copyright © 2015 Pearson Education, Inc. Figure 5.14 The binary search algorithm in pseudocode 5-30 def Search(List, TargetValue): if (List is empty): Report that the search failed else: TestEntry = middle entry in the List if (TargetValue == TestEntry): Report that the search succeeded if (TargetValue < TestEntry): Sublist = portion of List preceding TestEntry Search(Sublist, TargetValue) if (TargetValue < TestEntry): Sublist = portion of List following TestEntry Search(Sublist, TargetValue)

Copyright © 2015 Pearson Education, Inc. Figure

Copyright © 2015 Pearson Education, Inc. Figure

Copyright © 2015 Pearson Education, Inc. Figure

Copyright © 2015 Pearson Education, Inc. 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 Algorithm Efficiency 5-34

Copyright © 2015 Pearson Education, Inc. Figure 5.18 Applying the insertion sort in a worst-case situation 5-35

Copyright © 2015 Pearson Education, Inc. Figure 5.19 Graph of the worst-case analysis of the insertion sort algorithm 5-36

Copyright © 2015 Pearson Education, Inc. Figure 5.20 Graph of the worst-case analysis of the binary search algorithm 5-37

Copyright © 2015 Pearson Education, Inc. Proof of correctness –Assertions Preconditions Loop invariants Testing Software Verification 5-38

Copyright © 2015 Pearson Education, Inc. 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? Chain Separating Problem 5-39

Copyright © 2015 Pearson Education, Inc. Figure 5.21 Separating the chain using only three cuts 5-40

Copyright © 2015 Pearson Education, Inc. Figure 5.22 Solving the problem with only one cut 5-41

Copyright © 2015 Pearson Education, Inc. Figure 5.23 The assertions associated with a typical while structure 5-42

Copyright © 2015 Pearson Education, Inc. EndofChapter