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.

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.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Software Engineering and Design Principles Chapter 1.
© 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.
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.
Software Design i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, Marti Hearst, or James Landay.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 2: Algorithm Discovery and Design
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
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.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
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.
Chapters 7, 8, & 9 Quiz 3 Review 1. 2 Algorithms Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of.
© 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.
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.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
ALGORITHM CHAPTER 8. Chapter Outlines and Objectives  Define an algorithm and relate it to problem solving.  Define three construct and describe their.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
An Introduction to Sorting Chapter 8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with.
Algorithm: definition An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.
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.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Computer Science: An Overview Eleventh Edition
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.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
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.
Algorithms and Problem Solving
Q & A.
Computer Science: An Overview Eleventh Edition
Chapter 5: Algorithms 5.1 The Concept of an Algorithm
PROBLEM SOLVING SKILLS
Chapter 5: Algorithms Computer Science: An Overview Tenth Edition
Computer Science — An Overview J. Glenn Brookshear
Algorithms and Problem Solving
Computer Science: An Overview Tenth Edition
Presentation transcript:

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 5.3 Algorithm Discovery 5.4 Iterative Structures 5.5 Recursive Structures 5.6 Efficiency and Correctness

© 2005 Pearson Addison-Wesley. All rights reserved 5-3 Algorithm: definition An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

© 2005 Pearson Addison-Wesley. All rights reserved 5-4 Algorithms: levels of abstraction Problem = motivation for algorithm Algorithm = procedure to solve the problem –Often one of many possibilities Representation = description of algorithm sufficient to communicate it to the desired audience –Always one of many possibilities

© 2005 Pearson Addison-Wesley. All rights reserved 5-5 Figure 5.2 Folding a bird from a square piece of paper

© 2005 Pearson Addison-Wesley. All rights reserved 5-6 Figure 5.3 Origami primitives

© 2005 Pearson Addison-Wesley. All rights reserved 5-7 Pseudocode primitives Assignment name  expression Conditional selection if condition then action Repeated execution while condition do activity Procedure procedure name (generic names)

© 2005 Pearson Addison-Wesley. All rights reserved 5-8 Figure 5.4 The procedure Greetings in pseudocode

© 2005 Pearson Addison-Wesley. All rights reserved 5-9 Figure 5.6 The sequential search algorithm in pseudocode

© 2005 Pearson Addison-Wesley. All rights reserved 5-10 Problem solving steps 1. Understand the problem. 2. Get an idea how an algorithmic procedure might solve the problem. 3. Formulate the algorithm and represent it as a program. 4. Evaluate the program for accuracy and its potential as a tool for solving other problems.

© 2005 Pearson Addison-Wesley. All rights reserved 5-11 Techniques for “getting a foot in the door” Work 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 = top-down methodology –Popular technique because it produces modular programs

© 2005 Pearson Addison-Wesley. All rights reserved 5-12 Sample 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?

© 2005 Pearson Addison-Wesley. All rights reserved 5-13 Figure 5.5

© 2005 Pearson Addison-Wesley. All rights reserved 5-14 Figure 5.7 Components of repetitive control

© 2005 Pearson Addison-Wesley. All rights reserved 5-15 Figure 5.8 The while loop structure

© 2005 Pearson Addison-Wesley. All rights reserved 5-16 Figure 5.9 The repeat loop structure

© 2005 Pearson Addison-Wesley. All rights reserved 5-17 Figure 5.10 Sorting the list Fred, Alex, Diana, Byron, and Carol alphabetically

© 2005 Pearson Addison-Wesley. All rights reserved 5-18 Figure 5.11 The insertion sort algorithm expressed in pseudocode

© 2005 Pearson Addison-Wesley. All rights reserved 5-19 Figure 5.12 Applying our strategy to search a list for the entry John

© 2005 Pearson Addison-Wesley. All rights reserved 5-20 Figure 5.13 A first draft of the binary search technique

© 2005 Pearson Addison-Wesley. All rights reserved 5-21 Figure 5.14 The binary search algorithm in pseudocode

© 2005 Pearson Addison-Wesley. All rights reserved 5-22 Figure 5.15

© 2005 Pearson Addison-Wesley. All rights reserved 5-23 Figure 5.16

© 2005 Pearson Addison-Wesley. All rights reserved 5-24 Figure 5.17

© 2005 Pearson Addison-Wesley. All rights reserved 5-25 Software efficiency Measured as number of instructions executed  notation for efficiency classes –Example: insertion sort is  (n 2 ) Best, worst, and average case

© 2005 Pearson Addison-Wesley. All rights reserved 5-26 Figure 5.18 Applying the insertion sort in a worst-case situation

© 2005 Pearson Addison-Wesley. All rights reserved 5-27 Figure 5.19 Graph of the worst-case analysis of the insertion sort algorithm

© 2005 Pearson Addison-Wesley. All rights reserved 5-28 Figure 5.20 Graph of the worst-case analysis of the binary search algorithm

© 2005 Pearson Addison-Wesley. All rights reserved 5-29 Software verification Proof of correctness –Assertions Preconditions Loop invariants Testing

© 2005 Pearson Addison-Wesley. All rights reserved 5-30 Example problem: Chain separating 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?

© 2005 Pearson Addison-Wesley. All rights reserved 5-31 Figure 5.21 Separating the chain using only three cuts

© 2005 Pearson Addison-Wesley. All rights reserved 5-32 Figure 5.22 Solving the problem with only one cut

© 2005 Pearson Addison-Wesley. All rights reserved 5-33 Figure 5.23 The assertions associated with a typical while structure