PROBLEM SOLVING TECHNIQUES

69
PROBLEM SOLVING TECHNIQUES 1

Transcript of PROBLEM SOLVING TECHNIQUES

PROBLEM SOLVING TECHNIQUES

1

UNIT – I

INTRODUCTION TO COMPUTER PROBLEM SOLVING

2

INTRODUCTION Problem solving is an intricate process that requires,

• Much thought• Careful planning• Logical planning• Persistence and• Attention to detail

3

PROGRAMS AND ALGORITHMS

Program:• Computer solution to a problem is a set of explicit and unambiguous instructions expressed in a programming language.

• May also be thought of as an algorithm expressed in a programming language.

4

PROGRAMS AND ALGORITHMSAlgorithm:• A set of explicit and unambiguous finite steps which, when carried out for a given set of initial conditions, produce the corresponding output and terminate in a finite time.

• A solution to a problem that is independent of any programming language.

• Is capable of being implemented as a correct and efficient computer program.

5

INTRODUCTION

REQUIREMENTS FOR SOLVING PROBLEMS BY COMPUTER

• Depth of understanding• Data organization

Input / Output:• Supply the program with input or data so that the program manipulates the data according to its instructions and produces an output which represents the computer solution to the problem.

6

THE PROBLEM-SOLVING ASPECT

Problem Definition Phase:• Understand what it is we are trying to solve

• Work out what must be done rather than how to do it

• Extract from the problem statement a set of precisely defined tasks.

7

THE PROBLEM-SOLVING ASPECT

Getting started on a problem:• Many ways to solve most problems • Many solutions to most problems. • Difficult to find quickly which paths are fruitless and which are productive.

• Don’t concern with details of the implementation before understanding a problem.

• The sooner you start coding your program the longer it is going to take

8

THE PROBLEM-SOLVING ASPECTThe use of specific examples:

• Pick a specific example of the general problem to solve

• Try to work out the mechanism to solve this particular problem.

• Employ geometrical or schematic diagrams representing certain aspects of the problem.

• Examine the specifications or test cases for the problem carefully

• Check whether or not the proposed algorithm can meet those requirements.

9

THE PROBLEM-SOLVING ASPECT

Similarities among problems:

• See if there are any similarities between the current problem and other problems solved in the past.

• More tools and techniques can be brought in tackling a problem.

• It is wise to solve the problem independently.

• View the problem from a variety of angles – upside down, backwards, forwards, etc.. 10

THE PROBLEM-SOLVING ASPECT

Working backwards from the solution:

• Try to work backwards to the starting conditions if in some cases we have the solution to the problem.

• Write down the attempts whatever made along the various steps and explorations to systematize our investigations and avoid duplication of effort.

11

THE PROBLEM-SOLVING ASPECT

General Problem-Solving Strategies:

• Divide-and-conquer strategy:• The idea behind this strategy is to split the problem into smaller and smaller sub-problems until the sub-problems are enough to solve.

• Wide application in sorting, searching and selection algorithms.

• Dynamic programming:• To build a solution to a problem via a sequence of intermediate steps.

• Good solution to a large problem can sometimes be built up from good or optimal solutions to smaller problems.

• Greedy search, backtracking and branch-and-bound techniques. 12

TOP-DOWN DESIGN

• Top-down design or stepwise refinement:

• Allows to take the solution of a computer problem from a vague outline to a precisely defined algorithm and program implementation.

• Provides a way of handling logical complexity encountered in algorithms and allows to build solutions in a step wise fashion.

• Details of the implementation are encountered only at the stage when sufficient work on the various parts of the problem have done.

13

TOP-DOWN DESIGNBreaking a problem into subproblems:

• Before applying top-down design to a problem, do the groundwork that gives the outlines of a solution.

• The general outline may consist of a single statement or a set of statements.

• Top-down design suggests that,

• Take the general statements one at a time

• Break them into a set of more precisely defined subtasks. Subtasks should describe how the final goal is to be reached.

• Subtasks need to interact with each other and be precisely defined.

14

TOP-DOWN DESIGN

Choice of a suitable data structure:

• All programs operate on data and the way the data is organized can have a effect on every aspect of the solution.

• Inappropriate choice of data structure leads to inefficient and difficult implementations.

• Data structures and algorithms are linked to one another.

• A change in data organization can have a influence on the algorithm required to solve the problem.

• There are no rules stating for this class of problems this choice of data structure is appropriate. 15

WHILE SETTING UP DATA STRUCTURES• Be aware of

• Can the data structure be easily updated / searched?

• Does the data structure involve the excessive use of storage?

• Is it possible to impose some data structure on a problem that is not initially apparent?

