2. Problem Solving
Solving problems is the core of computer
science. Programmers must first understand
how a human solves a problem, then
understand how to translate this "algorithm"
into something a computer can do, and
finally how to "write" the specific syntax
(required by a computer) to get the job
done. It is sometimes the case that a
machine will solve a problem in a completely
different way than a human.
3. Problem Solving
Computer Programmers are problem solvers. In
order to solve a problem on a computer you must:
Know how to represent the information (data)
describing the problem.
Determine the steps to transform the
information from one representation into
another.
5. Algorithm
An algorithm is a clearly defined set of instructions
to be followed to solve a problem. Algorithms are
generally created independent of underlying
programming languages.
Informally, an algorithm is any well-defined
computational procedure that takes some value, or
set of values, as input and produces some value,
or set of values, as output. An algorithm is thus a
sequence of computational steps that transform
the input into the output. 5
6. Characteristics of an
Algorithm
Not all procedures can be called an algorithm. An algorithm
should have the following characteristics:
– Unambiguous − Algorithm should be clear and
unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only
one meaning.
– Input − An algorithm should have 0 or more well-
defined inputs.
– Output − An algorithm should have 1 or more well-
defined outputs, and should match the desired output.
6
7. Characteristics of an
Algorithm
– Finiteness − Algorithms must terminate
after a finite number of steps.
– Feasibility − Should be feasible with the
available resources.
– Independent − An algorithm should
have step-by-step directions, which should
be independent of any programming code.
8. Example
Problem − Design an algorithm to add two
numbers and display the result.
– Step 1 – START
– Step 2 – declare three integers a, b & c
– Step 3 – define values of a & b
– Step 4 – add values of a & b
– Step 5 – store output of Step 4 to c
– Step 6 – print c
– Step 7 – STOP
8
9. Example
Problem − Design an algorithm to add two
numbers and display the result.
– Step 1 – START ADD
– Step 2 – get values of a & b
– Step 3 – c a + b
– Step 4 – display c
– Step 5 – STOP
Second method is preferable in Data
Structure and Algorithms
9
10. Algorithm Analysis
Efficiency of an algorithm can be analyzed at two different
stages, before implementation and after implementation.
They are the following −
– A Priori Analysis − This is a theoretical analysis of an
algorithm. Efficiency of an algorithm is measured by
assuming that all other factors, for example, processor
speed, are constant and have no effect on the
implementation.
– A Posterior Analysis − This is an empirical analysis of
an algorithm. The selected algorithm is implemented
using programming language. This is then executed on
target computer machine. In this analysis, actual
statistics like running time and space required, are
collected 10
11. Pseudo Code and Algorithm
An algorithm is a systematic logical approach
used to solve problems in a computer.
Pseudocode is the statement in plain English
which may be translated later into a programming
language (program).
Pseudo code is an intermediary between an
algorithm and implemented program
12. Example
Pseudo code to add two numbers:
Take two number inputs
Add numbers using the + operator
Display the result
13. Pseudo Code Example
If Ali’s height is greater then 6 feet
Then
Ali can become a member of the Basket Ball
team
14. Conversion from Pseudo code to
Source code
Pseudo Code
If amir’s age is greater than Ammara’s age
then show message that amir is elder than
amara, otherwise show message that amir
is younger than ammara.
15. Conversion from Pseudo code
to Source code
Source Code
if (AmirAge > AmaraAge)
{
cout<< “Amir is older than Amara” ;
}
else
{
cout<<“Amir is younger than or of the same
age as Amara” ;
}
16. Algorithm : Find the largest number among three numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
17. Algorithm: Find the factorial of a
number
Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables
factorial ← 1
i ← 1
Step 4: Read value of n
Step 5: Repeat the steps until i = n
5.1: factorial ← factorial*i
5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop
18. Sorting Problem
we might need to sort a sequence of numbers into non
decreasing order. This problem arises frequently in practice
and provides fertile ground for introducing many standard
design techniques and analysis tools. Here is how we
formally define the sorting problem:
Input: A sequence of n numbers (a1, a2,……an).
19. Sorting Problem
For example, given the input sequence (31; 41; 59; 26; 41; 58) a
sorting algorithm returns as output the sequence (26; 31; 41; 41; 58;
59). Such an input sequence is called an instance of the sorting
problem. In general, an instance of a problem consists of the input
(satisfying whatever constraints are imposed in the problem
statement) needed to compute a solution to the problem.
Because many programs use it as an intermediate step, sorting is a
fundamental operation in computer science. As a result, we have a
large number of good sorting algorithms at our disposal. Which
algorithm is best for a given application depends on—among other
factors—the number of items to be sorted, the extent to which the
items are already somewhat sorted, possible restrictions on the item
values, the architecture of the computer, and the kind of storage
devices to be used: main memory, disks, or even tapes.
20. What kinds of problems are solved by
algorithms?
The Human Genome Project has made great progress toward the goals
of identifying all the 100,000 genes in human DNA, determining the
sequences of the 3 billion chemical base pairs that make up human
DNA, storing this information in databases, and developing tools for
data analysis. Each of these steps requires sophisticated algorithms.
The Internet enables people all around the world to quickly access and
retrieve large amounts of information. With the aid of clever algorithms,
sites on the internet are able to manage and manipulate this large
volume of data. Examples of problems that make essential use of
algorithms include finding good routes.
Electronic commerce enables goods and services to be negotiated and
exchanged electronically, and it depends on the privacy of personal
information such as credit card numbers, passwords, and bank
statements. The core technologies used in electronic commerce include
public-key cryptography and digital signatures which are based on
numerical algorithms and number theory.
21. What kinds of problems are solved by
algorithms?
Manufacturing and other commercial enterprises often need to
allocate scarce resources in the most beneficial way.
An oil company may wish to know where to place its wells in
order to maximize its expected profit.
A political candidate may want to determine where to spend
money buying campaign advertising in order to maximize the
chances of winning an election.
An airline may wish to assign crews to flights in the least
expensive way possible, making sure that each flight is covered
and that government regulations regarding crew scheduling are
met.
An Internet service provider may wish to determine where to
place additional resources in order to serve its customers more
effectively. All of these are examples of problems that can be
solved using linear programming algos.