Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 2.
Advertisements

1 CS101 Introduction to Computing Lecture 17 Algorithms II.
Programming Types of Testing.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
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.
Program Design and Development
Pseudocode and Algorithms
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Designing Algorithms Csci 107 Lecture 3. Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun.
1 Algorithms and Analysis CS 2308 Foundations of CS II.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Algorithmics - Lecture 21 LECTURE 2: Algorithms description - examples -
CS223 Algorithms D-Term 2013 Instructor: Mohamed Eltabakh WPI, CS Introduction Slide 1.
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
By the end of this session you should be able to...
Data Structures and Algorithms Introduction to Algorithms M. B. Fayek CUFE 2006.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CS101 Computer Programming I Chapter 4 Extra Examples.
Algorithm Design.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Structured Programming (4 Credits)
Programming and Languages Dept. of Computer and Information Science IUPUI.
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Textbook C for Scientists and Engineers © Prentice Hall 1997 Available at NUS CO-OP at S$35.10.
COMP Loop Statements Yi Hong May 21, 2015.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong
Introduction to Algorithms
ALGORITHMS AND FLOWCHARTS
Applied Discrete Mathematics Week 2: Functions and Sequences
COMP108 Algorithmic Foundations Basics
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Enough Mathematical Appetizers!
Lecture 4B More Repetition Richard Gesick
Computation.
Definition In simple terms, an algorithm is a series of instructions to solve a problem (complete a task) We focus on Deterministic Algorithms Under the.
Algorithm design and Analysis
Program Design Introduction to Computer Programming By:
Algorithm Discovery and Design
Applied Discrete Mathematics Week 6: Computation
Algorithm Discovery and Design
Programming Fundamentals (750113) Ch1. Problem Solving
Introduction to Algorithms
Programming Fundamentals (750113) Ch1. Problem Solving
Application: Algorithms
Application: Algorithms
COMP108 Algorithmic Foundations Basics
COMP108 Algorithmic Foundations Searching
Basic Concepts of Algorithm
COMP108 Algorithmic Foundations Searching
WJEC GCSE Computer Science
Algorithms.
Presentation transcript:

Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong

Algorithmic Foundations COMP108 Crossing Night 1 min 2 min 5 min 10 min each time, 2 persons share a torch they speed of slower person Target: all cross the bridge Can you do it in 17 mins?

Algorithmic Foundations COMP108 3 (Basics) Module Information Dr Prudence Wong Rm 3.18 Ashton Building, office hours: Wed 10-11am, Thu 3-4pm Demonstrators Mr Mihai Burcea, Mr Jude-Thaddeus Ojiaku References Main: Introduction to the Design and Analysis of Algorithms. A. V. Levitin. Addison Wesley. Reference: Introduction to Algorithms. T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein. The MIT Press

Algorithmic Foundations COMP108 4 (Basics) Module Information (2) Teaching, Assessments and Help 33 lectures, 11 tutorials 2 assessments (20%), 1 written exam (80%) Office hours, Tutorials/Labs Location : Seminar Rooms G12 or 2.23 (theoretical) or Labs 1, 2, 3 (practical) Week 2: Seminar Rooms, G12 Ashton Bldg or 2.23 Holt Bldg Class Tests: Weeks 7 & 11 (15 Mar & 3 May)

Algorithmic Foundations COMP108 5 (Basics) Why COMP108? Pre-requisite for: COMP202, COMP218 (COMP202 is pre-requisite for COMP309) Algorithm design is a foundation for efficient and effective programs "Year 1 modules do not count towards honour classification…" (Career Services: Employers DO consider year 1 module results)

Algorithmic Foundations COMP108 6 (Basics) Aims  To give an overview of the study of algorithms in terms of their efficiency.  To introduce the standard algorithmic design paradigms employed in the development of efficient algorithmic solutions.  To describe the analysis of algorithms in terms of the use of formal models of Time and Space. What do we mean by good? How to achieve? Can we prove?

Algorithmic Foundations COMP108 Ready to start … Learning outcomes  Able to tell what an algorithm is & have some understanding why we study algorithms  Able to use pseudo code to describe algorithm

Algorithmic Foundations COMP108 8 (Basics) What is an algorithm? A sequence of precise and concise instructions that guide you (or a computer) to solve a specific problem Daily life examples: cooking recipe, furniture assembly manual (What are input / output in each case?) Input Algorithm Output

