Übung Informatik I - Programmierung - Blatt 7
|
|
- Janice Hancock
- 6 years ago
- Views:
Transcription
1 RHEINISCH- WESTFÄLISCHE TECHNISCHE HOCHSCHULE AACHEN LEHR- UND FORSCHUNGSGEBIET INFORMATIK II RWTH Aachen D Aachen GERMANY LuFG Informatik II Prof. Dr. Jürgen Giesl Peter Schneider-Kamp, René Thiemann, Thomas Weiler Übung Informatik I - Programmierung - Blatt 7 (Lösungsvorschlag) Aufgabe 1 b) Stelligkeit: 419 c) ack(1, 100), ack(2, 100), ack(3, 8..15), ack(4, 0..1) d) Bit-Shifting: 2ms, wiederholtes Quadrieren: 1835ms, Einfache Iteration: 59585ms Aus ack(4, 2) = ergibt sich log 10 (ack(4, 3)) = log 10 (ack(3, ack(4, 2))) = log10 ( ) = log 10 (2) = Folglich hat ack(4, 3) ungefähr viele Stellen. Fibonacci import java. math. B i g I n t e g e r ; * Implementation of the Fibonacci function with precise * numbers ( <code >java. math. BigInteger </ code >). The class provides a slow and a fast routine. * thiemann
2 public class Fib { private f i n a l static B i g I n t e g e r zero = B i g I n t e g e r.zero; private f i n a l static B i g I n t e g e r one = B i g I n t e g e r.one; * Computes the Fibonacci function in the naive way n the input value as integer the precise value of fib(n) public static B i g I n t e g e r f i b S i m p l e ( int n ) { i f ( n <= 0) return zero ; i f ( n == 1) return one ; return ( f i b S i m p l e (n 1). add ( f i b S i m p l e (n 2) ) ) ; * Computes the Fibonacci function in time linear to n n the input value as integer the precise value of fib(n) public static B i g I n t e g e r f i b F a s t ( int n ) { return f i b F a s t 2 ( n, zero, one ) ; * idea: * the first parameter stores the number of necessary iterations * the second parameter is fib(i) * the third one is fib(i+1) private static B i g I n t e g e r f i b F a s t 2 ( int n, B i g I n t e g e r f i b i, B i g I n t e g e r f i b i p l u s 1 ) { i f ( n <= 0) return f i b i ; return f i b F a s t 2 ( n 1, f i b i p l u s 1, f i b i. add ( f i b i p l u s 1 ) ) ; Ackermann import java. math. B i g I n t e g e r ; 2
3 * The Ack class computes in various ways the Ackermann function * call the main routine with the specific calculation method as * a simple interface. ( the different calculation methods have very different * time and space consumption) * <p> * Beware of values ack(x,y) with x >= 5 or with x=4 and y >=3 * thiemann 1.0 ( ) public class Ack { * start simple interface asking for values x and y and * then computes and outputs ack(x,y); * additionally the computation time is displayed args There should be passed one argument: the method. * Valid methods ( from slow to fast) are " recursive", " iterative ", "iterativenolist", "direct" public static void main ( S t r i n g [ ] args ) { i f ( args. length! = 1 ) { System. e r r. p r i n t l n ( "Please specify exact one method" ) ; return ; S t r i n g method = args [ 0 ] ; S t r i n g [ ] methods = {"recursive", "iterative", "iterativenolist", " direct" ; for ( int i = 0 ; i < methods. length ; i ++) { i f ( method. e q u a l s ( I n t e g e r. t o S t r i n g ( i +1)) method. e q u a l s ( methods [ i ] ) ) { System. out. p r i n t l n ( "Method: "+methods [ i ] ) ; System. out. p r i n t ( "x: " ) ; int x = IO. readint ( ) ; System. out. p r i n t ( "y: " ) ; int y = IO. readint ( ) ; int a ; B i g I n t e g e r biga ; long time ; time = System. c u r r e n t T i m e M i l l i s ( ) ; switch ( i ) { case 0 : a = ack ( x, y ) ; time += System. c u r r e n t T i m e M i l l i s ( ) ; System. out. p r i n t l n ( a ) ; break ; 3
4 case 1 : a = a c k i t e r ( x, y ) ; time += System. c u r r e n t T i m e M i l l i s ( ) ; System. out. p r i n t l n ( a ) ; break ; case 2 : a = a c k i t e r 2 ( x, y ) ; time += System. c u r r e n t T i m e M i l l i s ( ) ; System. out. p r i n t l n ( a ) ; break ; case 3 : biga = a c k d i r e c t ( x, y ) ; time += System. c u r r e n t T i m e M i l l i s ( ) ; System. out. p r i n t l n ( biga ) ; int length = biga. t o S t r i n g ( ). length ( ) ; i f ( length > 80) { System. out. p r i n t l n ( "Length: " + length ) ; System. out. p r i n t l n ( "Time: "+time+"ms" ) ; return ; System. e r r. p r i n t l n ( "Wrong method! Please choose one of \n" ) ; for ( int i = 0 ; i < methods. length ; i ++) { System. e r r. p r i n t l n ( i+1+") "+methods [ i ] ) ; * computes the Ackermann function ack(x,y) in a recursive way x the first argument to ack y the second argument to ack the value of ack(x,y) as integer public static int ack ( int x, int y ) { i f ( x == 0) return ( y+1) ; i f ( y == 0) return ack ( x 1,1) ; return ack ( x 1, ack ( x, y 1) ) ; * computes the Ackermann function ack(x,y) in an iterative way; * here we use lists to store all the values of ack * ( ack(x,y) space consumption) x the first argument to ack y the second argument to ack the value of ack(x,y) as integer 4
5 public static int a c k i t e r ( int x, int y ) { // initialize fields and lists int [ ] ack want = new int [ x +1]; DoubleLinkedList [ ] v a l u e s = new DoubleLinkedList [ x +1]; I n t e g e r t e s t ; for ( int i x =0; i x <= x ; i x++) { ack want [ i x ] = 1; v a l u e s [ i x ] = new DoubleLinkedList ( ) ; ack want [ x ] = y ; // initialize loop int i x = x ; int i y = y ; // outer loop for x i x l o o p : while ( i x <= x ) { // inner loop for y i y l o o p : while ( v a l u e s [ i x ]. length ( ) <= ack want [ i x ] ) { i y = v a l u e s [ i x ]. length ( ) ; // case 1 i f ( i x == 0) { v a l u e s [ i x ]. addend ( i y + 1) ; continue ; // case 2 i f ( i y == 0) { t e s t = v a l u e s [ ix 1]. getpos ( 1 ) ; i f ( t e s t! = null ) { // value present v a l u e s [ i x ]. addend ( t e s t. intvalue ( ) ) ; continue ; ack want [ ix 1] = 1; i x = 1; continue i x l o o p ; 5
6 // case 3 // we know by construction that a(ix,y-1) is present int ay 1 = v a l u e s [ i x ]. getpos ( iy 1). intvalue ( ) ; t e s t = v a l u e s [ ix 1]. getpos ( ay 1 ) ; i f ( t e s t! = null ) { // value present v a l u e s [ i x ]. addend ( t e s t. intvalue ( ) ) ; continue ; // enforce computation of a(ix-1, ay_1) ack want [ ix 1] = ay 1 ; i x = 1; continue i x l o o p ; // so current ix level completed i x += 1; continue ; return v a l u e s [ x ]. getpos ( y ). intvalue ( ) ; * computes the Ackermann function ack(x,y) in an iterative way; * here we only store the last computed values a(x,i) for each x * ( linear space consumption in x) x the first argument to ack y the second argument to ack the value of ack(x,y) as integer public static int a c k i t e r 2 ( int x, int y ) { int [ ] ack want = new int [ x +1]; int [ ] ack have = new int [ x +1]; int [ ] a c k l a s t = new int [ x +1]; for ( int i x =0; i x <= x ; i x++) { ack want [ i x ] = 1; ack have [ i x ] = 1; a c k l a s t [ i x ] = 1; int i x = x ; int i y = y ; ack want [ x ] = y ; i x l o o p : while ( i x <= x ) { i y l o o p : while ( ack have [ i x ] < ack want [ i x ] ) { 6
7 i y = ack have [ i x ] + 1 ; // case 1 i f ( i x == 0) { a c k l a s t [ i x ] = i y + 1 ; ack have [ i x ] = i y ; continue ; // case 2 i f ( i y == 0) { i f ( ack have [ ix 1] >= 1) { // value present a c k l a s t [ i x ] = ack have [ i x ] = i y ; continue ; ack want [ ix 1] = 1; i x = 1; continue i x l o o p ; a c k l a s t [ ix 1]; // case 3 // we know by construction that a(ix,y-1) is present and is stored in ack_last[ix] int ay 1 = a c k l a s t [ i x ] ; i f ( ack have [ ix 1] >= ay 1 ) { // value present a c k l a s t [ i x ] = a c k l a s t [ ix 1]; ack have [ i x ] = i y ; continue ; // enforce computation of a(ix-1, ay_1) ack want [ ix 1] = ay 1 ; i x = 1; continue i x l o o p ; // so current ix level completed i x += 1; continue ; return a c k l a s t [ x ] ; * Easy access to BigIntegers 7
8 private static B i g I n t e g e r tobig ( int i ) { return new B i g I n t e g e r ( I n t e g e r. t o S t r i n g ( i ) ) ; private f i n a l static B i g I n t e g e r zero = tobig ( 0 ) ; private f i n a l static B i g I n t e g e r one = tobig ( 1 ) ; private f i n a l static B i g I n t e g e r two = tobig ( 2 ) ; private f i n a l static B i g I n t e g e r t h r e e = tobig ( 3 ) ; private f i n a l static B i g I n t e g e r maxint = tobig ( I n t e g e r.max VALUE) ; * Exponentation ( simple version) private static B i g I n t e g e r pow2simple ( B i g I n t e g e r x ) { B i g I n t e g e r r e s = one ; while ( x. compareto ( zero ) > 0) { r e s = r e s. multiply ( two ) ; x = x. s u b t r a c t ( one ) ; return r e s ; * Exponentation with repeated squaring * without bit- shifting operations private static B i g I n t e g e r pow2square ( B i g I n t e g e r x ) { B i g I n t e g e r r e s = one ; B i g I n t e g e r base = two ; while ( x. compareto ( zero ) > 0) { i f ( x. and ( one ). e q u a l s ( one ) ) { r e s = r e s. multiply ( base ) ; base = base. multiply ( base ) ; x = x. d i v i d e ( two ) ; return r e s ; * Exponentation with base 2 by bit- shifting private static B i g I n t e g e r pow2shift ( B i g I n t e g e r x ) { B i g I n t e g e r erg = one ; while ( x. compareto ( maxint ) > 0) { 8
9 erg = erg. s h i f t L e f t ( I n t e g e r.max VALUE) ; x = x. s u b t r a c t ( maxint ) ; erg = erg. s h i f t L e f t ( x. intvalue ( ) ) ; return erg ; * Choose method for potentiation private static B i g I n t e g e r pow2( B i g I n t e g e r x ) { // return pow2simple(x); // return pow2square(x); return pow2shift ( x ) ; * Computing towers of 2 s private static B i g I n t e g e r tower2 ( int h ) { B i g I n t e g e r r e s = one ; while ( h > 0) { r e s = pow2( r e s ) ; h ; return r e s ; * computes the Ackermann function ack(x,y) in an explicit way x the first argument to ack y the second argument to ack the value of ack(x,y) as BigInteger public static B i g I n t e g e r a c k d i r e c t ( int x, int y ) { i f ( x == 5 && y == 0) { x = 4 ; y = 1 ; i f ( x == 0) return tobig ( y+1) ; i f ( x == 1) return tobig ( y+2) ; i f ( x == 2) return tobig (2 y+3) ; i f ( x == 3) return pow2( tobig ( y+3)). s u b t r a c t ( t h r e e ) ; i f ( x == 4 && y <= 2) return tower2 ( y+3). s u b t r a c t ( t h r e e ) ; 9
10 // all other values are to big to be computed System. e r r. p r i n t l n ( "Computing..." ) ; for ( int i = 0 ; i < 20000; i++) { System. e r r. p r i n t ( "." ) ; System. e r r. p r i n t l n ( "\n1. Buy me much more memory" ) ; System. e r r. p r i n t l n ( "2. Buy me a very fast processor" ) ; System. e r r. p r i n t l n ( "3. Attach a display of at least 18*10^19727 * 8 pixel to display the number" ) ; System. e r r. p r i n t l n ( " (If we have 10 pixels per mm, then the width is only 6*10^19701 million light years)" ) ; System. e r r. p r i n t l n ( "4. Wait for output and take your time to read it" ) ; System. e r r. p r i n t l n ( "\notherwise I give up here," ) ; System. e r r. p r i n t l n ( "Your computer" ) ; System. e x i t ( 0 ) ; return null ; Aufgabe 2 * Double- linked list of integers with constant access * functions on both ends and constant length computation. * Direct access to elements in the middle costs at most * length/2 iterations. * Duplicates are allowed. * The implementation bases on the <code >Elem </ code > * data structure. * thiemann Elem public class DoubleLinkedList { private Elem head ; // head of the list; null, if list is empty private Elem end ; // end of the list; null, if list is empty private int length ; // the current list-length 10
11 * Standard- Representation ( linear time) a String representing the list from head to end public S t r i n g t o S t r i n g ( ) { return t o S t r i n g ( this. head, f a l s e ) ; * Standard- Representation of the reversed list ( linear time) a String representing the list from end to head public S t r i n g t o S t r i n g R e v e r s e ( ) { return t o S t r i n g ( this. head, true ) ; * produces a string representing the list beginning at * the given parameter " e" in given direction private S t r i n g t o S t r i n g ( Elem e, boolean r e v e r s e ) { i f ( e == null ) { return "" ; else { int v = e. getvalue ( ) ; i f ( r e v e r s e ) { return ( t o S t r i n g ( e. getright ( ), r e v e r s e )+" "+v ) ; else { return ( v+" "+t o S t r i n g ( e. getright ( ), r e v e r s e ) ) ; * Computes the list - length ( in constant time) the list length public int length ( ) { return this. length ; 11
12 * Creates an empty list public DoubleLinkedList ( ) { this. head = null ; this. end = null ; this. length = 0 ; * computes the value of the * first element of the list, if this exists ( constant time) null, if the list is empty; the first element otherwise public I n t e g e r gethead ( ) { return getpos ( 0 ) ; * computes the value of the last element of the list, * if this exists ( constant time) null, if the list is empty; the last element otherwise public I n t e g e r getend ( ) { return getpos ( this. length 1) ; * computes the value of the element at position p of the list. Here, we * start counting from 0, so a valid range for p is 0 <= p < length * ( linear time, but at most length/2 iterations) null, if the position is out of bounds, or otherwise * the element at the given position public I n t e g e r getpos ( int p ) { Elem e = getpospriv ( p) ; i f ( e == null ) { return null ; return new I n t e g e r ( e. getvalue ( ) ) ; 12
13 * Adds the value v in front of the list ( in constant time) v the integer to be added public void addhead( int v ) { Elem e = new Elem( v, null, head ) ; i f ( this. head! = null ) { // non-empty list this. head. s e t L e f t ( e ) ; this. head = e ; else { // empty list head = e ; end = e ; this. length ++; * Adds the value v at the end of the list ( in constant time) v the integer to be added public void addend ( int v ) { Elem e = new Elem( v, end, null ) ; i f ( this. end! = null ) { // non-empty list this. end. setright ( e ) ; this. end = e ; else { // empty list this. head = e ; this. end = e ; this. length ++; * adds the value v before element at position p; * to store an element at the end, p should be length; * look at <code >getpos </ code > for more information * about positions. * v the value to be inserted 13
14 p insert before this element; 0 <= p <= length true, if v was inserted; false, otherwise public boolean addpos ( int v, int p ) { // check for valid position i f ( p < 0 p > this. length ) { return f a l s e ; // deal with special cases i f ( p == 0) { addhead( v ) ; else i f ( p == this. length ) { addend ( v ) ; else { // now, we have to add the element in the middle // so, no update of head or end has to be done! // catch the right element Elem curr = getpospriv ( p 1) ; // and update all pointers from the elements Elem e = new Elem( v, curr, curr. getright ( ) ) ; curr. setright ( e ) ; e. getright ( ). s e t L e f t ( e ) ; this. length ++; return true ; * get the element at position p starting from the best side * null, if p is not valid private Elem getpospriv ( int p ) { // position valid? i f ( p < 0 p >= this. length ) return null ; // which way boolean forward = this. length p > p ; // start iteration i f ( forward ) { return DoubleLinkedList. getpos (p, forward, this. head ) ; else { return DoubleLinkedList. getpos ( this. length p 1, forward, this. end ) ; 14
15 * walks count steps from current Element in direction * and returns the element at this position private static Elem getpos ( int count, boolean forward, Elem c u r r e n t ) { i f ( count == 0) { return c u r r e n t ; else i f ( forward ) { return getpos ( count 1, forward, c u r r e n t. getright ( ) ) ; else { return getpos ( count 1, forward, c u r r e n t. g e t L e f t ( ) ) ; * searches for the value v in forward direction, or * backward direction, if " forward" = false private Elem search ( int v, boolean forward ) { i f ( forward ) { return DoubleLinkedList. searchf ( v, this. head ) ; else { return DoubleLinkedList. searchb ( v, this. end ) ; * searches for v beginning from current in forward direction private static Elem searchf ( int v, Elem c u r r e n t ) { // value found? i f ( c u r r e n t. getvalue ( ) == v ) { return c u r r e n t ; // reached end? else i f ( c u r r e n t. getright ( )! = null ) { // no, then iterate return searchf ( v, c u r r e n t. getright ( ) ) ; else { 15
16 // yes, then the value is not present return null ; * searches for v beginning from current in backward direction private static Elem searchb ( int v, Elem c u r r e n t ) { // value found? i f ( c u r r e n t. getvalue ( ) == v ) { return c u r r e n t ; // reached end? else i f ( c u r r e n t. g e t L e f t ( )! = null ) { // iterate return searchb ( v, c u r r e n t. g e t L e f t ( ) ) ; else { // failure return null ; * deletes the element e from the list private boolean d e l e t e ( Elem e ) { // delete null- element can not be done i f ( e == null ) return f a l s e ; // update with left neighbor // special case head- element i f ( e == this. head ) { this. head = e. getright ( ) ; // test, if head was not the only element i f ( this. head! = null ) { this. head. s e t L e f t ( null ) ; else { // usual left element e. g e t L e f t ( ). setright ( e. getright ( ) ) ; // update with right neighbor 16
17 // special case end- element i f ( e == this. end ) { this. end = e. g e t L e f t ( ) ; // test, if end was not the only element i f ( this. end! = null ) { this. end. setright ( null ) ; else { // usual right element e. getright ( ). s e t L e f t ( e. g e t L e f t ( ) ) ; this. length ; return true ; * Deletes the head of the list * (constant time) true, if there was a head ( if list was not empty); * false, otherwise public boolean deletehead ( ) { return d e l e t e ( this. head ) ; * Deletes the end of the list * (constant time) true, if there was a end ( if list was not empty); * false, otherwise public boolean deleteend ( ) { return d e l e t e ( this. end ) ; * Deletes the first occurence of the value v in the list * (linear time) v the value to be deleted true, iff something was deleted public boolean d e l e t e F i r s t ( int v ) { Elem e = search ( v, true ) ; 17
18 return d e l e t e ( e ) ; * Deletes the last occurence of the value v in the list * (linear time) v the value to be deleted true, iff something was deleted public boolean d e l e t e L a s t ( int v ) { Elem e = search ( v, f a l s e ) ; return d e l e t e ( e ) ; * deletes all elements with value v in the list * (linear time) v the value to be deleted the number of elements that have been deleted public int d e l e t e A l l ( int v ) { return d e l e t e A l l ( v, this. head ) ; * delete all elements that have value v * beginning from current, iterating in forward * direction * current must be non- null! private int d e l e t e A l l ( int v, Elem c u r r e n t ) { i f ( c u r r e n t == null ) return 0 ; // fetch next element v c u r r e n t = DoubleLinkedList. searchf ( v, c u r r e n t ) ; // found something? i f ( c u r r e n t! = null ) { // yes, then delete and look further d e l e t e ( c u r r e n t ) ; return (1+ d e l e t e A l l ( v, c u r r e n t. getright ( ) ) ) ; // no, then abort 18
19 return 0 ; * tests, whether the value v is stored in the list v the test value true, iff v was in the list public boolean elem ( int v ) { // direction does not matter Elem e = search ( v, true ) ; return ( e! = null ) ; 19
7. The Recursion Theorem
7. The Recursion Theorem Main result in this section: Kleene s Recursion Theorem. Recursive functions are closed under a very general form of recursion. For proof we will use the S-m-n-theorem. Used in
More information7. The Recursion Theorem
7. The Recursion Theorem Main result in this section: Kleene s Recursion Theorem. Recursive functions are closed under a very general form of recursion. For the proof we will use the S-m-n-theorem. Used
More informationIntroduction to Programming (Java) 3/12
Introduction to Programming (Java) 3/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction
More information1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;
1 Trees The next major set of data structures belongs to what s called Trees. They are called that, because if you try to visualize the structure, it kind of looks like a tree (root, branches, and leafs).
More informationCMSC 132, Object-Oriented Programming II Summer Lecture 6:
CMSC 132, Object-Oriented Programming II Summer 2016 Lecturer: Anwar Mamat Lecture 6: Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 6.1 Singly
More informationLecture 14: Nov. 11 & 13
CIS 2168 Data Structures Fall 2014 Lecturer: Anwar Mamat Lecture 14: Nov. 11 & 13 Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 14.1 Sorting
More information7. The Recursion Theorem
7. The Recursion Theorem Main result in this section: Kleene s Recursion Theorem. Recursive functions are closed under a very general form of recursion. For proof we will use the S-m-n-theorem. Used in
More informationCMSC 132, Object-Oriented Programming II Summer Lecture 10:
CMSC 132, Object-Oriented Programming II Summer 2016 Lecturer: Anwar Mamat Lecture 10: Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 10.1 RECURSION
More informationBinary Search Trees. Motivation
Binary Search Trees Motivation Searching for a particular record in an unordered list takes O(n), too slow for large lists (databases) If the list is ordered, can use an array implementation and use binary
More informationSearching, mainly via Hash tables
Data structures and algorithms Part 11 Searching, mainly via Hash tables Petr Felkel 26.1.2007 Topics Searching Hashing Hash function Resolving collisions Hashing with chaining Open addressing Linear Probing
More informationSection Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)
Section 2.4 Section Summary Sequences. o Examples: Geometric Progression, Arithmetic Progression Recurrence Relations o Example: Fibonacci Sequence Summations Special Integer Sequences (optional) Sequences
More informationIntroduction to Computer Programming, Spring Term 2018 Practice Assignment 5 Discussion: power(m,n) = m n
German University in Cairo Media Engineering and Technology Prof. Dr. Slim Abdennadher Dr. Mohammed Abdel Megeed Introduction to Computer Programming, Spring Term 2018 Practice Assignment 5 Discussion:
More information1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal
Präsenzstunden Today In the same room as in the first week Assignment 5 Felix Friedrich, Lars Widmer, Fabian Stutz TA lecture, Informatics II D-BAUG March 18, 2014 HIL E 15.2 15:00-18:00 Timon Gehr (arriving
More informationRecursion: Introduction and Correctness
Recursion: Introduction and Correctness CSE21 Winter 2017, Day 7 (B00), Day 4-5 (A00) January 25, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 Today s Plan From last time: intersecting sorted lists and
More informationOutline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University
COMP SCI / SFWR ENG 2S03 Department of Computing and Software McMaster University Week 10: November 7 - November 11 Outline 1 Resource 2 Introduction Nodes Illustration 3 Usage Accessing and Modifying
More informationTricks of the Trade in Combinatorics and Arithmetic
Tricks of the Trade in Combinatorics and Arithmetic Zachary Friggstad Programming Club Meeting Fast Exponentiation Given integers a, b with b 0, compute a b exactly. Fast Exponentiation Given integers
More informationCSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015 Today Registration should be done. Homework 1 due 11:59pm next Wednesday, April 8 th. Review math
More informationLecture 5: Sep. 23 &25
CIS 2168 Data Structures Fall 2014 Lecturer: Anwar Mamat Lecture 5: Sep. 23 &25 Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 5.1 Doubly Linked
More informationMathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:
Mathematical Background Mathematical Background CSE 373 Data Structures Today, we will review: Logs and eponents Series Recursion Motivation for Algorithm Analysis 5 January 007 CSE 373 - Math Background
More informationCS173 Running Time and Big-O. Tandy Warnow
CS173 Running Time and Big-O Tandy Warnow CS 173 Running Times and Big-O analysis Tandy Warnow Today s material We will cover: Running time analysis Review of running time analysis of Bubblesort Review
More informationElementary Sorts 1 / 18
Elementary Sorts 1 / 18 Outline 1 Rules of the Game 2 Selection Sort 3 Insertion Sort 4 Shell Sort 5 Visualizing Sorting Algorithms 6 Comparing Sorting Algorithms 2 / 18 Rules of the Game Sorting is the
More informationAdministrivia. COMP9020 Lecture 7 Session 2, 2017 Induction and Recursion. Lecture 6 recap. Lecture 6 recap
Administrivia COMP9020 Lecture 7 Session 2, 2017 Induction and Recursion Guidelines for good mathematical writing Assignment 1 Solutions now available; marks available soon Assignment 2 available on Saturday,
More informationIntroduction to Computing II (ITI 1121) FINAL EXAMINATION
Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of engineering School of Electrical Engineering and Computer Science Identification
More informationAlgorithms and Data Structures
Algorithms and Data Structures, Divide and Conquer Albert-Ludwigs-Universität Freiburg Prof. Dr. Rolf Backofen Bioinformatics Group / Department of Computer Science Algorithms and Data Structures, December
More informationThomas Jefferson Invitational Open in Informatics
Thomas Jefferson Invitational Open in Informatics Sample Problems (With Solutions) Version 2.01.1 By programmers, For programmers Preface Preface Welcome to the TJ IOI Sample Problems document. Here, you
More informationCMSC 132, Object-Oriented Programming II Summer Lecture 12
CMSC 132, Object-Oriented Programming II Summer 2016 Lecturer: Anwar Mamat Lecture 12 Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 12.1 Trees
More informationGoogle Go illustrated on the basis of Fibonacci numbers
Google Go illustrated on the basis of Fibonacci numbers Jan Pennekamp RWTH University Aachen Chair for Data Management and Data Exploration Prof. Dr. T. Seidl Supervision: Dipl.-Ing. Marwan Hassani Friday,
More informationLecture 4: Stacks and Queues
Reading materials Goodrich, Tamassia, Goldwasser (6th), chapter 6 OpenDSA (https://opendsa-server.cs.vt.edu/odsa/books/everything/html/): chapter 9.8-13 Contents 1 Stacks ADT 2 1.1 Example: CharStack ADT
More informationCS 61B Asymptotic Analysis Fall 2017
CS 61B Asymptotic Analysis Fall 2017 1 More Running Time Give the worst case and best case running time in Θ( ) notation in terms of M and N. (a) Assume that slam() Θ(1) and returns a boolean. 1 public
More informationIntroduction to Computing II (ITI 1121) FINAL EXAMINATION
Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of engineering School of Electrical Engineering and Computer Science Introduction
More informationSoftwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany
Softwaretechnik Lecture 13: Design by Contract Peter Thiemann University of Freiburg, Germany 25.06.2012 Table of Contents Design by Contract Contracts for Procedural Programs Contracts for Object-Oriented
More informationSoftwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany
Softwaretechnik Lecture 13: Design by Contract Peter Thiemann University of Freiburg, Germany 25.06.2012 Table of Contents Design by Contract Contracts for Procedural Programs Contracts for Object-Oriented
More informationLisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88
Lisp Introduction Dr. Neil T. Dantam CSCI-498/598 RPM, Colorado School of Mines Spring 28 Dantam (Mines CSCI, RPM) Lisp Spring 28 / 88 Outline Lisp Common Lisp by Example Implementation Details Typing
More informationObject Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20
Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20 This is a closed-book exam. Any form of cheating or lying will not be tolerated. Students can get zero scores and/or fail the class and/or
More informationMotivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis
Motivation Introduction to Algorithms Hash Tables CSE 680 Prof. Roger Crawfis Arrays provide an indirect way to access a set. Many times we need an association between two sets, or a set of keys and associated
More informationMathematical Induction
Mathematical Induction MAT231 Transition to Higher Mathematics Fall 2014 MAT231 (Transition to Higher Math) Mathematical Induction Fall 2014 1 / 21 Outline 1 Mathematical Induction 2 Strong Mathematical
More informationMATHEMATICAL OBJECTS in
MATHEMATICAL OBJECTS in Computational Tools in a Unified Object-Oriented Approach Yair Shapira @ CRC Press Taylor & Francis Group Boca Raton London New York CRC Press is an imprint of the Taylor & Francis
More informationProgramming Abstractions
Programming Abstractions C S 1 0 6 X Cynthia Lee Today s topics: Performance issues in recursion Big-O performance analysis 2 Announcement: Recursive art contest! Go to http://recursivedrawing.com/ Make
More informationTopic 17. Analysis of Algorithms
Topic 17 Analysis of Algorithms Analysis of Algorithms- Review Efficiency of an algorithm can be measured in terms of : Time complexity: a measure of the amount of time required to execute an algorithm
More information1. Write a program to calculate distance traveled by light
G. H. R a i s o n i C o l l e g e O f E n g i n e e r i n g D i g d o h H i l l s, H i n g n a R o a d, N a g p u r D e p a r t m e n t O f C o m p u t e r S c i e n c e & E n g g P r a c t i c a l M a
More informationIntroduction to Algorithmic Complexity. D. Thiebaut CSC212 Fall 2014
Introduction to Algorithmic Complexity D. Thiebaut CSC212 Fall 2014 http://youtu.be/p0tlbl5lrj8 Case Study: Fibonacci public class RecurseFib {! private static long computefibrecursively( int n ) { if
More informationData Structure Lecture#4: Mathematical Preliminaries U Kang Seoul National University
Data Structure Lecture#4: Mathematical Preliminaries U Kang Seoul National University U Kang 1 In This Lecture Set concepts and notation Logarithms Summations Recurrence Relations Recursion Induction Proofs
More informationCMSC 132, Object-Oriented Programming II Summer Lecture 11:
CMSC 132, Object-Oriented Programming II Summer 2016 Lecturer: Anwar Mamat Lecture 11: Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 11.1 Recursion
More informationLecture 5: Jun. 10, 2015
CMSC 132, Object-Oriented Programming II Summer 2015 Lecturer: Anwar Mamat Lecture 5: Jun. 10, 2015 Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor.
More informationRecurrence Relations and Recursion: MATH 180
Recurrence Relations and Recursion: MATH 180 1: Recursively Defined Sequences Example 1: The sequence a 1,a 2,a 3,... can be defined recursively as follows: (1) For all integers k 2, a k = a k 1 + 1 (2)
More informationHW8. Due: November 1, 2018
CSCI 1010 Theory of Computation HW8 Due: November 1, 2018 Attach a fully filled-in cover sheet to the front of your printed homework Your name should not appear anywhere; the cover sheet and each individual
More informationProblem Set 1 Solutions
Introduction to Algorithms September 24, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Piotr Indyk and Charles E. Leiserson Handout 7 Problem Set 1 Solutions Exercise 1-1. Do Exercise
More informationData Structures and Algorithms
Data Structures and Algorithms CS245-2017S-22 Dynamic Programming David Galles Department of Computer Science University of San Francisco 22-0: Dynamic Programming Simple, recursive solution to a problem
More informationCS-140 Fall 2018 Test 2 Version Practice Nov. 12, Name:
CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018 Name: 1. (10 points) For the following, Check T if the statement is true, or F if the statement is false. (a) T F : If a child overrides its parent
More informationData Structures and Algorithms Winter Semester
Page 0 German University in Cairo December 26, 2015 Media Engineering and Technology Faculty Prof. Dr. Slim Abdennadher Dr. Wael Abouelsadaat Data Structures and Algorithms Winter Semester 2015-2016 Final
More informationSDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version
SDS developer guide Develop distributed and parallel applications in Java Nathanaël Cottin sds@ncottin.net http://sds.ncottin.net version 0.0.3 Copyright 2007 - Nathanaël Cottin Permission is granted to
More informationCSCB63 Winter Week 11 Bloom Filters. Anna Bretscher. March 30, / 13
CSCB63 Winter 2019 Week 11 Bloom Filters Anna Bretscher March 30, 2019 1 / 13 Today Bloom Filters Definition Expected Complexity Applications 2 / 13 Bloom Filters (Specification) A bloom filter is a probabilistic
More informationCSE 311: Foundations of Computing. Lecture 26: Cardinality
CSE 311: Foundations of Computing Lecture 26: Cardinality Cardinality and Computability Computers as we know them grew out of a desire to avoid bugs in mathematical reasoning A brief history of reasoning
More informationIntroduction to Computing II (ITI1121) FINAL EXAMINATION
Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of engineering School of Electrical Engineering and Computer Science Identification
More informationLecture 2. Fundamentals of the Analysis of Algorithm Efficiency
Lecture 2 Fundamentals of the Analysis of Algorithm Efficiency 1 Lecture Contents 1. Analysis Framework 2. Asymptotic Notations and Basic Efficiency Classes 3. Mathematical Analysis of Nonrecursive Algorithms
More informationQuiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)
Introduction to Algorithms October 13, 2010 Massachusetts Institute of Technology 6.006 Fall 2010 Professors Konstantinos Daskalakis and Patrick Jaillet Quiz 1 Solutions Quiz 1 Solutions Problem 1. We
More informationimport java. u t i l. ;... Scanner sc = new Scanner ( System. in ) ;
CPSC 490 Input Input will always arrive on stdin. You may assume input is well-formed with respect to the problem specification; inappropriate input (e.g. text where a number was specified, number out
More informationRemainders. We learned how to multiply and divide in elementary
Remainders We learned how to multiply and divide in elementary school. As adults we perform division mostly by pressing the key on a calculator. This key supplies the quotient. In numerical analysis and
More informationSection Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence
Section 2.4 Section Summary Sequences. Examples: Geometric Progression, Arithmetic Progression Recurrence Relations Example: Fibonacci Sequence Summations Introduction Sequences are ordered lists of elements.
More informationLists, Stacks, and Queues (plus Priority Queues)
Lists, Stacks, and Queues (plus Priority Queues) The structures lists, stacks, and queues are composed of similar elements with different operations. Likewise, with mathematics: (Z, +, 0) vs. (Z,, 1) List
More informationBinary addition example worked out
Binary addition example worked out Some terms are given here Exercise: what are these numbers equivalent to in decimal? The initial carry in is implicitly 0 1 1 1 0 (Carries) 1 0 1 1 (Augend) + 1 1 1 0
More information1 Closest Pair of Points on the Plane
CS 31: Algorithms (Spring 2019): Lecture 5 Date: 4th April, 2019 Topic: Divide and Conquer 3: Closest Pair of Points on a Plane Disclaimer: These notes have not gone through scrutiny and in all probability
More informationAutomatic Verification of Parameterized Data Structures
Automatic Verification of Parameterized Data Structures Jyotirmoy V. Deshmukh, E. Allen Emerson and Prateek Gupta The University of Texas at Austin The University of Texas at Austin 1 Outline Motivation
More informationSolution suggestions for examination of Logic, Algorithms and Data Structures,
Department of VT12 Software Engineering and Managment DIT725 (TIG023) Göteborg University, Chalmers 24/5-12 Solution suggestions for examination of Logic, Algorithms and Data Structures, Date : April 26,
More informationCSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo
CSE 431/531: Analysis of Algorithms Dynamic Programming Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Paradigms for Designing Algorithms Greedy algorithm Make a
More informationHoare Calculus and Predicate Transformers
Hoare Calculus and Predicate Transformers Wolfgang Schreiner Wolfgang.Schreiner@risc.uni-linz.ac.at Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria http://www.risc.uni-linz.ac.at
More informationAside: Golden Ratio. Golden Ratio: A universal law. Golden ratio φ = lim n = 1+ b n = a n 1. a n+1 = a n + b n, a n+b n a n
Aside: Golden Ratio Golden Ratio: A universal law. Golden ratio φ = lim n a n+b n a n = 1+ 5 2 a n+1 = a n + b n, b n = a n 1 Ruta (UIUC) CS473 1 Spring 2018 1 / 41 CS 473: Algorithms, Spring 2018 Dynamic
More informationMathematical Background
Chapter 1 Mathematical Background When we analyze various algorithms in terms of the time and the space it takes them to run, we often need to work with math. That is why we ask you to take MA 2250 Math
More informationHeaps and Priority Queues
Heaps and Priority Queues Motivation Situations where one has to choose the next most important from a collection. Examples: patients in an emergency room, scheduling programs in a multi-tasking OS. Need
More informationData Structures and Algorithms Chapter 2
1 Data Structures and Algorithms Chapter 2 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples
More informationCSCI Honor seminar in algorithms Homework 2 Solution
CSCI 493.55 Honor seminar in algorithms Homework 2 Solution Saad Mneimneh Visiting Professor Hunter College of CUNY Problem 1: Rabin-Karp string matching Consider a binary string s of length n and another
More informationRunning Time Evaluation
Running Time Evaluation Quadratic Vs. Linear Time Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 19 1 Running time 2 Examples 3 Big-Oh, Big-Omega, and Big-Theta Tools 4 Time
More informationDNHI Homework 2 Solutions Recursion
Solutions Recursion Problem 1 Part A Write an iterative method that computes a value of x n for a positive integer n and a real number x. The return value of -1 indicates an error condition. 1 public static
More informationMathematical Induction
Mathematical Induction MAT30 Discrete Mathematics Fall 018 MAT30 (Discrete Math) Mathematical Induction Fall 018 1 / 19 Outline 1 Mathematical Induction Strong Mathematical Induction MAT30 (Discrete Math)
More informationAxiomatic Semantics. Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11 CSE
Axiomatic Semantics Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11 CSE 6341 1 Outline Introduction What are axiomatic semantics? First-order logic & assertions about states Results (triples)
More informationStacks. Definitions Operations Implementation (Arrays and Linked Lists) Applications (system stack, expression evaluation) Data Structures 1 Stacks
Stacks Definitions Operations Implementation (Arrays and Linked Lists) Applications (system stack, expression evaluation) Data Structures 1 Stacks Stacks: Definitions and Operations A LIFO list: Last In,
More information(a) Definition of TMs. First Problem of URMs
Sec. 4: Turing Machines First Problem of URMs (a) Definition of the Turing Machine. (b) URM computable functions are Turing computable. (c) Undecidability of the Turing Halting Problem That incrementing
More informationSharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017
Pieter van den Hombergh Fontys Hogeschool voor Techniek en Logistiek February 15, 2017 and /FHTenL February 15, 2017 is safe Idioms 1/34 and and is safe Idioms /FHTenL February 15, 2017 2/34 visibility
More informationAlgorithm efficiency analysis
Algorithm efficiency analysis Mădălina Răschip, Cristian Gaţu Faculty of Computer Science Alexandru Ioan Cuza University of Iaşi, Romania DS 2017/2018 Content Algorithm efficiency analysis Recursive function
More informationInsert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park
1617 Preview Data Structure Review COSC COSC Data Structure Review Linked Lists Stacks Queues Linked Lists Singly Linked List Doubly Linked List Typical Functions s Hash Functions Collision Resolution
More informationClock-driven scheduling
Clock-driven scheduling Also known as static or off-line scheduling Michal Sojka Czech Technical University in Prague, Faculty of Electrical Engineering, Department of Control Engineering November 8, 2017
More informationCS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:
CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : If a child overrides
More information= 1 when k = 0 or k = n. Otherwise, Fact 4.5 (page 98) says. n(n 1)(n 2) (n k + 3)(n k + 2)(n k + 1) (n k + 1)(n k + 2)(n k + 3) (n 2)(n 1) n
Solutions for Chapter 6 189 6.7 Solutions for Chapter 6 Sections 6.1, 6.2 and 6.3 1. Write an algorithm whose input is an integer n and whose output is the first n Fibonacci numbers. Algorithm: to compute
More informationCSC 7101: Programming Language Structures 1. Axiomatic Semantics. Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11.
Axiomatic Semantics Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11 1 Overview We ll develop proof rules, such as: { I b } S { I } { I } while b do S end { I b } That allow us to verify
More informationData Structure Lecture#4: Mathematical Preliminaries U Kang Seoul National University
Data Structure Lecture#4: Mathematical Preliminaries U Kang Seoul National University U Kang 1 In This Lecture Set Concepts and Notation Relation Logarithm and Summations Recurrence Relations Recursion
More informationIntroduction to Computing II (ITI 1121) MIDTERM EXAMINATION
Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of Engineering School of Electrical Engineering and Computer Science Identification
More information3. Algorithms. What matters? How fast do we solve the problem? How much computer resource do we need?
3. Algorithms We will study algorithms to solve many different types of problems such as finding the largest of a sequence of numbers sorting a sequence of numbers finding the shortest path between two
More informationArtificial Intelligence Chapter 7: Logical Agents
Artificial Intelligence Chapter 7: Logical Agents Michael Scherger Department of Computer Science Kent State University February 20, 2006 AI: Chapter 7: Logical Agents 1 Contents Knowledge Based Agents
More informationPart I: Definitions and Properties
Turing Machines Part I: Definitions and Properties Finite State Automata Deterministic Automata (DFSA) M = {Q, Σ, δ, q 0, F} -- Σ = Symbols -- Q = States -- q 0 = Initial State -- F = Accepting States
More informationLecture 27: Theory of Computation. Marvin Zhang 08/08/2016
Lecture 27: Theory of Computation Marvin Zhang 08/08/2016 Announcements Roadmap Introduction Functions Data Mutability Objects This week (Applications), the goals are: To go beyond CS 61A and see examples
More informationAdvanced topic: Space complexity
Advanced topic: Space complexity CSCI 3130 Formal Languages and Automata Theory Siu On CHAN Chinese University of Hong Kong Fall 2016 1/28 Review: time complexity We have looked at how long it takes to
More informationSoftware Engineering
Software Engineering Lecture 07: Design by Contract Peter Thiemann University of Freiburg, Germany 02.06.2014 Table of Contents Design by Contract Contracts for Procedural Programs Contracts for Object-Oriented
More informationLecture 2: Divide and conquer and Dynamic programming
Chapter 2 Lecture 2: Divide and conquer and Dynamic programming 2.1 Divide and Conquer Idea: - divide the problem into subproblems in linear time - solve subproblems recursively - combine the results in
More informationCSE 20. Lecture 4: Introduction to Boolean algebra. CSE 20: Lecture4
CSE 20 Lecture 4: Introduction to Boolean algebra Reminder First quiz will be on Friday (17th January) in class. It is a paper quiz. Syllabus is all that has been done till Wednesday. If you want you may
More informationSolving Recurrences. Lecture 23 CS2110 Fall 2011
Solving Recurrences Lecture 23 CS2110 Fall 2011 1 Announcements Makeup Prelim 2 Monday 11/21 7:30-9pm Upson 5130 Please do not discuss the prelim with your classmates! Quiz 4 next Tuesday in class Topics:
More informationData Structures. Outline. Introduction. Andres Mendez-Vazquez. December 3, Data Manipulation Examples
Data Structures Introduction Andres Mendez-Vazquez December 3, 2015 1 / 53 Outline 1 What the Course is About? Data Manipulation Examples 2 What is a Good Algorithm? Sorting Example A Naive Algorithm Counting
More informationThe Viterbi Algorithm EECS 869: Error Control Coding Fall 2009
1 Bacground Material 1.1 Organization of the Trellis The Viterbi Algorithm EECS 869: Error Control Coding Fall 2009 The Viterbi algorithm (VA) processes the (noisy) output sequence from a state machine
More informationCSE 20 DISCRETE MATH. Fall
CSE 20 DISCRETE MATH Fall 2017 http://cseweb.ucsd.edu/classes/fa17/cse20-ab/ Today's learning goals Define and compute the cardinality of a set. Use functions to compare the sizes of sets. Classify sets
More informationFPGA Resource Utilization Estimates for NI PXI-7854R. LabVIEW FPGA Version: 8.6 NI-RIO Version: 3.0 Date: 8/5/2008
FPGA Resource Utilization Estimates for NI PXI-7854R LabVIEW FPGA Version: 8.6 NI-RIO Version: 3.0 Date: 8/5/2008 Note: The numbers presented in this document are estimates. Actual resource usage for your
More informationOptimisation and Operations Research
Optimisation and Operations Research Lecture 12: Algorithm Analysis and Complexity Matthew Roughan http://www.maths.adelaide.edu.au/matthew.roughan/ Lecture_notes/OORII/
More information