16

TOP-DOWN DESIGN

Construction of loops:

• The initial conditions that need to apply before the loop begins to execute.

• The invariant relation that must apply after each iteration of the loop and

• The conditions under which the iterative process must terminate.

17

TOP-DOWN DESIGN

Establishing initial conditions for loops:• Set the loop variables to the values that they would have to assume in order to solve the smallest problem associated with the loop.

• Usually the number of iterations that must be made by a loop are in the range 0<=i<=n.

• The smallest problem usually corresponds to the case where i either equals 0 or 1.

• For e.g., in the algorithm of summation of n numbers, the smallest problem corresponds to the sum of zero numbers. The sum of zero numbers is zero and so the initial value of the loop variable i is 0 and the accumulated sum s is 0. 18

TOP-DOWN DESIGN

Finding the iterative construct:• Once we have the condition for solving the smallest problem the next step is to extend it to the next smallest problem.

• For e.g. in the summation of n numbers, the solution for n=1 is,

i=1s=a[1]

This solution for n=1 is built from the solution for n=0 using the values for i and s when n=0 and the two expressions

i=i+1s=s+a[i] ---------------------->

generalized solution for n>0

19

TOP-DOWN DESIGNTermination of loops:• The simplest condition for terminating a loop occurs when it is known in advance how many iterations need to be made.

for i=1 to n{This loop terminates unconditionally

after n iterations.}

• A second way in which loops can terminate is when some conditional expression becomes false.E.g. while (x>0 and x<10)

{}

• Number of iterations cannot be determined for the loop to terminate.

• No guarantee for a loop to terminate.20

TOP-DOWN DESIGNTermination of loops:

21

• Another way in which a loop can be terminated by forcing the condition under which the loop continue to iterate to become false.

a[n+1]=a[n];i=1;while a[i]<a[i+1]{

i=i+1}

IMPLEMENTATION OF ALGORITHMS

Use of procedures to emphasize modularity:• To assist with both the implementation and the readability of the main program, modularize the program using top-down design.

• Implement a set of independent procedures to perform specific and well defined tasks.

• In applying modularization, the process is not taken not too far

• The mechanism for the main program can then be implemented with calls to the various procedures that will be needed in the final implementation.

22

IMPLEMENTATION OF ALGORITHMS

Choice of variable names:• Choose appropriate variable and constant names to make programs more meaningful and easier to understand.

• This practice make programs more self-documenting.

• Each variable should only have one role in a program.

• A clear definition of all variables and constants at the start of each procedure can also be very useful. 23

IMPLEMENTATION OF ALGORITHMSDocumentation of programs:

• At the start of each modular part, give a brief accurate comment.

• Write programs with good documentation so that they can be executed and used by other people unfamiliar with the workings and input requirements of the program.

• The program must specify during execution exactly what responses and their format it requires from the user.

• The program should catch incorrect responses to its requests and inform the user in an appropriate manner.

24

IMPLEMENTATION OF ALGORITHMSDebugging programs:

• Carry out a number of tests to ensure that the program is behaving correctly according to its specifications.

• There may be logical errors in the program that is not shown in the compilation phase.

• To detect logical errors, build into the programs a set of statements that will print out information at strategic points in the computation.

• These statements can be made conditionally executable.If debug then /* debug is a boolean variable that is set to true when */

{ /* debugging output is required for the program */printf(……);

} 25

IMPLEMENTATION OF ALGORITHMS• Always work the program by hand before attempting to execute it.

• Draw up a two-dimensional table consisting of steps executed against all the variables used in the section of the program under consideration.

• Update the variables in the table as each variable is changed when the statements in the program section gets executed.

• A good rule when debugging is not to assume anything.

26

IMPLEMENTATION OF ALGORITHMSProgram testing:• Design to solve that it will cope with the limiting and unusual cases.

• For E.g., Tests for binary search algorithm.

• Will the algorithm handle the search of array of one element?

• Will the algorithm handle the case where the value sought is at an odd or even array location?

• Will it handle the case where all array values are equal?

• Programs should be accompanied by input and output assertions.

• Build into the program mechanisms that informatively respond to the user when it receives input conditions it was not designed to handle.

• Design algorithms to be very general so that they will handle a whole class of problems rather than just one specific case.

27

PROGRAM VERIFICATION• The application of mathematical proof techniques to establish that the results obtained by the execution of a program with arbitrary inputs are in accord with formally defined output specifications.

1. Computer model for program execution.2. Input assertion specify any constraints that have been placed on the values of the input variables used by the program.

3. Output assertion specify symbolically the results that the program is expected to produce for input data that satisfies the input assertion.