Algorithmic Foundations COMP108 9 (Basics) Why do we study algorithms? Given a map of n cities & traveling cost between them. What is the cheapest way to go from city A to city B? The obvious solution to a problem may not be efficient Simple solution  Compute the cost of each path from A to B  Choose the cheapest one A B

Algorithmic Foundations COMP (Basics) Shortest path to go from A to B How many paths between A & B? involving 1 intermediate city? Simple solution  Compute the cost of each path from A to B  Choose the cheapest one A B 2 The obvious solution to a problem may not be efficient

Algorithmic Foundations COMP (Basics) Shortest path to go from A to B How many paths between A & B? involving 3 intermediate cities? A B 2 Number of paths = 2 Simple solution  Compute the cost of each path from A to B  Choose the cheapest one The obvious solution to a problem may not be efficient

Algorithmic Foundations COMP108 Number of paths = 2 Number of paths = (Basics) Shortest path to go from A to B How many paths between A & B? involving 3 intermediate cities? A B 3 Simple solution  Compute the cost of each path from A to B  Choose the cheapest one The obvious solution to a problem may not be efficient

Algorithmic Foundations COMP (Basics) Shortest path to go from A to B How many paths between A & B? involving 3 intermediate cities? A B Number of paths = Simple solution  Compute the cost of each path from A to B  Choose the cheapest one The obvious solution to a problem may not be efficient

Algorithmic Foundations COMP108 Number of paths = (Basics) Shortest path to go from A to B How many paths between A & B? involving 3 intermediate cities? A B 6 Number of paths = = 11 Simple solution  Compute the cost of each path from A to B  Choose the cheapest one The obvious solution to a problem may not be efficient

Algorithmic Foundations COMP (Basics) Shortest path to go from A to B How many paths between A & B? involving 5 intermediate cities? A B For large n, it’s impossible to check all paths! We need more sophisticated solutions TOO MANY!! Simple solution  Compute the cost of each path from A to B  Choose the cheapest one The obvious solution to a problem may not be efficient

Algorithmic Foundations COMP (Basics) Shortest path to go from A to B A B There is an algorithm, called Dijkstra's algorithm, that can compute this shortest path efficiently.

Algorithmic Foundations COMP (Basics) Idea of Dijkstra's algorithm A B Go one step from A, label the neighbouring cities with the cost.

Algorithmic Foundations COMP (Basics) Idea of Dijkstra's algorithm Choose city with smallest cost and go one step from there. A B

Algorithmic Foundations COMP (Basics) Idea of Dijkstra's algorithm If an alternative cheaper path is found, record this path and erase the more expensive one. A B 7 0 This path must NOT be used because we find a cheaper alternative path Then repeat & repeat …

Algorithmic Foundations COMP (Basics) Shortest path to go from A to B A B Lesson to learn: Brute force algorithm may run slowly. We need more sophisticated algorithms.

Algorithmic Foundations COMP108 How to represent algorithms … Able to tell what an algorithm is and have some understanding why we study algorithms  Able to use pseudo code to describe algorithm

Algorithmic Foundations COMP (Basics) Algorithm vs Program Algorithms are free from grammatical rules  Content is more important than form  Acceptable as long as it tells people how to perform a task Programs must follow some syntax rules  Form is important  Even if the idea is correct, it is still not acceptable if there is syntax error An algorithm is a sequence of precise and concise instructions that guide a person/computer to solve a specific problem

Algorithmic Foundations COMP (Basics) Compute the n-th power Input: a number x & a non-negative integer n Output: the n-th power of x Algorithm: 1. Set a temporary variable p to Repeat the multiplication p = p * x for n times. 3. Output the result p.

Algorithmic Foundations COMP108 Pseudo Code pseudo code: p = 1 for i = 1 to n do p = p * x output p C++: p = 1; for (i=1; i<=n; i++) p = p * x; cout << p << endl; C: p = 1; for (i=1; i<=n; i++) p = p * x; printf("%d\n", p); Java: p = 1; for (i=1; i<=n; i++) p = p * x; System.out.println(p); Pascal: p := 1; for i := 1 to n do p := p * x; writeln(p); 24 (Basics)

