17 November 2015Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

Slides:



Advertisements
Similar presentations
Outline lecture Revise arrays Entering into an array
Advertisements

25 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Algorithms.
11 November 2014Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Chapter 8 Arrays and Strings
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Introduction to Computer Systems
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Chapter 8 Arrays and Strings
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
“The study of algorithms is the cornerstone of computer science.” Algorithms Fall 2011.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
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.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Computer Science: An Overview Eleventh Edition
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
13 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
3 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
Introduction to Computer Systems
24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
Introduction to Computer Systems
Introduction to Programming
Introduction to Programming
Applied Discrete Mathematics Week 2: Functions and Sequences
Outline lecture Revise arrays Entering into an array
Searching Given a collection and an element (key) to find… Output
Computer Programming BCT 1113
Algorithms and Flowcharts
Chapter 5: Algorithms Computer Science: An Overview Tenth Edition
Loop Structures and Booleans Zelle - Chapter 8
Introduction to Programming
Introduction to Programming
And now for something completely different . . .
Introduction to Programming
Computer Science: An Overview Tenth Edition
Presentation transcript:

17 November 2015Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems Autumn 2015 Week 8b: Algorithm Design

17 November 2015Brookshear, Section 5.32 Bottom Up Strategy Solve many small parts of the problem and gradually accumulate the information needed to solve the entire problem. Examples: evaluation of Boolean expressions, searching an unsorted list.

Example: Evaluation of a Boolean expression Suppose that A, B, C are Boolean variables such that A=0, B=1, C=0. Evaluate the Boolean expression (A AND B AND C) OR (A AND NOT(B)) 17 November 2015Birkbeck College3

Example: Searching an Unsorted List parameters: list L and element a printed output: True if a is in L otherwise False def search (L, a): if (Length(L) == 0): print(False) else: e = L[0] while ((e != a) and (e != last entry of L)): e = next entry of L if (e == a): print(True) else: print(False) 17 November 2015Compare Brookshear Section 5.44

17 November 2015Brookshear Section 5.35 Top Down Strategy Divide the problem into two or more simpler problems. Solve the simpler problems and then combine the answers. Examples: route finding, binary search of a sorted list.

Binary Search of a Sorted List parameters: sorted list L, element a printed output: True if a is in L, otherwise False def searchSortedList(L, a): if (Length(L) == 0): print(False) else: print(searchNonEmptySortedList(L, a)) 17 November 2015Compare Brookshear, Section 5.56

Search Non-empty Sorted List parameters: sorted non-empty list L, element a printed output: True if a is in L, otherwise False def searchNonEmptySortedList(L, a): i1 = 0; i2 = Length(L)-1 while (i2 > i1+1) j = largest integer ≤ (i1+i2)/2 if (L[j] == a): print(True); return if (a < L[j]): i2 = j else: i1 = j if (a == L[i1] or a == L[i2]): print(True) else: print(False) 17 November 2015Compare Brookshear, Section 5.57

Example of Binary Search L = {1, 2, 5, 7, 9, 11, 26, 31} a = 26 i1 = 0; i2 = Length[L]-1 = 7 i2 > i1+1 thus enter while loop j = 3 L[3] != a and not a < L[3], thus i1 = 3 If a is in L then it is in the segment {7, 9, 11, 26, 31} 17 November 2015Birkbeck College8

17 November 2015Birkbeck College9 Other Strategies Find out if somebody else has solved the problem Solve a simpler but related problem Iteration: repetition of a standard calculation Enumeration Find a data structure for the problem suited to a computer

17 November 2015Birkbeck College10 Solve a Simpler but Related Problem Print out all palindromes in the range 100 to 999 Print out all integers in the range 100 to 999 Test to see if an integer in the range 100 to 999 is a palindrome Palindromes: 161, 222, etc

Example of Iteration 17 November 2015Birkbeck College11

17 November 2015Birkbeck College12 Iteration def sqrt2(): x = 1 while ((x*x > 2.001) or (x*x < )): x = (1/x)+(x/2) return x # This strategy (update an estimate to get a # better estimate) is very widely used

17 November 2015Birkbeck College13 Enumeration List all the possible states of the problem and search for the solution Advantages: thorough, simple, good for small cases Disadvantage: very long run times if there are large numbers of cases

Example 1 Person A has the task of determining the ages of person B’s three children. B tells A that the product of the children’s ages is 36. A replies that another clue is required, so B tells A the sum of the children’s ages. Again, A replies that another clue is needed, so B tells A that the oldest child plays the piano. Then A tells B the ages of the children. How old are the children? 17 November 2015Brookshear Section 5.314

Solution by Enumeration  Possible triples: (1,1,36), (1,2,18), (1,3,12), (1,4,9), (1,6,6), (2,2,9), (2,3,6), (3,3,4)  Sums of triples: 38, 21, 16, 14, 13, 13, 11, 10  What next? 17 November 2017Brookshear, Section 5.315

17 November 2015Birkbeck College16 Ferry Problem A man wishes to ferry a wolf, a sheep and a bale of hay across a river. His boat will only hold one item at a time. The wolf cannot be left with the sheep and the sheep cannot be left with the hay. How can he ferry the three items?

17 November 2015Birkbeck College17 Data Structure State: WSHb Red: start side of river Blue and underlined italic : other side of river Example: WSHb There are 16 states of which 10 are allowed

17 November 2015Birkbeck College18 Solution HSWb

17 November 2015Birkbeck College19 Question 11 from 2003 Exam Write an algorithm for calculation of tax for a yearly salary. The tax is zero when the salary is £3000 or less. It is 10% for the next salary band from £3001 to £8000. It is 20% for the part from £8001 to £20000, and it is 40% for any part of the salary over £20000.

Example 2 Design an algorithm that tests two one dimensional arrays of the same length to see if the two arrays contain the same entries in the same order. 17 November 2017Birkbeck College20

17 November 2015Birkbeck College21 Example 3 Design an algorithm that lists all possible rearrangements of the symbols in a string of three distinct characters.

17 November 2015Birkbeck College22 Example 4 Design an algorithm that when given an arrangement of the digits 0,1,2,3,4,5,6,7,8,9 rearranges the digits so that the new arrangement represents the next larger value that can be represented by these digits (or reports that no such arrangement exists if no rearrangement produces a larger value). Thus would produce