Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

Problem Solving and Algorithm Design
Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
PRINCIPLES OF COMPUTATION PROBLEM SOLVING. WHAT IS COMPUTATION & COMPUTING  Computation is a process of performing computations or calculations. Just.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
Chapter 2: Algorithm Discovery and Design
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Program Design and Development
Lecture Notes 1/21/04 Program Design & Intro to Algorithms.
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Robo David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to the engineering.
© Copyright 2011 John Wiley & Sons, Inc.
Programming Fundamentals (750113) Ch1. Problem Solving
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Chapter 3 Planning Your Solution
CHANGING A FLAT TIRE WHY ME! I’M GOING TO BE LATE TO SCHOOL.
Adapted from slides by Marie desJardins
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Homework and Motivation
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
Simple Program Design Third Edition A Step-by-Step Approach
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
CS 1308 Computer Literacy and The Internet Software.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Algorithms CS139 – Aug 30, Problem Solving Your roommate, who is taking CS139, is in a panic. He is worried that he might lose his financial aid.
Problem Solving using the Science of Computing MSE 2400 EaLiCaRA Spring 2015 Dr. Tom Way.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Chapter 10 Algorithmic Thinking. Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Algorithms and Pseudocode
Presented by: Timothy Wheeler December 4 th, 2008 ENGL393, Mrs. Harris.
HOW TO CHANGE A TIRE. Take out the following items for you vehicle: - Spare Tire - Jack - Tire Iron - Lug Nut Key(if applicable) Spare Tire Jack Tire.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Introduction to Computing Systems and Programming Programming.
AS Computer Studies Algorithms & Problem Solving 1 & 2.
Algorithms and Flowcharts
 A car Jack  A wrench  A flat surface Put the car into park and apply the parking brake.
6 Things Every Driver Should Know. 1) How to Change a Tire 1 st : Once you have a flat, stop at a SAFE place to change your tire. 2 ND : Grab the spare,
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Pseudocode Upsorn Praphamontripong CS 1110 Introduction to Programming
Algorithm and Ambiguity
Program Design Introduction to Computer Programming By:
An Introduction to Control Structures
Problem Solving and Algorithm Design
Problem Solving and Algorithm Design
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Programming We have seen various examples of programming languages
Algorithm and Ambiguity
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
An Introduction to Control Structures
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Presentation transcript:

Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to algorithms

IMPORTANT… Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self- explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

BUILDING A TABLE

Building a flat-pack table

Secure each of the four flanges to a corner of the table top using 3 screws each. Next, screw a table leg into each flange. 1.For each corner of the table top secure the flange to the corner using 3 screws 2.For each flange screw a table leg into the flange Lay the table top upside-down on the ground Turn the completed table the right way up!

Building a flat-pack table position a flange on the corner and secure it with 3 screws, then screw the leg into the flange. Attach a table leg to each corner of the table top. For each corner of the table top attach a table leg to corner. For each corner of the table top 1.position a flange on the corner 2.secure flange in place with 3 screws 3.screw a leg into the flange Lay the table top upside-down on the ground Turn the completed table the right way up!

Building a flat-pack table Could you tell the difference? 1.For each corner of the table top secure the flange to the corner using 3 screws 2.For each flange screw a table leg into the flange For each corner of the table top 1.position a flange on the corner 2.secure flange in place with 3 screws 3.screw a leg into the flange

CHANGING A FLAT TYRE

Your turn… Explain how to change a flat tyre. Provide an “algorithm” (set of instructions or a procedure) that solves the problem.

Changing a flat-tyre Sample solution_1 1.Ensure the car is parked safely and cannot roll. 2.Remove wheel with flat tyre 3.Replace it with the spare wheel 4.Drive off Sample solution_2 1.Ensure the car is parked safely and cannot roll. 2.Take the jack, spanner and spare wheel from the trunk. 3.Slacken the wheel nuts on the wheel with the flat tyre 4.Jack the car up so the wheel with the flat tyre can be removed. 5.Remove all the nuts from wheel with the flat tyre 6.Take the wheel off and place it in the trunk 7.Put the spare wheel on in place of the one with the flat tyre 8.Put the nuts on and tighten as much as possible 9.Lower the car off the jack 10.Fully tighten the nuts on the new wheel 11.Put the jack and spanner back in the trunk 12.Drive off Which one?

Changing a flat-tyre Sample solution_3 1.Ensure the car is parked safely and cannot roll. 2.Remove wheel with flat tyre 1.Take the jack, spanner and spare wheel from the trunk. 2.Slacken the wheel nuts on the wheel with the flat tyre 3.Jack the car up so the wheel with the flat tyre can be removed. 4.Remove all the nuts from wheel with the flat tyre 5.Take the wheel off and place it in the trunk 3.Replace it with the spare wheel 1.Put the spare wheel on in place of the one with the flat tyre 2.Put the nuts on and tighten as much as possible 3.Lower the car off the jack 4.Fully tighten the nuts on the new wheel 5.Put the jack and spanner back in the trunk 4.Drive off The best of both solutions?