Algorithmic Foundations COMP (Basics) Pseudo Code iterationip start end5 suppose n=4, x=3 trace table p = 1 for i = 1 to n do p = p * x output p Another way to describe algorithm is by pseudo code similar to programming language more like English Combination of both

Algorithmic Foundations COMP (Basics) Pseudo Code: conditional Conditional statement if condition then statement if condition then statement else statement if a > 0 then b = a else b = -a output b if a < 0 then a = -a b = a output b What is computed? absolute value

Algorithmic Foundations COMP (Basics) Pseudo Code: iterative (loop) Iterative statement for var = start_value to end_value do statement while condition do statement repeat statement until condition condition to CONTINUE the loop condition to STOP the loop condition for while loop is NEGATION of condition for repeat-until loop

Algorithmic Foundations COMP (Basics) for loop i=1 i <= n? sum = sum+i No Yes Sum of 1 st n nos.: input: n sum = 0 for i = 1 to n do begin sum = sum + i end output sum i=i+1 sum=0 for var = start_value to end_value do statement the loop is executed n times

Algorithmic Foundations COMP (Basics) for loop iterationisum start end5 suppose n=4 Sum of 1 st n nos.: input: n sum = 0 for i = 1 to n do begin sum = sum + i end output sum for var = start_value to end_value do statement the loop is executed n times

Algorithmic Foundations COMP (Basics) while loop while condition do statement Sum of 1 st n numbers: input: n sum = 0 i = 1 while i <= n do begin sum = sum + i i = i + 1 end output sum do the same as for-loop in previous slides condition to CONTINUE the loop

Algorithmic Foundations COMP (Basics) Sum of all input numbers: sum = 0 while (user wants to continue) do begin ask for a number sum = sum + number end output sum while loop – example 2 execute undetermined number of times continue? ask for number sum = sum+number No Yes

Algorithmic Foundations COMP (Basics) repeat-until repeat statement until condition condition to STOP the loop Sum of all input numbers: sum = 0 repeat ask for a number sum = sum + number until (user wants to stop) output sum Stop? ask for number sum = sum+number Yes No  also execute undetermined number of times  How it differs from while-loop? execute at least once

Algorithmic Foundations COMP108 More Example 1 33 (Basics) input: m, n r = m q = 0 while r  = n do begin r = r − n q = q + 1 end output r and q end of) iteration rq suppose m=14, n=4 end of) iteration rq suppose m=14, n=5 end of ) iteration rq suppose m=14, n=7 What is computed? remainder & quotient

Algorithmic Foundations COMP108 input: m, n if m < n then swap m & n i = n while i  = 1 do begin if m%i==0 && n%i==0 then output i i = i-1 end suppose m=15, n=6 More Example 2 34 (Basics) end of ) iteration output (this iteration) i suppose m=12, n= What values are output? a%b remainder of a divided b all common factors

Algorithmic Foundations COMP108 More Example 3 input: m, n if m < n then swap m & n i = n found = false while i  = 1 && !found do begin if m%i==0 && n%i==0 then found = true else i = i-1 end output i What value is output? Questions:  what value of found makes the loop stop?  when does found change to such value? Highest Common Factor (HCF) Greatest Common Divisor (GCD) 35 (Basics)

Algorithmic Foundations COMP (Basics) Pseudo Code: Exercise Write a while-loop to 1. Find the product of all integers in interval [x, y]  e.g., if x is 2 & y is 5, then output is 2*3*4*5 = 120 assuming x and y are both integers product = ?? i = ?? while ?? do begin ?? i = ?? end output ??

Algorithmic Foundations COMP (Basics) Pseudo Code: Exercise Write a while-loop to 1. Find the product of all integers in interval [x, y] assuming x and y are both integers product = 1 i = x while i <= y do begin product = product * i i = i+1 end output product

Algorithmic Foundations COMP (Basics) Pseudo Code: Exercise 2 Write a while-loop for this: 2. Given two positive integers x and y, list all factors of x which are not factors of y  if x is 30 & y is 9, output is 2, 5, 6, 10, 15, 30 (not 1 or 3) i = ?? while ?? do begin if ?? then output ?? i = ?? end

Algorithmic Foundations COMP (Basics) Pseudo Code: Exercise 2 Write a while-loop for this: 2. Given two positive integers x and y, list all factors of x which are not factors of y i = 1 while i <= x do begin if x%i == 0 && y%i != 0 then output i i = i+1 end

