1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 9: Algorithms and Pseudo-code.

Slides:



Advertisements
Similar presentations
Algorithms.
Advertisements

College of Information Technology & Design
MATH 224 – Discrete Mathematics
Algorithm and Complexity Analysis
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Introduction to Analysis of Algorithms
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Complexity Analysis (Part I)
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Analysis of Algorithms (Chapter 4)
CSE 830: Design and Theory of Algorithms
Program Design and Development
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Chapter 1 Program Design
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Design and Analysis of Algorithms
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Simple Program Design Third Edition A Step-by-Step Approach
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
CSCE 3110 Data Structures & Algorithm Analysis Algorithm Analysis I Reading: Weiss, chap.2.
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Fundamentals of Algorithms MCS - 2 Lecture # 1
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Problem Solving using the Science of Computing MSE 2400 EaLiCaRA Spring 2015 Dr. Tom Way.
Chapter 1 Program design Objectives To describe the steps in the program development process To introduce the current program design methodology To introduce.
Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester,
Chapter 5 Algorithms. © 2005 Pearson Addison-Wesley. All rights reserved 5-2 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation.
Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.
Computer Science: An Overview Eleventh Edition
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
Algorithm Analysis (Big O)
DATA STRUCTURES (CS212D) Overview & Review Instructor Information 2  Instructor Information:  Dr. Radwa El Shawi  Room: 
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
CPS Today’s topics l Algorithms and pseudocode l Notes adapted from Marti Hearst at UC Berkeley and David Smith at Georgia Tech Upcoming ä Beginning.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
Algorithms and Problem Solving
Data Structures and Algorithms
Lecture 3 of Computer Science II
Data Structures (CS212D) Overview & Review.
PROBLEM SOLVING SKILLS
Objective of This Course
Chapter 5: Algorithms Computer Science: An Overview Tenth Edition
Data Structures (CS212D) Overview & Review.
Algorithms and Problem Solving
Revision of C++.
Analysis of Algorithms
Computer Science: An Overview Tenth Edition
Presentation transcript:

1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 9: Algorithms and Pseudo-code

2 The central role of algorithms in computer science From Brookshear; Copyright 2003 Pearson Education

3 What is an algorithm? The idea behind the computer program Stays the same independent of –Which kind of hardware it is running on –Which programming language it is written in Solves a well-specified problem in a general way Is specified by –Describing the set of instances (input) it must work on –Describing the desired properties of the output Adapted from

4 Brookshear: The definition of an algorithm From Brookshear; Copyright 2003 Pearson Education The terminating part comes from theoretical considerations.

5 Important Properties of Algorithms Correct –always returns the desired output for all legal instances of the problem. Unambiguous Precise Efficient –Can be measured in terms of Time Space –Time tends to be more important Adapted from

6 Algorithms Hand-waving not allowed! Specifying algorithms requires you to say what is really involved in making it work. Example: –How does a computer work? –Hand-wave: zeros & ones –Real answer: see first three weeks of class. You learn to know when you don’t know –“I know nothing except the fact of my ignorance.” –Socrates, from Diogenes Laertius, Lives of Eminent Philosophers

7 Expressing Algorithms English description Pseudo-code High-level programming language More precise More easily expressed Adapted from

8 Pseudo-code A shorthand for specifying algorithms Leaves out the implementation details Leaves in the essence of the algorithm < Code corrected from Brookshear; Copyright 2003 Pearson Education

9 Sequential search algorithm in pseudo-code From Brookshear; Copyright 2003 Pearson Education

10 Origami as Algorithm: Folding a bird from a square piece of paper. From Brookshear; Copyright 2003 Pearson Education

11 From Brookshear; Copyright 2003 Pearson Education Origami as Algorithm: Folding a bird from a square piece of paper.

12 Origami primitives Syntax vs. Semantics From Brookshear; Copyright 2003 Pearson Education

13 Origami primitives Syntax vs. Semantics From Brookshear; Copyright 2003 Pearson Education

14 Primitive Operations Assign a value to a variable Call a method Arithmetic operation Comparing two numbers Indexing into an array Following an object reference Returning from a method

15 The RAM Model Random Access Machine (not Memory) An idealized notion of how the computer works –Each "simple" operation (+, -, =, if) takes exactly 1 step. –Each memory access takes exactly 1 step –Loops and method calls are not simple operations, but depend upon the size of the data and the contents of the method. Measure the run time of an algorithm by counting the number of steps. Adapted from

16 Counting Primitive Operations Algorithm ArrayMax(A,n) Input: An array A storing N integers Output: The maximum element in A. currentMax A[0] for i 1 to n-1 do if currentMax < A[i] then currentMax A[i] return currentMax Adapted from Goodrich & Tamassia

17 Counting Primitive Operations Algorithm ArrayMax(A,n) Input: An array A storing N integers Output: The maximum element in A. currentMax A[0] for i 1 to n-1 do if currentMax < A[i] then currentMax A[i] return currentMax 2 steps + 1 to initialize i 2 steps 1 step 2 step each time (compare i to n, inc i) n-1 times How often done?? It depends on the order the numbers appear in in A[] Between 4(n-1) and 6(n-1) in the loop Adapted from Goodrich & Tamassia

18 Algorithm Complexity Worst Case Complexity: –the function defined by the maximum number of steps taken on any instance of size n Best Case Complexity: –the function defined by the minimum number of steps taken on any instance of size n Average Case Complexity: –the function defined by the average number of steps taken on any instance of size n Adapted from

19 Problem Solving Polya’s Advice –Understand the problem –Devise a plan for solving the problem –Carry out the plan –Evaluate the solution for accuracy Reality –They are intermixed –Sometimes don’t know the full problem statement until a solution is found!

20 Problem Solving Strategies Try to work the problem backwards –Reverse-engineer –Once you know it can be done, it is much easier to do –What are some examples? Stepwise Refinement –Break the problem into several sub-problems –Solve each subproblem separately –Produces a modular structure Look for a related problem that has been solved before –Java design patterns

21 Example of Stepwise Refinement Spiderman Peter Parker’s goal: Make Mary Jane fall in love with him To accomplish this goal: –Get a cool car –To accomplish this goal: Get $3000 To accomplish this goal: –Appear in a wrestling match … Each goal requires completing just one subgoal

22 Example of Stepwise Refinement Star Wars Episode IV Luke Skywalker’s goal: Make Princess Leia fall in love with him (they weren’t siblings yet) To accomplish this goal: –Rescue her from the death star –To accomplish this goal: 1.Land on Death Star 2.Remove her from her Prison Cell 3.Disable the Tractor Beam 4.Get her onto the Millennium Falcon –To accomplish subgoal (2) Obtain Storm Trooper uniforms –Have Wookie pose as arrested wild animal Find Location of Cell –Have R2D2 communicate coordinates –To accomplish subgoal (3) Have last living Jedi walk across catwalks –To accomplish subgoal (4) Run down hall Survive in garbage shoot –Fight garbage monster –Have R2D2 stop compaction …

23 Practice Stepwise Refinement + Pseudo-code Recipe for Strawberry Rhubarb Pie Define Pseudo-code at different levels of description

24 Pair Programming Two people programming together Both are engaged Only one keyboard and mouse NOT working separately and then joining results. Advantages? –Prevent bugs –Better code –Mutual learning –Almost as fast as two independent programmers