Escolar Documentos
Profissional Documentos
Cultura Documentos
Day 1
References
1. Weiss M, W. (1993) Data Structures and Algorithm Analysis in C, Benjamin Cummings. 2. McConnell, S. (1993) Code complete, Microsoft Press 3. Alfred V Aho, John E Hopcraft, Jeffrey D Ullman, Design & Analysis of Computer Algorithms, Addison Wesley Publishing Company. 4. Ellis Horowitz, Sartaj Sahni, Sanguthevar, (1998)Fundamentals of Computer Algorithms, Galgotia Publications private limited, New Delhi. 5. Donald E Knuth (1938)The Art of Computer Programming, Fundamental Algorithms, Volume 1, Third Edition, Addison Wesley
Case Study
Analysis of Algorithms
Unit 1 - Introduction
Muhammad al-Khowarizmi, from a 1983 USSR commemorative stamp scanned by Donald Knuth Reference: ACM Trans - Algorithms
What is an Algorithm?
Finite set of instructions to accomplish a task. The properties of an algorithm are as follows:
Finiteness
Effectiveness
Algorithm
Definiteness
Output
Input
10
Pseudo Code
An algorithm is independent of any language or machine whereas a program is dependent on a language and machine. To fill the gap between these two, we need pseudo codes. Psuedo-code is a way to represent the step by step methods in finding the solution to the given problem. Example: Algorithm arrayMax (A,n) Input array A of n integers Output maximum element of A CurrentMax A[0] for I = 1 to n-1 do if A[i] > currentMax then currentMax A[i] return currentMax
11
Pseudo Code
An Algorithm should be independent of any programming language. Psuedo-code is a way to represent the step by step methods in finding the solution to the given problem. Example:
Algorithm arrayMax (A, n) Input array A of n integers Output maximum element of A currentMax for I A[0]
return currentMax
12
13
14
15
Analysis of Algorithms
An algorithm when implemented, uses the computers primary memory and Central Processing Unit. Analyzing the amount of resources (time &/ space) needed for solving the problem). The Analysis is done at two stages:
Priori Analysis:
16
Efficiency Measures
Performance of a solution
Most of the software problems do not have a single best solution. Then how do we judge these solutions? The solutions are chosen based on performance measures.
Performance Measures
Time Quality
Simplicity
17
Example 1: Consider a personnel management product that an organization can purchase and use to maintain information about its employees. If employee details were to be stored in an array, the array would have to be declared large enough to be able to hold the maximum number of records the system was rated to handle. This would always take up a large amount of memory. With a linked list implementation on the other hand, there would be better utilization of memory. Which implementation would provide faster access to an employee with a given employee number? Which implementation would be easier to code? Which implementation would be easier to test?
18
19
20
Summary of Unit-1
What is an Algorithm? Properties of an Algorithm. Life Cycle of an Algorithm Performance Measures
21
Analysis of Algorithms
Unit 2 - Code Tuning Techniques
Code Tuning refers to modifying the implementation of a specific design rather than modifying the design itself Related to how to code better How to review a given code with performance as a concern
23
CTT - Loops
Looping constructs in a program are executed many times. There are quite a few of code tuning techniques which can be applied to looping constructs in order to improve the performance of the code. Jamming of loops: Jamming of loops is an outcome of combining loops which operate over the same range of values.
for (k = 1 to n) { initialize T[k] }; for (m = 1 to n) { Max[m] = m * Max[m] }; for (k = 1 to n) { initialize T[k]; Max[k] = k * Max[k] };
Copyright 2004, Infosys Technologies Ltd 24
Unswitch loops that contain if tests, if the results of these tests do not change inside the loop
Let 10,000 employees of a company got loan in a bank. At the start of every month, the bank has to deduct the monthly installment of the loan from each employee if salary is credited in their account. for (i=0;i<noofemployees;i++){ If (salary_credited = YES) { if (salary_credited = YES) { for (i=0; i<noofemployees;i++){ ........ ........ ........ ........ } } }}
26
Need to compute n/2 in every iteration is removed. Load of the loop is reduced.
27
28
All operations which do not depend on loop variant may be moved outside loops and the operations are converted into cheaper ones. for (i = 1 to Num ) { commission ( i ) = i * Revenue * BaseCommission * Discount }
comm = Revenue * BaseCommission * Discount sum = comm for ( i = 1 to Num ) { commission ( i ) = sum sum = sum + comm }
ER/CORP/CRS/SE15/003 Version No: 2.0
29
CTT Logic
Order tests in case statements by frequency:
Every time, the more frequent, Grade 7 is encountered, the code executes faster
30
While doing linear search come out as soon as the element is found.
31
If the same array element is repeatedly referred to inside a loop, then move it outside the loop
for (a=0; a < 5; a++) { for (b =0; b < 10; b++) { total[b] = total[b] * sum[a]; }
for (a=0; a < 5; a++) { sum_now = sum[a]; for (b =0; b < 10; b++) { total[b] = total[b] * sum_now; }
32
For example we can add an index to the linear linked list data structure. This index helps in speeding up the search operation in a linked list which is other wise strictly linear.
In a character array, the length can be augmented at the start, so that finding the length of the string can be done easily without actually calculating it every time.
If a linked list is having 1000 elements, then searching the list on an average will cost 500 operations. If the same linked list is augmented with another small list of pointers each pointing to every 10th element in the original list, then the number of searches is reduced to an average of 55.
Copyright 2004, Infosys Technologies Ltd 33 ER/CORP/CRS/SE15/003 Version No: 2.0
CTT Expressions
Use constants of the correct type:
float x; x = 5;
int i; i = 3.14;
34
y = log(x) / log(2) b = log(a) / log(2) instead have LOG2 = log(2) y = log(x) / LOG2 b = log(a) / LOG2
35
< y, we can use the algebraic identity which says x < y only when x < y. So it is enough to check if x < y
in this case.
36
37
38
39
40
41
42
43
Summary of Unit-2
Code Tuning Techniques for Loops Code Tuning Techniques for logic Code Tuning Techniques for Data Transformations Code Tuning Techniques for Expressions SQL Query Tuning
44
Summary of Day-1
Introduction to Analysis of algorithms
What is an Algorithm? Properties of an Algorithm Life cycle of an Algorithm
45
Thank You!
Copyright 2004, Infosys Technologies Ltd 46 ER/CORP/CRS/SE15/003 Version No: 2.0