4. Implications and symbolic execution.5. Verification of straight-line program segments.6. Verification of program segments with branches / loops / arrays.

7. Proof of termination.28

THE EFFICIENCY OF ALGORITHMS

• Design algorithms that are economical in the use of CPU time and memory because of high cost of computing resources.

• Suggestions that are useful in designing efficient algorithms.• Redundant computations.• Referencing array elements.• Inefficiency due to late termination.• Early detection of desired output conditions.

• Trading storage for efficiency gains.

29

FUNDAMENTAL ALGORITHMS

EXCHANGING THE VALUES OF TWO VARIABLES

Problem Statement:Given two variables a and

b, interchange the values of the variables.

30

EXCHANGING THE VALUES OF TWO VARIABLES

Example: Starting

Configuration

31

100 200

200 100

Final Configuration

a b

ba

ALGORITHM:

1. Save the value of variable a in variable t.2. Assign to variable a the value of variable

b.3. Assign to variable b the value of variable

a stored in variable t.

32

EXCHANGING THE VALUES OF TWO VARIABLES

IMPLEMENTATION:

void exchange (int a, int b){int t; //temporary variablet=a;a=b;b=t;

}

33

EXCHANGING THE VALUES OF TWO VARIABLES

Applications: Used in SORTING

COUNTING

Problem Statement:Given a set of n

students examination marks (in the range 0 to 100), make a count of number of students passed the examination. A pass is awarded for all marks of 50 and above.

34

COUNTING

Example:Given following are the marks

obtained by 5 students in a particular subject.

45,67,89,34,82

O/P: Number of students passed the subject is 3.

35

ALGORITHM:

1. Prompt and read the number of marks to be processed.2. Initialize count to zero.3. While there are still marks to be processed

repeatedly doa. Read next mark.b. If it is a pass then add one to count.

4. Print total number of passed students.

36

COUNTING

IMPLEMENTATION:/* count the number of passes (>=50) in a set of marks */void passcount(){

const int passmark = 50;int count; /* contains the number of passes on termination */int i; /* current number of marks processed */int m; /* current mark */int n; /* total number of marks to be processed */printf("Enter a number of marks n on a separate line followed by the marks:");scanf("%d",&n);

{assert( n >= 0);}count = 0;i = 0;

{invariant: count = number of marks in the first I read that are >=passmark and i<=n}

while (i < n ) /* read next mark, test it for pass and update count if necessary */{i = i + 1;scanf("%d",&m);if( m >= passmark)count = count + 1;}

{assert: count = number of pass in the set of n marks read} printf("Number of passes = %d\n",count);}Applications: All forms of counting 37

COUNTING

SUMMATION OF A SET OF N NUMBERS

Problem Statement:Given a set of n numbers,

design an algorithm that adds these numbers and returns the resultant sum. Assume n is >=0.

38

SUMMATION OF A SET OF N NUMBERS

Example:Given following are the set of

numbers to find the resultant sum. 45,67,89,34,82

Resultant sum = 45+67+89+34+82 = 317

39

ALGORITHM:1. Prompt and read total numbers to sum.2. Initialize sum for zero numbers.3. While less than n numbers have been summed

repeatedly doa.Read next mark.b.Compute current sum by addin

4. Print total number of passed students.

40

SUMMATION OF A SET OF N NUMBERS

IMPLEMENTATION:

/* computes sum of n real numbers for n >= 0 */void sum(){

int i; /* summing loop index */int n; /* number of numbers to be summed */float a; /* current number to be summed */float s; /* sum of n numbers on termination */printf("Input n on a separate line, followed by numbers to be summed : ");scanf("%d",&n);{ assert (n>=0) }i = 0;s = 0.0;{invariant: s=sum of first I numbers read and i<=n}while(i<n) {

i = i + 1;scanf("%f",&a);s = s + a;

}{assert: s = sum of n numbers read} printf("sum of n = %d numbers = %f\n",n,s);

}

41

SUMMATION OF A SET OF N NUMBERS

SUMMATION OF A SET OF N NUMBERS

• The algorithm uses n additions to sum n numbers.

Applications: Average calculations, variance and least square calculations.

42

FACTORIAL COMPUTATION

Problem Statement:Given a number n,

compute n factorial (written as n!) where n>=0.

43

FACTORIAL COMPUTATION

Example:5!= 5*4*3*2*1=1200!=11!=1*1

In general,n!=n*(n-1)! for n>=1(or)n!=1*2*3*…………*(n-1)*n for n>=1

44

ALGORITHM:

1. Establish n, the factorial required where n>=0.2. Set product p for 0! Also set product count to zero.3. While less than n products have been calculated

