CompSci 001 7.1 Today’s topics Designing and Implementing Algorithms Problem solving Pseudocode Java Syntax and Grammars Upcoming More Java Acknowledgements.

Slides:



Advertisements
Similar presentations
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
Advertisements

1 CS101 Introduction to Computing Lecture 17 Algorithms II.
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Chapter 2: Algorithm Discovery and Design
CSE 830: Design and Theory of Algorithms
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Introduction to a Programming Environment
Chapter 1 Program Design
Introduction Algorithms and Programming. Computer Programming  A form of problem solving  Or, more accurately, a way to solve problems  What we will.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Algebra Problems… Solutions
1 Programming Languages Examples: C, Java, HTML, Haskell, Prolog, SAS Also known as formal languages Completely described and rigidly governed by formal.
1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Introducing Java.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
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
CompSci Today’s topics Networks and the Internet Problem Solving Pseudocode Upcoming ä Programming language syntax ä Python Reading Brookshear,
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Algorithms and Programming
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
The Algorithmic Model. What is Computer Science What is Programming Algorithms –Definition –Properties of Good Algorithms –Describing Algorithms –Examples.
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Robert Crawford, MBA West Middle School.  Explain how the binary system is used by computers.  Describe how software is written and translated  Summarize.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Introduction to programming in the Java programming language.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
CPS Today’s topics Algorithms Java Syntax and Grammars Upcoming More Java Reading Great Ideas, Chapter 2.
Cornell System of Note-Taking The Cornell Method of note making is a 3 part system of making notes. Making notes, as distinguished from taking notes, is.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
CPS Topics l Networks revisited l Java intro l Decision Trees l Problem solving and computing with numbers l Upcoming ä Numeric computation Course.
Java Buzzwords.
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
Today’s topics Java Syntax and Grammars Sample Programs Upcoming More Java Reading Great Ideas, Chapter 2.
Chapter 10 Algorithmic Thinking. Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Algorithms and Pseudocode
CompSci Today’s topics Problem Solving Pseudocode Notes from Zachary Dodds’ CS 5 course at Harvey Mudd Upcoming ä More Python Reading Brookshear,
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
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.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
Advanced Algorithms Analysis and Design
Introduction to Algorithms
Component 1.6.
Software Development.
Fermi Questions Enrico Fermi.
Objective of This Course
Algorithms and Problem Solving
Introduction to Algorithms
Tonga Institute of Higher Education IT 141: Information Systems
Algorithms vs. Programming
Tonga Institute of Higher Education IT 141: Information Systems
Software Development Techniques
Algorithms vs. Programming
Presentation transcript:

CompSci Today’s topics Designing and Implementing Algorithms Problem solving Pseudocode Java Syntax and Grammars Upcoming More Java Acknowledgements Marti Hearst, UC Berkeley David Smith, Georgia tech Reading Computer Science, Chapter 5 Great Ideas, Chapter 2 Back of the Envelope Calculations, excerpt from Jon Bentley’s Programming Pearls

CompSci Problem Solving Programming is a strenuous exercise in problem solving G. Polya. How to Solve It, 2 nd ed., Princeton University Press, l Understand the problem ä What are its parts? unknown, data, condition ä Does the problem make sense? Is it feasible? ä Think about the problem, get a sense of what it needs l Make a plan ä Find the connection between givens and result ä What kind of problem is it? Is it familiar? ä Think about generalizations, specializations, variants l Carry out the plan ä Check each step l Examine the result ä Does it make sense?

CompSci Back of the envelope calculations Jon Bentley. Programming Pearls. 2 nd edition. A-W, l Engineering technique to approximate and check answers ä Two answers are better than one ä Quick checks ä Rules of thumb ä Practice l Ad claims that salesperson drove 100,000 miles in a year. True? l Newspaper article states that a United States quarter dollar coin has “an average life of 30 years.” How can you check that claim?

CompSci Why “back of the envelope” estimates? l Often need to make rapid estimates ä to eliminate candidate solutions ä establish feasibility ä sketch out potential trade-offs l Most remember key numbers related to their field, not every detail l Hence we need to estimate ä which numbers are important ä values of numbers needed ä how to perform the calculation l Emphasis is on “order of magnitude” estimates ä to nearest factor of 10 (or 2)

CompSci Orders of Magnitude l How far away is home? Is it more like 1, or 10, or 100 miles? ä Probably do not know exactly ä Is it approximately "a couple", or "a few", or "a lot” ä Estimate based on powers rather than multiples of 10 l How tall is your dorm? More like 1, 10, 100, 1000 feet? ä 1 foot tall is like a doll house, so that’s out ä What do we know that is about 10 feet big? Hmm... People ä If building is a couple of people high, 10 sounds good. ä But that means 1000, would be 100 people high, so that’s out ä So 10 or 100 depending on how many people tall the building is l Use orders of magnitude as brackets to find reasonable range

CompSci Example: How many piano tuners in NYC l Approximately how many people are in New York City? ä 10,000,000 l Does every individual own a piano? ä No l Reasonable to assert “individuals do not own pianos; families do”? ä Yes l About how many families are there in a city of 10 million people? ä Perhaps there are 2,000,000 families l Does every family own a piano? ä No l Perhaps one out of every five does ä That would mean there are about 400,000 pianos in NYC

