170 likes | 189 Views
Efficiency of Algorithms. Node - data : Object - link : Node + createNode() + getData() + setData() + getLink() + setLink() + addNodeAfter() + removeNodeAfter(). LinkedList - head : Node + createLinkedList() + add(Object) + add(Object, int) + clear() + get(int) : Node
E N D
Node - data : Object - link : Node + createNode() + getData() + setData() + getLink() + setLink() + addNodeAfter() + removeNodeAfter() LinkedList - head : Node + createLinkedList() + add(Object) + add(Object, int) + clear() + get(int) : Node + isEmpty() : boolean + size() : int + find(Object) : int + remove(Object) + remove(int) - getReference(int) : Node UML "Has-A" Relationship 0..* 1
LinkedList - head : Node + createLinkedList() + add(Object) + add(Object, int) + clear() + get(int) : Node + isEmpty() : boolean + size() : int + find(Object) : int + remove(Object) + remove(int) - getReference(int) : Node Stack + createStack() + push(Object) + pop() : Object + peek() : Object + isEmpty() : boolean Is-A Relationship
Truck - make : String - model : String - year : int - vin : String - tag : String - cost : double - netWeight : int - grossWeight : int . . . Car - make : String - model : String - year : int - vin : String - color : int - tag : String - cost : double . . .
Truck - netWeight : int - grossWeight : int . . . Car - color : int . . . Vehicle - make : String - model : String - year : int - vin : String - tag : String - cost : double
There are always multiple ways to implement a data structure or algorithm. • We can judge the efficiency of each and choose the one that best meets our needs. • We can gauge the efficiency of each in two ways: • The amount of memory used (space complexity) • The amount to time it takes (time complexity) Efficiency of Algorithms
You don’t compare programs, you compare algorithms. • You don’t have to worry about the quality of the coding. • You don’t have to worry about the type of computer or language to use. • You don’t have to worry that the data adequately represents the problem. • Analyzing the algorithms avoids all of this. What to Compare?
Counting an algorithm’s operations is a way to determine its efficiency. • An algorithm’s time requirements can be measured as a function of the problem size. • An algorithm’s growth rate enables comparison of one algorithm to another. • Typically, we only worry about efficiency when dealing with large problems. How to Compare?
Best Case – When searching a list for a value, it is in the first node we look at. When sorting, the list is already in sorted order. • Average Case – Difficult to determine. • Worst Case – Normally used for all comparisons. The number of operations can easily be determined. Three Comparisons
This “Order of Magnitude” analysis is expressed in Big O notation. • Uses the upper-case O to specify an algorithm’s order. • Example: O(f(n)) or O(n) • Algorithm A is said to be of order f(n) if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥ n0 Big O Notation
Node current = head; while( current != null ) { total += current.getData(); current = current.getLink(); } On each pass of the loop, 2 statements are executed. The loop executes once for each node in the list. O (f(3 * n)) or O( 3 * n ) or O(n) Traversing a Linked List
for( int i = 0; i < array.length - 1; i++ ) { if( array[i] > array[i + 1] ) { swap( i, i + 1 ); for( j = i; j >= 0; j-- ) { if( j – 1 > j ) swap( j – 1, j ); } } } The main loop executes n – 1 times. The inside loop executes n – 2 times. Drop the low order constants, and we get O(n * n) or O( n2 ). Bubble Sort
Throughout the course of an analysis, keep in mind that you are interested only in significant differences in efficiency • When choosing an ADT’s implementation, consider how frequently particular ADT operations occur in a given application Keeping Things in Perspective
If the problem size is always small, you can probably ignore an algorithm’s efficiency • Weigh the trade-offs between an algorithm’s time requirements and its memory requirements • Order-of-magnitude analysis focuses on large problems Keeping Things in Perspective