Changing a flat-tyre Sample solution_4 1.Ensure the car is parked safely and cannot roll. 2.Remove wheel with flat tyre 3.Replace it with the spare wheel 4.Drive off 2.Remove wheel with flat tyre 1.Take the jack, spanner and spare wheel from the trunk. 2.Slacken the wheel nuts on the wheel with the flat tyre 3.Jack the car up so the wheel with the flat tyre can be removed. 4.Remove all the nuts from wheel with the flat tyre 5.Take the wheel off and place it in the trunk 3.Replace it with the spare wheel 1.Put the spare wheel on in place of the one with the flat tyre 2.Put the nuts on and tighten as much as possible 3.Lower the car off the jack 4.Fully tighten the nuts on the new wheel 5.Put the jack and spanner back in the trunk The best?

SUPERMARKET SHOPPING

Supermarket Shopping Given a shopping list, go to the supermarket, buy all the items on it and take them home. List checkout exitentrance shelves 1.With your shopping list, go to the market 2.From the market, buy the items on your shopping list. 3.Return home with the items you purchased.

Supermarket Shopping 1.Get a shopping cart 2.For each shelf in the market if the item on the shelf is on the list then take the item from the shelf add the item to the shopping cart 3.Go to checkout and pay for items in trolley 4.Leave market with items purchased. Collect a shopping cart on the way into the market. Walk around the market looking at the items on each shelf. If the item on the shelf is on your shopping list, take it from the shelf and add it to the items in your shopping cart. After visiting all the shelves, go to the checkout and pay for the items in your trolley. Leave the empty trolley and exit the market with the items you purchased. exitentrance

Supermarket Shopping exitentrance Collect a shopping cart on the way into the market. For each item on your shopping list, go to the shelf containing the item, take it from the shelf and add it to the items in your shopping cart. After completing your shopping list, go to the checkout and pay for the items in your trolley. Leave the empty trolley and exit the market with the items you purchased. 1.Get a shopping cart 2.For each item on the shopping list go to the shelf with the item on take the item from the shelf add the item to the shopping cart 3.Go to checkout and pay for items in cart 4.Leave market with items purchased. if the item is in the market then

Supermarket Shopping Get a shopping cart For each shelf in the market if the item on the shelf is on the list then take the item from the shelf add the item to the shopping cart Go to checkout and pay for items in trolley Get a shopping cart For each item on the shopping list if the item is in the market then go to the shelf with the item on take the item from the shelf add the item to the shopping cart Go to checkout and pay for items in cart Possible Problems? Which is best?

KEY IDEAS…

Terminology Algorithm Sequence of steps to solve a problem Natural language English, Turkish, French, German, … Computer code/program Instructions a computer can “understand” Pseudocode “False” code, semi-structured English Trace program or algorithm you follow the steps (instead of machine!)

Tips… Be consistent in naming things Rewrite Natural language into “pseudocode” forms (take care with numbering, indentation) High-level solution first, then, after checking correctness and completeness, solve each piece separately Consider alternative solutions (may prefer some for other reasons)

Algorithms An algorithm is a procedure/method comprising the steps to solve a given problem. It must be understandable, effective, correct, & must stop Algorithms comprise: Sequence, decision, repetition Do … and next do … then do... First do … Finally do … if … then do... if … then do … else/otherwise do … for all/each/every do... while … do … repeat … until …

Design - The role of Abstraction Break the task into several pieces/sub-tasks Divide & conquer strategy How many pieces should we have? Ask: “if each of these sub-tasks can be done and are combined as specified, will the main task be correctly solved?” If not, change the sub-tasks and/or the way they are combined until they do solve the problem. Note that we do not need to know how to solve a particular sub-task (e.g. “attach leg to corner of table”, “go to the shelf…”). We are assuming we can eventually figure out how to do it (but if we can’t, we will need to revise our solution!) Only when we are sure the pieces correctly solve the problem, should we go ahead and try to solve each of them. Note they are now independent problems and can be solved in any order (or even simultaneously by teams).

HOMEWORK Your turn…

Homework Our supermarket shopping algorithm says “Go to checkout and pay for items in cart” Develop an algorithm that the cashier (system) could follow to solve this. Assume a low-tech solution in which the cashier has a calculator and a written price list (that gives the price for each item in the market).

Other examples.. Open the book at the first page while there are unread pages in the book read the page turn to the next page * Open the book at the first page repeat read the page turn to the next page * until there are no unread pages. for each page in the book from first to last read the page turn to the next page ** if it’s raining then take an umbrella else take your sunglasses

Bad solution forms…? 1. find item on list 2. add item to cart 3. repeat for all items on list Repeat what exactly? steps 1, 2, 3? only steps 1 & 2? then why 3? 1. find item on list 2. add item to cart 3. if more items then go to 1. Better, but “goto” is no longer considered a suitable form.

Step-by-step… Who says flat-pack instructions are difficult to follow?