COMP108 Algorithmic Foundations Basics

Slides:



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

Programming Types of Testing.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
CS107 Introduction to Computer Science Lecture 2.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Adapted from slides by Marie desJardins
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
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)
Chapter 1 Introduction Chapter 1 Introduction 1 st Semester 2015 CSC 1101 Computer Programming-1.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CS101 Computer Programming I Chapter 4 Extra Examples.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong
Overview Go over parts of quiz? Another iteration structure for loop.
COMP Loop Statements Yi Hong May 21, 2015.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
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
Algorithms.
Introduction to Algorithms
ALGORITHMS AND FLOWCHARTS
CS1010 Programming Methodology
REPETITION CONTROL STRUCTURE
CS1022 Computer Programming & Principles
COMP108 Algorithmic Foundations Algorithm efficiency
GC101 Introduction to computers and programs
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 5: Control Structure
Introduction To Flowcharting
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Lecture 07 More Repetition Richard Gesick.
Algorithm and Ambiguity
Computer Science 101 While Statement.
LOOPS.
Lecture 4B More Repetition Richard Gesick
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.
Repetition and Loop Statements
Designing and Debugging Batch and Interactive COBOL Programs
Learning to Program in Python
Program Design Introduction to Computer Programming By:
Programming Fundamentals (750113) Ch1. Problem Solving
Java Programming Loops
Algorithm Discovery and Design
Loops CIS 40 – Introduction to Programming in Python
Problem Solving Skill Area 305.1
Algorithm and Ambiguity
Chapter 5: Control Structure
Let’s all Repeat Together
Programming Fundamentals (750113) Ch1. Problem Solving
Introduction to Algorithms
Boolean Expressions to Make Comparisons
ICT Gaming Lesson 2.
Programming Fundamentals (750113) Ch1. Problem Solving
Loops.
Application: Algorithms
Application: Algorithms
COMP108 Algorithmic Foundations Basics
Developing a Program.
Basic Concepts of Algorithm
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Using C++ Arithmetic Operators and Control Structures
Chapter 1 Introduction to Programming
WJEC GCSE Computer Science
Advanced Analysis of Algorithms
Presentation transcript:

COMP108 Algorithmic Foundations Basics Prudence Wong http://www.csc.liv.ac.uk/~pwong/teaching/comp108/201617

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

Module Information Professor Prudence Wong Demonstrators References Rm 3.18 Ashton Building, pwong@liverpool.ac.uk office hours: Tue 10-11am Demonstrators Mr Thomas Carroll, Mr Reino Niskanen 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

Module Information (2) Teaching, Assessments and Help Tutorials/Labs 36 lectures, 11 tutorials 2 assessments (20%), 1 written exam (80%) Office hours, email Tutorials/Labs Location : Lecture Rooms (theoretical) or Lab (practical) Week 2: Theoretical – Lecture Rooms

Module Information (3) Each assessment has two components Assessment 1 Tutorial participation (25%) Class Test (75%) Assessment 1 Tutorials 1 – 6 (Weeks 2-7) Class Test 1: Week 8, Thu 23rd Mar Assessment 2 Tutorials 7 – 11 (Weeks 8-12) Class Test 2: Week 12, Thu 11th May

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)

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?

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

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?) Algorithm Input Output

Why do we study algorithms? The obvious solution to a problem may not be efficient Given a map of n cities & traveling cost between them. What is the cheapest way to go from city A to city B? B Simple solution Compute the cost of each path from A to B Choose the cheapest one 10 8 5 7 9 6 11 2 12 3 4 1 A

Shortest path to go from A to B The obvious solution to a problem may not be efficient 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 B 2 A

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

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

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

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

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

Shortest path to go from A to B There is an algorithm, called Dijkstra's algorithm, that can compute this shortest path efficiently. B A

Idea of Dijkstra's algorithm Go one step from A, label the neighbouring cities with the cost. B 10 10 8 5 7 9 6 11 2 12 3 4 1 5 A 7

Idea of Dijkstra's algorithm Choose city with smallest cost and go one step from there. B 10 10 8 5 7 9 6 11 2 12 3 4 1 5 A 7

Idea of Dijkstra's algorithm This path must NOT be used because we find a cheaper alternative path If an alternative cheaper path is found, record this path and erase the more expensive one. B 10 8 10 8 5 7 9 6 11 2 12 3 4 1 Then repeat & repeat … 5 A 7

Shortest path to go from A to B Lesson to learn: Brute force algorithm may run slowly. We need more sophisticated algorithms. B A

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

Algorithm vs Program Algorithms are free from grammatical rules An algorithm is a sequence of precise and concise instructions that guide a person/computer to solve a specific problem 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

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

Pseudo Code C: p = 1; for (i=1; i<=n; i++) pseudo code: p = p * x; printf("%d\n", p); 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; Pascal: p := 1; for i := 1 to n do p := p * x; writeln(p); Java: p = 1; for (i=1; i<=n; i++) p = p * x; System.out.println(p);

