Week 3 -Wednesday What did we talk about last time? Project 1 More - - PowerPoint PPT Presentation

week 3 wednesday what did we talk about last time project
SMART_READER_LITE
LIVE PREVIEW

Week 3 -Wednesday What did we talk about last time? Project 1 More - - PowerPoint PPT Presentation

Week 3 -Wednesday What did we talk about last time? Project 1 More on Big Oh notation O establishes an upper bound f ( n ) is O ( g ( n )) if there exist positive numbers c and N such that f ( n ) cg ( n ) for all n N


slide-1
SLIDE 1

Week 3 -Wednesday

slide-2
SLIDE 2

 What did we talk about last time?  Project 1  More on Big Oh notation

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7

 O establishes an upper bound

  • f(n) is O(g(n)) if there exist positive numbers c and N such that f(n) ≤

cg(n) for all n ≥ N

 Ω establishes a lower bound

  • f(n) is Ω(g(n)) if there exist positive numbers c and N such that f(n) ≥

cg(n) for all n ≥ N

 Θ establishes a tight bound

  • f(n) is Θ(g(n)) if there exist positive numbers c1,c2 and N such that

c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ N

slide-8
SLIDE 8

 Implement binary search  How much time does a binary search take at most?  What about at least?  What about on average, assuming that the value is in the list?

slide-9
SLIDE 9

 Give a tight bound for n1.1 + n log n  Give a tight bound for 2n + a where a is a constant  Give functions f1 and f2 such that f1(n) and f2(n) are O(g(n)) but

f1(n) is not O(f2(n))

slide-10
SLIDE 10

int count = 0; for( int i = 0; i < n; i += 2 ) for( int j = 0; j < n; j += 3 ) count++;

slide-11
SLIDE 11

int count = 0; for( int i = 0; i < n; i++ ) for( int j = 0; j < n; j++ ) { if(j == n - 1) i = n; count++; }

slide-12
SLIDE 12
slide-13
SLIDE 13

 An interface is a set of methods which a class must have  Implementing an interface means making a promise to

define each of the listed methods

 It can do what it wants inside the body of each method, but it

must have them to compile

 A class can implement as many interfaces as it wants

slide-14
SLIDE 14

 An interface looks a lot like a class, but all its methods are empty

  • In Java 8 and higher, default implementations can be given, but never

mind that now

 Interfaces have no members except for (static final)

constants

public interface Guitarist { void strumChord(Chord chord); void playMelody(Melody notes); }

slide-15
SLIDE 15

public class RockGuitarist extends RockMusician implements Guitarist { public void strumChord( Chord chord ) { System.out.print("Totally wails on that " + chord.getName() + " chord!"); } public void playMelody( Melody notes ) { System.out.print("Burns through the notes " + notes.toString() + " like Jimmy Page!" ); } }

slide-16
SLIDE 16

 A class has an is-a relationship with interfaces it implements,

just like a superclass it extends

 Code that specifies a particular interface can use any class

that implements it

public static void perform(Guitarist guitarist, Chord chord, Melody notes) { System.out.println("Give it up " + "for the next guitarist!"); guitarist.strumChord( chord ); guitarist.playMelody( notes ); }

slide-17
SLIDE 17
slide-18
SLIDE 18

 From a formal perspective, a type is a set of data values and

the operations you can perform on them

Type Values Operations int Integers from -2147483648 to 2147483647

+, -, *, /, %, <<, >>, >>>, |, &

double Floating points numbers

+, -, *, /, %

String All possible Java String objects +, length(), charAt(),

substring(), etc.

Wombat All possible Wombat objects

toString(), eat(), etc.

slide-19
SLIDE 19

 So, you have a type with operations  Do you need to know how those operations are implemented

to be able to use them?

 No!

  • In fact, in OOP (including Java), the data is usually hidden from you

 Enter the Abstract Data Type (ADT)

  • It does something
  • We aren't necessarily concerned with implementation
slide-20
SLIDE 20

 The idea of an interface has a strong connection to an ADT  Let's look at the List<E> interface  Some of its methods:

  • boolean add(E element)
  • void add(int index, E element)
  • void clear()
  • E get(int index)
  • int size()
  • boolean remove(Object o)
slide-21
SLIDE 21

 There are lots of different ways of keeping a list of data  The List ADT doesn't care how we do it  And there are lots of implementations that Java provides:

  • ArrayList
  • LinkedList
  • Stack
  • Vector

 You can use whichever you think best suits your task in terms

  • f efficiency
slide-22
SLIDE 22

 A bag is an ADT that is iterable but otherwise only has one

real operation

 Add

  • Put an element in the bag

 It's a collection of things in no particular order  A bag is also called a multiset  The book talks about bags partly because it's hard to imagine

a simpler ADT

slide-23
SLIDE 23

 The list ADT is not entirely standardized

  • Some lists allow insertion at the beginning, end, or at arbitrary locations
  • Some lists allow elements to be retrieved from an arbitrary location

 Let's focus on an list that allows the following operations  Add

  • Insert element at the end of the list

 Add at index

  • Insert element at an arbitrary location

 Get

  • Retrieve element from arbitrary location
slide-24
SLIDE 24

 A stack is an ADT with three main operations  Push

  • Add an item to the top of the stack

 Pop

  • Remove an item from the top of the stack

 Top

  • Retrieve the item at the top of the stack

 Stacks are often implemented with a dynamic array or a

linked list

slide-25
SLIDE 25

 A queue is an ADT with three main operations  Enqueue

  • Add an item to the back of the queue

 Dequeue

  • Remove an item from the front of the queue

 Front

  • Retrieve the item at the front of the queue

 Queues are also often implemented with a dynamic array or a

linked list

slide-26
SLIDE 26
slide-27
SLIDE 27

 A stack is a simple (but useful) ADT that has three basic

  • perations:
  • Push Put an item on the top of the stack
  • Pop

Remove an item from the top of the stack

  • Top

Return the item currently on the top of the stack (sometimes called peek)

slide-28
SLIDE 28

 When are stacks used?

  • Implicitly, in recursion (or in any function calls)
  • Explicitly, when turning recursive solutions into iterative solutions
  • When parsing programming languages
  • When converting infix to postfix
slide-29
SLIDE 29
slide-30
SLIDE 30

 Advantages:

  • Pop is Θ(1)
  • Top is Θ(1)

 Disadvantages

  • Push is Θ(n) in the very worst case, but not in the amortized case
slide-31
SLIDE 31

public class ArrayStack { private int[] data; private int size; public ArrayStack() {} public void push(int value) {} public int pop() {} public int peek() {} //instead of top public int size() {} }

slide-32
SLIDE 32
slide-33
SLIDE 33
slide-34
SLIDE 34
slide-35
SLIDE 35
slide-36
SLIDE 36
slide-37
SLIDE 37
slide-38
SLIDE 38
slide-39
SLIDE 39

 Finish stacks  Queues

slide-40
SLIDE 40

 Read section 1.3  Finish Assignment 1