You are on page 1of 38

Identification of Computational

Problems
What Is Computer Science?

• It is fundamentally about computational


problem solving
• i.e., solving problems by the use of computation.
What is computation?
• It is any type of calculation that includes both
arithmetical and non-arithmetical steps and
follows a well-defined model
– E.g.: Algorithm.
• Computational thinking can be split into four
parts,
Decomposition
Pattern recognition
Abstraction
Algorithmic design
Computational thinking
• It allows us,
To take a complex problem
Understand what the problem is
Develop possible solutions
 Present these solutions in a way that a computer,
a human, or both, can understand.
Types of Computational Problems
Decision Problems

Search Problems

Counting Problems

Optimization Problems
Requirements to solve computational
problems
• In order to solve a problem computationally,
two things are needed,
A representation that captures all the relevant
aspects of the problem
An algorithm that solves the problem by use of
the representation
Man, Cabbage, Goat, Wolf problem
• Simple algorithmic
approach - brute force
approach.
• Start State (initial state)

• End State (goal state)

• Valid State

• Abstraction
What Is an Algorithm?
• It is a finite number of clearly described, unambiguous “doable” steps
that can be systematically followed to produce a desired result for
given input in a finite amount of time
• Computer algorithms are central to computer science.
• They provide step-by-step methods of computation that a machine can
carry out.
• Having high-speed machines (computers) that can consistently follow
and execute a given set of instructions provides a reliable and effective
means of realizing computation.
• However, the computation that a given computer performs is only as
good as the underlying algorithm used.
• Understanding what can be effectively programmed and executed by
computers, therefore, relies on the understanding of computer
algorithms
• Once an algorithm for solving a given problem is developed or found,
an important question is, “Can a solution to the problem be found in a
reasonable amount of time?” If not, then the particular algorithm is
of limited practical use.
Traveling Salesman Problem

• It is a classic computational problem in computer


science.
• The problem is to find the shortest route of travel for a
salesman needing to visit a given set of cities.
• In a brute force approach, the lengths of all possible
routes would be calculated and compared to find the
shortest one.
Traveling Salesman Problem
• For ten cities, the number of possible routes is 10! or over
three and a half million (3,628,800).
• For twenty cities, the number of possible routes is 20!, or over
two and a half quintillion (2,432,902,008,176,640,000).
• If we assume that a computer could compute the lengths of
one million routes per second, it would take over 77,000 years
to find the shortest route for twenty cities by this approach.
• For 50 cities, the number of possible routes is over 1064 .
• In this case, it would take more time to solve than the age of
the universe!.
• For problems such as this in which a brute-force approach is
impractical to use, more efficient problem-solving methods
must be discovered that find either an exact or an approximate
solution to the problem.
Representation of Algorithm
• There are two main ways that algorithms can
be represented,
Pseudocode
Flowcharts
Pseudocode
• “Pseudo”  Imitation or False
• “code”  Instructions written in a programming language
• Pseudocode  will be a representation that almost looks
like a code written in a programming language.
• It is also called Program Design Language (PDL)
• There are several formats which are used to write pseudo-
codes and most of them take down the structures from
languages such as C, Lisp, FORTRAN, etc.
• It allows you to include several control structures such
as While, If-then-else, Repeat-until, for and case, which is
present in many high-level languages
• It should be concise and the keyword should be in capital
letter good pseudocode should be language independent .
Flowchart
• It is the graphical or pictorial representation of an algorithm
General Rules for flowcharting
• In drawing a proper flowchart, all necessary requirements should be
listed out in logical order.
• The flowchart should be clear, neat and easy to follow. There should
not be any room for ambiguity in understanding the flowchart.
• The usual direction of the flow of a procedure or system is from left
to right or top to bottom.
• Only one flow line is used in conjunction with terminal symbol.

• Only one flow line should come out from a process symbol.

• Only one flow line should enter a decision symbol, but two or three
flow lines, one for each possible answer, should leave the decision
symbol.
General Rules for flowcharting
• Write within standard symbols briefly. As necessary,
you can use the annotation symbol to describe data
or computational steps more clearly.

• Avoid the intersection of flow.


