120 likes | 281 Views
Efficiency of Algorithms. Efficiency of Algorithms. Two main issues related to the efficiency of algorithms: Speed of algorithm Efficient memory allocation We will focus on the speed of algorithms. The speed of an algorithm ( running time ) is determined
E N D
Efficiency of Algorithms • Two main issues related to the efficiency of algorithms: Speed of algorithm Efficient memory allocation • We will focus on the speed of algorithms. The speed of an algorithm (running time) is determined by the number of elementary operations: addition, subtraction, multiplication, division, comparison. The number of elementary operations depends on problem size nature of input data
Analysis of Running Time • Running time of an algorithm is a function of input size. • Two kind of analyses of running time: Worst-case running time analysis Average-case running time analysis Most results in the analysis of algorithms concern the worst-case running time.
Efficiency of Algorithms: Example • Recall the Traveling Salesman Problem (TSP): There are n cities. The salesman starts his tour from City 1, visits each of the cities exactly once, and returns to City 1. For each pair of cities i,j there is a cost cijassociated with traveling from City i to City j . • Goal: Find a minimum-cost tour.
“Exhaustive enumeration” algorithm • One way of solving the problem is by an algorithm which is based on exhaustive enumeration (brute force): 1) Compute the costs of all possible tours; 2)Choose the tour with minimum cost. • What is the running time of this algorithm? 1) The number of possible tours is (n-1)! The cost of each tour is the sum of n individual costs requires n-1 additions. Thus, computing the costs of all possible tours requires (n-1)∙(n-1)! elementary operations. 2) Choosing the tour with minimum cost requires (n-1)!-1 comparisons. Summarizing, (n-1)∙(n-1)! + (n-1)! -1 = n! -1 elementary operations are needed for implementing the algorithm.
Efficiency of the “Exhaustive enumeration” algorithm • Is the “exhaustive enumeration” algorithm efficient? Assume that each elementary operation can be done in 1 nanosecond = 10-9 seconds. Then the running time:
“Nearest neighbor” algorithm • Consider another method for solving the TSP, the “Nearest neighbor” algorithm: In every iteration (except the last one) go to the closest city not visited yet. • What is the running time of this algorithm? In each iteration we choose one of the n cities; so there are n iterations. In each iteration, we need at most n comparisons to choose the next city. Thus, the total number of elementary operations is at most n2.
Efficiency of the “Nearest Neighbor” algorithm Compare the running times of “Nearest neighbor” and “Exhaustive enumeration” algorithms: Note: The “Nearest Neighbor” algorithm might not return an optimal solution.
Bad case example for the “Nearest Neighbor” algorithm 1 2 6 4 3 5 Output of the “Nearest neighbor” algorithm Optimal solution
Overwhelming terms in running times • Suppose in the “Exhaustive enumeration” algorithm, we need to preprocess the costs before applying the main routine (e.g., converting kilometers to miles). • Preprocessing requires at most n2 multiplications; thus, the running time of the new algorithm is n!+n2. • For large n, the term n! overwhelms the term n2. Thus, the running time n!+n2 qualitatively is not much different from n! .
Order of an Algorithm • Definition: Let A be an algorithm. Let w(n) be the maximum number of elementary operations required to execute A for all possible input sets of size n. If w(n) isO(f(n)) , we say that A has a (worst case) order of f(n) . • Ex.:Suppose the maximum number of operations needed to execute algorithm A is 5n2+3n+7 . Then A has an order of n2 . • Definition: An algorithm is called polynomial-time if it has an order of f(n)=nk for some constant k. • Polynomial-time algorithms are normally considered efficient.