CompSci 100 Prog Design and Analysis II Sept 16, 2010 Prof. Rodger CompSci 100, Fall 20101.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

CSCE 3400 Data Structures & Algorithm Analysis
Hashing as a Dictionary Implementation
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
Computer Science II Recursion Professor: Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hashing General idea: Get a large array
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
1. 2 Problem RT&T is a large phone company, and they want to provide enhanced caller ID capability: –given a phone number, return the caller’s name –phone.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CompSci 100E 10.1 Solving Problems Recursively  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Compsci 06/101, Fall Designing Algorithms and Programs l Designing algorithms to be correct and efficient  The most important of these is _______________.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
CPS 100, Spring From Recursion to Self Reference public int calc(int n){ return n*calc(n-1); } l What is the Internet?  A network of networks.
CompSci 100E 12.1 Solving Problems Recursively  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Comp 335 File Structures Hashing.
CompSci 100e 6.1 Hashing: Log ( ) is a big number l Comparison based searches are too slow for lots of data  How many comparisons needed for a.
Compsci 100, Fall From Recursion to Self Reference public int calc(int n){ return n*calc(n-1); } l What is the Internet?  A network of networks.
1 HASHING Course teacher: Moona Kanwal. 2 Hashing Mathematical concept –To define any number as set of numbers in given interval –To cut down part of.
Hashing Hashing is another method for sorting and searching data.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
CPS 100, Fall From Recursion to Self Reference public int calc(int n){ return n*calc(n-1); } l What is the Internet?  A network of networks.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
A Computer Science Tapestry 10.1 Solving Problems Recursively l Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems.
CompSci Review of Recursion with Big-Oh  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
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.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
CompSci 100e 7.1 Hashing: Log ( ) is a big number l Comparison based searches are too slow for lots of data  How many comparisons needed for a.
Recursion Fundamentals of Recursion l Base case (aka exit case)  Simple case that can be solved with no further computation  Does not make a recursive.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
CPS Solving Problems Recursively l Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved simply.
A Computer Science Tapestry 10.1 Solving Problems Recursively l Recursion is an indispensable tool in a programmer’s toolkit ä Allows many complex problems.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
CPS Today’s topics Programming Recursion Invariants Reading Great Ideas, p Brookshear, Section Upcoming Copyrights, patents, and.
Recursion Topic 5.
Plan for the Week Review for Midterm Source code provided
Solving Problems Recursively
LEARNING OBJECTIVES O(1), O(N) and O(LogN) access times. Hashing:
Hashing Exercises.
Compsci 201 Recursion+Percolation
Java Software Structures: John Lewis & Joseph Chase
Searching.
Announcements Final Exam on August 17th Wednesday at 16:00.
Announcements Final Exam on August 19th Saturday at 16:00.
Announcements Final Exam on December 25th Monday at 16:00.
How to use hash tables to solve olympiad problems
Announcements HW3 grades will be announced this week
Hashing.
Solving Problems Recursively
Solving Problems Recursively
Presentation transcript:

CompSci 100 Prog Design and Analysis II Sept 16, 2010 Prof. Rodger CompSci 100, Fall 20101

Announcements Markov due Sept 21 (part1) and Sept 22 (part2) APT-TWO due Sept 22 Recitation this week on Markov CompSci 100, Fall 20102

Using the Debugger in Eclipse Debugger allows you to pause your program during execution and look around. – First set breakpoints (right click on line of code, toggle breakpoint) – Under Run menu, select “Debug” Stops when it reaches a breakpoint You can examine the values of variables CompSci 100, Fall 20103

Hashing Storage – hash table or hash buckets (implementation could be array, map, tree, or other structure) Each data item has a key Hash function that maps key to address in the hash table H(key) = address Best if the “key” is near the address “collision” if two items map to the same address CompSci 100, Fall 20104

Example Hashing Duke's ACM Chapter wants to be able to quickly find out info about its members. Also add, delete and update members. Doesn't need members sorted John Smith Jack Adams Betty Harris Rose Black Possible Hash Function: H(ssn) = last 3 digits Hash Table size is 1000, range from 0 to 999 Will there be a collision with data above? CompSci 100, Fall 20105

Bucket Hashing Buckets (not all buckets shown) CompSci 100, Fall Buckets hold a lot of values Apply hash function to data. What happens? H( ) = 432 John Smith H( ) = 142 Jack Adams H( )= 100 Betty Harris H( ) = 142 Rose Black