CompSci Example: Piano Tuners continued l How many piano tuners are needed for 400,000 pianos? ä Some people never get around to tuning their piano ä Some people tune their piano every month ä Assume "on the average" every piano gets tuned once a year, then there are 400,000 every year l How many piano tunings can one piano tuner do? ä Assume that average piano tuner can tune four pianos a day ä Assume that there are 200 working days per year ä That means every tuner can tune about 800 pianos per year l How many piano tuners are needed in NYC? ä Number of tuners is approximately 400,000/800 or 500

CompSci Example: Piano Tuners summary l “Back of the Envelope” estimates have ä Formulas: provide roadmap to upcoming calculations ä Estimates: brief justification of approximations in formula ä Calculations: estimates and known facts are use in formula l Piano Tuner example ä Formula: # tuners = # pianos x # repairs / # repairs per day x # days ä Estimates # pianos ~= 400,000 (20% of 2,000,000 families own pianos) # repairs ~= 1 per piano (some many, some none) # repairs per day ~= 4 #working days ~= 200 (5 x 50 – vacation, sickness) ä Calculation # tuners ~= (400,000 x 1) / (4 x 200) = 500

CompSci Estimation General Principles l Recall Einstein's famous advice ä Everything should be made as simple as possible, but no simpler Do not worry about constant factors of 2, π, etc. ä Round to “easy” number or nearest order of magnitude l Guess numbers you do not know ä Within bounds of common sense (accuracy increases with experience) l Adjust geometry, etc., to suit you ä Assume a cow is spherical if it helps l Extrapolate from what you do know ä Use ratios to assume unknown value is similar to known quantity l Apply a ‘plausibility’ filter ä If answer seems unbelievable, it probably is ä Can usually set range of reasonable values that indicates major mistake (e.g., speed cannot be faster than light!)

CompSci Central role of algorithms in CS

CompSci What’s wrong with this algorithm? (From back of shampoo bottle) Directions: Wet Hair Apply a small amount of shampoo Lather Rinse Repeat

CompSci Properties of good algorithms l Good algorithms must be ä Correct ä Complete ä Precise ä Unambiguous l And should be ä Efficient ä Simple ä Contain levels of abstraction

CompSci Algorithms l Hand-waving not allowed! l Specifying algorithms requires you to say what is really involved in making it work. l Example: ä How does a computer work? ä Hand-wave: zeros & ones ä Real answer: see later part of class. l 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

CompSci Describing Algorithms Pictures Natural language (English) Pseudo-code Specific high-level programming language More easily expressed More precise

CompSci Pseudocode l A shorthand for specifying algorithms l Leaves out the implementation details l Leaves in the essence of the algorithm l What does this algorithm do? l How many times does it print Hello? <

CompSci Sequential search

CompSci Picking courses 1.Make a list of courses you want to register for, in order of priority 2.Start with empty schedule. Number of courses = 0. 3.Choose highest priority class on list. 4.If the chosen class is not full and its class time does not conflict with classes already scheduled, then register for the class (2 steps): 1.Add the class to the schedule 2.Increment the number of classes scheduled 5.Cross that class off of your list. 6.Repeat steps 3 through 5 until the number of classes scheduled is >= 4, or until all classes have been crossed out. 7.Stop.

CompSci Flowcharts End Begin Make list of classes you want to take Num Classes = 0 Choose highest priority class on list Add the class to your schedule. Increment Num Classes. Cross the class off your list. yes no Is this class full? Is there a time conflict? Num Classes >= 4? More classes on list? yes no

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

CompSci Components of Computing Algorithms Any computing algorithm will have AT MOST five kinds of components: Data structures to hold data Instructions change data values Conditional expressions to make decisions Control structures to act on decisions Modules to make the algorithm manageable by abstraction, i.e., grouping related components

CompSci Java! l Java is a buzzword-enabled language l From Sun (the developers of Java), “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.” l What do all of those terms mean?