Algorithmic Foundations COMP108 Challenges … Convert while-loops to for-loops & repeat-loop

Algorithmic Foundations COMP (Basics) Convert to for loops Find the product of all integers in interval [x, y] assuming x and y are both integers product = ?? for i = ?? to ?? do begin ?? end output ??

Algorithmic Foundations COMP (Basics) Convert to for loops Find the product of all integers in interval [x, y] assuming x and y are both integers product = 1 for i = x to y do begin product = product * i end output product

Algorithmic Foundations COMP (Basics) Convert to repeat loops Find the product of all integers in interval [x, y] assuming x and y are both integers product = 1 i = x repeat product = product * i i = i+1 until i > y output product What’s wrong with this? If given x is larger than y?

Algorithmic Foundations COMP (Basics) Convert to repeat loops Find the product of all integers in interval [x, y] assuming x and y are both integers product = 1 if x <= y then begin i = x repeat product = product * i i = i+1 until i > y end output product

Algorithmic Foundations COMP (Basics) Convert to for loops (2) Given two positive integers x and y, list all factors of x which are not factors of y for i = ?? to ?? do begin if ?? then ?? end

Algorithmic Foundations COMP (Basics) Convert to for loops (2) Given two positive integers x and y, list all factors of x which are not factors of y for i = 1 to x do begin if x%i == 0 && y%i != 0 then output i end

Algorithmic Foundations COMP (Basics) Convert to repeat loops (2) Given two positive integers x and y, list all factors of x which are not factors of y i = ?? repeat if ?? then ?? i = ?? until ??

Algorithmic Foundations COMP (Basics) Convert to repeat loops (2) Given two positive integers x and y, list all factors of x which are not factors of y i = 1 repeat if x%i==0 && y%i!=0 then output i i = i+1 until i > x

Algorithmic Foundations COMP108 Searching …

Algorithmic Foundations COMP108 Searching  Input: n numbers a 1, a 2, …, a n ; and a number X  Output: determine if X is in the sequence or not  Algorithm (Sequential search): 1. From i=1, compare X with a i one by one as long as i <= n. 2. Stop and report "Found!" when X = a i. 3. Repeat and report "Not Found!" when i > n. 50 (Basics)

Algorithmic Foundations COMP108 Sequential Search  six numbers 7number X   found! To find 7 51 (Basics)

Algorithmic Foundations COMP108 Sequential Search (2)   not found! To find (Basics)

Algorithmic Foundations COMP108 Sequential Search – Pseudo Code i = 1 while i <= n do begin if X == a[i] then report "Found!" and stop else i = i+1 end report "Not Found!" Challenge: Modify it to include stopping conditions in the while loop 53 (Basics)

Algorithmic Foundations COMP108 Number of comparisons? i = 1 while i <= n do begin if X == a[i] then report "Found!" & stop else i = i+1 end report "Not Found!" Best case: X is 1st no.  1 comparison Worst case: X is last OR X is not found  n comparisons 54 (Basics) How many comparisons this algorithm requires?

Algorithmic Foundations COMP108 Finding maximum / minimum... 2 nd max / min…

Algorithmic Foundations COMP (Basics) Finding max from n +ve numbers input: a[1], a[2],..., a[n] M = 0 i = 0 while (i < n) do begin i = i + 1 M = max(M, a[i]) end output M What about minimum?

Algorithmic Foundations COMP (Basics) Finding min from n +ve numbers input: a[1], a[2],..., a[n] M = a[1] i = 1 while (i < n) do begin i = i + 1 M = min(M, a[i]) end output M How many comparisons? n-1

Algorithmic Foundations COMP (Basics) Finding 1 st and 2 nd min input: a[1], a[2],..., a[n] M1 = min(a[1], a[2]) M2 = max(a[1], a[2]) i = 2 while (i < n) do begin i = i + 1 if (a[i] < M1) then M2 = M1, M1 = a[i] else if (a[i] < M2) then M2 = a[i] end output M1, M2 Two variables: M1, M2 How to update M1, M2?

Algorithmic Foundations COMP (Basics) Finding location of minimum input: a[1], a[2],..., a[n] loc = 1 // location of the min number i = 1 while (i < n) do begin i = i + 1 if (a[i] < a[loc]) then loc = i end output a[loc] end of) Iteration loca[loc]i Example a[]={50,30,40,20,10}