• If the flowchart becomes complex, it is better to use
connector symbols to reduce the number of flow
lines
• Ensure that the flowchart has a logical start and
finish
• It is useful to test the validity of the flowchart by
passing through it with a simple test data.
Advantages Of flowcharts
• Communication: Flowcharts are better way of communicating
the logic of a system to all concerned or involved.
• Effective analysis: With the help of flowchart, problem can be
analyzed in more effective way therefore reducing cost and
wastage of time.
• Proper documentation: Program flowcharts serve as a good
program documentation, which is needed for various purposes,
making things more efficient.
• Efficient Coding: The flowcharts act as a guide or blueprint
during the systems analysis and program development phase.
• Proper Debugging: The flowchart helps in debugging process.
• Efficient Program Maintenance: The maintenance of
operating program becomes easy with the help of flowchart. It
helps the programmer to put efforts more efficiently on that
part
Disadvantages Of flowcharts
• Complex logic:
If the program logic is quite complicated, flowchart
becomes complex and clumsy.
This will become a pain for the user, resulting in a
waste of time and money trying to correct the
problem
• Alterations and Modifications:
If alterations are required the flowchart may require
re-drawing completely.
This will usually waste valuable time.
Building Block of Algorithm
Sequence structure
Decision Structure or Selection Structure
Repetition or Iteration Structure
Sequence structure
• This is the most elementary structure
• In this, the steps in an algorithm are
constructed in such a way that, no
condition step is required
• It is the logical equivalent of a straight
line.
Problem: Find the average of six numbers, given
the sum of the numbers.

ALGORITHM:
1. Start
2. Read the sum from the user
3. Calculate average by dividing the number by 6.
4. Print the average
5. Stop
Problem: Find the average of six numbers, given
the sum of the numbers.

Pseudocode: Flowchart:
START Terminal
READ the sum
CALCULATE Average = sum / 6 Input

OUTPUT the Average


STOP Process

Output

Terminal
Decision Structure or Selection Structure
• It is the case where in the algorithm, one has to make a choice of
two alternatives by making decision depending on a given
condition.
• Selection structures are also called case selection structures when
there are two or more alternatives to choose from

Flowchart Pseudocode

IF condition is true THEN


Do task A
ELSE
Do Task-B
ENDIF
Flowchart Pseudocode

IF condition is true THEN


Do Task-A
END IF

In this case, if condition is false, nothing happens. Otherwise Task-


A is executed
Problem: Determine and Output
Whether Number N is Even or Odd
ALGORITHM:
1. Start.
2. Get a number from the user.
3. If the number is exactly divisible by 2 then
print the given number is even, if not odd.
4. Stop.
Problem: Determine and Output Whether
Number N is Even or Odd
Flowchart Pseudocode
START
READ number N
IF N modulo 2 is equal to 0 THEN
Display “The number N is even”
ELSE
Display “The number N is odd”
STOP
Repetition or Iteration Structure
• This structure causes the certain steps to be
repeated
• The Repetition structure can be implemented
using,
The While Loop
The For Loop
Repeat Until Loop
• Any program instruction that repeats some
statement or sequence of statements a number of
times is called iteration or a loop
• The commands used to create iterations or loops
are all based on logical tests
Pseudocode : (WHILE loop)
WHILE (a condition is true)
A statement or block of
statements
ENDWHILE

Pseudocode :( DO WHILE loop)


DO
A statement or block of
statements
WHILE (a condition is true)
Pseudocode (FOR loop):
FOR (Initialization, stopping condition, increment/decrement)
Statements
ENDFOR
Pseudocode (REPEAT UNTIL):
REPEAT
A statement or block of statements
UNTIL condition
Problem: To print from 1 to 20
ALGORITHM:
1. Start
2. Set x=1
3. If x is less than or equal to 20 goto step 4, if not
goto step 7.
4. Print x
5. Increment the value of x
6. Goto step 3.
7. Stop
Problem: To print from 1 to 20
WHILE FOR DO WHILE REPEAT UNTIL
SET x=1 SET x=1 SET x=1 SET x=1
WHILE (x<=20) FOR(x, x<=20, x=x+1) DO REPEAT
PRINT x PRINT x PRINT x PRINT x
x=x+1 END FOR x=x+1 x=x+1
END WHILE WHILE (x<=20) UNTIL (x=21)
Store Check-out Process
Convert Temperature from Fahrenheit (℉) to Celsius (℃)

Pseudocode:
START
READ temperature in Fahrenheit (F)
CALCULATE Celcius (C) temperature
with formula C=5/9*(F-32),
PRINT C
STOP
Determine Whether a Temperature is Below or
Above the Freezing Point
Pseudocode:
START
INPUT temperature
IF (temperature <32) THEN
PRINT “Below freezing point"
ELSE
PRINT “Above freezing point"
STOP
Print sum of first 100 natural numbers
Pseudocode:
START
SET N=1, Sum=0
WHILE N<=100
Sum=Sum+N
N=N+1
END WHILE
PRINT Sum
STOP
Difference between Algorithm,
Flowchart, and Pseudo Code
• An algorithm is a sequence of instructions used to solve a
particular problem.
• Flowchart and Pseudo code are tools to document and
represent the algorithm.
• In other words, an algorithm can be represented using a
flowchart or a pseudo code.
• Flowchart is a graphical representation of the algorithm.
• Pseudo code is a readable, formally styled English like
language representation of the algorithm.
• Both flowchart and pseudo code use structured constructs of
the programming language for representation.
• The user does not require the knowledge of a programming
language to write or understand a flowchart or a pseudo
code.

You might also like