Problem Solving Techniques Slides
Problem Solving Techniques Slides
UNIT I
INTRODUCTION TO COMPUTER PROBLEM SOLVING
INTRODUCTION
Problem solving is an intricate process that requires, Much thought Careful planning Logical planning Persistence and Attention to detail
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.
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.
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.
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. Dont concern with details of the implementation before understanding a problem. The sooner you start coding your program the longer it is going to take
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
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
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
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 DESIGN
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
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
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
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=1 s=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+1 s=s+a[i] ----------------------> generalized solution for n>0
19
TOP-DOWN DESIGN
Termination 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 DESIGN
Termination of loops:
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 }
21
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
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 ALGORITHMS
Debugging 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 ALGORITHMS
Program 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
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
Problem Statement: Given two variables a and b, interchange the values of the variables.
30
Final Configuration
a 200 b 100
31
ALGORITHM:
1. 2. 3.
Save the value of variable a in variable t. Assign to variable a the value of variable b. Assign to variable b the value of variable a stored in variable t.
32
void exchange (int a, int b) { int t; //temporary variable t=a; a=b; b=t; }
Applications: Used in SORTING
33
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
COUNTING
ALGORITHM:
1. 2. 3.
Prompt and read the number of marks to be processed. Initialize count to zero. While there are still marks to be processed repeatedly do
a. b.
4.
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); }
37
Applications:
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
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. 2. 3.
Prompt and read total numbers to sum. Initialize sum for zero numbers. While less than n numbers have been summed repeatedly do
a. b.
4.
40
41
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=120 0!=1 1!=1*1 In general, n!=n*(n-1)! for n>=1 (or) n!=1*2*3**(n-1)*n for n>=1
44
FACTORIAL COMPUTATION
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
46
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)
47
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 do a. 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
50
51
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
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. 2. 3. 4.
5.
Prompt and read the number of fibonacci numbers to be generated. Assign first two fibonacci numbers a and b. Initialize count of number generated. 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. If n even then write out last two fibonacci numbers else write out second last fibonacci number.
54
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.
56
Problem Statement: Design an algorithm accepts a positive integer reverses the order of its digits.
that and
57
Example: If the inputted integer is 125 then, the digits of the integer in reversed order is 521.
58
ALGORITHM:
1. 2. 3.
Establish n, the positive integer to be reversed. Set the initial condition for the reversed integer dreverse. 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
The number of steps to reverse the digits in an integer is directly proportional to the number of digits in the integer.
60
BASE CONVERSION
61
BASE CONVERSION
EXAMPLE:
The octal representation of decimal 275 is 423.
62
BASE CONVERSION
ALGORITHM:
1.
2. 3.
Establish the newbase and initialize the quotient q to the decimal number to be converted. Set the new digit count ndigit to zero. 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
Implementation:
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 CONVERSION
65
Problem Statement: Given the character representation of an integer convert it to its conventional decimal format.
66
Example: Character representation 125 when converted to decimal format results in 125.
67
2. 3. 4.
5.
Establish the character string for conversion to decimal and its length n. Initialize decimal value to zero. Set base0 value to the ascii or ordinal value of 0. While less than n characters have been examined do a. Convert next character to corresponding decimal digit. b. Shift current decimal value to the left one digit and add in digit for current character. Return decimal integer corresponding to input character representation.
68
69