Chapter 2: Algorithm Discovery and Design

Slides:



Advertisements
Similar presentations
CHAPTER 2 GC101 Program’s algorithm 1. COMMUNICATING WITH A COMPUTER  Programming languages bridge the gap between human thought processes and computer.
Advertisements

1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 Algorithm Discovery and Design Chapter 2 Topics: Representing Algorithms Algorithmic Problem.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Chapter 2: Algorithm Discovery and Design
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
Program Design and Development
Chapter 2 The Algorithmic Foundations of Computer Science
Chapter 2: Design of Algorithms
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Chapter 1 Program Design
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Chapter 3 Planning Your Solution
Review Algorithm Analysis Problem Solving Space Complexity
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
Fundamentals of C programming
ALGORITHMS AND FLOWCHARTS
Adapted from slides by Marie desJardins
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
Simple Program Design Third Edition A Step-by-Step Approach
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
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.
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.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
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.
Programming Logic and Design Fifth Edition, Comprehensive
Algorithms and Pseudocode
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Introduction to Computing Systems and Programming Programming.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
PROGRAMMING FUNDAMENTALS INTRODUCTION TO PROGRAMMING. Computer Programming Concepts. Flowchart. Structured Programming Design. Implementation Documentation.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Progression in KS3/4 Algorithms MONDAY 30 TH NOVEMBER SUE SENTANCE.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
Program design Program Design Process has 2 phases:
ALGORITHMS AND FLOWCHARTS
Algorithms and Flowcharts
Introduction To Flowcharting
ALGORITHMS AND FLOWCHARTS
Control Structure Senior Lecturer
Unit# 9: Computer Program Development
Algorithm Discovery and Design
ALGORITHMS AND FLOWCHARTS
Algorithm Discovery and Design
ICT Gaming Lesson 2.
Discovery and Design of Algorithms
Presentation transcript:

Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition

Objectives In this chapter, you will learn about: Representing algorithms Examples of algorithmic problem solving Invitation to Computer Science, C++ Version, Third Edition

Introduction This chapter discusses algorithms and algorithmic problem solving using three problems: Searching lists Finding maxima and minima Matching patterns Invitation to Computer Science, C++ Version, Third Edition

Methods for Representing Algorithms Natural language Language spoken and written in everyday life Examples: English, Spanish, Arabic, etc. Problems with using natural language for algorithms Verbose Imprecise Relies on context and experiences to give precise meaning to a word or phrase Invitation to Computer Science, C++ Version, Third Edition

The Addition Algorithm of Figure 1.2 Expressed in Natural Language Invitation to Computer Science, C++ Version, Third Edition

Methods for Representing Algorithms High-level programming language Examples: C++, Java Problem with using a high-level programming language for algorithms During the initial phases of design, we are forced to deal with detailed language issues Invitation to Computer Science, C++ Version, Third Edition

Figure 2.2 The Beginning of the Addition Algorithm of Figure 1.2 Expressed in a High-Level Programming Language Invitation to Computer Science, C++ Version, Third Edition

Methods for Representing Algorithms Pseudocode English language constructs modeled to look like statements available in most programming languages Steps presented in a structured manner (numbered, indented, etc.) No fixed syntax for most operations is required Invitation to Computer Science, C++ Version, Third Edition

Pseudocode (continued) Less ambiguous and more readable than natural language Emphasis is on process, not notation Well-understood forms allow logical reasoning about algorithm behavior Can be easily translated into a programming language Invitation to Computer Science, C++ Version, Third Edition

Algorithmic Operations Types of algorithmic operations Sequential Conditional Iterative Invitation to Computer Science, C++ Version, Third Edition

Sequential Operations Computation operations Example Set the value of “variable” to “arithmetic expression” Variable Named storage location that can hold a data value Invitation to Computer Science, C++ Version, Third Edition

Sequential Operations (continued) Input operations To receive data values from the outside world Example Get a value for r, the radius of the circle Output operations To send results to the outside world for display Print the value of Area Invitation to Computer Science, C++ Version, Third Edition

Figure 2.3: Algorithm for Computing Average Miles per Gallon An Algorithm Using Only Sequential Operations Figure 2.3: Algorithm for Computing Average Miles per Gallon Invitation to Computer Science, C++ Version, Third Edition

Conditional and Iterative Operations Sequential algorithm ( example previous slide) Also called straight-line algorithm Executes its instructions in a straight line from top to bottom and then stops Control operations Conditional operations Iterative operations Invitation to Computer Science, C++ Version, Third Edition