repeatedly do,a. Increment product count.b. Compute the ith product p by multiplying I by the most recent product.

4. Return the result n!.

45

FACTORIAL COMPUTATION

IMPLEMENTATION:/* computes and returns n! for n >= 0 */int nfactorial(int n){

int i; /* loop index representing the ith factorial */int factor; /* i! */{assert (n >= 0)}factor = 1;{invariant: factor = i! after the ith iteration and i<=n}for ( i = 2; i<= n; i++)factor = i * factor;

{ assert: nfactorial = factor}

return factor;}int main(){

int n;printf(“Enter n! to compute:”);scanf(“%d”,&n);printf("%d! = %d\n", n, nfactorial(n));return 0;

} 46

FACTORIAL COMPUTATION

FACTORIAL COMPUTATION

• The algorithm uses n multiplications to compute n!.

• Possible to express n! in terms of (n/2)! (Similar to calculating nth fibonacci number)

Applications: Probability, Statistical and mathematical computations.

47

SINE FUNCTION COMPUTATION

Problem Statement:Design an algorithm to evaluate

the function sin(x) as defined by the infinite series expansion

sin(x) = x/1! – x3/3! + x5/5! – x7/7! + ….

48

ALGORITHM:

1. Set up initial conditions for the first term that cannot be computed iteratively.

2. While the absolute value of current term is greater than the acceptable error doa. Identify the current ith term. b. generate current term from its predecessor.c. add current term with the appropriate sign to the accumulated sum for the sine function.

49

SINE FUNCTION COMPUTATION

IMPLEMENTATION:const float error = 1.0e-6;

float Abs(float x){ if(x>0.0) return x; else return -x;}/* function returns sin(x) with an accuracy of <= error */float Sin(float x){ int i; /* variable to generate sequence 1 3 5 7 */ float x2; /* x squared */ float term; float tsin; /* current sum of terms - eventually approximates sin*/ {assert: -1.0 <= x <= 1.0} term = x; tsin = x; i = 1; x2 = x * x;

50

SINE FUNCTION COMPUTATION

SINE FUNCTION COMPUTATION{invariant: after the jth iteration, i=2j+1 and term = (-1)^j *

(x^i)/ i! and tsin is sum of first (j+1) terms}while(Abs(term) > error) /* generate and accumulate successive terms of sine expression */ { i = i + 2; term = -term * x2/(i*(i-1)); tsin = tsin + term; } return tsin; {assert: tsin~= sine(x) and abs(term)<=error}}

int main(){ float l=0; for(l=0;l<10;l+=1) printf("sin(%f) is %f\n",l,Sin(l)); return 0;}Applications:

Mathematical and Statistical computations.51

GENERATION OF THE FIBONACCI SEQUENCE

Problem Statement:Generate and print the first n terms of

the fibonacci sequence where n>=1.

The first few terms are: 0,1,1,2,3,5,8,13….

Each term beyond the first two is derived from the sum of its two nearest predecessors.52

GENERATION OF THE FIBONACCI SEQUENCE

Example:If n=5, the generated fibonacci sequence is 0,1,1,2,3

New term = preceding term + term before preceding term

53

ALGORITHM:1. Prompt and read the number of fibonacci numbers to be

generated.2. Assign first two fibonacci numbers a and b.3. Initialize count of number generated.4. While less than n fibonacci numbers have been generated

do,a. Print next two fibonacci numbers.b. Generate next fibonacci number by keeping

variable a relevant.c. Generate next fibonacci number from most

recent pair by keeping variable b relevant for next computation.

d. Update count of number of fibonacci numbers generated, i.

5. If n even then write out last two fibonacci numbers else write out second last fibonacci number.

54

GENERATION OF THE FIBONACCI SEQUENCE

void Fibonacci(){

int a,b; /* Fibonacci number variables */int i; /* number of Fibonacci numbers generated */int n; /* number of Fibonacci numbers to be generated */a = 0;b = 1;i = 2;printf("Enter the number of Fibonacci numbers to be generated : ");scanf("%d",&n);{assert( n > 0)}

{invariant: after the jth iteration I = 2j+2 and first I fibonacci numbers have been generated and a=(i-1)th Fib. No. and b= ith fib. No}while(i<n) {printf("%d %d ",a,b);a = a + b;b = a + b;i = i + 2;}if( i == n)printf("%d %d\n",a,b);elseprintf("%d\n",a);

{assert: first n fibonacci numbers generated and written out} }

55

GENERATION OF THE FIBONACCI SEQUENCE Implementation:

GENERATION OF THE FIBONACCI SEQUENCE