Buckets (not all buckets shown) CompSci 100, Fall Apply hash function to data. What happens? H( ) = 432 John Smith H( ) = 142 Jack Adams H( )= 100 Betty Harris H( ) = 142 Rose Black John Smith

Buckets (not all buckets shown) CompSci 100, Fall Apply hash function to data. What happens? H( ) = 432 John Smith H( ) = 142 Jack Adams H( )= 100 Betty Harris H( ) = 142 Rose Black John Smith Jack Adams Betty Harris Where does Rose Black go?

Buckets (not all buckets shown) CompSci 100, Fall Apply hash function to data. What happens? H( ) = 432 John Smith H( ) = 142 Jack Adams H( )= 100 Betty Harris H( ) = 142 Rose Black If buckets are one array, where does Rose Black go? John Smith Jack Adams Rose Black Betty Harris

If instead of Buckets, one String array 0-999, Where would Rose Black go? CompSci 100, Fall Collision, must be resolved, one way – next possible bucket (slot) Other Collision resolution methods H( ) = 432 John Smith H( ) = 142 Jack Adams H( )= 100 Betty Harris H( ) = 142 Rose Black John Smith Jack Adams Betty Harris Rose Black

Hash Functions Want hash function with the fewest collisions, data distributed Random number Generator with seed – H(key) is random(key)*N Shift folding with 100 buckets – H( ) is ( ) mod 100 Use ascii code – H(BANKS) = = 367 – Where ascii(B) = 66, etc. Java – hashCode() CompSci 100, Fall

From Recursion to Self Reference public int calc(int n){ return n*calc(n-1); } What is the Internet? – A network of networks. Or … What is recursive DNS? – What IP is fxyztl.com? What is PageRank? – Where is it used? 12CompSci 100, Fall 2010

Other Topics Structure driving ‘self- referential’ code – motivation Basic understanding of recursion – Principles – Examples Blob and grid examples Art of the Fugure (Bach) 13CompSci 100, Fall 2010

Quota Exceeded: coping with storage You’re running out of disk space – Buy more – Compress files – Delete files How do you find your “big” files? – What’s big? – How do you do this? 14CompSci 100, Fall 2010

BlobCount or edge detection or … How do we find images? Components? Paths? – Create information from data 15CompSci 100, Fall 2010

Tools: Solving Computational Problems Algorithmic techniques and paradigms – Brute-force/exhaustive, greedy algorithms, dynamic programming, divide-and-conquer, … – Transcend a particular language – Designing algorithms, may change when turned into code Programming techniques and paradigms – Recursion, memo-izing, compute-once/lookup, tables, … – Transcend a particular language – Help in making code work Cope with correctness and maintenance Cope with performance problems 16CompSci 100, Fall 2010

Tools: Solving Computational Problems Java techniques – java.util.*, Comparator, Priority Queue, Map, Set, … – These aren’t really Java-specific, but realized in Java – Map, Comparator, Set: C++, Python, …. – We learn idioms in a language and talk about abstractions Analysis of algorithms and code – Mathematical analysis, empirical analysis – We need a language and techniques for discussion – Theory and practice, real problems and in-the-limit issues “In theory there is no difference between theory and practice, but in practice there is.” (attributed to many) 17CompSci 100, Fall 2010

Recursive structure matches code public static final long THRESHOLD = L; // one million bytes public static void findBig(File dir, String tab) { File[] dirContents = dir.listFiles(); System.out.println(tab+"**:"+dir.getPath()); for(File f : dirContents){ if (f.isDirectory()) { findBig(f,tab+"\t"); } else { if (f.length() > THRESHOLD){ System.out.printf("%s%s%8d\n",tab,f.getName(), f.length()); } Does findBig call itself? 18CompSci 100, Fall 2010

Solving Problems Recursively Recursion: indispensable in programmer’s toolkit – Allows many complex problems to be solved simply – Elegance and understanding in code often leads to better programs: easier to modify, extend, verify (and sometimes more efficient!!) – Sometimes recursion isn’t appropriate, when it’s bad it can be very bad---every tool requires knowledge and experience in how to use it The basic idea is to get help solving a problem from coworkers (clones) who work and act like you do – Ask clone to solve a simpler but similar problem – Use clone’s result to put together your answer Need both: call on the clone and use the result 19CompSci 100, Fall 2010

