Unit I Algorithmic Problem Solving
Unit I Algorithmic Problem Solving
The term ‗algorithm‘ was derived from the name of Mohammed al-Khowarizmi, a
Persian mathematician in the ninth century. Al-Khowarizmi → Algorismus (in Latin) →
Algorithm.
People of different professions have their own form of procedure in their line of work,
and they call it different names. For instance, a cook follows a procedure commonly known
as a recipe that converts the ingredients (input) into some culinary dish (output), after a
certain number of steps.
The state of an algorithm is defined as its condition regarding stored data. The stored
data in an algorithm are stored as variables or constants. The state shows its current values or
contents.
In computer science, control flow (or flow of control) is the order in which
individual statements, instructions or function calls of an algorithm are executed or evaluated.
For complex problems our goal is to divide the task into smaller and simpler functions
during algorithm design. So a set of related sequence of steps, part of larger algorithm is
known as functions.
Sequence: A sequence is a series of steps that occur one after the other, in the same order
every time. Consider a car starting off from a set of lights. Imagine the road in front of the car
is clear for many miles and the driver has no need to slow down or stop. The car will start in
first gear, then move to second, third, fourth and finally fifth. A good driver (who doesn‘t
have to slow down or stop) will move through the gears in this sequence, without skipping
gears.
Selection: A selection is a decision that has to be made. Consider a car approaching a set of
lights. If the lights turn from green to yellow, the driver will have to decide whether to stop or
continue through the intersection. If the driver stops, she will have to wait until the lights are
green again.
Iteration: Iteration is sometimes called repetition, which means a set of steps which are
repeated over and over until some event occurs. Consider the driver at the red light, waiting
for it to turn green. She will check the lights and wait, check and wait. This sequence will be
repeated until the lights turn green.
Let as look at two simple algorithms to find the greatest among three numbers,
as follows:
Algorithm 1.1:
Step 1: Start.
Step 2: Read the three numbers A,B,C.
Step 3: Compare A and B. If A is greater perform step 4 else perform step 5.
Step 4: Compare A and C. If A is greater, output ―A is greater‖ else output ―C is greater‖.
Step 5: Compare B and C. If B is greater, output ―B is greatest‖ else output ―C is greatest‖.
Step 6: Stop.
Algorithm 1.2:
Step 1: Start.
Step 2: Read the three numbers A,B,C.
Step 3: Compare A and B. If A is greater, store Ain MAX, else store B in MAX.
Step 4: Compare MAX and C. If MAX is greater, output ―MAX is greater‖ else output ―C is
greater‖.
Step 5: Stop.
Both the algorithms accomplish same goal, but in different ways. The
programmer selects the algorithm based on the advantages and disadvantages of each
algorithm. For example, the first algorithm has more number of comparisons, whereas in
second algorithm an additional variable MAX is required.
In the Algorithm 1.1, Algorithm 1.2, step 1 and 2 are in sequence logic and step 3, 4,
and 5 are in selection logic. In order to illustrate iteration logic, consider one more example.
Design an algorithm for adding the test scores given as: 26, 49, 98, 87, 62, 75
Algorithm 1.3:
Step 1: Start
Step 2: Sum = 0
Step 3: Get a value
Step 4: sum = sum + value
Step 5: If next value is present, go to step 3. Otherwise, go to step 6
Step 6: Output the sum
Step 7: Stop
In the Algorithm 1.3 step 5 have a go to statement with a back ward step
reference, so it means that iteration logic.
1.3.2 Pseudocode
Pseudocode ("sort of code") is another way of describing algorithms. It is
called "pseudo" code because of its strong resemblance to "real" program code. Pseudocode
is essentially English with some defined rules of structure and some keywords that make
it appear a bit like program code.
Note: These are not "strict" guidelines. Any words used that have similar form and function
may be used in an emergency - however, it would be best to stick to the pseudocode words
used here.
1.3.3 Flowcharts
Flowcharts are a diagrammatic method of representing algorithms. They use
an intuitive scheme of showing operations in boxes connected by lines and arrows that
graphically show the flow of control in an algorithm.
Table 1.1lists the flowchart symbol drawing, the name of the flowchart
symbol in Microsoft Office (with aliases in parentheses), and a short description of where and
how the flowchart symbol is used.
NAME
SYMBOL DESCRIPTION
(ALIAS)
Flow Line
Flow line connectors show the direction that the process
(Arrow,
flows.
Connector
)
Terminator
(Terminal Terminators show the start and stop points in a process.
Point, Oval)
The Data flowchart shape indicates inputs to and outputs
Data
from a process. As such, the shape is more often referred
(I/O
to as an I/O shape than a Data shape.
)
Pretty self-explanatory - the Document flowchart symbol
Document
is for a process step that produces a document.
Show a Process or action step. This is the most common
Process
symbol in flowcharts.
Indicates a question or branch in the process flow.
Decision Typically, a Decision flowchart shape is used when there
are 2 options (Yes/No, No/No-Go, etc.)
This symbol is typically small and is used as a Connector
to show a jump from one point in the process flow to
another. Connectors are usually labeled with capital letters
Connector (A, B, AA) to show matching jump points. They are handy
(Inspection for avoiding flow lines that cross other shapes and flow
) lines. They are also handy for jumping to and from a
sub-
processes defined in a separate area than the main
flowchart.
A Predefined Process symbol is a marker for another
Predefined
process step or series of process flow steps that are
Process
formally defined elsewhere. This shape commonly depicts
(Subroutine)
sub-processes (or subroutines in programming flowcharts).
As the names states, any process step that is a Preparation
Preparation
process flow step, such as a set-up operation.
Magnetic
The most universally recognizable symbol for a data
Disk
storage location, this flowchart shape depicts a database.
(Database)
Each control structures can be built from the basic elements as shown below.
Sequence: A sequence is a series of steps that take place one after another. Each step is
represented here by a new line.
Pseudocode Flowchart
BEGIN
Statement
Statement
END
Pseudocode Flowchart
BEGIN
1st Gear
2nd Gear
3rd Gear
4th Gear
5th Gear
END
Selection : A Selection is a decision. Some decisions may be answered as Yes or No. These
are called binary selections. Other decisions have more than two answers. These are
called multiway selections.
Pseudocode Flowchart
IF (question) THEN
statement
ELSE
statement
ENDIF
Pseudocode Flowchart
IF (lights are green) THEN
Go
ELSE
Stop
ENDIF
Pseudocode Flowchart
CASEWHERE (question)
Alternative 1: Statement
Alternative 2 : Statement
OTHERWISE : Statement
ENDCASE
An example of someone at a set of traffic lights follows :
Pseudocode Flowchart
Green : Go
Amber : Slowdown
Red : Stop
ENDCASE
Repetition: A sequence of steps which are repeated a number of times, is called repetition.
For a repeating process to end, a decision must be made. The decision is usually called a test.
The position of this test divides repetition structures into two types : Pre-test and Post-test
repetitions.
Pre-test repetitions (sometimes called guarded loops) will perform the test before any part
of the loop occurs.
Post-test repetitions (sometimes called un-guarded loops) will perform the test after the
main part of the loop (the body) has been performed at least once.
Pseudocode Flowchart
WHILE (question)
Statement
ENDWHILE
A traffic lights example follows :
Pseudocode Flowchart
wait
ENDWHILE
Pseudocode Flowchart
REPEAT
Statement
UNTIL (Question)
Pseudocode Flowchart
REPEAT
Wait
Pseudocode Flowchart
subprogram
Consider the total concept of driving a car. This activity is made up of a number of sub-
processes which each contribute to the driver arriving at a destination. Therefore, driving may
involve the processes of "Starting the car"; "Engaging the gears"; "Speeding up and slowing
down"; "Steering"; and "Stopping the car".
Pseudocode Flowchart
BEGIN
Start Car
Engage Gears
Navigate Car
Stop Car
END
Note that the sub-program "Navigate Car" could be further developed into:
Pseudocode Flowchart
BEGINSUBPROGRAM Navigate
Steer Car
ENDSUBPROGRAM
In the example above, each of the sub-programs would have whole algorithms of their own,
incorporating some, or all, of the structures discussed earlier.
1.4 RECURSION
1.4.1 Example
So now we have another way of thinking about how to compute the value of n!, for all
nonnegative integers n:
When we're computing n! in this way, we call the first case, where we immediately know the
answer, the base case, and we call the second case, where we have to compute the same
function but on a different value, the recursive case.
Base case is the case for which the solution can be stated non‐recursively(ie. the answer is
known). Recursive case is the case for which thesolution is expressed in terms of a smaller
version ofitself.
1.4.2 Recursion in Step Form
Recursion may be described as follows:
Syntax for recursive algorithm
Step 1: Start
Step 2: Statement(s)
Step 3: Call Subprogram(argument)
Step 4: Statement(s)
Step 5: Stop
Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n) and store the result in f
Step 4: Print factorial f
Step 5: Stop
Begin
Subprogram subprogram(argument)
call(argument)
yes
Read N Is
N==1
Fact=Factorial(N)
No
Stop
End Factorial
END
The problem given should be understood completely. Check if it is similar to some standard
problems & if a Known algorithm exists. Otherwise a new algorithm has to be developed.
Ascertain the capabilities of the computational device:
Once algorithm is developed, it is necessary to show that it computes answer for all the
possible legal inputs. The solution is stated in two forms, exact solution or approximate
solution. Examples of problems where an exact solution cannot be obtained are i) Finding a
square root of number. ii) Solutions of nonlinear equations.
Some algorithms do not demand any ingenuity in representing their inputs. Some others are
in fact are predicted on ingenious data structures. A data type is a well-defined collection of
data with a well-defined set of operations on it. A data structure is an actual implementation
of a particular abstract data type. The Elementary Data Structures are Arrays: These let you
access lots of data fast. (good) .You can have arrays of any other data type. (good) .However,
you cannot make arrays bigger if your program decides it needs more space. (bad). Records:
These let you organize non-homogeneous data into logical packages to keep everything
together. (good) .These packages do not include operations, just data fields (bad, which is
why we need objects) .Records do not help you process distinct items in loops (bad, which is
why arrays of records are used) Sets: These let you represent subsets of a set with such
operations as intersection, union, and equivalence. (good) .Built-in sets are limited to a
certain small size. (bad, but we can build our own set data type out of arrays to solve this
problem if necessary)
Creating an algorithm is an art which may never be fully automated. By mastering these
design strategies, it will become easier for you to develop new and useful algorithms.
Dynamic programming is one such technique. Some of the techniques are especially useful in
fields other than computer science such as operation research and electrical engineering.
Some important design techniques are linear, nonlinear and integer programming
There are mainly two options for specifying an algorithm: use of natural language or
pseudocode& Flowcharts. A Pseudo code is a mixture of natural language & programming
language like constructs. A flowchart is a method of expressing an algorithm by a collection
of connected geometric shapes.
Once algorithm is developed, it is necessary to show that it computes answer for all the
possible legal inputs .We refer to this process as algorithm validation. The process of
validation is to assure us that this algorithm will work correctly independent of issues
concerning programming language it will be written in.
Analysing algorithms:
A user has a list of numbers and wishes to find the minimum value in the list.
An Algorithm is required which will allow the user to enter the numbers, and which will
calculate the minimum value that are input. The user is quite happy to enter a count of the
numbers in the list before entering the numbers.
Finding the minimum value in a list of items isn‘t difficult. Take the first
element and compare its value against the values of all other elements. Once we find a
smaller element we continue the comparisons with its value. Finally we find the minimum.
Step 1: Start
Step 2: Declare variables N, E, i and MIN.
Step 3: READ total number of element in the List as N
Step 4: READ first element as E
Step 5: MIN =E
Step 6: SET i=2
Step 7: IF i>n go to Step 12 ELSE go to step 8
Step 8: READ ith element as E
Step 9: IF E < MIN THEN SET MIN = E
Step 10: i=i+1
Step 11: go to step 7
Step 12: Print MIN
Step 13: Stop
We start from the high end of the array and check to see if that's where we want to insert the
data. If so, fine. If not, we move the preceding element up one and then check to see if we
want to insert x in the ―hole‖ left behind. We repeat this step as necessary.
Thus the search for the place to insert x and the shifting of the higher elements of the array
are accomplished together.
Step 1: Start
Step 2: Declare variables N, List[], i, and X.
Step 3: READ Number of element in sorted list as N
Step 4: SET i=0
Step 5: IF i<N THEN go to step 6 ELSE go to step 9
Step 6: READ Sorted list element as List[i]
Step 7: i=i+1
Step 8: go to step 5
Step 9: READ Element to be insert as X
Step 10: SET i = N-1
Step 11: IF i>=0 AND X<List[i] THEN go to step 12 ELSE go to step15
Step 12: List[i+1]=List[i]
Step 13: i=i-1
Step 14: go to step 11
Step 15: List[i+1]=X
Maybe you guessed 1, then 2, then 3, then 4, and so on, until you guessed the
right number. We call this approach linear search, because you guess all the numbers as if
they were lined up in a row. It would work. But what is the highest number of guesses you
could need? If the computer selects N, you would need N guesses. Then again, you could be
really lucky, which would be when the computer selects 1 and you get the number on your
first guess. How about on average? If the computer is equally likely to select any number
from 1 to N, then on average you'll need N/2 guesses.
But you could do something more efficient than just guessing 1, 2, 3, 4, …,
right? Since the computer tells you whether a guess is too low, too high, or correct, you can
start off by guessing N/2. If the number that the computer selected is less than N/2, then
because you know that N/2 is too high, you can eliminate all the numbers from N/2 to N from
further consideration. If the number selected by the computer is greater than N/2, then you
can eliminate 1 through N/2. Either way, you can eliminate about half the numbers. On your
next guess, eliminate half of the remaining numbers. Keep going, always eliminating half of
the remaining numbers. We call this halving approach binary search, and no matter which
number from 1 to N the computer has selected, you should be able to find the number in at
most log2N+1 guesses with this technique. The following table shows the maximum number
of guesses for linear search and binary search for a few number sizes:
Highest Number Max Linear Search Guesses Max Binary Search Guesses
10 10 4
100 100 7
1,000 1,000 10
10,000 10,000 14
100,000 100,000 17
1,000,000 1,000,000 20
Step 1: Start
Step 2: SET Count =0
Step 3: READ Range as N
Step 4: SELECT an RANDOMNUMBER from 1 to N as R
Step 6: READ User Guessed Number as G
Step 7: Count = Count +1
Step 8: IF R==G THEN go to step 11 ELSE go to step 9
Step 9: IF R< G THEN PRINT ―Guess is Too High‖ AND go to step 6 ELSE go to step10
Step 10: IF R>G THEN PRINT ―Guess is Too Low‖ AND go to step 6
Step 11: PRINT Count as Number of Guesses Took
SET Count =0
READ Range as N
SELECT an RANDOM NUMBER from 1 to N as R
WHILE TRUE
READ User guessed Number as G
Count =Count +1
IF R== G THEN
BREAK
ELSEIF R<G THEN
DISPLAY ―Guess is Too High‖
ELSEIF R>G THEN
DISPLAY ―Guess is Too Low‖
ENDIF
ENDWHILE
DISPLAY Count as Number of guesses Took
Rules
Only one disk can be moved among the towers at any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.
Figure 1.9Step by Step Moves in Solving Three Disk Tower of Hanoi Problem
Algorithm:
To write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem
with lesser amount of disks, say → 1 or 2. We mark three towers with name, source,
destination and aux (only to help moving the disks). If we have only one disk, then it can
easily be moved from source to destination tower.
If we have 2 disks −
So now, we are in a position to design an algorithm for Tower of Hanoi with more than two
disks. We divide the stack of disks in two parts. The largest disk (nth disk) is in one part and
all other (n-1) disks are in the second part.
Our ultimate aim is to move disk n from source to destination and then put all other (n-1)
disks onto it. We can imagine to apply the same in a recursive way for all given set of disks.
The steps to follow are −
Step 1 − Move n-1 disks from source to
aux Step 2 − Move nth disk from source to
dest Step 3 − Move n-1 disks from aux to
dest
A recursive Step based algorithm for Tower of Hanoi can be driven as follows −
Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps.