1 / 17

Efficiency of Algorithms

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

stringfield
Download Presentation

Efficiency of Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Efficiency of Algorithms

  2. 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

  3. 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

  4. 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 . . .

  5. Truck - netWeight : int - grossWeight : int . . . Car - color : int . . . Vehicle - make : String - model : String - year : int - vin : String - tag : String - cost : double

  6. 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

  7. 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?

  8. 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?

  9. Algorithm Growth Rates

  10. 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

  11. 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

  12. 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

  13. 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

  14. Some Example Growth Rates

  15. Big O Notation

  16. 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

  17. 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

More Related