• To generate n fibonacci numbers, n steps are required. The algorithm works correctly for all values of n >=1

• Throughout the computation, the variables a and b always contain the two most recently generated fibonacci numbers.

Applications: Botany, Electrical network theory, sorting and searching.

56

REVERSING THE DIGITS OF AN INTEGER

Problem Statement:Design an algorithm that

accepts a positive integer and reverses the order of its digits.

57

REVERSING THE DIGITS OF AN INTEGER

Example:If the inputted integer is

125 then, the digits of the integer in reversed order is 521.

58

ALGORITHM:1. Establish n, the positive integer to be reversed.2. Set the initial condition for the reversed integer dreverse.3. While the integer being reversed is greater than zero do,

a. Use the remainder function to extract the rightmost digit of the number being reversed.

b. Increase the previous reversed integer representation dreverse by a factor of 10 and add to it the most recently extracted digit to give the current dreverse value.

c. Use integer division by 10 to remove the rightmost digit from the number being reversed.

59

REVERSING THE DIGITS OF AN INTEGER

Dreverse(int n){

int reverse; {assert: n>=0 and n contains k digits a(1), a(2), a(3), …., a(k)}

reverse=0; {invariant: after jth iteration, n=a(1), a(2), a(3), ….a(k-j) and reverse = a(k), a(k-1)

…. a(k-j+1)}while (n>0){reverse=reverse*10+((n % 10));n=n/10;}

{assert: reverse = a(k), a(k-1),…..a(1)}printf(“Reversed number=%d”,reverse);

}

• The number of steps to reverse the digits in an integer is directly proportional to the number of digits in the integer.

Applications:Hashing and information retrieval, database applications.

60

REVERSING THE DIGITS OF AN INTEGER Implementation:

BASE CONVERSION

Problem Statement:Convert a decimal

integer to its corresponding octal representation.

61

EXAMPLE:The octal representation of decimal 275 is 423.

62

BASE CONVERSION

ALGORITHM:1. Establish the newbase and initialize the

quotient q to the decimal number to be converted.

2. Set the new digit count ndigit to zero.3. Repeatedly,

A. Compute the next most significant digit i.e octal from the current quotient q as the remainder r after division by newbase.B. Convert r to appropriate ascii value.C. increment new digit count ndigit and store r in output array newrep.D. Compute the next quotient q from its predecessor using integer division by newbase until the quotient is zero.

63

BASE CONVERSION

void basechange( int n, int newbase){int i; /* index for new digit output array */int ascii; /* ascii value of current digit */int ndigit; /* current counter of new digits computed*/int q; /* current quotient */int r; /* current digit for newbase representation*/int zero; /* ascii value of zero character*/char newrep(100); /* output array */{assert: n>0 and 2<=newbase<=36}zero=‘0’;q=n; ndigit=0;do{ r=q%newbase;

ndigit=ndigit+1;ascii=zero+r;if ascii>’9’ then ascii=ascii+7;newrep[ndigit]=ascii;q=q/newbase;

}while(q=0);{assert: newrep[1..ndigit] contains the ndigit representation of n in the base

newbase in reverse order}printf (“Base %d representation of %d is”,newbase,n);for(i=ndigit;i>0;i--) printf(“%c”,newrep[i]);} 64

BASE CONVERSIONImplementation:

Applications:Interpretation of stored computer data and instructions.

65

BASE CONVERSION

CHARACTER TO NUMBER CONVERSION

Problem Statement:Given the character

representation of an integer convert it to its conventional decimal format.

66

CHARACTER TO NUMBER CONVERSION

Example:Character representation ‘125’ when

converted to decimal format results in

125.

67

CHARACTER TO NUMBER CONVERSION

Algorithm:

1. Establish the character string for conversion to decimal and its length n.

2. Initialize decimal value to zero.3. Set base0 value to the ascii or ordinal value

of ‘0’.4. While less than n characters have been

examined doa. Convert next character to corresponding decimal digit.b. Shift current decimal value to the left one digit and add in digit for current character.

5. Return decimal integer corresponding to input character representation. 68

CHARACTER TO NUMBER CONVERSION

Implementation:int chrtodec(char str[],int n){int i; /*Index for count of characters converted*/int dec /*Used to build converted decimal integer*/int base0/*ascii of character 0*/{assert: n>=0 and string [1..n] represents a non-negative

number}dec=0;base0=‘0’;{invariant: after the ith iteration, dec contains the I

leftmost digits of the string in integer form and i<=n}for i=1 to n

dec=dec*10+str[i]-base0;{assert: dec contains the integer representation of the n

digits in string}return dec;}Applications: Business applications, tape processing.

69