## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Day 1

**Objectives of the course
**

• • • • • To introduce the concept of ‘Analysis of Algorithms’. To learn the various factors that affect the performance of an algorithm. To learn Code Tuning Techniques. To Analyze well known Algorithms. To introduce Intractable problems.

Copyright © 2004, Infosys Technologies Ltd

2

ER/CORP/CRS/SE15/003 Version No: 2.0

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

Copyright © 2004, Infosys Technologies Ltd

3

ER/CORP/CRS/SE15/003 Version No: 2.0

**Course Plan Day 1
**

• Introduction to Analysis of algorithms

– What is an Algorithm? – Properties of an Algorithm – Life cycle of an Algorithm

• •

Code Tuning Techniques SQL Tuning Techniques

Copyright © 2004, Infosys Technologies Ltd

4

ER/CORP/CRS/SE15/003 Version No: 2.0

Greedy. Decrease & Conquer) – Analysis of some well known algorithms Copyright © 2004. Infosys Technologies Ltd 5 ER/CORP/CRS/SE15/003 Version No: 2.Course Plan (cont. Divide & Conquer.) Day 2 • Analyzing Algorithms – Basic Mathematical principles – Order of magnitude – Introduction to Asymptotic notations • Best case • Worst case • Average case • Analysis of well known algorithms – Algorithm design techniques ( Brute force.0 .

0 .) (Dynamic Programming) Intractable problems – – – – Deterministic Vs Non-Deterministic machines P Vs NP NP Complete NP Hard • Case Study Copyright © 2004.Course plan (cont.) Day 3 • • Algorithm Design techniques (contd. Infosys Technologies Ltd 6 ER/CORP/CRS/SE15/003 Version No: 2.

Plan for Day-1 • Introduction to Analysis of algorithms – What is an Algorithm? – Properties of an Algorithm – Life cycle of an Algorithm • • Code Tuning Techniques SQL Tuning Techniques Copyright © 2004.0 . Infosys Technologies Ltd 7 ER/CORP/CRS/SE15/003 Version No: 2.

Analysis of Algorithms Unit 1 .Introduction .

0 . Infosys Technologies Ltd 9 ER/CORP/CRS/SE15/003 Version No: 2. from a 1983 USSR commemorative stamp scanned by Donald Knuth Reference: ACM Trans . The word Algorithm is derived from the name of the Persian author “Abu Jafar Mohammad ibn Musa al Khowarizmi” Muhammad al-Khowarizmi.Algorithms Copyright © 2004.Introduction to Analysis of Algorithms Where does the word algorithm came from? King Algor of Castile ? Algiros + Arithmos meaning Painful + Numbers ? NO The etymology of the word Algorithm dates back to the 8th Century AD.

The properties of an algorithm are as follows: Finiteness Effectiveness Algorithm Definiteness Output Input Copyright © 2004.What is an Algorithm? • • Finite set of instructions to accomplish a task. Infosys Technologies Ltd 10 ER/CORP/CRS/SE15/003 Version No: 2.0 .

• To fill the gap between these two. Psuedo-code is a way to represent the step by step methods in finding the solution to the given problem.0 . we need pseudo codes. Infosys Technologies Ltd 11 ER/CORP/CRS/SE15/003 Version No: 2.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 Copyright © 2004.Pseudo Code • An algorithm is independent of any language or machine whereas a program is dependent on a language and machine. Example: Algorithm arrayMax (A.

n) Input array A of n integers Output maximum element of A currentMax for I A[0] 1 to n-1 do if A [i] > currentMax then currentMax A [i] return currentMax Copyright © 2004.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. Infosys Technologies Ltd 12 ER/CORP/CRS/SE15/003 Version No: 2. Example: Algorithm arrayMax (A.0 .

Life Cycle of an Algorithm • • • • Design the Algorithm Write (Implementation of the Algorithm) Test the Algorithm Analyze the Algorithm Copyright © 2004.0 . Infosys Technologies Ltd 13 ER/CORP/CRS/SE15/003 Version No: 2.

Life Cycle of an Algorithm (Contd…) Copyright © 2004. Infosys Technologies Ltd 14 ER/CORP/CRS/SE15/003 Version No: 2.0 .

Infosys Technologies Ltd 15 ER/CORP/CRS/SE15/003 Version No: 2.Resources available in a computer Copyright © 2004.0 .

The Analysis is done at two stages: – Priori Analysis: • • » Analysis done before implementation. Copyright © 2004. Analyzing the amount of resources (time &/ space) needed for solving the problem). Infosys Technologies Ltd 16 ER/CORP/CRS/SE15/003 Version No: 2. – Posteriori Analysis: » Analysis done after implementation. uses the computer’s primary memory and Central Processing Unit.0 .Analysis of Algorithms • An algorithm when implemented.

Infosys Technologies Ltd 17 ER/CORP/CRS/SE15/003 Version No: 2.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.0 . • Performance Measures • • Time Quality • Simplicity… Copyright © 2004.

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? Copyright © 2004. This would always take up a large amount of memory. Infosys Technologies Ltd 18 ER/CORP/CRS/SE15/003 Version No: 2. With a linked list implementation on the other hand. there would be better utilization of memory.0 .Efficiency Measures (Contd…) • Space Time Tradeoff Example 1: Consider a personnel management product that an organization can purchase and use to maintain information about its employees. 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. If employee details were to be stored in an array.

If the employee database is on a different machine. Option b: keep the complete list of employees in memory and refer to it each time the list is dropped down. In your opinion which is the preferred option and why? Copyright © 2004. then there are two options: Option a: fire a SQL and retrieve the relevant employee names each time the list is dropped down.0 .Efficiency Measures (Contd …) Example 2: Think of a GUI drop-down list box that displays a list of employees whose names begin with a specified sequence of characters. Infosys Technologies Ltd 19 ER/CORP/CRS/SE15/003 Version No: 2.

a2.0 .Efficiency Measures (Contd …) Example 3: Which of the following programs is more complicated? • Design a computer program which produces an output 1.…an of numbers. where n is any natural number. if the word is of length 3n (n=0. Infosys Technologies Ltd 20 ER/CORP/CRS/SE15/003 Version No: 2.…) and 0.1. example: If the input is “aabcef” the output is 1 If the input is “aabc” then the output is 0 Design a computer program that sorts ( in Ascending order ) and outputs the result for any input sequence a1. otherwise. Hint: consider the RAM size required in both the programs • Copyright © 2004.2.

Life Cycle of an Algorithm Performance Measures Copyright © 2004.Summary of Unit-1 • • • • What is an Algorithm? Properties of an Algorithm. Infosys Technologies Ltd 21 ER/CORP/CRS/SE15/003 Version No: 2.0 .

Analysis of Algorithms Unit 2 .Code Tuning Techniques .

0 . Infosys Technologies Ltd 23 ER/CORP/CRS/SE15/003 Version No: 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 • • Copyright © 2004.

for (k = 1 to n) { initialize T[k] }. Copyright © 2004. 2n Loop Checks After Jamming. Infosys Technologies Ltd 24 Before Jamming. 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. n Loop Checks ER/CORP/CRS/SE15/003 Version No: 2.CTT . for (m = 1 to n) { Max[m] = m * Max[m] }.0 . Jamming of loops: Jamming of loops is an outcome of combining loops which operate over the same range of values.Loops Looping constructs in a program are executed many times. for (k = 1 to n) { initialize T[k]. Max[k] = k * Max[k] }.

... the bank has to deduct the monthly installment of the loan from each employee if salary is credited in their account.. if the results of these tests do not change inside the loop Let 10. } } }} Move the if condition outside the loop Copyright © 2004..000 employees of a company got loan in a bank... ....0 .. Infosys Technologies Ltd 25 ER/CORP/CRS/SE15/003 Version No: 2..... ........ for (i=0.. .i++){ .. At the start of every month...i<noofemployees..i++){ If (salary_credited = YES) { if (salary_credited = YES) { for (i=0..CTT – Loops (Contd…) Unswitching of Loops: Unswitch loops that contain if tests. i<noofemployees.

CTT – Loops (Contd…) Unrolling of Loops: i = 1. } Before unrolling. n/2 loop checks Copyright © 2004. n loop checks i = 1. i = i + 2. Infosys Technologies Ltd 26 ER/CORP/CRS/SE15/003 Version No: 2. a( i + 1 ) = i + 1.0 . } After unrolling. i = i + 1. while(i < num) { a( i ) = i. while(i < num) { a( i ) = i.

} Need to compute n/2 in every iteration is removed. Fprimes(i * i * i). Copyright © 2004.CTT – Loops (Contd…) Minimize work performed inside loops: For (i = 1 to n/2) { Fprimes(i * i). } n_2 = n/2.0 . Load of the loop is reduced. Fprimes(m). Infosys Technologies Ltd 27 ER/CORP/CRS/SE15/003 Version No: 2. Fprimes(m * i). For (i = 1 to n_2) { m=i * i.

Infosys Technologies Ltd 28 . While (x< >a[i]) { i = i+1.CTT – Loops (Contd…) Use of Sentinel Values: In a character string in C language.0 Copyright © 2004. } One boundary check is reduced. the ‘\0’ is the sentinal While (i< n) and (x< >a[i]) { i= i+1. as it is achieved by the sentinel ER/CORP/CRS/SE15/003 Version No: 2. } a[n+1] = x.

0 Copyright © 2004. 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.CTT – Loops (Contd…) Reduce the strength of operations performed inside loops: All operations which do not depend on loop variant may be moved outside loops and the operations are converted into cheaper ones. Infosys Technologies Ltd 29 .

} 5: {….} 3: {….} 2: {…. Infosys Technologies Ltd 30 ER/CORP/CRS/SE15/003 Version No: 2.} ….0 . Grade 7 is encountered.} endcase Every time. the more frequent.} ….CTT – Logic Order tests in case statements by frequency: read (empNo) case Grade(empNo) of 1: {…. 7: {….} 6: {…. 1: {….} endcase read (empNo) case Grade(empNo) of 7: { …. the code executes faster Copyright © 2004.

} } While doing linear search come out as soon as the element is found. Infosys Technologies Ltd 31 ER/CORP/CRS/SE15/003 Version No: 2. Copyright © 2004.0 .CTT – Logic (Contd…) Stop testing when you know the result: if(a < 10 ) and (b < 20) then { …. } if(a < 10) then { if(b < 20) then { ….

for (b =0. Infosys Technologies Ltd 32 ER/CORP/CRS/SE15/003 Version No: 2. b++) { total[b] = total[b] * sum[a]. } Copyright © 2004. b++) { total[b] = total[b] * sum_now. a < 5.CTT – Data Transformations Minimize array references: If the same array element is repeatedly referred to inside a loop. } for (a=0.0 . a++) { sum_now = sum[a]. b < 10. then move it outside the loop for (a=0. a++) { for (b =0. a < 5. b < 10.

If a linked list is having 1000 elements. Copyright © 2004. This index helps in speeding up the search operation in a linked list which is other wise strictly linear.0 . so that finding the length of the string can be done easily without actually calculating it every time. Infosys Technologies Ltd 33 ER/CORP/CRS/SE15/003 Version No: 2. then searching the list on an average will cost 500 operations.CTT – Data Transformations (Contd…) Augment data structures with indexes: For example we can add an index to the linear linked list data structure. then the number of searches is reduced to an average of 55. In a character array. If the same linked list is augmented with another small list of pointers each pointing to every 10th element in the original list. the length can be augmented at the start.

14 to 3 store into i “Converting to a desired type” is an overhead Copyright © 2004. Convert 5 to 5.0 .CTT – Expressions Use constants of the correct type: float x. Convert 3.0 store into x int i. Infosys Technologies Ltd 34 ER/CORP/CRS/SE15/003 Version No: 2. i = 3. x = 5.14.

Infosys Technologies Ltd 35 ER/CORP/CRS/SE15/003 Version No: 2.CTT – Expressions (Contd…) Precompute results: y = log(x) / log(2) b = log(a) / log(2) instead have LOG2 = log(2) y = log(x) / LOG2 b = log(a) / LOG2 Four Function Calls Three Function Calls Copyright © 2004.0 .

0 . Whenever we need to find whether √x not (A or B) is cheaper than not A and not B Copyright © 2004. So it is enough to check if x < y in this case.CTT – Expressions (Contd…) Exploit Algebraic Identities: Algebraic identities can be used to replace costlier operations by cheaper ones < √y. Infosys Technologies Ltd 36 ER/CORP/CRS/SE15/003 Version No: 2. we can use the algebraic identity which says √x < √y only when x < y.

Infosys Technologies Ltd 37 ER/CORP/CRS/SE15/003 Version No: 2.0 .CTT – Expressions (Contd…) Strength Reduction in Expressions: Strength reduction refers to replacing costlier operations by cheaper ones. This can be achieved by: Replacing multiplication with addition Replacing exponentiation with multiplication… Ax3 + Bx2 + Cx + D is better computed as ((Ax + B)x + C)x + D Copyright © 2004.

provided accuracy which is more often wasted.CTT – Expressions (Contd…) Be Wary of System Routines: System Routines.0 . like the math routines. For example the math routine which computes log(x) provides the result in a floating point number whereas most of the times we might be interested only in the integral part. If we do not need the level of accuracy as provided by the math routines then it makes sense for us to write the piece of code for the same. Copyright © 2004. Infosys Technologies Ltd 38 ER/CORP/CRS/SE15/003 Version No: 2.

SQL Query Tuning Concatenation of different data types in SQL queries An SQL query involving concatenation of different data types takes more time to execute. Ex: SELECT * FROM dwtable2 WHERE empno||ename='1234name1234'. Copyright © 2004. Infosys Technologies Ltd 39 ER/CORP/CRS/SE15/003 Version No: 2. Instead. we can re write the above query as: SELECT * FROM dwtable2 WHERE empno=1234 AND ename='name1234'.0 .

Copyright © 2004. Ex: Assume you have table1 with 10000000 records and table 2 with 1000 records. Infosys Technologies Ltd 40 ER/CORP/CRS/SE15/003 Version No: 2. It is advisable to write the query as follows: Select count(*) from table1.0 . table2 where rownum < 500.SQL Query Tuning (Contd…) Position of table with fewer rows in the select…from query It is advisable to put the table that returns the fewest rows at the end of from list.

idno = emp2. Ex: SELECT idno.empno AND emp2.0 .empno FROM emp1 e1.SQL Query Tuning (Contd…) Usage of Table Aliases If more than one table is used in a query. as it would enhance the speed of parse phase of the query.empno<=100. emp2 WHERE emp1. Infosys Technologies Ltd 41 ER/CORP/CRS/SE15/003 Version No: 2.empno AND e2. The same query can be written more efficiently as: SELECT idno. then it is advisable to use table aliases. empno FROM emp1.empno<=100.idno=e2. emp2 e2 WHERE e1. Copyright © 2004.

Copyright © 2004. Infosys Technologies Ltd 42 ER/CORP/CRS/SE15/003 Version No: 2. one should avoid the usage of NOT or != operator in SQL queries.0 . a FTS (Full Table Scan) is done and will degrade the performance of the transactions. Reason: Whenever these operators are used.SQL Query Tuning (Contd…) Usage of NOT or != operators Unless it is absolutely necessary.

updation and deletion processes are involved.SQL Query Tuning (Contd…) Usage of Index/Indexes Index enables faster retrieval of data. Infosys Technologies Ltd 43 ER/CORP/CRS/SE15/003 Version No: 2. Copyright © 2004.0 . but is an overhead when insertion.

Infosys Technologies Ltd 44 ER/CORP/CRS/SE15/003 Version No: 2.0 .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 Copyright © 2004.

0 . Infosys Technologies Ltd 45 ER/CORP/CRS/SE15/003 Version No: 2.Summary of Day-1 • Introduction to Analysis of algorithms – What is an Algorithm? – Properties of an Algorithm – Life cycle of an Algorithm • • Code Tuning Techniques SQL Tuning Techniques Copyright © 2004.

Thank You! Copyright © 2004.0 . Infosys Technologies Ltd 46 ER/CORP/CRS/SE15/003 Version No: 2.

- ADVT SQL Plan ExplainedSaeed Meethal
- Solve the Cube Intermediate MethodSaravanan Mari
- dba_checklist14[1]Nageswara Rao Yallavula
- The Impact of Psychoacoustic Technology on Steganongrpybagilit
- DBA Checklist 14kumarbadwal
- Problem Solving CSC415Muhammad Syawal
- ScriptSunny Tian
- DSA interview questions QuestionChirag
- STRUCTURED QUERY LANGUAGEshilpapillai
- EC2209 SET 2Mani Kandan K
- C Assignmentsshvshnkr
- Chapters 2 -Algorithmrpipalia
- DBA Checklist 14PranabKanojia
- Circular Linked ListsVishnu Prasad Goranthala
- Sample Data Structures and Algorithms Made Easyawilsm
- whole numbersapi-294491980
- Skyit 11g Db SPLS PT 6dayswise2011Anonymous zNpLA5nCo
- 32 Tips for Oracle SQL Query Writing and Performance TuningSenthil Kumar Jayabalan
- A+New+Algorithm+for+the+Design+of+a+Transportation+Fleet.pdfRodolfo Peña
- DATASTR1arunsmile
- ORDER by Slow Despite the Ordered Column Being Indexedexbis
- 1989-06_HP Journal PapersElizabeth Williams
- I.T.sck210585
- iTop 1-2 Admin Guidejhariti
- 01 DS and Algorithm Session 01Daya Nand Mishra
- An Improved Parallel Thinning AlgorithmBorja Ruiz Torres
- sapnote_0000631668Marlon Javier Forero
- HW2Ahui Chung
- CSCI 215 SyllabusJohn
- B.scRythemBansal

- tmp8BC6Frontiers
- UT Dallas Syllabus for opre7313.001.11s taught by Milind Dawande (milind)UT Dallas Provost's Technology Group
- Determining the shortest path for Travelling Salesman Problem using Nearest Neighbor AlgorithmInternational Journal for Scientific Research and Development
- UT Dallas Syllabus for cs6363.002.08s taught by Balaji Raghavachari (rbk)UT Dallas Provost's Technology Group
- Comparison of different Sub-Band Adaptive Noise Canceller with LMS and RLSInternational Journal for Scientific Research and Development
- Appraisal of PSO Algorithm over Genetic Algorithm in WSN Using NS2International Journal for Scientific Research and Development
- Comparative Analysis of Optimization Algorithms Based on Hybrid Soft Computing AlgorithmInternational Journal for Scientific Research and Development
- tmpCB3F.tmpFrontiers
- Analysis & Design Algorithm MCQ'SGuruKPO
- Hashing Algorithm: MD5International Journal for Scientific Research and Development
- OCR for Gujarati Numeral using Neural NetworkInternational Journal for Scientific Research and Development
- Scheduling Resources In a Hetero-Gene Cloud Using Genetic AlgorithmInternational Journal for Scientific Research and Development
- A review on Development of novel algorithm by combining Wavelet based Enhanced Canny edge Detection and Adaptive Filtering Method for Human Emotion RecognitionInternational Journal for Scientific Research and Development
- UT Dallas Syllabus for cs2305.002 05f taught by Timothy Farage (tfarage)UT Dallas Provost's Technology Group
- tmpDF60.tmpFrontiers
- A Survey of Modern Data Classification TechniquesInternational Journal for Scientific Research and Development
- UT Dallas Syllabus for cs3333.001.11s taught by Jeyakesavan Veerasamy (veerasam)UT Dallas Provost's Technology Group
- Introduction to Multi-Objective Clustering EnsembleInternational Journal for Scientific Research and Development
- tmpC0CF.tmpFrontiers
- Adoption of Parallel Genetic Algorithms for the Solution of System of EquationsWhite Globe Publications (IJORCS)
- tmp904.tmpFrontiers
- Principles of parallel algorithm models and their objectivesInternational Journal for Scientific Research and Development
- UT Dallas Syllabus for cs3345.501 05s taught by Greg Ozbirn (ozbirn)UT Dallas Provost's Technology Group
- Clustering on Uncertain DataInternational Journal for Scientific Research and Development
- UT Dallas Syllabus for se3345.502.07f taught by Ivor Page (ivor)UT Dallas Provost's Technology Group
- Public Cloud Partition Using Load Status Evaluation and Cloud Division RulesInternational Journal for Scientific Research and Development
- UT Dallas Syllabus for opre7313.001.08f taught by Milind Dawande (milind)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs4349.501 06f taught by Ramaswamy Chandrasekaran (chandra)UT Dallas Provost's Technology Group
- Improved Positive and Negative Quantitative Association Rule Mining using SAMInternational Journal for Scientific Research and Development
- A Survey on Gesture RecognitionInternational Journal for Scientific Research and Development

- tmpDADA.tmpFrontiers
- As 1774.21-2005 Refractories and Refractory Materials - Physical Test Methods Determination of Workability InSAI Global - APAC
- Multibiometric Secure Index Value Code Generation for Authentication and RetrievalInternational Journal for Scientific Research and Development
- tmpA0C1.tmpFrontiers
- As 1774.23.1-2001 Refractories and Refractory Materials - Physical Test Methods Abradability Index - ObliqueSAI Global - APAC
- 67657_2010-2014FRASER: Federal Reserve Archive
- 62242_2010-2014FRASER: Federal Reserve Archive
- Performance is Overrated - NEDB 2012Facebook
- 1978_1960-1964.pdfFRASER: Federal Reserve Archive
- 67645_2010-2014FRASER: Federal Reserve Archive
- Harvey Mudd Facebook Clinic Poster 2011Facebook
- An Enhanced Framework for Improving Spatio-Temporal Queries for Global Positioning SystemsWhite Globe Publications (IJORCS)
- 62240_2010-2014FRASER: Federal Reserve Archive
- Query as Region Partition in Managing Moving Objects for Concurrent Continuous QueryWhite Globe Publications (IJORCS)
- Indexing The World Wide Web: The Journey So FarLJ's infoDOCKET
- In Memory Database - Optimal Performance SolutionsInternational Journal for Scientific Research and Development
- 63646_2010-2014FRASER: Federal Reserve Archive
- Quality of Cluster Index Based on Study of Decision TreeWhite Globe Publications (IJORCS)
- As 1774.23.2-1999 Refractories and Refractory Materials - Physical Test Methods Abradability Index - PerpendiSAI Global - APAC
- 68438_2010-2014FRASER: Federal Reserve Archive
- 62195_2010-2014FRASER: Federal Reserve Archive
- 69531_2005-2009FRASER: Federal Reserve Archive
- 63646_2005-2009FRASER: Federal Reserve Archive

Sign up to vote on this title

UsefulNot usefulClose Dialog## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Loading