CSC 205 – Java Programming II Lecture 22 March 1, 2002.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

 O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Introduction and a Review of Basic Concepts
Cmpt-225 Algorithm Efficiency.
Analysis of Recursive Algorithms
TTIT33 Algorithms and Optimization – Lecture 1 Jan Maluszynski - HT Analysis of Algorithms Introductory Example Principles of Analysis Big-Oh notation.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Lecture 9 Concepts of Programming Languages
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CSE332: Data Abstractions Section 2 HyeIn Kim Spring 2013.
Information and Computer Sciences University of Hawaii, Manoa
Analysis of Algorithms
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
CSE 1342 Programming Concepts Recursion. Overview of Recursion nRecursion is present when a function is defined in terms of itself. nThe factorial of.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
Chapter 8 Recursion Modified.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.
Complexity of Algorithms
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Introduction to Programming (in C++) Complexity Analysis of Algorithms
Lecture 3 Analysis of Algorithms, Part II. Plan for today Finish Big Oh, more motivation and examples, do some limit calculations. Little Oh, Theta notation.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
5.3 EVALUATION OF POSTFIX EXPRESSION For example, consider the evaluation of the following postfix expression using stacks: abc+d*f/- where, a=6, b=3,
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
UNIT 17 Recursion: Towers of Hanoi.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
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.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Week 15 – Monday.  What did we talk about last time?  Tries.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Recursion.
Algorithm Analysis 1.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Analysis of Recursive Algorithms
Analysis of Algorithms
Analysis of Algorithms
Stacks.
Week 15 – Monday CS221.
STACKS.
Lecture 9 Concepts of Programming Languages
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
What is CS 253 about? Contrary to the wide spread belief that the #1 job of computers is to perform calculations (which is why the are called “computers”),
CSE 1342 Programming Concepts
CSC 205 Java Programming II
Analysis of Algorithms
Queues CSC212.
CSC 143 Recursion.
Lecture 16 Stacks and Queues CSE /26/2018.
Analysis of Recursive Algorithms
CS210- Lecture 3 Jun 6, 2005 Announcements
Lecture 16 Stacks and Queues CSE /26/2018.
Analysis of Algorithms
Algorithms and data structures: basic definitions
Lecture 9 Concepts of Programming Languages
Analysis of Recursive Algorithms
Presentation transcript:

CSC 205 – Java Programming II Lecture 22 March 1, 2002

User’s v.s. Developer’s Views User’s view –A better name could be client or external view –A user or client of an object o shouldn’t be bothered with the implementation details of o How does o store its state? How does o perform an action? Developer’s view –A better name could be internal view

Client of Server Roles Program that uses collection add remove size contains Client object (e.g. a MyMusic object) Server object (e.g. the LinkedCollection object) Entities

Why Do We Need Abstraction

Using Collection Objects cd1 cd2null cassette1null 0 1 musics: Linked Collection MyMusics aTitle 1 cassette2 musics[0]: LinkedCollection musics[1]: LinkedCollection m: Music Add a Music object m musics[m.getType()].add(m); Remove a Music object m musics[m.getType()].remove(m);

Internal View YESNO MAYBE head newEntry null Add a new entry newEntry newEntry.next = head; head = newEntry;

The equals Method prublic boolean equals (Object o) { if ( !(o instance of Date) ) return false; else Date d = (Date) o; return (year==d.getYear() && month==d.getMonth() && day==d.getDay()); }

Big O Notations A rough estimation of how fast a function f(n) increase with the growth of n The hierarchy of orders O(1)  O(log 2 n)  O(n)  O(n log 2 n)  O(n 2 )  …  O(2 n ) When n = 1,000,000 – log 2 n ~ 20 – n = 1,000,000 –n log 2 n ~ 20,000,000 – n 2 = 1,000,000,000,000

The Smallest Upper Bound Question 4(a) –Expand the expression f(n) = (2+n)(3+log 2 n) = 6+2log 2 n+3n+n log 2 n –Find the highest order term n log 2 n  O(n log 2 n) –Use definition f(n) = K In this case K = 8, C = 4.

Show Equivalency Question 4(b) –Use definition f(n) is O(n+7) means f(n) = K –Substitute C(n +7) = Cn +7C = C –Proved f(n) = K ’  f(n) is O(n) where C ’ = C+7, K ’ = C

Recursion private static int puzzle (int n) { if ( (n % 3) == 2 ) return 1; //base case else if ( (n % 3) == 1 ) return ( puzzle (n + 1) + 2 ); else return ( puzzle (n / 3) + 1 ); }

Tracing Recursive Processes puzzle(9) LV: n=9 RA: return puzzle(3)+1 puzzle(3) LV: n=3 RA: return puzzle(2)+1 puzzle(2) LV: n=2 RA: return puzzle(1)+1 puzzle(1) return 1 Push execution state in stack: Local Variables Return Address Pop execution state from stack: Local Variables Return Address

Towers of Hanoi

Algorithm move (n, orig, dest, temp) { if (n == 1) Move disk from orig to dest; else { move (n - 1, orig, temp, dest); move (1, orig, dest, temp); move (n - 1, temp, dest, orig) ; } // else }

Tracing Recursive Processes