Genome Revolution: COMPSCI 006G 15.1 Recursion and Recursive Structures l Definition in the dictionary: in mathematics an expression in which a value is.

Slides:



Advertisements
Similar presentations
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
Advertisements

10-Jun-15 Fibonacci Numbers A simple example of program design.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Recursion.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Building Java Programs Chapter 12 Lecture 12-2: recursive programming reading:
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Chapter 8 Recursion Modified.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
A Computer Science Tapestry 12.1 Tools for programming l Syntatic details of control and variables  If, while, switch, function calls  Scope and lifetime.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
1 CS161 Introduction to Computer Science Topic #9.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
Software Design 14.1 CPS 108 l Object oriented design and programming of medium-sized projects in groups using modern tools and practices in meaningful.
CPS What is Computer Science? What is it that distinguishes it from the separate subjects with which it is related? What is the linking thread.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
CompSci Review of Recursion with Big-Oh  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
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.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Welcome to Software Engineering. Lecture 1 Metaphysics of Software Engineering Alex
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Compsci 101.2, Fall Plan for ThBThB l Programming and understanding …  Hierarchical structures and concepts What is a file system on a computer?
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
CompSci 101 Introduction to Computer Science April 14, 2016 Prof. Rodger compsci101 spring161.
Sections 4.1 & 4.2 Recursive Definitions,
Chapter 15 Recursion.
Lecture 10: recursive programming reading:
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Plan for the Week Review for Midterm Source code provided
Chapter 15 Recursion.
Java Software Structures: John Lewis & Joseph Chase
Building Java Programs
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Output Input
Building Java Programs
Building Java Programs
Lecture 10: recursive programming reading:
CSE 143 Lecture 14 (A) More Recursive Programming
Announcements Final Exam on August 19th Saturday at 16:00.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Building Java Programs
CSE 143 Lecture 14 More Recursive Programming; Grammars
Announcements Final Exam on December 25th Monday at 16:00.
slides created by Alyssa Harding
Building Java Programs
Main() { int fact; fact = Factorial(4); } main fact.
Lecture 10: recursive programming reading:
slides created by Alyssa Harding
Presentation transcript:

Genome Revolution: COMPSCI 006G 15.1 Recursion and Recursive Structures l Definition in the dictionary: in mathematics an expression in which a value is calculated by using preceding terms of the expression  Pretty worthless, doesn’t convey details or power l Consider folders and files in a computer, what’s in a folder?  Files, other folders, anything else?  Is there a distinction between a folder and a file? l Viewpoint is everything, who is the viewer? The viewee?  What does this have to do with physics?  How do we look at files via programming

Genome Revolution: COMPSCI 006G 15.2 Files and Folders: Files and Directories l OS X view of Directory Structure

Genome Revolution: COMPSCI 006G 15.3 Different OS, Same View l Windows 2000 Files and Directories

Genome Revolution: COMPSCI 006G 15.4 java.io.File l An abstract representation of file and directory pathnames  C:\eclipse\workspace\dirstuff\DirectoryViewer.java  /Users/ola/Desktop/eclipse/workspace/dirstuff/DV.java l What is a pathname?  Given the path, can you find/navigate to the file/folder?  Is there more than one way to get to a folder? Why? l What accessor methods exist for Files/Directories  getName()  length(), lastModified()  isDirectory(), isFile()

Genome Revolution: COMPSCI 006G 15.5 Problem? How 'large' is a directory? l The size accessor method and file-system view tells how much diskspace the directory entry needs  This is small, it requires name, date, list-of-files  The list-of-files isn't really part of the directory size, but it doesn't matter, we want SIZE l How does the 'search' command work to find a file?  Search everything from scratch each time  Cache results with indexing to avoid re-searching l How do interact programmatically with file system in platform independent way?  Need an abstraction (hint: see java.io.File)

Genome Revolution: COMPSCI 006G 15.6 Code to visit a Folder (dirstuff) public void visit(File f, int level){ if (f.isDirectory()){ System.out.println(tab(level)+f.length() + " **** " + f.getName()); File[] files = f.listFiles(); for(int k=0; k < files.length; k++){ visit(files[k],level+1); } else { System.out.println(tab(level)+f.length() + "\t" + f.getName()); }

Genome Revolution: COMPSCI 006G 15.7 Reasoning about File/Directory code What is the purpose of the method visit ?  What does visit do?  What methods does visit call?  How does visit use java.io.File ? l Does the method call itself?  No, but it calls a method named visit, with a different parameter  Think of hundreds of little visit clones, do some work, pass other work off to clone  How can we modify the method to print directory last?  How can we modify method to calculate "real" size?

Genome Revolution: COMPSCI 006G 15.8 Who is Alan Perlis? l It is easier to write an incorrect program than to understand a correct one l Simplicity does not precede complexity, but follows it l If you have a procedure with ten parameters you probably missed some l If a listener nods his head when you're explaining your program, wake him up l Programming is an unnatural act l Won first Turing award

Genome Revolution: COMPSCI 006G 15.9 What is recursion (revisited)? l Structure in which a container can hold/use other containers  A directory holds file and directories  A Russian doll holds other russian dolls?  A method that calls 'itself' or clones l Do russian dolls nest infinitely? l Do folders hold infinite subfolders? l Can a method call methods infinitely? l We need a way out of the mess  Last doll, directory with no subdirectories  Method that makes no calls

Genome Revolution: COMPSCI 006G Mathematical Definition of Factorial l 6! = 1 x 2 x 3 x 4 x 5 x 6 l What's n!  If n equals 1 or 0, then it's 1  Otherwise it's n x (n-1)! l Does this get the job done? Is it constructive?  What does the method below return for 5?  What about –1? public static int factorial(int n){ if (n == 1 || n == 0) return 1; else return n * factorial(n-1); }

Genome Revolution: COMPSCI 006G Recursive Structure and Methods l There must be some way out of the recursion  A structure with no substructure  A method with no recursive method calls  This is the base-case of the recursion l The substructure must be similar to the original structure  Russian dolls don't hold machine guns (recursively)  Each russian doll holds a smaller doll  Each call of visit() gets closer "to the bottom"  Each call of factorial() gets closer to 0 or 1  Ensure the base case is eventually reached

Genome Revolution: COMPSCI 006G Recursion and Bioinformatics l Methods we "studied" for sequence alignment  How do we align sequences optimally?  What about lots of sequences?  Dynamic programming is related to recursion (often implement DP recursively) l How do we find CG rich regions in a sequence?  Divide sequence in half, look for CG rich regions in each half and the overlap of halves  How do look for CG rich regions in halves?

Genome Revolution: COMPSCI 006G Fred Brooks l … on computing pioneer Howard Aiken "the problem was not to keep people from stealing your ideas, but to make them steal them." l Duke valedictorian 1953, started UNC Computer Science Dept in 1964, won Turing Award in 1999 l Mythical-Man Month, "Adding man-power to a late project makes it later", … "There is no silver-bullet for Software Engineering… [because of essential complexity]" l Highest paid faculty in UNC Arts and Sciences