SCSX 1001 DESIGN AND ANALYSIS OF ALGORITHMS Syllabus
SCSX 1001 -DESIGN AND ANALYSIS OF ALGORITHMS
Syllabus: SCSX 1001 DESIGN AND ANALYSIS OF ALGORITHMS L T P Credits 3 0 0 3 Total Marks 100 UNIT 1 : INTRODUCTION TO COMPUTER PROBLEM SOLVING 12 Hrs Introduction, The Problem-Solving aspect, top-down design-Implementation of Algorithms-program verification-The efficiency of algorithms-The analysis of algorithms-Fundamental Algorithms: Introduction. Exchanging the values of two variables-Counting-Summation of a set of Numbers-factorial computation-Sine function computation-Generation of the Fibonacci sequence-Reversing the digits of an integer, base converstion-Character to Number conversion. UNIT 2 : FACTORING METHOD 12 Hrs Introduction-Finding the square root of a number-The smallest divisor of an integer-The greatest common divisor of two integers-Generating Prime Numbers-Computing the Prime Factors of an integer-Generation of Psedo-random Numbers-Raising a Number to a Large Power-Computing the nth Fibonacci Number. UNIT 3 : ARRAY TECHNIQUES 12 Hrs Introduction-Array Order Reversal-Array Counting or Histogramming-Finding the maximum Number in a Set -Removal of Duplicates from an Ordered Array-Partitioning an Array-Finding the kth smallest Element. Longest Monotone Subsequence. UNIT 4 : MERGING SORTING AND SEARCHING 12 Hrs Introduction, The Two-way Merge-Sorting by Selection-Sorting by Exchange-Sorting by Insertion-Sorting by Diminishing Increment-Sorting by Partitioning-Binary Search-Hash Searching. UNIT 5 : TEXT PROCESSING AND PATTERN SEARCHING 12 Hrs Introduction-Text Line Length Adjustment-Left and Right Justification of Text-Keyword Searching in Text Line Editing-Linear Pattern search- Sublinear Pattern Search.
TEXT BOOK : 1. Dromey. R. G, ”How to Solve it by Computer”, Prentice-Hall of India, Eighth Indian Reprint, 1996. REFERENCE BOOKS : 1. Aho. A. V. , Hopcroft. J. E and Ullman. J. D, ”The Design and Analysis of Computer Algorithms”, Addison-Wesley, Reading Mass. 2. Knuth, D. E. , ”The Art of computer programming Vol 1: Fundamental Algorithms”, Addison – Wesley , Reading Mass 3. Knuth, D. E. , ”Mathematical Analysis of algorithms”, Proceedings IFIP congress, Ljubljana UNIVERSITY EXAM QUESTION PAPER PATTERN Max Marks : 80 Exam Duration: 3 Hrs. PART A : 10 questions of 2 marks each – No choice 20 marks PART B : 5 questions from each of the FIVE units of internal choice, carrying 12 marks each 60 marks
Aim: This deals with the complete development of algorithm for complicated problems. It also deals with the design techniques and analysis of algorithms for efficiency, complexity and overall effectiveness. Objectives: §To analyze the efficiency of different algorithms for the same problem. §To study the various algorithm design techniques.
UNIT 1 : INTRODUCTION TO COMPUTER PROBLEM SOLVING Introduction: • Algorithm Step by step procedure for solving a problem Solution to a problem that is independent of any Programming language An algorithm is a sequence of computational steps that transform the input into the output” Correct algorithm halts with the correct output for every input instance “Algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.
Algorithm criteria: All algorithms must satisfy the following criteria: 1) Input 2) Output 3) Definiteness 4) Finiteness 5) Effectiveness. For example : sorting problem Input: A sequence of n numbers 31, 41, 59, 26, 41, 58 Output: Reordering of the input sequence 26, 31, 41, 58, 59 Definiteness: Sorting Technique Finiteness: Procedure for proper concerns Effectiveness: High Throughput .
• Programs: Set of instructions expressed in any programming Language (C, C++, JAVA, VB, J 2 EE, . NET etc. , ) A program is the expression of an algorithm in a programming language. • Data structure: A data structure is a way to store and organize data in order to facilitate access and modifications. Program=Algorithm + Data structure
Expressing Algorithms: q English description q Pseudo-code: Ø Like a programming language but its rules are less Ø Written as a combination of English and programming constructs Ø Based on selection (if, switch) and iteration (while, repeat) constructs in high-level programming languages ØIndependent of actual programming language
The Problem-Solving aspect: Requirements for solving problems by computer • Algorithm • Data Structure • Programming Language Problem Algorithm & Programming Language with data structure input “computer” output
The Problem solving aspect phases: 1. Problem definition phase 2. Getting started on a problem 3. The use of specific examples 4. Similarities among problems 5. Working backwards from the solution 6. General problem solving strategies 1. Problem Definition Phase: q Understand the problem (success in solving) q Work out what must be done rather than how to do it q Lot of care must be taken in working out what must be done q Example : § Finding the square root § Finding the greatest common divisor q From the definition develop an algorithm
2. Getting started on a problem • There may be many ways to solve the problem and also many solutions to most problems. • Gather more detail about problem • You start coding for the problem 3. The use of specific examples v Use some properties to try to get a solution for the problem Example : Find out the maximum number from the given set of numbers v Use some geometrical or schematic diagrams representing certain aspects of the problem
4. Similarities among problems: § See if there any similarities between the current problem and other problems that we have solved or we have seen solved § Try to solve the problem independently 5. Working backwards from the solution If we do not know where to start on a problem, We can work backwards to the starting conditions (if the expected result and initial conditions are known) Whatever attempts that we make to get started on a problem write down the various steps and explorations we made Once we have solved a problem we must remember the steps that we went about discovering the solution The most important one is practice
6. General Problem solving strategies: Divide and Conquer Binary doubling Dynamic programming §Backtracking §Branch and Bound §Greedy method Divide and Conquer: It is defined as one large complex problem is divided into number of sub problems and finds the solution. The sub problem solutions are combine to form the solution for large problem. Example: Merge sort algorithm Binary doubling: convert the binary numbers to decimal numbers Example: : 1 ->1, 10 ->2, 11 ->3, 100 ->4, 1000 ->8
Dynamic programming: Dynamic programming is an algorithm that can be used when the solution to a problem can be viewed as the result of a sequence of decisions. Example: TRAVELING SALESMAN PROBLEM §Backtracking: During the search if infeasible solution is sensed then backtrack to previous node. Example: 8 -queens problem §Branch and Bound: Branch- splitting procedure Bound- computes upper and lower bounds Example: Knapsack problem §Greedy method: Find the feasible solution from the set of solution for the given problem Example: job scheduling
Design v v Technique for Designing algorithm Another name for top down design is Stepwise Refinement It provides a way of handling complexity It allows us to build our solutions to a problem in a stepwise fashion 2. 1 Breaking Problem into Sub problems : Divide the task into subtasks. v Way in which the subtask need to interact with each other. v Subtask into smaller subtask. v
Subtask 1 a
n 2. 2 Choice of a suitable Data Structure v. Inappropriate choice DS leads to clumsy, inefficient and difficult in implementation. v Small change in Data Organization can have a significant influence on the algorithm required to solve the problem. Influence Of Data Structure : 1. Can the data structure be easily searched? 2. Can the data structure be easily updated? 3. Does the data structure provide a way of recovering an earlier state in the computation ? 4. Does the data structure involve the excessive use of storage? 5. Can the problem be formulated in terms of one of the common data structures (e. g. array, queue, stack, tree graph, list)
n 2. 3 Construction of Loops: To implement subtasks series of Iterative constructs or loops are needed Loop: It is defined as repetitively execute the set of instructions defined number of times. n Eg. . While, for To construct any loop consider 3 things • Initial Conditions that need to apply before the loop begins to execute i=0; • Invariant relation that must apply after each iteration of the loop i<=10; i++ • Conditions under which the iterative process must terminate for(i=0; i<=10; i++) Trouble In constructing loops • Getting correct initial condition. • Getting right number of times to execute loop.
2. 4 Establishing initial conditions for loops – Set the loop variables Variable: Value can be changed during run time – Set the number of iterations ‘n’ in the range I=0 to n -1 – Example : Find the sum of set of numbers • Solution : – Set the loop variable as ‘i’ – Sum variable as ‘S’ – The sum of zero numbers is zero and so the initial values of ‘i’ and ‘S’ as zero i : = 0; S : = 0;
• 2. 5 Finding the iterative construct Once we know the conditions for solving the smallest problem next step is try to extend it to the next smallest problem Solution to summation problem for n>=0 n= 5; while i < n do begin i= i+1; S=S+a[i] end • Need to consider about the termination of loops •
2. 6 Termination of loops Number of iterations are known in advance Example : for i = 1 to n do begin. . end The above loop terminates unconditionally after ‘n’ iterations v Terminates only When some conditional expression becomes false Example : while (x>0) and (x<10) do begin. end • v
3. Implementation of Algorithms – Use of procedures to emphasize modularity – Choice of variable names – Documentation of programs – Debugging programs – Program testing
3. 1 Use of procedures to emphasize modularity Modularity : “one complex larger task is divided into number of smaller tasks for simplicity” v v v In the first phase of implementation just place a write statement in the skeleton procedure indicating the name of procedure Implement and test the procedures one by one Implement a set of procedures to perform specific and well defined tasks Procedure should not be lengthy Main program can be implemented with calls to various procedures Example : procedure quicksort; begin writeln/printf(‘Sort called ’); end
• 3. 2 Choice of variable names v Make the programs more meaningful and easier to understand by choosing appropriate variable and constant names v Example: To make manipulations on the days of the week use variable name “day” instead of “a”. v Each variable should have only one role in a given program v Define all variables and constants at the start of each procedure clearly. •
• 3. 3 Documentation of programs Provide an accurate comment with each begin statement used v Because begin statement tells that some modular part of the Computation is about to follow v Provide an information about the programs to the user during the execution phase v Write a program in such a way that they can be used and executed by the people who are not familiar with language. v Provide a user manual v The program should “catch” incorrect responses to its requests and inform the user in an appropriate manner v
• 3. 4 Debugging Programs Debugging: “process of finding and reducing or fix the number of errors, or defects, in a computer program” • While implementing an algorithm check whether the program is working correctly for the given specifications • Detect logical errors during the compilation phase • Workout the program before attempting to execute it • The program is executing but producing incorrect results mean • Use a debugging trace to print out necessary information • Follow the program through by hand stepwise and check against the computer debugging output
Example : (Binary Search) lower : = 1; upper : = n; while lower<upper do begin middle : =(lower+upper) div 2; if x>a[middle] then lower: =middle+1 else upper: =middle end; found: =(x=a[lower])
3. 5 Program Testing v Check whether the program solves the smallest possible problem v Check whether it can handle the case when all data values. v whether it is able to handle all input condition?
Efficiency of algorithm mainly depends upon Ø Design Ø Implementation Ø Analysis of algorithms Every algorithm must use some of a computer’s resources to complete its task (Ex : CPU time, internal memory). Suggestions to design efficient algorithms v Redundant computations v Referencing array elements v Inefficiency due to late termination v Early detection of desired output conditions v Trading storage for efficiency gains
5. 1 Redundant computations : Problem here is v Unnecessary storage is used v Effect will be more serious when it is embedded within a loop The most common mistake is v Recalculate part of an expression that remains constant throughout the entire execution phase of the loop repeatedly Example : x: =0; for i: =1 to n do begin x: =x+0. 01; y: =(a*a*a+c)*x*x+b*b*x; writeln(‘x=‘, x, ’y=‘, y) end
The unnecessary multiplications and additions can be removed by pre computation before executing the loop a 3 c : = a*a*a+c; b 2: =b*b; X: =0; For i: =1 to n do begin x: =x+0. 01; y: =a 3 c*x*x+b 2*x; writeln(‘x=‘, x, ’y=‘, y) end
5. 2 Referencing Array elements : Example : To Find the maximum and its position in an array Version 1 : p: =1; for i: =2 to n do if a[i]>a[p] then p: =i; max: =a[p] Version 2 : p: =1; max: =a[1]; for i: =2 to n do if a[i]>max then begin max: =a[i]; p: =i end max : = a[p] –
– The version 2 is preferred because the conditional test ( a[i] > max) is more efficient than the test in version 1 – Use of variable max needs only one memory reference – Introduction of variable max makes it clear what task is to be accomplished In version 1, a[p] requires 2 memory references
5. 3 Inefficiency due to late termination : While linear search an alphabetically ordered list of names In an inefficient implementation all names were examined even if the point in the list was reached v v // Inefficient Algorithm while name s < > c and no end-of-file do get next name from list A more efficient implementation would be while name s > c and not end-of-file do get next name from list test if c is equal to name s
5. 4 Early detection of desired output conditions : It sometimes happen due to the nature of the input data, that algorithm establishes desired output condition before the general conditions for termination have been met. Eg) Floating point Add/Sub 5. 5 Trading storage for efficiency gains : To improve the performance of an algorithm v Avoid unnecessary test and computation. v
VERIFICATION Ø - Program verification Application of mathematical proof techniques Check the results obtained by the execution of a program with arbitrary inputs according to the output specifications -
4. 1 Computer Model for program execution : Ø We must know what happens when a program is executed under the given input conditions Ø Execution path that is followed for the given input conditions - Program may have more execution paths leading to successful termination - For a given set of input conditions only one of these path will be followed Ø Progress of computation may be thought of as a sequence of transitions from one computation state to another Ø Each state is defined by the values of all variables at the corresponding point in time Ø A state transition and progress towards completion is made by • Changing the value of variable • Transfer of control to the next instruction on the current execution path • Instruction that change the computation state
4. 2 Input and Output assertion Ø Formal statement has two parts • Input assertion • Output assertion - describe the State of executing program’s variables v Input Assertion - should specify any constraints that have been placed on the values of input variables used by the program Example : divide by zero (variable x, value 0) Input assertion becomes x< >0 When there are no restrictions on the values of input variables the input assertion will have the Boolean
v Output Assertion - specify symbolically the results for input data that satisfies the input assertion Example : Design a program to calculate the quotient q and the remainder r resulting from the division of x by y
4. 3 Verification of program segments with branches - To handle program segments that contain branches Set up and prove verification conditions for each branch separately Example : x is less than or equal to y readln(x, y); A {assert PA: true} if x>y then begin t: =x; x: =y; y: =t; end B {assert PB((x<=y)^(x=xo^y=y 0))V(x=y 0^y=x 0)}
- Slides: 41