Pseudo Code Another way to describe algorithm is by pseudo code p = 1 for i = 1 to n do p = p * x output p similar to programming language Combination of both more like English

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

Pseudo Code: iterative (loop) var automatically increased by 1 after each iteration Iterative statement for var = start_value to end_value do statement while condition do condition to CONTINUE the loop

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

for loop iteration i sum start 1 2 3 6 4 10 end 5 for var = start_value to end_value do statement suppose n=4 iteration i sum start 1 2 3 6 4 10 end 5 Sum of 1st n nos.: input: n sum = 0 for i = 1 to n do begin sum = sum + i end output sum the loop is executed n times trace table

condition to CONTINUE the loop while loop condition to CONTINUE the loop while condition do statement Sum of 1st 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 It requires to increment i explicitly

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

More Example 1 r q 14 1 10 2 6 3 input: x, y r = x q = 0 suppose x=14, y=4 (@ end of) iteration r q 14 1 10 2 6 3 input: x, y r = x q = 0 while r = y do begin r = r − y q = q + 1 end output r and q suppose x=14, y=5 (@ end of) iteration r q 1 9 2 4 suppose x=14, y=7 What is computed? (@ end of ) iteration r q 1 7 2 remainder & quotient

More Example 1 - Note if condition is r >= ?? input: x, y r = x q = 0 while r = y do begin r = r − y q = q + 1 end output r and q if condition is r >= ?? then in the loop we need to decrease r if condition is r <= ?? then in the loop we need to increase r

More Example 2 a%b remainder of a divided b input: x, y i = 1 suppose x=12, y=4 (@ end of ) iteration output (this iteration) i 1 2 3 4 5 a%b remainder of a divided b input: x, y i = 1 while i <= y do begin if x%i==0 && y%i==0 then output i i = i+1 end suppose x=15, y=6 1 2 3 4 5 6 7 What values are output? all common factors

Highest Common Factor (HCF) Greatest Common Divisor (GCD) More Example 3 What value is output? input: x, y i = y found = false while i >= 1 && !found do begin if x%i==0 && y%i==0 then found = true else i = i-1 end output i Highest Common Factor (HCF) Greatest Common Divisor (GCD) Questions: what value of found makes the loop stop? when does found change to such value?

Developing pseudo code assuming x and y are both integers Write a while-loop to Find the product of all integers in interval [x, y] Examples x y calculation product 2 5 2 x 3 x 4 x 5 120 10 12 10 x 11 x 12 1320 -4 -2 -4 x -3 x -2 -24 -6 -5 -6 x -5 30 1 -2 x -1 x 0 x 1

Developing pseudo code assuming x and y are both integers Write a while-loop to Find the product of all integers in interval [x, y] product = ?? i = ?? while ?? do begin ?? end output ??

Find the product of all integers in interval [x, y] What variables do we need? one to store answer, call it product one to iterate from x to y, call it i product = ?? i = ?? What to do in loop? update product multiply it by i product = product * i Loop condition? continue as long as i is at most y while i <= y initial value of i? how i changes in loop? i start from x; i increase by 1 in loop product = ?? i = x while ?? do begin ?? i = i + 1 end initial value of product? multiplication identity product = 1

Developing pseudo code Find the product of all integers in interval [x, y] product = 1 i = x while i <= y do begin product = product * i i = i+1 end output product

Common Mistakes product = 0 while x <= y do product * x i = x while i <= y do begin product = product * i i = i+1 end output product answer becomes 0 while x <= y do infinite loop because x does not get changed in the loop product * x incorrect! will multiply x for y-x+1 times, i.e., calculate xy-x+1 forget i=i+1 infinite loop because i does not get changed in the loop

Pseudo Code: Exercise Write a while-loop for this: Given two positive integers x and y, list all factors of x which are not factors of y Examples x y factors of x output 6 3 1, 2, 3, 6 2, 6 30 9 1, 2, 3, 5, 6, 10, 15, 30 2, 5, 6, 10, 15, 30 1, 3 -

Pseudo Code: Exercise Write a while-loop for this: Given two positive integers x and y, list all factors of x which are not factors of y i = ?? while ?? do begin if ?? then output ?? end

Pseudo Code: Exercise Write a while-loop for this: Given two positive integers x and y, list all factors of x which are not factors of y Two subproblems: find all factors of x if it is not a factor of y, output it

Find all factors of x factor of x must be between 1 and x variable i to iterate from 1 to x i = 1 while i <= x do begin … i = i + 1 end Therefore: i = 1 while i <= x do begin if x%i==0 then output i i = i + 1 end if i is divisible by x, then it is a factor of x remainder of i divided by x is 0 if x%i==0 then output i

2. Factors of x but not factor of y 1. All factors of x i = 1 while i <= x do begin if x%i==0 then output i i = i + 1 end 3. Finally, i = 1 while i <= x do begin if x%i==0 && y%i!=0 then output i i = i + 1 end 2. Factors of x but not factor of y remainder of i divided by x is 0 remainder of i divided by y is not 0 if x%i==0 && y%i!=0 then output i