Professional Documents
Culture Documents
Computational Problem Solving PDF
Computational Problem Solving PDF
Computational Problem Solving PDF
In problem analysis phase, one has to analyze the problem clearly. The input data should be
analyzed and the expected output should also be defined. Then a solution should be
designed. In designing a solution, the types of data should be identified, how the data is
represented and structured is to be figured out and then an algorithm should be worked
out for solving the problem. Once the data representation and algorithm are finalized, the
algorithm can be implemented using a programming language. Then, the program is tested
for a selected set of problem instances. If any errors are detected, they are corrected and
then tested again.
Initially, the problem should be analyzed clearly. As an example, consider the Man,
Cabbage, Goat, Wolf [MCGW] problem. A man stays on the east side of a river with a
cabbage, goat and a wolf. He has to go to the other bank of the river carrying his
possessions. He has a small boat which can accommodate only one of his possessions along
with him. If he leaves the wolf and goat together, the wolf will eat the goat. If he leaves the
goat and the cabbage together, the goat will eat the cabbage. He has to find a solution to
carry all his possessions to the other bank. Given this problem, the problem has to be
analyzed for a solution. One way of finding a solution to this problem is to try all
possibilities and get a solution. This approach of finding a solution is called as the Brute
Force Approach. The problem is analyzed to identify the input data and the expected
output. The input data is the four elements, Man, Cabbage, Goat and Wolf on the East side of
the river. The expected output is Man, Cabbage, Goat and Wolf on the West side of the river.
Having analyzed input and output data, let us move on to the next phase of problem
solving.
Problem analysis involves the process of defining the problem. It involves the analysis of
the problem and generating appropriate response patterns for its solution. The four steps
in defining a problem are
i. Data collection and Requirements Gathering: In this phase, the data required for the
solution is collected and the missing gaps are filled in.
ii. Domain Knowledge: The domain knowledge is obtained by consultation with people who
are familiar with the problem domain. Ex. If a banking software is developed, it is
important to understand the functionalities by consulting with the bank employees.
iii. Viewing the Problem first hand: As far as possible, every detail about the problem
should be obtained as first hand information from the client.
Software development also follows a similar style as the general problem solving.
Customer / Client: A client is the one who places the request for software to be developed.
Software Developer: The developer team usually consists of a team to build the software.
User: The end user is the one who will use the software finally.
In software development, the software analysis is equivalent to the problem definition and
design+implementation is equivalent to the problem solution.
In software projects the problem definition starts with the requirement analysis. Analysis
needs the customer and developer interaction to prepare a legal agreement. The
requirement document records what is to be built and not how it is to be built. Successful
design and implementation requires an adequate analysis.
• Input
• Processing
• Output
• Error handling
The non-functional requirements include
• Reliability
• Safety
• Security
• Performance
• Delivery
• Help Facilities
Required components:
– A display
– Set/Reset button
– Up button
– Down button
– Number buttons (from 0 to 9)
– Close Button
Functional Requirements
Example 2:
An application that would play a video file with required buttons for the user to play, pause,
stop the video and for raising and lowering the volume.
Functional Requirements
Once the functional requirements are gathered, the requirements should be validated, to
check whether they are correct, consistent or complete. Correctness means that customers,
users, and developers understand in the specified requirements in the same way. So, to
check correctness, good functional requirement can be translated into a logical proposition.
One example of converting the requirement V2(pause) to logical expression is as follows:
– “If the video has already finished playing, then this button has no effect”.
The requirements should be validated for consistency also. Consistency means no two
functions contradict each another. For example, consider adding a new function V5 to
restart with the description “When the video is playing and reaches the end, it immediately
restarts playing with the play/pause button functioning as a play button.” This would
contradict V1(play) specification that play/pause button should function as a pause button
when videos are playing.
The requirements should also be verified for completeness. Completeness takes careful
consideration to be certain that every possible scenario has been considered and explained
in one or more requirement. The two ways to incorporate completeness includes:
• Manual checking
Solution design to a problem consists of two important activities. One is the data
representation and the other one is the algorithm design. The representation should
capture all the relevant aspects of the problem and the algorithm should be designed in
such a way that it solves the problem using that representation.
For example, consider the problem of finding the simple interest. You can represent the
input data principal amount using an integer variable, number of years using another
integer variable and rate of interest using a floating point variable. The output value can be
stored in a floating point variable. Choosing independent variables for representation
would be appropriate if the problem involves only a few data.
If you have a problem of finding the day of the week in which a date falls, the required data
can be represented as follows: The date can be represented using a date data type like, 25-
May-2017. The day of week of the first day of the month can be represented using a string
variable like ‘Mon’.
When the problem has a collection of data of the same data type, a one dimensional list
called as an array would be the appropriate representation. For example, in the Man, Cow,
Goat, Cabbage problem, the data can be represented as follows. The elements can be stored
in one list of string data type as [M, C, G, W] and their initial states can be stored using
another list of character data type as [E, E, E, E] to denote the east bank. The final state of
the elements can be represented as [M, C, G, W] and [W,W,W,W].
Similarly, if you have a problem in which you have to represent the seasons of a year, you
can use a string list like
The colors of a rainbow can be represented using a list of character data type like,
[V, I, B, G, Y, O, R]
The marks of a student in five subjects can be represented using a list of integer data type
like,
Complex problems with more dimensions of data of the same data type they can be
represented using multidimensional list or multidimensional arrays. Two dimensional
arrays are also called as tables or matrices. For example, matrices can be represented using
two dimensional lists.
Ex.
1 2 4
4 5 6
3 8 7
The data for a travelling salesman problem can be represented using a list of lists. Ex.
[ [‘Atlanta’, [‘Boston’, 1110], [‘Chicago’, 718], [‘Los Angeles’, 2175], [‘New York’, 888],
[‘Boston’, [‘Chicago’, 992], [‘Los Angeles’, 2991], [‘New York’, 215], [‘San Francisco’, 3106] ],
[‘Chicago’, [‘Los Angeles’, 2015], [‘New York’, 791], [‘San Francisco’, 2131] ],
Similarly, for chess problem, the data can be represented using a table or two dimensional
list.
If the problem consists of multiple data of different data types, the data can be represented
using complex data types like structures. For example, if you want to represent a data for
an object like a book, it can be represented using a structure:
struct book
bookId number, bookTitle string, author string, no_of_pages number, price number
}
9.2.2 Algorithm Design
Once the data in a problem is represented, the solution can be designed systematically. The
step by step solution to a problem is called as an algorithm.
Once the algorithm, is designed, an appropriate programming language is selected and the
algorithm is implemented using the programming language.
9.4 Testing
• Abstraction: Hiding irrelevant complexities and exposing the relevant details alone.
• Algorithm Design: Developing the step by step instructions for solving this and
similar problems
The process of breaking down a complex problem or system into smaller sub problems
with more manageable parts is known as Problem Decomposition. Decomposition helps to
solve complex problems and manage large projects. Large problems can be tackled with
“divide and conquer”. Decompose the problem so that, each sub problem is at (roughly) the
same level of detail. Each sub problem can be solved independently. The solutions to the
sub problems can be combined or integrated to solve the original larger problem. This
approach of solving a problem is known as top down approach or modular programming.
This involves tasks such as arranging staff, booking coaches, booking hotels, getting
consent letters, checking weather, checking resources etc. Each task can be assigned to a
different student or to different student teams.
Typically this would be decomposed as years and subjects, further decomposed into terms,
units of work and individual lessons or activities. Notice how the project is tackled by a
team working together, and how important it is for the parts to integrate properly.
This list has broken down the complex problem of creating an app into much simpler
problems that can now be worked out. You may also be able to get other people to help you
with different individual parts of the app. For example, you may have a friend who can
create the graphics, while another will be your tester.
If a problem is not decomposed, it is much harder to solve. Dealing with many different
stages all at once is much more difficult than breaking a problem down into a number of
smaller problems and solving each one, one at a time. Breaking the problem down into
smaller parts means that each smaller problem can be examined in more detail. Similarly,
trying to understand how a complex system works is easier using decomposition.
Advantages of Decomposition
– Maintenance is easier.
Disadvantages of Decomposition
– The solutions to the sub problems might not combine to solve the original
problem.
– Poorly understood problems are hard to decompose.
9.5.2 Abstraction
To drive a car - we don’t need to know how the car works under the hood. An abstraction is
anything that allows us to concentrate on important characteristics while deemphasizing
less important, perhaps distracting, details. Focusing on relevant things and hiding the
irrelevant things is known as abstraction. Abstraction is the process of making an easier to
understand version of a complex system by focusing only on the key information.
Abstraction helps in reducing the complexity of systems being considered. Abstraction
allows controlled exposure to details as a series of layers. Computer scientists learn to
focus on the most important issues through abstraction. An abstraction is the distinction
between an entity’s external properties and its internal representation when perceived by
a third party (observer) who wants to use that entity. Abstraction creates a ‘component’ by
ignoring their internal representation. Abstraction allows us to combine similar
components at the same level and to build upon components at lower levels to create
higher order components.
Types of Abstraction
Advantages of Abstraction
Every user will get his own view of the data according to his requirements and will
not get confused with unnecessary data.
– Deductive Reasoning
– Inductive Reasoning
Deductive Reasoning
Arriving at a conclusion for a specific instance from a general rule or pattern is known as
deductive reasoning. Deductive reasoning is used for solving many problems.
Sequential task or sequential logic involves the ordering of tasks one after the other (Line
by line execution).
2. Send a message
1. priceWithTax 0
2. itemCost 100
4. priceWithTax 105.5
Examples
– The program controlling an online website must choose when to bill a
customer.
IF the user has struck the Confirm Purchase button, THEN initiate
billing procedures.
C B
– The program in your digital camera chooses how long to leave the shutter
open when taking a picture.
SC
– The program controlling your portable music player chooses which song to
play next.
IF a song has just finished playing, THEN begin playing the next song
in the playlist.
FN
Cause Effect
Username and password have been Check if username password
entered combination is valid
User clicks the close button of a window Remove the closed window from the
display
Laptop battery is below 10% Popup a window warning user for low
power
A new email message arrives at the Ring a bell to notify the user
inbox
Cause – effect relationship is used to apply general rules for a specific situation. Therefore,
it is an application of deductive reasoning.
Ex. Swapping
3. Transfer the contents of the temporary variable to the second variable [y=temp]
r =n%10
s=s+r
n=n/10
Inductive Reasoning
Examples
If the problem is a simple problem for which the solution can be found by the application of
a formula, the solution can be found out using the direct calculation method.
area = 3.14 * r * r
F = 9/5*C + 32
Algorithm:
For problems like the MCGW problem, the algorithm can be found by Brute Force
approach. In this approach, the solution is found out by trying out all possible solutions and
then choosing the best solution from the list of possible solutions.
For problems where standard algorithms exist, they can be used for solving them.
Ex.
Sorting an array
Chess problem
For problems, that do not have standard algorithms, new algorithms can be designed by
the programmers.
Algorithms that work well in general but are not guaranteed to give the correct result for
each specific problem are known as heuristic algorithms. Such algorithms find a solution
out of all possible ones, but they do not guarantee that the best would be found. Therefore
such algorithms are considered to be approximate and not accurate algorithms. These
algorithms arrive at fast solutions and may find a solution that is close to the best solution.
They always give a good solution but not the best. They are most often employed when
approximate solutions are sufficient and the exact solutions are computationally complex
and expensive.
i. Algorithm
The solution to a problem can be represented as step by step instructions in
plain natural language. This is known as an algorithm.
ii. Flowchart
The pictorial representation of the solution (algorithm) is known as a flowchart
iii. Pseudocode
Representing the solution using some specific keywords is called as a
pseudocode.
iv. Program
Implementing the solution in a programming language is known as a program.
i. Input
An algorithm should have an input. The input may be a static or a dynamic one.
Static inputs are provided to the program before execution whereas dynamic
inputs are provided during runtime. Input refers to the data that is to be
transformed during the computation to produce the output.
ii. Output
The data or information that is resulting from the computation is called as the
output.
iii. Definiteness
An algorithm should specify the clear sequence of the events. It should describe
the details of each step including how to handle errors.
iv. Effectiveness
An algorithm should contain feasible instructions. The instructions should not be
ambiguous.
v. Finiteness
An algorithm must eventually stop.
9.8 FlowChart
Example 1
Example 2
9.8.1.2 Selection
Selecting one of two possible actions based on a particular condition is called selection.
This logic is used when the solution to a problem involves a decision making.
Example 1
Example 2
9.8.1.3 Iteration
If a few tasks have to be repeated in a solution, then the logic is known as iteration logic.
Loop structures are used for solving iteration. A loop tests a condition and if it is satisfied,
performs an action. Then it tests the Iteration structure condition again. If the condition is
still satisfied, the action is repeated. This is repeated until the condition is not satisfied.
Example 1
Example 2
9.8.2 Advantages of Flowcharts
• Effective Analysis: With the help of flowchart, problems can be analyzed in more
effective ways.
• Efficient Coding: Flowcharts act as a guide or blueprint during system analysis and
program development phase.