Chapter 2: Design of Algorithms

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CS101: Introduction to Computer programming
CS107 Introduction to Computer Science Lecture 2.
Efficiency of Algorithms
Repetition Control Structures School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 9, Friday 3/07/2003)
Computer Science 101 Overview of Algorithms. Example: Make Pancakes Prepare batter Beat 2 eggs Add 1 tablespoon of brown sugar Add 1 cup of milk Add 2.
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 Algorithm Discovery and Design Chapter 2 Topics: Representing Algorithms Algorithmic Problem.
Efficiency of Algorithms
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
CS107 Introduction to Computer Science Lecture 2.
Chapter 2 The Algorithmic Foundations of Computer Science
CHAPTER 2 CS
Designing Algorithms Csci 107 Lecture 3. Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun.
Algorithms and Efficiency of Algorithms February 4th.
Designing Algorithms Csci 107 Lecture 4.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Chapter 2: Algorithm Discovery and Design
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
The Program Design Phases
ALGORITHMS AND FLOWCHARTS
Adapted from slides by Marie desJardins
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
Muhammad Adnan Talib Lec#4 27 th Sep,2012 Computer Science Department COMSATS Institute of Information Technology Sahiwal Introduction to Computer Programming.
计算机科学概述 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.
CPS120 Introduction to Computer Programming The Programming Process.
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.
Flowcharts.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
CPSC 171 Introduction to Computer Science More Algorithm Discovery and Design.
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.
Control Structures CPS120: Introduction to Computer Science Lecture 5.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
CSCI-100 Introduction to Computing
Program Design. The design process How do you go about writing a program? –It’s like many other things in life Understand the problem to be solved Develop.
ALGORITHMS.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
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.
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.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
 Problem Analysis  Coding  Debugging  Testing.
PROGRAMMING: What’s It All About?
ALGORITHMS AND FLOWCHARTS
Applied Discrete Mathematics Week 2: Functions and Sequences
Algorithm and Ambiguity
ALGORITHMS AND FLOWCHARTS
Algorithm Discovery and Design
ALGORITHMS AND FLOWCHARTS
Algorithm Discovery and Design
Computer Science Core Concepts
Flowcharts and Pseudo Code
Discovery and Design of Algorithms
Lecture 3 Pseudocode (S&G, §2.2)
Software Development Techniques
Presentation transcript:

Chapter 2: Design of Algorithms Representing algorithms How to express algorithms the best? Possible solutions: Natural language Programming language Pseudo-Code + Known by everyone (that speaks it) -- Hides the structure of the algorithm -- Allows ambiguity in expressions + Language that computer understands (e.g. Fortran, C, Pascal) + Very exact and well structured -- Very restrictive particularly in early design phases

Representing Algorithms Pseudo-Code: golden medium Bases on a natural language (e.g. English) Uses “programming-language”-like notation (e.g. statements, conditions etc.) Examples Set x to the value 45 Return Go to step 100 if condition is satisfied Types of pseudo-code instructions Sequential operations Conditional operations Iterative operations

Representing Algorithms Sequential operations Input/Output: Communication with the outside Input: Get the value for “variable1” “variable2” … Example: Get the value of x y z Output Print the value for “variable1” “variable2” … Example: Print the value of x y z Print the message ‘message content’ Example: Print the message ‘An error has been detected’ Computation: Set the value of “variable” to “arithmetic operation” Example: Set the value of Area to p*r2

Representing Algorithms Conditional operations: If “condition” is true then Set of algorithmic operations Else Example: If denominator = 0 then Print the message ‘Operation cannot be done’ Else Set fraction to numerator/denominator

Representing Algorithms Iterative operations Repeat Step i to j until “condition” becomes true Step i: … Step j: Repeat until “condition” becomes true Set of operations End of loop While “condition” remains true do End of the loop

Representing Algorithms Examples of iterative operations Printing squares of 1 to n: Get the value of n While n > 0 do Print n2 Set n to n-1 The same algorithm with a repeat loop: If n > 0 then Repeat until n < 1 becomes true Difference between while and repeat: While: pre-check (continuation) condition then perform work (# of times 0, 1, 2, …) Repeat: perform work then post-check (termination) condition (# of times 1, 2, …)

Representing Algorithms Are the three types of operations (sequential, conditional, iterative) sufficient to represent ANY possible valid algorithm?  Y E S ! Even the most complex algorithms used in e.g. international switching systems can be represented using said operations Compare: We only need few letters (26 in English) to write the most marvelous novels.

Algorithmic Problem Solving Sequential Search Algorithm Problem Statement: Given a name N and a telephone book including names N1 to N1000 and phone numbers T1 to T1000 Task: Find the phone number T of the name N First solution: 1. Get N, N1,…,N1000, T1, …T1000 2. If N = N1 then Print the value T1 … 1001. If N = N1000 then Print the value T1000 1002. Stop

Sequential Search Algorithm Assessment of the first solution +: Algorithm prints all phone numbers of the given name N --: Too long listing (1001 lines) --: Slow: it checks always all entries even after the phone number has been already found --: No error message if the name is not in the list

Sequential Search Algorithm Second solution: 1. Get N, N1, …, N1000, T1, …, T1000 2. Set i to 1 and mark the N as not yet found 3. Repeat 4 to 7 until N is found 4. If N = Ni then 5. Print the Phone number is Ti 6. Mark N as found Else 7. Add 1 to i 8. Stop

Sequential Search Algorithm Assessment of the second solution +: Very short listing (only 8 lines) +: Quick if N is in the list, it does not check all entries in all cases --: Big problem: Endless loop if N is not in the list Final solution: 1. Get N, N1, …, N1000, T1, …, T1000 2. Set i to 1 and mark the N as not yet found 3. Repeat 4 to 7 until either N is found or i > 1000 4. If N = Ni then 5. Print the Phone number is Ti 6. Mark N as found Else 7. Add 1 to i 8. If N is not marked as found then Print message ‘Name is not in the directory’ 9. Stop

Finding the Maximum Given a list of n numbers Task: find the largest number in the list Solution: 1. Get the value n 2. Get the values A1, A2, …, An 3. Set the value of maximum to A1 4. Set the value of location to 1 5. Set the value of i to 2 6. While i <= n do 7. If Ai > maximum then 8. Set maximum to Ai 9. Set location to I 10. Add 1 to I End of the loop 11. Print maximum and location 12. Stop

Pattern Matching Given a text T (sequence of letters) and a pattern (e.g. word) p Find the occurrence of p in T Example: Text: let me know whether or not to go Pattern: no Output: Match starting at position 9 Pattern: no Output: Match starting at position 24 Example: Genome research Genome: … T C G A T T G T C C C A G T G C A A A C T G C A T … Probe: A A A (a match)

Pattern Matching Solution: 1. Get values n and m the size of the text and the pattern 2. Get T1,…,Tn and P1,…,Pm 3. Set k to 1 4. Repeat until k > n-m+1 (until we treated the entire text) 5. Try to match the pattern P1…Pm at position k 6. If there was a match then 7. Print the value of k the starting location of the match 8. Add 1 to k (slides to the right) End of loop 9. Stop

Pattern Matching Step 5 ??? 5. Try to match the pattern P1…Pm at position k Situation: Text: T1 T2 … Tk Tk+1…Tk+m-1 Tk+m …Tn Pattern: P1 P2 … Pm Algorithm for Step 5 Set i to 1 and mismatch to no Repeat until (i > m) or mismatch = yes If Pi not equal Tk+i-1 then Set mismatch to yes Else Set i to i+1 End of loop