2. Problem
A well-defined problem has five components:
• A clearly defined initial situation given
• A clearly defined goal
• A clearly defined set of resources
• A clearly defined constraints
• Ownership
3. Case Study
You are at a river that you want to cross with
all your goods. Your goods consist of a chicken, a
bag of grain and your cat. You have to cross the
river in your boat but can only take one
passenger with you at a time – the chicken, the
cat or the bag of grain. You can’t leave the
chicken alone with the grain as the chicken will
eat the grain. You can’t leave your cat alone
with the chicken as cat will eat the chicken.
However, you know that cat does not eat grain.
How do you get everything across the river
intact?
4. Problem
• Initial Situation: You, the Chicken, the Bag of Grain and the
Cat are on one bank of a river with access to a boat.
• Resources: The boat and your knowledge and problem-solving
skills.
• Constraints: You can take only one passenger, you must not
leave Cat with the Chicken, you must not leave the Chicken
with the Grain.
• Goal: You, the Chicken, the Bag of Grain and the Cat on the
opposite bank of river.
• Ownership: You will be involved in planning the solution and
carrying it out
5. Solution
• Take the chicken across the river and leave it on the
other side. Return to where you have left Cat and the
Grain.
• Take Cat across the river and leave him on the other
side. Take back the chicken.
• Leave the chicken where you started. Take the bag of
grain across the river and leave it with the Rover.
• Go back and fetch the chicken and take it across the
river with you.
6. Problem Solving
• Problem solving is a process of identifying a
problem and finding the best solution for it.
• Problem solving is a skill that can be
developed by following a well organized
approach.
• Different strategies, techniques and tools
are used to solve a problem.
7. Problem Solving (Cont.)
When faced with a problem:
1. We first clearly define the problem
2. Think of possible solutions
3. Select the one that we think is the best
under the prevailing circumstances
4. And then apply that solution
5. If the solution woks as desired, fine; else we
go back to step 2
8. Problem Solving (Cont.)
• Computers are used as a tool to solve a complex
problems by developing computer programs.
• Computer programs contain different instructions
for computer.
• A programmer writes instructions and computer
executes these instructions to solve a problem.
• Different problem solving techniques are as follows:
Algorithm
Flowchart
Program
9. Algorithm
• From programming point of view, an
algorithm is a step-by-step procedure to
resolve any problem.
• An algorithm is an effective method
expressed as a finite set of well-defined
instructions.
10. Three Requirements
1. Sequence is:
Precise
Consists of limited number of steps
2. Each step is:
Unambiguous
Executable
3. The sequence of steps terminates in the form of a
solution
11. Properties of Algorithm
• Following are some properties of an algorithm:
The given problem should be broken down into simple
and meaningful steps.
The steps should be numbered sequentially
The steps should be descriptive and written in simple
English.
12. Sample Algorithm
Write an algorithm to find area of a rectangle
Step 1: Start
Step 2: get l,b values
Step 3: Calculate A=l*b
Step 4: Display A
Step 5: Stop
13. Sample Algorithm - 2
Write an algorithm to add two numbers
1. Start
2. Read the value of num1
3. Read the value of num2
4. Add num1 and num2
5. Store the result in a variable sum
6. Display sum
7. End
14. Analysis of Algorithm
• Analysis in the context of algorithms is concerned with
predicting the resources that are required:
Computational Time
Memory
Bandwidth
Logic functions
• However, Time – generally measured in terms of number
of steps required to execute an algorithm is the resource of
most interest
• By analyzing several algorithms, the most efficient one
can be identified
15. Algorithm Techniques
Different kind of algorithm strategies are there
which people follow to write an algorithm:
• Greedy Algorithm
• Deterministic Algorithm
• Randomized Algorithm
• Brute Force Strategy
16. Traveling Salesman Problem
• A salesman needs to visit each of the n cities.
• Is there is a route that takes the salesman
through every city and back to starting city A at a
cost of $520?
17. Few Questions
• Is that the best possible sequence?
• How do you know?
• How do I determine the best sequence? (How
do I determine the sequence that I draw is the best
one, shortened one and optimist one.)
Solution: you have to list down all possible
combinations, compute all possible distances.
Have to find minimum distance
18. SYNTAX & SEMANTICS
• An algo. is correct if its:
Syntax is correct
Semantics are correct
• Semantics: The concept embedded in an
algorithm(the soul!)
• Syntax: The actual representation of an
algorithm(the body!)
19. Pseudo code
• Algorithms are written in a language that
is similar to simple English called pseudo
code.
• There is no standard to write pseudo code.
• It is used to specify program logic in an
English like manner that is independent of
any particular programming language.
20. Pseudo Code (Cont.)
• Pseudo code simplifies program development by
separating it into two main parts:
Logic Design
Coding
• Logic Design: In this part, the logic of the program
is designed. We specify different steps required to
solve the problem and the sequence of these steps.
• Coding: In this part, the algorithm is converted into
a program. The steps of algorithm are translated into
instructions of any programing language.
22. Sample Pseudocode
Write pseudocode to add two numbers
BEGIN
NUMBER num1, num2, sum
OUTPUT ("Input number1:")
INPUT num1
OUTPUT ("Input number2:")
INPUT num2
sum= num1 + num2
OUTPUT sum
23. Difference Between Algorithm and
Pseudocode
Parameters Algorithm Pseudocode
Meaning and Definition An algorithm is a systematic,
logical approach that provides a
step-by-step procedure for
computers to solve a specific
problem.
Pseudocode is a simplified
version of programming codes,
written in plain English
language and used to outline a
program before its
implementation.
Expression and Use Algorithms can be expressed
using flowcharts, natural
language, and other methods.
Pseudocode includes various
control structures such as
repeat-until, if-then-else, while,
for, and case.