Print words read, but print backwards Could store words and print in reverse order, but … – Probably the best approach, recursion works too public void printReversed(Scanner s){ if (s.hasNext()){ // reading succeeded? String word = s.next(); // store word printReversed(s); // print rest System.out.println(word); // print the word } The function printReversed reads a word, only prints word after the clones finish printing – Each clone has own version of the code, own word variable – Who keeps track of the clones? – How many words are created when reading N words? Can we do better? 20CompSci 100, Fall 2010

Exponentiation Computing x n means multiplying n numbers – Does it require n multiplies? – What’s the simplest value of n when computing x n ? – To only multiply once, what can you ask a clone? public static double power(double x, int n){ if (n == 0){ return 1.0; } return x * power(x, n-1); } Number of multiplications? – Note base case: no recursion, no clones – Note recursive call: moves toward base case (unless …) 21CompSci 100, Fall 2010

Faster exponentiation Recursive calls made to compute ? – How many multiplies on each call? Is this better? public static double power(double x, int n){ if (n == 0) return 1.0; double semi = power(x, n/2); if (n % 2 == 0) return semi*semi; return x * semi * semi; } What about an iterative version of this function? – Why might we want such a version? 22CompSci 100, Fall 2010

Back to Recursion Recursive functions have two key attributes – There is a base case, aka exit case: no recursion! See print reversed, exponentiation – All other cases make a recursive call, with some measure (e.g., parameter value) that decreases towards the base case Ensure that sequence of calls eventually reaches the base case “Measure” can be tricky, but usually it’s straightforward Example: finding large files in a directory (on a hard disk) – Why is this inherently recursive? – How is this different from exponentation? 23CompSci 100, Fall 2010

Thinking recursively: recursive Max public static double recMax(double[] a, int index){ if (index == a.length-1){ // last element, done return a[index]; } double maxAfter = recMax(a,index+1); return Math.max(a[index],maxAfter); } What is base case (conceptually)? – Do we need variable maxAfter ? Use recMax to implement arrayMax as follows: – Introduce auxiliary variable from arrayMax(a) to … – return recMax(a,0); Recursive methods sometimes use extra parameters; helper methods set this up 24CompSci 100, Fall 2010

Recognizing recursion: public void change(String[] a, int first, int last){ if (first < last) { String temp = a[first]; // swap first/last a[first] = a[last]; a[last] = temp; change(a, first+1, last-1); } // original call (why?): change(a, 0, a.length-1); What is base case? (no recursive calls) What happens before recursive call made? How is recursive call closer to the base case? Recursive methods sometimes use extra parameters; helper methods set this up 25CompSci 100, Fall 2010

More recursion recognition public static int value(int[] a, int index){ if (index < a.length) { return a[index] + value(a,index+1); } return 0; } // original call: int v = value(a,0); What is base case, what value is returned? How is progress towards base case realized? How is recursive value used to return a value? What if a is array of doubles, does anything change? Recursive methods sometimes use extra parameters; helper methods set this up 26CompSci 100, Fall 2010

From programming techniques to Java Is recursion a language independent concept? – Do all languages support recursion? – What are the issues in terms of computer/compiler/runtime support? We use a language and its libraries, study them? – Should we know how to implement ArrayList What are the building blocks, what are our tools – Should we know how to implement different sorts Should we know how to call existing sorts 27CompSci 100, Fall 2010

Fran Allen IBM Fellow, Turing Award – Optimizing compilers Taught high school for two years, then Master’s degree and IBM – Teachers excited me to learn I’ve always felt that theory without practice is maybe nice and maybe pretty, but it’s not going to influence computing as much as the practice side. But the practice has to be backed up with the ability to talk about it, reason about it, and formulate it so that it can be reproduced.

Blob Counting, Flood Fill Flood a region with color – Erase region, make transparent,.. – How do find the region? Finding regions, blobs, edges,.. – See blob counting code – What is a blob? Recursion helps, but necessary? – Performance, clarity, … – Ease of development 29CompSci 100, Fall 2010

Details and Idioms in blob code Method blobFill has four parameters – (row,column) of where search starts – Character being searched for (initially * or blob) – Character to fill with on success (e.g., count ‘2’ or ‘4’) Mark for visualization Mark to ensure we don’t search again! If (row,column) is part of blob, count it and ask neighbors for their counts – They’re part of blob (if never visited before) Return total of yourself and neighbors – Key to recursion: do one thing and ask for help 30CompSci 100, Fall 2010

Blob questions What changes if diagonal cells are adjacent? – Conceptually and in code How do we find blob sizes in a range? – Not bigger than X, but between X and Y How would we number blobs by size rather than by when they’re found? – Do we have the tools to do this in existing code? Can we avoid recursion and do this iteratively? 31CompSci 100, Fall 2010