0 ratings0% found this document useful (0 votes) 15 views9 pagesUnit-1 - PST (IT) - Notes
Problem Solving Techniques
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Problem Solving Techniques Arunkumar, A.P/C.S - MMC
Unit
Introduction: Notion of algorithms and programs — Requirements for solving problems by computer ~ The
problem-solving aspect: Problem definition phase, Getting started on a problem, ‘The use of specific examples,
Similarities among problems, Working backwards from the solution - General problem-solving strategies.
‘Top-Down Programming: Problem solving using top-down design — Implementation of algorithms — The
concept of Recursion
Program
‘A specifie set of ordered operations for a computer to perform.
‘These instructions can be written in a programming language.
© A program may also be thought of as an algorithm expressed in a programming language.
Algorithm
© Astep by step procedure for solving a problem.
© Itis.an ordeted set of rules to solve a problem,
© It isa representation of a solution to a problem.
«It consists set of instructions that take some value o set of values as input, and produces some value or set
of values as output.
Algorithm
Input —— | Astep by step method for solving
a problem
—_ Output
Steps involved in algorithm development:
An algorithm can be defined as “a complete, unambiguous, finite number of logical steps for solving a
specific problem“
- Step1. Identification of input: For an algorithm, there are quantities to be supplied called input and these
are fed externally. The input is to be indentified first for any specified problem.
- Step2: Identification of output: From an algorithm, at least one quantity is produced, called for any
specified problem.
- Step3 : Identification the processing operations : All the calculations to be performed in order to lead
to output from the input are to be identified in an orderly manner.
- Step4 : Processing Definiteness : The instructions composing the algorithm must be clear and there
should not be any ambiguity in them.
- StepS : Processing Finiteness : If we go through the algorithm, then for all cases, the algorithm should
terminate after a finite number of steps.
- Step6 : Possessing Effectiveness : The instructions in the algorithm must be sufficiently basic and in
practice they can be carries out easily
Requirements for solving problems by computer
An algorithm must possess the following properties
1. Finiteness: An algorithm must terminate in a finite number of steps
2. Definiteness: Each step of the algorithm must be precisely and unambiguously stated
3. Effectiveness: Each step must be effective, in the sense that it should be primitive easily convert able into
program statement) can be performed exactly in a finite amount of time.
1=BSc (CS) - Uni Page 1Problem Solving Techniques G.Arunkwmar, A.P/C.S - MMC
4. Generality: The algorithm must be complete in itself so that it can be used to solve problems of a specific
type for any input data.
5. Input/output: Each algorithm must take zero, one or more quantities as input data produce one or more
output values. An algorithm can be written in English like sentences or in any standard representation
sometimes, algorithm written in English like languages are called Pseudo Code,
The Problem-solving aspects:
© Problem-solving is a cre
ive process which largely defies systemization and mechanization,
© There are number of steps that can he taken to raise a solution for any problem.
* It provides different strategies to work on different problems.
© The aspects of problem-solving are,
¥ Problem definition phase
Getting started on a problem
The use of specific examples
Similarities among problems
Working backwards from the solution
General problem-solving strategies
SARS
Problem definition pha
* It is a preliminary investigation of the problem solving process.
‘© This phase focused on ‘what must be done’ rather than ‘how to do it.
‘© The first condition of solving a problem is understanding the problem, which can be done by way of a
problem statement.
It is during this phase the problem is being flushed out by the program development team by means of a
problem statement.
‘© A problem statement is a description of an issue to be addressed or a condition to be improved upon,
‘© identifies the gap between the current problem and goal.
© The problem statement should be designed to address the issues and requirements.
Getting started on a problem.
«The thorough understanding and identification of the problem for which the program or software is to be
developed.
All the factors like Input / Output, processing requirement, memory requirements, error handling, and
interfacing with other programs have to be taken into consideration in this stage.
© Then the problem is further delineated by the constraints of the project.
© The best advice here is not to be too concerned about detail. That can come later when the complexit
the problem as a whole has been brought under control,
The old computer proverb which says “the sooner you start coding your program the
longer it is going to take”
The use of specific examples
© A.usefil strategy when we are stuck is to use some props or heuri
start with the problem.
s (ie., rules of thumbs) to try to get a
BSc (CS) - Unit- Page 2Problem Solving Techniques G.Arunkwmar, A.P/C.S - MMC
‘© An approach that often allows us to make a start on a problem is to pick a specific example of the general
problem we wish to solve and try to work out the mechanism that will allow us to solve the particular
problem.
Its very easy (o fall into a rap of thinking that the solution to a specific problem or a specific class of
problems is also a solution to the general problem.
Sometimes this happens but we should always be very wary of making such an assumption.
Similarities among problem
‘© It is important to see if there are any similarities between the current problem and other problems that we
have solved or we have seen solved.
© Once we had a little experience in computer problem-solving it is unlikely that a new problem will be
completely divorced from other problems we have seen,
© A good habit therefore is to always make an effort to be aware of the similarities among problems.
* The more experience one has the more tools and techniques one can bring to bear in tackling a given
problem.
Working backwards from the solution.
‘* Whatever attempts that we make to get started on a problem we should write down as we go along the
various steps and explorations made,
‘© This can be important in allowing us to systemize our investigations and avoid duplication of effort.
‘Another practice that helps us develop our problem-solving skills is, once we have solved a problem, to
consciously reflect back on the way we went about discovering the solution,
‘© This can help us significantly. The most crucial thing of all in developing problem-solving skills is
practice.
Piaget summed this up very nicely with the statement that “we learn most when we have to invent”,
General Problem-solving strate
© There are a number of general and powerful computational strategies that are repeatedly used in various
guises in computing science.
© Probably the most widely known and most often used of these principles is the divide and conquer
strategy,
The basic idea with divide-and-conquer is to divide the original problem into two or more subproblems
which can hopefully be solved more efficiently by the same techniques.
‘© The splitting process eventually reach the stage where the subproblems are small enough to be solved
without further splitting,
This way of breaking down the solution to a problem has found wide application in particular with sorting,
selection, and searching algorithms.
* Another general problem-solving strategy is dynamic programming.
© This method is used most often when we have to build up a solution to a problem via a sequence of
intermediate steps.
The main difference is that in divide and conquer, you break down the problem into smaller pieces and
then solve each one separately, whereas, in dynamic programming, you break down the problem into
smaller pieces and then solve each one together.
© Bg., Greedy search, Backtracking and Branch-and-Bound.
BSc (CS) - Unit- Page 3Problem Solving Techniques G.Arunkumar, A.P/C.S ~ MMC
‘Top-Down Programming
© Itis.a software development technique that imposes a hierarchical structure on the design of the program.
It starts out by defining the solution at the high level of functionality and breaking it down further and
further into small routines that can be easily documented and coded.
‘We can apply this to take the solution of a computer problem from a vague outline to a precisely defined
algorithm and program implementation.
© It allows us to build our solutions to a problem in a stepwise fashion. So it is also called as “Stepwise
refinement”,
‘¢ The following steps are involved in the top — down programming approach,
Y Breaking a problem into a sub problems
Choice of a suitable data structure
Construction of loops
stablishing initial conditions for loops
Finding the iterative construct
Termination of loops
Breaking a problem into a sub problems:
‘The general outline may consist of a single statement or a set of statements.
* Top — Down design suggests us to break the general outline down into a set of more precisely defined
subtasks.
‘© These subtasks should more accurately describe how the final goal is to be reached.
‘¢ With each splitting of a task into subtasks it is essential that the way in which the subtasks need to interact
with each other be precisely defined.
‘* The process of repeatedly breaking a task down into subtasks and then each subtask into still smaller
subtasks must continue till we eventually end up with subtasks that can be implemented as program
statements,
SANK
General ovine
ST
ae nes ae
Taput conditions Ouiper requirements
Body of elgorithm
aN
Subtask 1 Subtask 3
Subtesk 2
Subiaskta}
Subtask 2a
Choice of a suitable data structure:
© One of the most important decisions we have to make in formulating computer solutions to problems is the
choice of appropriate data structures.
© A data structure is « specialized format for organizing, processing, retrieving and storing data.
BSc (CS) - Unit- Page 4Problem Solving Techniques G.Arunkwmar, A.P/C.S - MMC
‘All programs operate on data and consequently the way the data is organized can have a profound effect on
every aspect of the final solution,
‘© A data structure helps us to treat such data logically.
‘* Some of the most common operations include search, access, insert, delete
© Queue, Stack, Array, Linked List, and Hash table are some of the complex structures available in data
structure.
* To make the best choice possible, we have to consider how the data structure will be used and how often
© Try to choose the one that has the lowest costs for the operations done the most often.
‘Elements to be considered while choosing the data structure,
Y Time Complexity ~ The performance of your application might be significantly impacted by the
time complexity of your data structure. (Eg., Hash table for applications which require frequent
search or retrieval operations)
Y Space Complexity ~ If your application is memory intensive, choose a data structure with less space
complexity, such as array. If space is not concern, you may use a data structure with a greater space
complexity, such as a tree.
Y Read vs Write operations — If your application utilizes a lot of write operations, pick a data
structure with a quicker insertion performance, like a hash table. If your application calls for many
read operations, pick a data structure with a quicker search speed, such as a binary search tree.
Y Type of data — you may use a tree-based data structure if your data is hierarchical. If you have a
simple data that needs to be accessed randomly, choosing an array-based data structure can be the
best option.
¥ Available libraries ~ Consider the libraries that are readily accessible for the data structure you are
considering.
Construction of loop:
While moving towards subtasks, we are lead to a series of iterative constructs, or loops, and structures that
are conditionally executed.
© To construct any loop we must take into account three things,
¥- The initial conditions that need to apply before the loop begins to execute
¥ The invariant relation that must apply each iteration of the loop
Y And the conditions under which the iterative process must cerminate.
* In constructing loops people often have trouble in getting the initial conditions correct and in getting the
loop execute the right number of times.
Establishing initial conditions for loops:
© Typically the number of iterations m that must be made by a loop are in the range 0 n.
‘© The smallest problem usually corresponds to the case where I either equals to 0 or 1.
© Eg., We can take a simple array addition,
SH (a tart cesses ta)
Tr can equivalently be denoted as,
n
S=Yai
in
Finding the iterative construct:
‘¢ Iterative process means repeating a set of actions a certain number of times to perform some task.
© There are two constructs,
BSc (CS) - Unit- Page 5Problem Solving Techniques G.Arunkwmar, A.P/C.S - MMC
‘© Entry controlled loop ~ It checks the condition at the time of entry. Only if the condition is true, the
program control enters the body of the loop. For and while loops are entry controlled loops.
‘* Exit controlled loop ~ It checks the condition after executing its body. If the condition is true, loop will
perform the next iteration otherwise program contro! will move out of the loop. Do while loop is an exit
controlled loop.
for (initialization:condition:update)
{
Mloop-body
1
dof
Moop-body
}while(condition);
while (condition) (
/Moop-body
}
’ermination of loops.
The simplest condition for terminating a loop occurs when it is known in advance how much iteration need
to be made.
for i:=1 tondo
begin
end
This loops terminates unconditionally afer n iterations.
A second way in which loops can terminate is when some conditional expres
example is:
jon becomes false, An
while (x>0) and (x<10) do
begin
end
with loops of this kind, it cannot be directly determined in advance how many iterations there will be
before the loop will terminate. In fact there is no guarantee that loops of this kind will terminate at all.
Implementation of algorithins:
If.an algorithm has been properly designed the path of execution should flow in a straight line from top to
bottom, Programs implemented in this way are usually much easier to understand and debug.
Use of procedures to emphasize modularity:
‘© Modularizing the problem is helpful for both implementation and the readability of the main program.
. allows us to implement a set of independent procedures to perform specific and well-deti
ed
© For example, if as part of the algorithm it is required to sort an array, then a specific independent procedure
should be used for the sort.
© The mechanism for the main program can then be implemented with calls to the various procedures that
will be needed in the final implementation.
BSc (CS) - Unit- Page 6Problem Solving Techniques G.Arunkwmar, A.P/C.S - MMC
‘In the first phase of the implementation, before we have implemented any of the procedures, we can just
place a write statement in the skeleton procedures which simply writes out the procedure’s name when itis,
called.
procedure sort
begin
writeln(‘sort called”)
end
This practice allows us to (est the mechanism of the main program at an early stage and test and implement
the procedures one by one.
Choice of variable name:
Choosing appropriate variable and constant names will make the program more meaningful and easier to
understand,
© For example, if we have to make manipulations on day of the week, we are much better off using the
variable day rather than the single letter a.
* Inaddition, each variable should only have one role in a given program,
# This practice tends to make programs much more self-documenting.
Documentation of programs:
© Program documentation is the information that the program presents to the user during the execution
phase.
# A good programming practice is to always write programs so that they can be executed and used by other
people unfamiliar with the workings and input requirements of the program.
Debugging programs:
‘© Debugging is the process of finding and fixing errors and bugs in the source code of any software.
‘© When software does not work as expected, computer programmers study the code to determine why any
errors occurred.
* The debugging process typically requires error identification, error analysis, fix and validation
Some type of errors that often require the process of debugging is listed below.
¥ Syntax errors — It occurs when a computer program has an incorrectly typed statement. It is the
equivalent of a typo or spelling error in word processing. The program will not compile or run if
syntax errors are present,
¥- Semantic errors ~ We occurs due to the improper use of the programming statements. It is generated
if the code make no sense. Bg., int x=12.3; Here 12.3 is not a integer literal and there is no implicit
conversion from 12.3 to int, so this statement does not make sense.
¥ Logic errors ~ It occurs when the programmers misrepresent the step-wise process or algorithm of
1 computer program. For example, the code may exit a loop too early or may have an incorrect if-
then outcome. These errors can be identified through the code for several different input/output
scenarios.
Y Runtime errors — It occurs due to computing environment in which the software code runs.
Examples include insufficient memory space or stack overflow.
Program testing
Test will handle all variations of the problem it was designed to solve by coping with the unusual test
cases.
C Functional Test Cases ee Nate
1_| Verify ia user will be able to login with a valid username and valid password: Positive
BSc (CS) - Unit- Page 7Problem Solving Techniques G.Arunkwmar, A.P/C.S - MMC
2_| Verify ifa user cannot login with a valid username and an invalid password. Negative
| Verily the login page for both, when the field is blank and Submit button is Sippaka
cegative
clicked.
[Verify the “Forgot Password” funetionalit Positive
S_| Verify the messages for invalid login. Positive
6_| Verify the “Remember Me™ functional Positive
7_| Verily i the duta in password field is either visible as asterisk or bullet signs. Positive
‘Verify if'a user is able to login with a new password only after he/she has
8 | changed the password. Rosine
Verity if the login page allows to log in simultaneously with different
a 3 Positive
credentials in a different browser.
10 | Verify ifthe “Enter” Key of the keyboard is working correctly on the login 3
ve ositive
Other Test Cases
11] Verily the time taken t log in witha valid username and password Performance &
Positive Testin;
12 | Verily ifthe Tont, text color, and color coding ofthe Login page is as per the Ur Testing &
* | standard. Positive Testing
13 | Verily ifthere is a Cancel" buiton available to erase the entered text. Usability Testing
Verify the login page and all its controls in different browsers Browser
4 Compatibility &
Positive Testing
The concept of recursion.
‘© Recursion is a method of solving problems that involves breaking a problem down into smaller and smaller
sub problems until you get to a small enough problem that it can be solved trivially
© Usually recursion involves a function calling itself. (The normal function is called by main function)
‘© While it may not seem like much on the surface, recursion allows us to write elegant solutions to problems
that may otherwise be very difficult to program.
For Example,
main ()
{
A);
Funetion ealled by main
FIO): Function definition
fC);
}
Example programs on Recursion
Write a program to find the factorial of given non-negative integer using recursive function.
Hinelude
main ()
Function called by itsel
1=BSe (CS) - Unit-1 Page 8{
int result, n;
printf{ “ Enter any non-negative integer\n");
seanf(“%d", & n);
result = fact(
print{(“ The factorial of Yd is Yd \n, n, result);
)
fact(n)
int n;
(
inti
i
if( 1) return (i);
else
{
i=i* fact(n- Dr
return (i);
}
}
Reference:
* R.G, Dromey, How to Solve it by Computer, Pearson India, 2007,
© Problem solving & Python programming, Dr.A.Vincent Antony Kumar, Dr. R. Sivakami, Dr. P. Pushpa
lakshmi, Dr. N. Pandeeswari, Charulatha publications, 2019.
1-BSc (CS) - Unit-1 Page 9