Conditional Operations Ask questions and choose alternative actions based on the answers Example ( syntax vs semantics – show flow chart) if x is greater than 25 then print x else print x times 100 Invitation to Computer Science, C++ Version, Third Edition

Figure 2.4: Second Version of the Average Miles per Gallon Algorithm (What types of operations ?) Invitation to Computer Science, C++ Version, Third Edition

Iterative Operations Iterative operations Perform “looping” behavior; repeating actions until a continuation condition becomes false Loop The repetition of a block of instructions Invitation to Computer Science, C++ Version, Third Edition

Iterative Operations (continued) Examples (syntax) while j > 0 do set s to s + aj set j to j - 1 repeat do print ak print ak set k to k + 1 set k to k+1 until k > n while k ≤ n Invitation to Computer Science, C++ Version, Third Edition

Iterative Operations (continued) Components of a loop Continuation condition Loop body Infinite loop The continuation condition never becomes false An error Invitation to Computer Science, C++ Version, Third Edition

Figure 2.5: Third Version of the Average Miles per Gallon Algorithm (What type of operations are used ?) Invitation to Computer Science, C++ Version, Third Edition

Iterative Operations (continued) Pretest loop ( semantics - show flow chart ) Continuation condition tested at the beginning of each pass through the loop It is possible for the loop body to never be executed While loop Invitation to Computer Science, C++ Version, Third Edition

Conditional and Iterative Operations (continued) Posttest loop ( semantics - show flow chart ) Continuation condition tested at the end of loop body Loop body must be executed at least once Do/While loop ( or Repeat/Until loop ) Invitation to Computer Science, C++ Version, Third Edition

Summary of Pseudocode Language Instructions Figure 2.6 Summary of Pseudocode Language Instructions Invitation to Computer Science, C++ Version, Third Edition

Algorithm Development (1) Algorithms have three basic phases Input Processing Output Invitation to Computer Science, C++ Version, Third Edition

Algorithm Development (2) Input - refers to the stage in which data and/or other processing information is given to the algorithm. If calculating Area from Length and Width - values for L and W are provided If summing the numbers from 1 to N - value for N is provided If searching a list of names for a specific name - the list and the name to search for are given Invitation to Computer Science, C++ Version, Third Edition

Algorithm Development (3) Processing – refers to the stage in which the data and/or other information is manipulated to obtain the desired result. This is sometimes divided into two parts Process Initialization (not always needed – give examples) Set values of process parameters and/or, Manipulate original data in some way Process Execution Algorithmic operations followed until the desired result is obtained. Invitation to Computer Science, C++ Version, Third Edition

Algorithm Development (4) Output - The desired result is made available to the user or other computing agent If calculating Area from Length and Width - the value of Area is displayed If summing numbers from 1 to N - the value of N is displayed If searching a list of names for a specific name - display info associated with name, or - report that name not found Invitation to Computer Science, C++ Version, Third Edition

Algorithm Development (5) Algorithms are developed in order to solve a problem or accomplish a task. The task or problem is usually expressed in: words, as in a word problem, or a specification document How do I convert the problem description into an algorithm that can be programmed? Invitation to Computer Science, C++ Version, Third Edition

Algorithm Development (6) Problem description → Algorithm Read description until you grasp the problem. Determine what data and other input information is required Determine the desired output or outcome Formulate a ‘high level’ description or plan for processing the input to get the desired result Write a first draft of the algorithm Refine the algorithm until it is in ‘pseudocode’ Invitation to Computer Science, C++ Version, Third Edition

Example 1: Looking, Looking, Looking Examples of algorithmic problem solving Sequential search: find a particular value in an unordered collection Find maximum: find the largest value in a collection of data Pattern matching: determine if and where a particular pattern occurs in a piece of text Invitation to Computer Science, C++ Version, Third Edition

Example 1: Looking, Looking, Looking (continued) Task Find a particular person’s name from an unordered list of telephone subscribers Algorithm outline Start with the first entry and check its name, then repeat the process for all entries Invitation to Computer Science, C++ Version, Third Edition

Example 1: Looking, Looking, Looking (continued) Algorithm discovery Finding a solution to a given problem Naïve sequential search algorithm ( example next slide ) For each entry, write a separate section of the algorithm that checks for a match Problems Only works for collections of exactly one size Duplicates the same operations over and over Invitation to Computer Science, C++ Version, Third Edition

