solving recurrences & java collections 15-211 fundamental data structures and algorithms ananda...
TRANSCRIPT
Solving Recurrences & Java Collections
15-211 Fundamental Data Structures and Algorithms
Ananda Guna
January 21, 2002
Based on lectures given by Peter Lee, Avrim Blum, Danny Sleator, William Scherlis, Ananda Guna & Klaus Sutner
Complexity of AlgorithmsCounting & Recurrence Relations
To understand the complexity of an algorithm
To analyze algorithms independent of processor, OS or language
To obtain a closed form solution
To use a mathematical framework to describe algorithm performance
Binary Search - counting
public int search(int first, int last, int key) {
if (first > last) return –1;
int mid = (first + last)/2;
if (A[mid] == key) return mid;
else if (A[mid] > key) search(mid+1,last,key);
else search(first, mid-1,key);
}
What is the complexity of this algorithm? How do we find out?
Clearly the process can be described by the following recurrence formula.
T(n) = c + T(n/2) where T(n) is the time required to search a list of size n.
Lets solve this and find a closed form solution, T(n) = c log n
Quick Sort
Choose a Pivot P = a[j]
Divide the array into two segmentsLeft array is less than the pivot
Right array is greater than the pivot
a[0] a[1]….a[j-1] P a[j+1] … a[n]
How do we determine the complexity of quicksort?
More later…
Merge Sort
Merge Sort is a divide and conquer algorithm (more details later)
Merge Sort involve the following steps
If list is size 0 or 1 stop
Otherwise divide the list into two halves and sort them separately
Merge the two lists into one single sorted list.
Merge Sort Analysis
Merge step of the process takes n (linear) time.
So we can write a recurrence formula as:T(n) = cn + 2T(n/2), where T(n) is the
time to sort a list of n items.
Note that T(n) is “not” the clock time
It is some measure that is independent of environment variables
Show that T(n) = k n log n
Bubble Sort Analysis- Counting Operations
loop I = 1 to n-1
loop j = 1 to n-I-1
if (A[j] > A[j+1])
swap(A[j], A[j+1]) Here we can count the operations(comparisons)
It is : (n-1) + (n-2) + … + 2 + 1
This is n/2*(n-1) < c n2 for some c
Divide and Conquer
GCD(a,b) = GCD(a, a-b)
GCD(a,b) = GCD(b, a%b)Without loss of generality assume a > b in each
iteration
Recursive Definition GCD(a,0) = a
GCD(a,b) = GCD(b, a%b) if a > b
GCD(a,b) = GCD(a, b%a) if b > a
Prove that t(a,b) = c*log(a) where a > b
Hint: if a >= b then a mod b < a/2
Performance and Scaling
Suppose we have three algorithms to choose from.
Which one to select?
Systematic analysis reveals performance characteristics.
For a problem of size n (i.e., detecting cycles out of n nodes).
1. 100n sec
2. 7n2 sec
3. 2n sec
Performance and Scaling
n 100n sec 7n2 sec 2n sec
1 100 s 7 s 2 s
5 .5 ms 175 s 32 s
10 1 ms .7 ms 1 ms
45 4.5 ms 14 ms 1 year
100 … … …
1,000
10,000
1,000,000
Performance and Scaling
n 100n sec 7n2 sec 2n sec
1 100 s 7 s 2 s
5 .5 ms 175 s 32 s
10 1 ms .7 ms 1 ms
45 4.5 ms 14 ms 1 year
100 … … …
1,000
10,000
1,000,000
What?! One year?
210 = 1,0241024 sec 1 ms
245 = 35,184,372,088,8323.5 * 1013 sec = 3.5 * 107 sec
1.1 year
Performance and Scaling
n 100n sec 7n2 sec 2n sec
1 100 s 7 s 2 s
5 .5 ms 175 s 32 s
10 1 ms .7 ms 1 ms
45 4.5 ms 14 ms 1 year
100 100 ms 7 sec 1016 year
1,000 1 sec 12 min --
10,000 10 sec 20 hr --
1,000,000 1.6 min .22 year --
Lists in the Java 2 Platform
The Java language comes with a large libraries of classes.
For stand-alone desktop applications, the Java 2 Platform, Standard Edition (J2SE) is used.
The javadoc for J2SE is available at http://java.sun.com/j2se/1.3/docs/api/index.html
Package java.util contains interfaces and classes that implement lists in something called the Collection hierarchy.
J2SE Collection API (excerpt)
Collection
List SetAbstractCollection
AbstractList
LinkedList
Vector
HashSet
interfaces
AbstractSequentialList
AbstractSet
implementation classes
Cloneable
Inheritance of interfaces
Classes can implement one or more interfaces.
public class LinkedList extends AbstractSequentialList{…}
This means that the class provides everything that the superclass provides, plus possibly more, and also possibly modifying superclass methods.
Inheritance of classes
Classes can extend exactly one class.
public class LinkedList implements List, Cloneable {…}
This means that the class provides code for all of the methods specified in the interface(s).
The class Object
Every class is an extension of class object
The class Object also has some “magic” in it.
For example, it has a method called clone().
Object.clone() is able to make a complete copy of any object that implements a special interface called Cloneable.
public class LinkedList implements List, Cloneable {…}
The clone() method
For a class to implement clone(), all it needs to do is invoke the clone() method in the superclass.
public Object clone () throws CloneNotSupported { IntList res = (IntList) super.clone(); res.next = (IntListInterface) next.clone(); return res;}
explained next time
invoke the clone() method in the superclass
Where does clone() go?
Remember that IntList and EmptyIntList are subclasses of IntListInterface.
But we can’t put code in an interface, and so we can’t write clone() into IntListInterface.
The solution is to use an abstract class.
Abstract base class
public abstract class List {public abstract boolean isEmpty();
public abstract int length();
public abstract List append(Object o);
public Object clone() { return (List) super.clone(); }}
We can put code here!
Abstract base class
And then we can say:
public class EmptyList extends List {…}
public class ListCell extends List {…}
Java lists are mutable
Notice that the add() method (which we have been calling append()) has a different functionality:
Ours:
public List append (int n);
J2SE’s:
public void add (int index, Object o);
Can we implement the J2SE version?
So…
…no problem, right?
Wrong. There is one bad case:
IntListInterface L = new EmptyIntList();
L.append(123);
Creates a new list, and then forgets where it is.
The code
public class EmptyIntList { … public IntListInterface append (int x) { return new IntList(x); } …}
Wisdom
Always worry about special cases, degenerate situations.
You may have to spend more time getting the strange cases right than the "real" part of the code.
Is-A versus Has-A
A Human has a father and a mother.
A Human has a head.
Should Human inherit from Father? Mother? Head?
No, Human should have instance variables for: father, mother, and head.
Animal
Human Canine
Dog WolfProfessor Student
Reptile Mammal
Lorises
Java class hierarchy
Click here
Object
NumberMathCompilerClassLoaderClassCharacterBoolean
Byte ShortIntegerFloatDouble
33
Java.lang
Object
NumberMathCompilerClassLoaderClassCharacterBoolean
Byte ShortIntegerFloatDouble
All Classes in Java Ultimately Inherit from
Object
34
Java.lang
Object
NumberMathCompilerClassLoaderClassCharacterBoolean
Byte ShortIntegerFloatDouble
Inheritance is transitive:Short IS-A Number IS-A Object
thereforeShort IS-A Object
35
Abstract Classes
Rectangle
Square
Parallelogram Ellipse
Circle
Triangle
Shape
public class Shape {public void draw();public double area();public Point upperLeft();public void moveTo(Point );public void setColor(Color );
}
36
Abstract Classes
An abstract class defines an ADT.
It makes sense to do things like draw shapes.
But we never create objects of an abstract class, only one of the subclasses.
e.g.., Since Shape is an abstract class, “new Shape” is not allowed.
37
abstract class
abstract class Shape {public void draw() {}public double area() {}public Point upperLeft() {}public void moveTo(Point ) {}public void setColor(Color ) {}
}
38
abstract methods
abstract class Shape {abstract public void draw();abstract public double area();abstract public Point upperLeft();abstract public void moveTo(Point );abstract public void setColor(Color );
}
Abstract methods define operations that must be implemented by a subclass. They cannot have a body, I.e., no {}.
39
Rectangle
Square
Parallelogram Ellipse
Circle
Triangle
Shape
Defining the subclasses
public class Rectangle extends Shape {public Rectangle(Point ul, Point lr,
Color c) { … }public void draw() { … }public double area() { … }public Point upperLeft() { … }public void moveTo(Point ) { … }public void setColor(Color ) { … }
private Point ul;private Color color;private Point lr;
}
40
Defining a subclass
Rectangle
Shape parent class/superclass
child class/subclass
class child extends parent {...
}
41
Rectangle
Square
Parallelogram Ellipse
Circle
Triangle
Shape
Defining the subclasses
public class Parallelogram extends Shape {public Parallelogram(Point ul, Point lr,
double angle,Color c) {…}public void draw() { … }public double area() { … }public Point upperLeft() { … }public void moveTo(Point ) { … }public void setColor(Color ) { … }
:private Point ul;private Color color;private int height;private int length;private double angle;
}