CPS 100, Spring 2010 3.1 Topdown v Bottomup l Programming is changing our world  Empowering, liberating, equalizing,… l Everything is a bit: all 0’s and.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Counting the bits Analysis of Algorithms Will it run on a larger problem? When will it fail?
the fourth iteration of this loop is shown here
CompSci 100 Prog Design and Analysis II Sept 9, 2010 Prof. Rodger CompSci 100, Fall n  i=1 i.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
CS503: First Lecture, Fall 2008 Michael Barnathan.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
CPS 100, Spring Conventions in Compsci 100 projects l We want you to concentrate on algorithms and data structures  Not on rendering fonts, interacting.
Week 2 CS 361: Advanced Data Structures and Algorithms
CPS 100, Spring Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance and to reason about alternative algorithms.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Iterative Algorithm Analysis & Asymptotic Notations
Invitation to Computer Science, Java Version, Second Edition.
Recursion, Complexity, and Sorting By Andrew Zeng.
Analysis of Algorithms
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Complexity of Algorithms
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CompSci 100E 9.1 Stack: What problems does it solve?  Stacks are used to avoid recursion, a stack can replace the implicit/actual stack of functions called.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
CompSci 100e 3.1 Plan for the week l Classes and Objects l Model-View-Controller l Maps and Hashing l Upcoming  Towards Algorithm analysis l APTs:  IsomorphicWords,
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
Compsci 100, Fall Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance  Reason about alternative algorithms.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Searching Topics Sequential Search Binary Search.
Written by: Dr. JJ Shepherd
Compsci 100, Fall Topdown v Bottomup l Programming is changing our world  Empowering, liberating, equalizing,… l Everything is a bit: all 0’s.
Algorithmics - Lecture 41 LECTURE 4: Analysis of Algorithms Efficiency (I)
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
CPS Solving Problems Recursively l Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved simply.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
AP National Conference, AP CS A and AB: New/Experienced A Tall Order? Mark Stehlik
Complexity Analysis (Part I)
CMPT 438 Algorithms.
Analysis: Algorithms and Data Structures
Tools: Solve Computational Problems
Algorithm Analysis CSE 2011 Winter September 2018.
Algorithm Analysis (not included in any exams!)
Objective of This Course
Introduction to Algorithms Analysis
Searching, Sorting, and Asymptotic Complexity
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Compsci 201, O-Notation and Maps (Interfaces too)
Presentation transcript:

CPS 100, Spring Topdown v Bottomup l Programming is changing our world  Empowering, liberating, equalizing,… l Everything is a bit: all 0’s and 1’s  From jpg to mp3 to … l It’s about problems! It’s about details!  Should we think about problems to get to the details?  Should we master details before grand thinking? l See Wikipedia on topdown v bottomup designWikipedia on topdown v bottomup design

CPS 100, Spring Plan for the week l OO-Overview  Conventions and idioms for object-oriented Compsci 100  What is MVC as it applies to code you write and use? l Prelude to analysis of execution time  What is “efficiency”? How to measure it?  Engineering, math, and science combined l How to read code and influence people  When you need to understand and when you don’t  How to modify code, make additions, inter-operate

CPS 100, Spring Conventions in Compsci 100 projects l We want you to concentrate on algorithms and data structures  Not on rendering fonts, interacting with users  This is important! But not what this course is about l We try to build GUIs or views that facilitate projects  You write the brains, we build the skull/manikin  Our GUI communicates with your code Requires following conventions in interacting code l GUI libraries are similar across languages, but…  Deeper Java specific details than HashMap

CPS 100, Spring KWIC: Key word in Context Arise, fair sun, and kill the envious moon, Who I. Yet I should kill thee with much cherishing. shortly, for one would kill the other. Thou! why, those twenty could but kill one life. I beg wherefore, villain, didst thou kill my cousin? That villain mean, But 'banished' to kill me- 'banished'? O friar, thou happy. Tybalt would kill thee, But thou slewest cell there would she kill herself. Then gave I heaven finds means to kill your joys with love! l Read file, find word and it’s context, print  Can find all words, but how do we get context?  Loop and look: create context line for each occurrence  See ContextModel.java

CPS 100, Spring Use KWIC example to motivate study l Dissect and inspect KWIC code to understand conventions  Understand Model and View interaction  Facilitates doing RSG and Markov text-generation l Review some basic coding idioms and ideas  Avoiding recomputing same value, readability, modifiability, … l Errors: possible for a method to fail but still work?  See KWIC code when same key occurs twice on a line!

CPS 100, Spring MVC Example, key-word-in-context l User loads file  Where? Communicate to?  What changes in model?  What happens in view? l User chooses word  Process in Model  Alternatives?  Generate context, display  How to show in any view? Model initialize addView process fillMap justify SimpleView showMessage showError update addModel