Sequential Search – Attempt 1 (100 items) Get values for list of Names, Numbers Get target name If Name1 equals target Then Print Message “Found ”, Name1, “Phone Number”, Numbers1 Endif If Name2 equals target Then Print Message “Found ”, Name2, “Phone Number”, Numbers2 If Name3 equals target Then Print Message “Found ”, Name3, “Phone Number”, Numbers3 …. If Name100 equals target Then Print Message “Found ”, Name100, “Phone Number”, Numbers100 Invitation to Computer Science, C++ Version, Third Edition

Example 1: Looking, Looking, Looking (continued) Correct sequential search algorithm Uses iteration to simplify the task Refers to a value in the list using an index (or pointer) Handles special cases (like a name not found in the collection) Uses the variable Found to exit the iteration as soon as a match is found Invitation to Computer Science, C++ Version, Third Edition

Figure 2.9: The Sequential Search Algorithm (flowchart next slide) Invitation to Computer Science, C++ Version, Third Edition

Invitation to Computer Science, C++ Version, Third Edition

Example 2: Big, Bigger, Biggest Task Find the largest value from a list of values Algorithm outline Keep track of the largest value seen so far and its location Compare each value to the largest seen so far, and keep the larger as the new largest Invitation to Computer Science, C++ Version, Third Edition

Example 2: Big, Bigger, Biggest (continued) Find Largest algorithm Uses iteration and indices like previous example Updates location and largest so far when needed in the loop Invitation to Computer Science, C++ Version, Third Edition

Algorithm to Find the Largest Value in a List Figure 2.10 Algorithm to Find the Largest Value in a List Invitation to Computer Science, C++ Version, Third Edition

Find Largest Algorithm – Pseudo Code Simulator Version Steps for FindLargest With Array A Set Value of Size to A.length Set Value of LargestSoFar to A[0] Set Value of Location to 0 Set Value of Counter to 1 While Value of Counter < Size Do If Value of A[Counter] > LargestSoFar Then Set Value of LargestSoFar to A[Counter] Set Value of Location to Counter Endif Set Value of Counter to Counter + 1 Endwhile Output Value of LargestSoFar Output Value of Location Stop Input Initialization Processing Output Invitation to Computer Science, C++ Version, Third Edition

Example 3: Meeting Your Match Task Find if and where a pattern string occurs within a longer piece of text Algorithm outline Try each possible location of pattern string in turn At each location, compare pattern characters against string characters Invitation to Computer Science, C++ Version, Third Edition

Example 3: Meeting Your Match (continued) Concept of Abstraction Separating high-level view from low-level details Key concept in computer science Makes difficult problems intellectually manageable Allows piece-by-piece development of algorithms Invitation to Computer Science, C++ Version, Third Edition

Example 3: Meeting Your Match (continued) Top-down design When solving a complex problem: Create high-level operations in first draft of an algorithm After drafting the outline of the algorithm, return to the high-level operations and elaborate each one Repeat until all operations are primitives Invitation to Computer Science, C++ Version, Third Edition

Example 3: Meeting Your Match (continued) Pattern-matching algorithm (graphical example on board) Contains a loop within a loop External loop iterates through possible locations of matches to pattern Internal loop iterates through corresponding characters of pattern and string to evaluate match Invitation to Computer Science, C++ Version, Third Edition

Pattern Matching Pattern Matching Demonstration Invitation to Computer Science, C++ Version, Third Edition

Final Draft of the Pattern-Matching Algorithm Figure 2.12 Final Draft of the Pattern-Matching Algorithm Invitation to Computer Science, C++ Version, Third Edition

Problem solving with algorithms The selection of an algorithm to solve a problem is greatly influenced by the way the data for that problem are organized Searching is more efficient if list is sorted first Once an algorithm has been developed, it may itself be used in the construction of other, more complex algorithms Sorting may use find largest or find smallest Invitation to Computer Science, C++ Version, Third Edition

Problem solving with algorithms Library A collection of useful algorithms An important tool in algorithm design and development Invitation to Computer Science, C++ Version, Third Edition

Summary Algorithm design is a first step in developing an algorithm Must also: Ensure the algorithm is correct Ensure the algorithm is sufficiently efficient Pseudocode is used to design and represent algorithms Invitation to Computer Science, C++ Version, Third Edition

Summary Pseudocode is readable, unambiguous, and analyzable Algorithm design is a creative process; uses multiple drafts and top-down design to develop the best solution Abstraction is a key tool for good design Invitation to Computer Science, C++ Version, Third Edition