CompSci l A programming language ä A vocabulary and set of syntactical (grammatical) rules for instructing a computer to perform specific tasks ä You can do most anything in any programming language ä A particular language encourages one to do things in a certain way l A Question for the course: What makes a good language? “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l Based on popular languages called C and C++ l C: old, pretty bare bones language l C++: newer, more complicated language l Start from C and add some of C++’s more useful features ä From Gosling, the creator, “Java omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefits.” l Question: Is Java really all that simple? “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l The object-oriented paradigm ä Problems and their solutions are packaged in terms of classes ä The information in a class is the data ä The functionality in a class is the method ä A class provides the framework for building objects l Object-oriented programming (OOP) allows pieces of programs to be used in other contexts more easily “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l A distributed system is one where multiple separate computer systems are involved ä Electronic card catalogs ä The web l Java was designed for the web l Question: What are examples of a distributed task in your lives? “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l Java a high-level language l High-level languages must be translated to a computer’s native tongue, machine language l Interpreted high-level languages are translated to an intermediate form and then converted to machine language and run l Why? l We’ll learn more about this later “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l Programs will have errors, but a good program degrades reasonably l A robust program may not do exactly what it is supposed to do, but it should not bring down other unrelated programs down with it l Question: Give me an example of a non-robust program you have seen? “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l Security: techniques that ensure that data stored on a computer cannot be read or compromised l A program is running on your computer. What is to stop it from erasing all of your data, accidentally or otherwise? l Question: Is Java really all that secure? “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l A language is architecture-neutral if it does not prefer a particular type of computer architectures l E.g. The Macintosh processor family (PowerPC) and the PC (x86-Pentium) family have their own respective strengths and weaknesses. It is not too hard to construct a program that will run faster on one than an other. l A particular program is never entirely architecture neutral though l Question: When is being architecturally neutral a bad thing? “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l A program is portable if it will work the same (roughly) on many different computer systems l HTML is also platform-independent or portable l A whole lot of effort is currently spent porting non-portable code “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l Performance: speed in completing some task l Performance is everything to most computer and software manufacturers. l Story: ä If the transportation industry kept up with the computer industry, one would be able to now buy a Roll Royce that could drive across country in 5 minutes for $35. l Rebuttal: ä It would crash once a week, killing everyone on board. “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l A thread is a part of the program that can operate independently of its other parts l Multi-threaded programs can do multiple things at once ä e.g. download a file from the web while still looking at other web pages l Question: What is the problem with multiple agents working at the same time? ä Synchronization “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci l Dynamic refers to actions that take place at the moment they are needed rather than in advance ä Antonym: static l A dynamic program can ä Ask for more or less resources as it runs ä Use the most recent version of some code that is available l Question: Why is being dynamic a good thing? “Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language.”

CompSci A Java Program import java.awt.*; public class HelloWorld extends java.applet.Applet { TextField m1; public void init() { m1 = new TextField(60); m1.SetText(“Hello World”); add(m1); }

CompSci Definitions l Algorithm : ordered set of unambiguous executable steps, defining a terminating process l Program : instructions executed by a computer Applet : Java program that is executed in a program such as appletviewer or a Java-enabled web browser l Class : family of components sharing common characteristics consisting of: ä Data : information ä Method : functionality l Object : instance of a class l Variable : represent value stored in computer memory. A variable must be defined or declared before being used ä Sometimes synonymous with object

CompSci Grammar l English and other natural languages have structure => => | | => | => DOG | FLEAS | PERSON |... => RAN | BIT |... l Process of taking sentence and fitting it to grammar is called parsing DOG BIT PERSON l Parsing English is complex because of context dependence

CompSci Formal specifications l Need a precise notation of syntax of a language l Grammars can be used for generation and also can be used l Context-free grammars => sequence of letters and/or digits that begins with a letter => guessB => msg42 l Substitute as many times as necessary. All legal statements can be generated this way  Want person = firstn + " " + lastn; ä How do we get this from our grammar?

CompSci A Grammar for Java l Need a set of rules l Our first one was a good start: ä => any string of alphanumeric symbols that begins with a letter l Let’s add something to define a simple statement:  => = ; l And then work on the details:  => | |  => ä =>  => ” any sequence of charcters ”

CompSci A Simple Statement Now have enough to generate a statement like: msg = “hello”; ä Start with: => = ; ä Then using: => any string of alphanumeric symbols that begins with a letter msg = ; ä Then, using: => | | msg = ; ä Using: => msg = ;  Using: => ” any sequence of charcters ” msg = ”hello” ;

CompSci A Grammar for Java l Including more rules to describe programs we have: 1. => any string of alphanumeric symbols that begins with a letter 2. => = ; 3. => = new ( ); 4. =>. ( ); | ( ); 5. => possibly empty list of s separated by commas 6. => | | 7. => + 8. => 9. = ” any sequence of characters ” 10. =

CompSci Using our Grammar Use this to generate: person = firstn + ” ” + lastn; Rule Statement being Generated # 2: => = ; 1: => person = ; 6: => person = ; 7: => person = + ; 8: => person = + ; 10: => person = + ; 1: => person = firstn + ; 7: => person = firstn + + ; 8: => person = firstn + + ; 9: => person = firstn + " " + ; 8: => person = firstn + " " + ; 10: => person = firstn + " " + ; 1: => => person = firstn + " " + lastn;

CompSci Proving Grammatical Correctness Why go through the process we went through? ä Shows that desired statement can be generated from this grammar l Actually proves that the statement is grammatically correct! ä Same rigor as a mathematical proof l (Doesn’t prove that logic is correct, though) l Actually need more rules to handle the level of Java we’ve covered so far ä Summary of rules shown on pages of Great Ideas ä Also give an example for a complete applet ä Too long to go through in class – Please Read!

CompSci Game l 10 coins ä You and a friend have a stack of 10 coins ä On each person’s turn, they remove either 1 or 2 coins from the stack ä The person who removes the last coin wins. ä Can you win? l 10 coins with a twist ä 10 coins, can now ALSO place 1 or 2 coins back on the stack ä Person who removes last coin wins ä Should you go first or second, and what’s your strategy