LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, 2003-2009 Algorithms Problem Solving  Readings: [SG] Ch. 2  Chapter Outline: 1.Chapter.

Slides:



Advertisements
Similar presentations
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Animation of Algorithm Goal: To understand an algorithm by animating its execution,
Advertisements

MATH 224 – Discrete Mathematics
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Algorithms  Readings: [SG] Ch. 2 & 3  Chapter Outline: 1.Chapter Goals 2.What are Algorithms 1.Real Life.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Algorithms (more examples…)  Supplementary Notes: 1.For your reference… (esp. those new to programming)
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm:
CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 Algorithm Discovery and Design Chapter 2 Topics: Representing Algorithms Algorithmic Problem.
Efficiency of Algorithms
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Lecture 2: Fundamental Concepts
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Program Design and Development
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
Chapter 2 The Algorithmic Foundations of Computer Science
CHAPTER 2 CS
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Chapter 2: Design of Algorithms
Algorithms and Efficiency of Algorithms February 4th.
Designing Algorithms Csci 107 Lecture 4.
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 1 Program Design
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, C++ Version, Fourth Edition.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
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
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
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.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, Integrating Different Ideas Together  Reading Materials:  Ch 3.6 of [SG]  Contents:
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
By the end of this session you should be able to...
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
2/13/2003CSCI 150: Introduction to Computer Science1 Introduction to Computer Science CSCI 150 Section 002 Session 9 Dr. Richard J. Bonneau IONA Technologies.
Procedural programming in Java Methods, parameters and return values.
Pseudocode Algorithms Using Sequence, Selection, and Repetition Simple Program Design Third Edition A Step-by-Step Approach 6.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution,
Data Structure Introduction Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2010.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Efficiency of Algorithms  Readings: [SG] Ch. 3  Chapter Outline: oAttributes.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
1 UNIT-I BRUTE FORCE ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 3:
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.
DATA STRUCTURES (CS212D) Overview & Review Instructor Information 2  Instructor Information:  Dr. Radwa El Shawi  Room: 
Loop Invariants and Binary Search Chapter 4.4, 5.1.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Notice….  No tutorial this Friday (12-Feb)  Special class on BASIC Algorithms.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Introduction to Computing Systems and Programming Programming.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Algorithms Problem Solving  Readings: [SG] Ch. 2  Chapter Outline: 1.Chapter.
Python: Control Structures
GC211Data Structure Lecture2 Sara Alhajjam.
Introduction To Flowcharting
Control Structure Senior Lecturer
Algorithm Discovery and Design
Algorithm Discovery and Design
Discovery and Design of Algorithms
Introduction to the Lab
Chapter 13 Control Structures
Presentation transcript:

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Algorithms Problem Solving  Readings: [SG] Ch. 2  Chapter Outline: 1.Chapter Goals 2.What are Algorithms 3.Pseudo-Code to Express Algorithms 4.Some Simple Algorithms [SG] Ch Computing Array-Sum 2.Structure of Basic Iterative Algorithm 5.Examples of Algorithmic Problem Solving

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 2 © Leong Hon Wai, Simple iterative algorithm: Array-Sum(A,n)  Input:  Input: List of numbers: A 1, A 2, A 3, …., A n  Output: To compute the sum of the numbers Note: Store numbers in array A[1], A[2], …, A[n] Array-Sum(A, n); (* Find the sum of A1, A2,…,An. *) begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; return Sum; end; Sum_SF represents the Sum-So-Far Click to see alg. animation

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 3 © Leong Hon Wai, Exercising Algorithm Array-Sum(A,n): A[1] A[2] A[3] A[4] A[5] A[6] n= k Sum-SF Sum ? 0 ? 1 2 ? 2 7 ? 3 17 ? 4 20 ? 5 32 ? 6 56 ? Sum is 56 Input: Processing: Output:

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 4 © Leong Hon Wai, Structure of “basic iterative algorithm” Array-Sum(A, n); (* Find the sum of A1, A2,…,An. *) begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum end; Name of AlgorithmParameters: A and n Some comments for human understanding Initialization block Iteration block; the key step where most of the work is done Post-Processing block Structure of Basic iterative algorithm Recall Recurring Principle: The Power of Iterations

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 5 © Leong Hon Wai, Re-use of “basic iterative algorithm”  Once an algorithm is developed,  Give it a name (an abstraction): Array- Sum(A,n) oIt can be re-used in solving more complex problems oIt can be modified to solve other similar problems  Modify algorithm for Array- Sum(A,n) to… oCalculate the average and sum-of-squares oSearch for a number; find the max, min  Develop a algorithm library oA collection of useful algorithms oAn important tool-kit for algorithm development

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 6 © Leong Hon Wai, Algorithms (Introduction)  Readings: [SG] Ch. 2  Chapter Outline: 1.Chapter Goals 2.What are Algorithms 3.Pseudo-Code to Express Algorithms 4.Some Simple Algorithms 5.Examples of Algorithmic Problem Solving [Ch. 2.3] 1.Searching Example, 2.Finding Maximum/Largest 3.Modular Program Design 4.Pattern Matching

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 7 © Leong Hon Wai, Algorithmic Problem Solving  Examples of algorithmic problem solving 1.Sequential search: find a particular value in an unordered collection 2.Find maximum: find the largest value in a collection of data 3.Pattern matching: determine if and where a particular pattern occurs in a piece of text

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 8 © Leong Hon Wai, Task 1: Looking, Looking, Looking…  Task oFind a particular person’s name from an unordered list of telephone subscribers  Algorithm outline oStart with the first entry and check its name, then repeat the process for all entries

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 9 © Leong Hon Wai, Task 1: Looking, Looking, Looking…  Sequential search algorithm  Re-use the basic iterative algorithm of Sum(A,n) oRefers to a value in the list using an index i (or pointer/subscript) oUses the variable Found to exit the iteration as soon as a match is found oHandles special cases u like a name not found in the collection  Question: What to change in Initialization, Iteration, Post-Processing?

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 10 © Leong Hon Wai, Figure 2.9: The Sequential Search Algorithm Task 1: Sequential Search Algorithm Initialization block Iteration block; the key step where most of the work is done Post-Processing block

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 11 © Leong Hon Wai, Algorithm Sequential Search (revised) Seq-Search(N, T, m, NAME); begin i  1; Found  No; while (Found = No) and (i <= m) do if (NAME = N[i]) then Print T[i]; Found  Yes; else i  i + 1; endif endwhile if (Found=No) then Print NAME “is not found” endif end;  Preconditions: The variables NAME, m, and the arrays N[1..m] and T[1..m] have been read into memory. Initialization block Iteration block; the key step where most of the work is done Post-Processing block

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 12 © Leong Hon Wai, Note the differences…  Seq-Search () is a high-level primitive oIt takes in something as input, oComputes something, oReturn some computed values. Seq-Search N, T m NAME Found

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 13 © Leong Hon Wai, Task 2: Big, Bigger, Biggest Task: oFind the largest value from a list of values  Algorithm outline oKeep track of the largest value seen so far u Initialize: Set Largest-So-Far to be the first in the list u Iteration: Compare each value to the Largest-So-Far, and keep the larger as the new largest oUse location to remember where the largest is. u Initialize: … (Do it yourself) u Iteration: …. (Do it yourself)

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 14 © Leong Hon Wai, Figure 2.10: Algorithm to Find the Largest Value in a List Task 2: Finding the Largest Initialization block Iteration block; the key step where most of the work is done Post-Processing block

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 15 © Leong Hon Wai, Algorithm Find-Max Find-Max(A,n); (* find max of A[1..n] *) begin Max-SF  A[1]; Location  1; i  2; (* why 2, not 1? *) while (i <= n) do if (A[i] > Max-SF) then Max-SF  A[i]; Location  i; endif i  i + 1 endwhile Max  Max-SF; return Max, Location end;  Preconditions: The variable n and the arrays A have been read into memory. Initialization block Iteration block; the key step where most of the work is done Post-Processing block

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 16 © Leong Hon Wai, Modular Program Design  Software are complex oHUGE (millions of lines of code) eg: Linux, Outlook oCOMPLEX; eg: Flight simulator  Idea: Divide-and-Conquer Method (or decomposition) oComplex tasks can be divided and oEach part solved separately and combined later.  Modular Program Design oDivide big programs into smaller modules oThe smaller parts are ucalled modules, subroutines, or procedures uDesign, implement, and test separately oModularity, Abstraction, Division of Labour oSimplifies process of writing alg/programs

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 17 © Leong Hon Wai, Task 3: Pattern Matching  Algorithm search for a pattern in a source text Given: A source text S[1..n] and a pattern P[1..m] Question: Find all occurrence of pattern P in text S? CATATCATA S ATA P 1 23 Output of Pattern Matching Algorithm: There is a match at position 2 There is a match at position 7

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 18 © Leong Hon Wai, Example of Pattern Matching ATA P 1 23 k Align pattern P with text S starting at pos k = 1; Check for match (between S[1..3] and P[1..3]) Result – no match CATATCATA S

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 19 © Leong Hon Wai, Example of Pattern Matching ATA P 1 23 k Align pattern P with text S starting at pos k = 2; Check for match (between S[2..4] and P[1..3]) Result – match! Output: There is a match at position 2 CATATCATA S

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 20 © Leong Hon Wai, Example of Pattern Matching ATA P 1 23 k Align pattern P with text S starting at pos k = 3; Check for match (between S[3..5] and P[1..3]) Result – No match. CATATCATA S

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 21 © Leong Hon Wai, Example of Pattern Matching ATA P 1 23 k Align pattern P with text S starting at pos k = 4; Check for match (between S[4..6] and P[1..3]) Result – No match. CATATCATA S

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 22 © Leong Hon Wai, Example of Pattern Matching ATA P 1 23 k Align pattern P with text S starting at pos k = 5; Check for match (between S[5..7] and P[1..3]) Result – No match. CATATCATA S

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 23 © Leong Hon Wai, Example of Pattern Matching ATA P 1 23 k Align pattern P with text S starting at pos k = 6; Check for match (between S[6..8] and P[1..3]) Result – No match. CATATCATA S Align S[k..(k+m–1)] with P[1..m]

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 24 © Leong Hon Wai, Example of Pattern Matching ATA P 1 23 k Align pattern P with text S starting at pos k = 7; Check for match (between S[7..9] and P[1..3]) Result – match! Output: There is a match at position 7 CATATCATA S Note: k = 7 is the last position to test; After that S is “too short”. In general, it is k = n–m+1

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 25 © Leong Hon Wai, Pattern Matching: Decomposition Task: Find all occurrences of the pattern P in text S;  Algorithm Design: Top Down Decomposition oModify from basic iterative algorithm (index k)  At each iterative step (for each k) oAlign pattern P with S at position k and oTest for match between P[1..m] and S[k.. k+m –1]  Define an abstraction (“high level operation”) Match(S, k, P, m) = Yes if S[k..k+m–1] = P[1..m] No otherwise

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 26 © Leong Hon Wai, Pattern Matching: Pat-Match Pat-Match(S,n,P,m); (* Finds all occurrences of P in S *) begin k  1; while (k <= n-m+1) do if Match(S,k,P,m) = Yes then Print “Match at pos ”, k; endif k  k+1; endwhile end;  Preconditions: The variables n, m, and the arrays S and P have been read into memory. Use the “high level operation” Match(S,k,P,m) which can be refined later.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 27 © Leong Hon Wai, Match of S[k..k+m-1] and P[1..m] Align S[k..k+m–1] with P[1..m] (Here, k = 4) Match(S,k,P,m); begin i  1; MisMatch  No; while (i <= m) and (MisMatch=No) do if (S[k+i-1] not equal to P[i]) then MisMatch=Yes else i  i + 1 endif endwhile Match  not(MisMatch); (* Opposite of *) end; ATA P 1 23 CATATCATA S i

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 28 © Leong Hon Wai, Example: Match of S[4..6] and P[1..3] i Align S[k..k+m–1] with P[1..m] (Here, k = 4) [k = 4] With i = 1, Compare S[4] and P[1] (S[k+i-1] and P[i]) They are equal, so increment i ATA P 1 23 CATATCATA S

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 29 © Leong Hon Wai, Example: Match of S[4..6] and P[1..3] Align S[k..k+m–1] with P[1..m] (Here, k = 4) [k = 4] With i = 2, Compare S[5] and P[2] (S[k+i-1] and P[i]) They are equal, so increment i ATA P 1 23 CATATCATA S i

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 30 © Leong Hon Wai, Example: Match of T[4..6] and P[1..3] Align S[k..k+m–1] with P[1..m] (Here, k = 4) [k = 4] With i = 3, Compare S[6] and P[3] (S[k+i-1] and P[i]) They are not equal, so set MisMatch=Yes ATA P 1 23 CATATCATA S i

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 31 © Leong Hon Wai, Our Top-Down Design oAchieves good division-of-labour  Made use of top-down design and abstraction oSeparate “high-level” view from “low-level” details oMake difficult problems more manageable oAllows piece-by-piece development of algorithms oKey concept in computer science Pat-Match(S,n,P,m) Match(S,k,P,m) “higher-level” view “high-level” primitive  Our pattern matching alg. consists of two modules

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 32 © Leong Hon Wai, Pattern Matching: Pat-Match (1 st draft) Pat-Match(S,n,P,m); (* Finds all occurrences of P in S *) begin k  1; while (k <= n-m+1) do if Match(S,k,P,m) = Yes then Print “Match at pos ”, k; endif k  k+1; endwhile end;  Preconditions: The variables n, m, and the arrays S and P have been read into memory. Use the “high level primitive operation” Match(S,k,P,m) which can be de/refined later.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 33 © Leong Hon Wai, Figure 2.12: Final Draft of the Pattern-Matching Algorithm Pattern Matching Algorithm of [SG] This part compute Match(T,k,P,m) THINK:: How can Mismatch=NO here?

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 34 © Leong Hon Wai, Pattern Matching Algorithm of [SG]  Pattern-matching algorithm oContains a loop within a loop uExternal loop iterates through possible locations of matches to pattern uInternal loop iterates through corresponding characters of pattern and string to evaluate match

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 35 © Leong Hon Wai, Summary  Specify algorithms using pseudo-code oUnambiguous, readable, analyzable  Algorithm specified by three types of operations oSequential, conditional, and repetitive operations  Seen several examples of algorithm design oDesigning algorithm is not so hard oRe-use, Modify/Adapt, Abstract your algorithms  Algorithm design is also a creative process oTop-down design helps manage complexity oProcess-oriented thinking helps too

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 36 © Leong Hon Wai, Summary  Importance of “doing it” oTest out each algorithm to find out “what is really happening” oRun some of the animations in the lecture notes  If you are new to algorithms oread the textbook otry out the algorithms odo the exercises … The End …

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 37 © Leong Hon Wai, Thank you!

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 38 © Leong Hon Wai, Additional Slides (optional)…  The next slide is for your info only.  They give variants of algorithm using for-loop oSum and Pattern-Match  A reminder – ofor-loops will not be tested in UIT2201.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 39 © Leong Hon Wai, Variant of Sum(A,n) – with for-loop  We can also use a for-loop instead of a while-loop.  Note: This version is more compact, right? Sum2(A, n); (* Find the sum of A1, A2,…, An. *) begin Sum_sf  0; for k  1 to n do Sum_sf  Sum_sf + A[k]; endfor Sum  Sum_sf; Print “Sum is”, Sum end;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 40 © Leong Hon Wai, Pattern Matching: Pat-Match (with for-loop) Pat-Match2(T,n,P,m); (* Finds all occurrences of P in T *) begin for k  1 to n-m+1) do if Match(T,k,P,m) = Yes then Print “Match at pos ”, k; endif endfor end;  Preconditions: The variables n, m, and the arrays T and P have been read into memory.  Note: This version is more compact, isn’t it?