CPS 100, Spring Key Word in Context Explained l For every different word, store where it occurs  love is the 1 st, 3 rd, 50 th, and 1237 th word in the file l This data is kept in a map, key is word, value is ??  How do we generate the data in the map? l Given a word, how do we find its context? How do we format?  All words are in an array, in order  Memory concerns?  Original KWIC paper by Parnas as comparison

CPS 100, Spring Code Interlude l Examine ContextModel.process  Called when user enters word, parameter is the word  If file already read, we don’t need map, where is this?  Error checking? When and when happens  How does Model communicate back to View? l Examine ContextModel.justify  What is method doing  What is parameter, where was it constructed, issues?  What about ‘magic numbers’, e.g., 30?  What about comments, should we add some?

CPS 100, Spring KWIC main program/class public class ContextMain { public static void main(String[] args){ ContextModel model = new ContextModel(); SimpleViewer view = new SimpleViewer("Compsci 100 KWIC", "word>"); view.setModel(model); } l What changes in above, e.g., for RSG assignment?  How can view communicate with any model?  View doesn’t change, model does! Requires using a Java interface to capture commonality

CPS 100, Spring Model View Controller, MVC l Gui is the View and often the controller  Separate user-interaction from updates to data l User loads file, chooses word, …  Model notified, computes, updates view l Model has all the state and knows when it changes  Communicates changes to views (via controller)  Must be initialized, updated, etc. l Very common Design Pattern  Capture common solutions to problems in a context  Iterator, Composite, Decorator seen in Compsci 100

CPS 100, Spring Convention Summary l Classes start with capital letter and then we have:  They’re public, except nested class? Protected means …  camelCaseForMethods and ForClasses  Ivars, fields, instance variables, mySize, myMap, …  Constants (public static) are ALL_CAPS l Interfaces are IModel, IView, and so on  Not true for standard Java classes, yes for Compsci 100  Don’t need to label methods as abstract, but can l Supply AbstractDefault implements IThing  Constructor, some state, some common behavior: extend!

CPS 100, Spring Eugene (Gene) Myers l Lead computer scientist/software engineer at Celera Genomics, then at Berkely, now at Janelia Farms Research Institute (HHMI) l BLAST and WG-Shotgun "What really astounds me is the architecture of life. The system is extremely complex. It's like it was designed." … "There's a huge intelligence there.”

CPS 100, Spring Methods, Interfaces, Inheritance l A method by any other name would smell as sweet  Method in OO languages, functions, procedures in others  Parameters and return value: communication Do objects or methods communicate?: OO v procedural Static : Math.sqrt, Character.isUpperCase, …  Don’t belong to an object, invoked via class (clue above?)  Java API helpful here l Interface: implement class with required, related methods  HashMap, TreeMap  ArrayList, LinkedList, Vector

CPS 100, Spring Interfaces continued l In the beginning  Make code work, don’t worry about generalizing  But, if you write code using Map rather than TreeMap Can swap in a new implementation, coded generally! l Don’t know how to optimize: space or time  Facilitate change: use interface rather than concrete class  My DVD connects to my TV, regardless of brand, why?  How do you turn on a Nokia cell phone? Motorola? But! l Interfaces facilitate code refactoring  Don’t add functionality, change speed or memory or …

CPS 100, Spring What does Object-Oriented mean? l Very common method of organizing code  Design classes, which encapsulate state and behavior  Some classes can be similar to, but different from their parent class: inheritance Super class, subclass  Inherit behavior, use as is or modify and use or both l Complex to design a hierarchy of classes, but important  More of this in Compsci 108 or on-the-job training  We’re solving simple problems, not designing re-usable libraries l Simple does not mean straight-forward, not Vista!

CPS 100, Spring Inheritance and Interfaces l Interfaces provide method names and parameters  The method signature we can expect and thus use!  What can we do to an ArrayList? To a LinkedList?  What can we do to a Map or Set or PriorityQueue?  java.util.Collection is an interface l Abstract classes can implement core, duplicated code  If we can add one object to a [set,map,list], can we add an entire list of objects? java.util.AbstractCollection  If we can iterate can we remove? Convert to array? Obtain size?

CPS 100, Spring Random Java stuff l What happens when you call new, when do you?  Creates object, assign reference/pointer somewhere (or?)  Two ‘labels/variables’ share same object, consequences? Why isn’t this a concern with String objects/labels? l What about int, double, byte, char, long  Related, but different. What’s the same, what’s different?  Range of values for each?  Arithmetic with each?  Casting vs promotion (pass int to Math.sqrt?)

CPS 100, Spring Jaron LanierJaron Lanier ( Jaron Lanier is a computer scientist, composer, visual artist, and author. He coined the term ‘Virtual Reality’ … he co- developed the first implementations of virtual reality applications in surgical simulation, vehicle interior prototyping, virtual sets for television production, and assorted other areas. In 2010 writes: “ You are Not a Gadget ” "What's the difference between a bug and a variation or an imperfection? If you think about it, if you make a small change to a program, it can result in an enormous change in what the program does. If nature worked that way, the universe would crash all the time.”

CPS 100, Spring Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance  Reason about alternative algorithms and implementations  It’s faster! It’s more elegant! It’s safer! It’s less filling! l Need empirical tests, analytical/mathematical tools  Given two methods, which is better? Run them to check. 30 seconds vs. 3 seconds, easy. 5 hours vs. 2 minutes, harder What if it takes two weeks to implement the methods?  Use mathematics to analyze the algorithm,  The implementation is another matter, cache, compiler optimizations, OS, memory,…

CPS 100, Spring How fast does the code run? l “As soon as an Analytical Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will then arise — by what course of calculation can these results be arrived at by the machine in the shortest time?”  Babbage, Analytical Engine 1864 Babbage, Analytical Engine 1864 l Question: What’s the fastest way to sort a million 32-bit integers? l Obama’s answer: I don’t think the bubble sort is the way to go Obama’s answer

CPS 100, Spring Quantitative Measurements of Code l Typically measure running time (memory?)  Other things to measure?  What about wall-clock v CPU time? Java: wall-clock l Typically change size of input/problem to validate runtime hypotheses  Not the data itself, but the number of data items  Size of string vs. number of strings in array? l Doubling hypothesis: What effect does doubling input size have on running time?  Linear: time doubles, quadratic: factor of four, …

CPS 100, Spring Different measures of complexity l Worst case  Gives a good upper-bound on behavior  Never get worse than this  Drawbacks? l Average case  What does average mean?  Averaged over all inputs? Assuming uniformly distributed random data?  Drawbacks? l Best case  Linear search, useful?

CPS 100, Spring Notations for measuring complexity l O-notation or big-Oh: O(n 2 ) is used in most algorithmic analysis, e.g., Compsci 130 at Duke. It’s an upper bound in the limit  Correct to say that linear algorithm is O(n 2 ), but useful? Theta-notation or  (n 2 ) is a tight bound, solid guarantee that algorithmic analysis is exact, both upper and lower bound Omega is lower bound:  (n log n) is a lower bound for comparison based sorts  Can’t do better than that, very hard to prove l Sedgewick/Wayne uses tilde notation ~ n 2 means leading term is n squared  We’ll use this, but abuse big-Oh since we want “best” answer

CPS 100, Spring Big-Oh, O-notation: concepts & caveats l Count how many times “simple” statements execute  In the body of a loop, what matters? (e.g., another loop?)  Assume simple statements take a second, cost a penny,… What’s good, what’s bad about this assumption? Alternatives? l In a recursive method what do we do? Recurrence relations  Like a loop, but easier! (and trickier) to analyze l In real life: cache behavior, memory behavior, swapping behavior, library gotchas, things we don’t understand,…

CPS 100, Spring Multiplying and adding big-Oh l Suppose we do a linear search then do another one  What is the complexity?  If we do 100 linear searches?  If we do n searches on a vector of size n? l Binary search followed by linear search?  What are big-Oh complexities? Sum?  What about 50 binary searches? What about n searches? l What is the number of elements in the list (1,2,2,3,3,3); (1,2,2,3,3,3,4,4,4,4)?  What about (1,2,2, …, n,n,…,n)?

CPS 100, Spring Helpful formulae l We always mean base 2 unless otherwise stated  What is log(1024)?  log(xy) log(x y ) log(2 n ) 2 (log n) l Sums (also, use sigma notation when possible)  … + 2 k = 2 k+1 – 1 =  … + n = n(n+1)/2 =  a + ar + ar 2 + … + ar n-1 = a(r n - 1)/(r-1)= log(x) + log(y) y log(x) n log(2) = n 2 (log n) = n k  i=0 2i2i n  i=1 i n-1  i=0 ar i

CPS 100, Spring Complexity: ideas and measurements l In word-counting program, avoiding rescan of text?  Store and lookup in map, avoid counting more than once  Changed from UT to T*log T to T + U (or T + U*log U) l In Markov assignment we’ll do something similar  It’s easy to rescan text, store entire text and no more  Avoid rescanning requires storing more information  If we only do something once, should we optimize? l Before coding analysis: what approach will work?  Why is this a good idea?

CPS 100, Spring Sergey Brin l Simple ideas sometimes can change the world [wikipedia]  Works because of scale l Co-created pagerank (Larry Page), which evaluates links to a page and the importance of those links, based on the importance of the page from which the links come which …!