0% found this document useful (0 votes)
4 views53 pages

1. Algorithms

Python algorithms

Uploaded by

PPTV
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
4 views53 pages

1. Algorithms

Python algorithms

Uploaded by

PPTV
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 53

GE8151 - PROBLEM SOLVING AND PYTHON PROGRAMMING

ALGORITHMIC PROBLEM SOLVING

Mr. R.VIMAL RAJA, M. Tech.


Assistant Professor,
Department of Computer Science & Engineering,
CKCET
PROBLEM SOLVING
Problem Solving Model
Better understanding of how the six step problem solving
model works.
1) 1. Define the problem
2) 2. Determine the root cause
3) 3. Develop Alternative Possible Solutions
4) 4. Select a solution
5) 5. Implement the Solution
6) 6. Evaluate the Outcome
PROBLEM SOLVING TECHNIQUES

Problem solving can be expressed in the form of


1. Algorithms
2. Flowcharts
3. Pseudo codes
4. Programs
ALGORITHMS
ALGORITHM

• An Algorithm is defined as a step by step


procedure for solving any problem.

• An algorithm is a set of instructions


designed to perform a specific task.
ALGORITHM

 Algorithm is an ordered sequence of


finite, well defined, unambiguous
instructions for completing a task.
 Algorithm is independent of any
programming language.
Guidelines for writing algorithm?

1. An algorithm should be clear, precise, and well-defined.


2. It should always begin with the word “Start” and
end with the word “Stop”.
3. Each step should be written in a separate line.
4. Steps should be numbered.
1. Write an algorithm to print “Good Morning”

Step 1 : Start
Step 2 : Print “Good Morning”
Step 3 : Stop
2. Algorithm for planting a Seed
Step 1 : Start
Step 2 : Take a Pot
Step 3 : Fill pot with Soil
Step 4 : Poke hole in Soil
Step 5 : Put Seed in hole
Step 6 : Cover Seed with Soil
Step 7 : Water the Pot
Step 8 : Put pot in Sun-light
Step 9 : Stop
Algorithm for planting a Seed
3. Design an algorithm to add two numbers and display
the result

Step 1 − START
Step 2 − Declare three integers a, b & sum
Step 3 − Define values of a & b
Step 4 − Add values of a & b
Step 5 − Store output of step 4 to sum
Step 6 − Print sum
Step 7 − STOP
4. Design an algorithm find the largest number among
three numbers

Input: A = 2, B = 8, C = 1
Output: Largest number = 8
Input: A = 231, B = 4751, C = 75821
Output: Largest number = 75821
Design an algorithm find the largest number among three
numbers
Step 1. Start
Step 2. Read the three numbers to be compared, as A, B and C.
Step 3. Check if A is greater than B.
3.1 If true, then check if A is greater than C.
3.1.1 If true, print 'A' as the greatest number.
3.1.2 If false, print 'C' as the greatest number.
3.2 If false, then check if B is greater than C.
3.1.1 If true, print 'B' as the greatest number.
3.1.2 If false, print 'C' as the greatest number.
Step 4. End
Characteristics of an Algorithm

1. Should be written in Simple English.


2. Algorithm has a finite number of inputs.
3. Each and every instruction should be precise and unambiguous.
4. Instructions in an algorithm should not be repeated infinitely.
5. Algorithm should conclude after a finite number of steps.
6. Should have an end point.
7. The desired output should be obtained only after the algorithm
terminates.
Properties of an algorithm
1. Finite – An algorithm must be terminated after a finite number of
steps.
2. Definite – Each step of an algorithm must be precisely (clearly)
defined .
3. Input – The data must be present before any operations can be
performed on it. The initial data is supplied by a READ instruction.
A variable can be initialized using SET instruction.
Example : READ A, B
SET N = 0
Properties of an algorithm

4. Output - After executing all the steps of the algorithm at least one
output must be obtained. The WRITE statement is used to print
messages.
5. Effective - The operations to be performed in the algorithm can be
carried out manually in finite intervals of time.
Qualities of a good algorithm

1. Time - Lesser the time taken better the quality.


2. Memory - Require minimum computer memory.
3. Accuracy - Should provide accurate result.
4. Sequence - Procedure of an algorithm in a sequential form.
5. Generality - fit to all type of inputs.
Advantages of an algorithm

1. It is simple to understand.
2. It is very easy to debug.
3. It is independent of programming language.
4. It can be easily coded into High Level Language.
REAL-TIME EXAMPLES
How it helps?
To understand the usage of algorithm, look over the following
conversation.
How it helps?

Lets discuss about the conversation, tom wants brush his teeth,
so he asks chitti to bring brush, what happens chitti returns
cleaning brush.
Why this was happened ?
 Because the statement given by tom was not well defined and it
is ambiguous statement so chitti get confused and bring some
brush to Tom.
 This is what happen if the user gives ambiguity statement to the
computer.
 Therefore an algorithm should be simple and well defined.
How an algorithm should be?
 It should be in simple English, what a programmer wants to say.
 It has a start, a middle and an end.

Start
1. In the middle it should have set of tasks that computer wants to do
and it should be in simple English and clear.
2. To avoid ambiguous should give no for each step.
Stop
REAL TIME EXAMPLE FOR ALGORITHM (ATM Machine)

Step 1: Insert Card Step 2: Select your language


REAL TIME EXAMPLE FOR ALGORITHM (ATM Machine)

Step 3 : Enter your PIN Step 4 : Transaction Type


REAL TIME EXAMPLE FOR ALGORITHM (ATM Machine)

Step 5 : Account Type Step 6 : Enter Amount


REAL TIME EXAMPLE FOR ALGORITHM (ATM Machine)

Step 7 : Take Your Money Step 8: Another Transactions


ATM

Algorithm
1. ATM reads your information on the
magnetic strip of your debit card.
2. Sends information to your bank’s
server.
3. Confirmation sent back to ATM.
4. After you key in your PIN the
information again sent to bank
server.
5. Confirmation sent back to ATM.
6. ATM dispense cash.
TO FIND A MINIMUM IN A LIST
Problem Description:
• Minimum in a list of elements can be achieved in different ways.
• One way is to sort the list of element in ascending order and get the
first element as minimum.
• Another method is to compare each element with other.
• Assume the first element as minimum element and start comparing
with the next element.
• If the next element is smaller than assume the second the minimum
and keep repeating the procedure till the last element.
ILLUSTRATIVE PROBLEMS
Find a Minimum in a List
Algorithm:
• Step 1: Start
• Step 2: Read n
• Step 3:Initialize i=0
• Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5
• Step4.1: Read a[i]
• Step 4.2: i=i+1 goto step 4
• Step 5: Compute min=a[0]
• Step 6: Initialize i=1
• Step 7: If i<n, then go to step 8 else goto step 9
• Step 8: If a[i]<min, then goto step 8.1,8.2 else goto 8.2
• Step 8.1: min=a[i]
• Step 8.2: i=i+1 goto 7
• Step 9: Print min
• Step 10: Stop
Pseudocode:
• BEGIN READ n
• FOR i=0 to n, then
• READ a[i]
• INCREMENT i
• END FOR
• COMPUTE min=a[0]
• FOR i=1 to n, then
• IF a[i]<min, then
• CALCULATE min=a[i]
• INCREMENT i
• ELSE
• INCREMENT i
• END IF-ELSE
• END FOR
• PRINT min
• END
INSERTING A CARD IN A LIST OF
SORTED CARDS
Problem Statement:
• Imagine that you are playing a card game. You are holding the cards in
your hand, and these cards are sorted.
• You are given exactly one new card.
• You have to put it into the correct place so that the cards you are
holding are still sorted.
Insert a card in a list of sorted cards
Algorithm:
• Step 1: Start
• Step 2: Read n
• Step 3:Initialize i=0
• Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5
• Step4.1: Read a[i]
• Step 4.2: i=i+1 goto step 4
• Step 5: Read item
• Step 6: Calculate i=n-1
• Step 7: If i>=0 and item<a[i], then go to step 7.1, 7.2 else goto step 8
• Step 7.1: a[i+1]=a[i]
• Step 7.2: i=i-1 goto step 7
• Step 8: Compute a[i+1]=item
• Step 9: Compute n=n+1
• Step 10: If i<n, then goto step 10.1, 10.2 lse goto st 11
• Step10.1: Print a[i]
• Step10.2: i=i+1 goto step 10
• Step 11: Stop
Pseudocode:
• BEGIN
• READ n
• FOR i=0 to n, then
• READ a[i]
• INCREMENT i
• END FOR
• READ item
• FOR i=n-1 to 0 and item<a[i], then
• CALCULATE a[i+1]=a[i]
• DECREMENT i
• END FOR
• COMPUTE a[i+1]=item
• COMPUTE n=n+1
• FOR i=0 to n, then
• PRINT a[i]
• INCREMENT i
• END FOR
• END
Guessing an integer number in
a range.
PROBLEM STATEMENT:
• Imagine you are trying to guess a number in a range. The objective is
to randomly generate integer number from 0 to n. Then the player
has to guess the number. If the player guesses the correctly, output an
appropriate message.
ALGORITHM:
Step 1: Start
Step 2: Generate a random number and call it num.
Step 3: Enter the number to guess.
if (guess is equal to num)
Print “You guessed the correct number”
Otherwise
if(guess is less than num)
print “ Your guess is lower than the number. Guess again!”
otherwise
print “Your guess is higher than the number. Guess again!”
Step 4: End
PSEUDOCODE:
START
READ Guess_num,input_num
IF guess_num>input_num THEN
WRITE “Your guess is higher than the input_number!”
ELSE
IF guess_num<input_num THEN
WRITE “Your guess is lower than the input_number!”
ELSE
WRITE “You guessed the correct number!”
START

READ num

Read Guess number

<num >num
If Guess ?

Print “Your guess is higher than the


number guess again”
Print “Your guess is lower than the Print “You guessed the correct number”
number guess again”

STOP
Explain with relevant diagrams and algorithm the Towers of Hanoi
problem.
Tower of Hanoi:
Problem:
The smaller one sits over the larger one .
The objective is to move the entire disk to some another tower without
violating the sequence of arrangement.
Rules to be followed:
1 Only one disk can be moved among the towers at any given time.
2. Only the “Top” disk can be removed.
3. No large disk can sit over a small disk.
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Calculate move = pow(2,n)-1
Step 4: Function call T(n,Beg,Aux,End) recursively until n=0
Step 4.1: If n=0, then goto Step 5 else Goto Step 4.2
Step4.2: T(n,Beg,End,Aux) T(1,Beg,Aux,End) , Move disk from
source to destination T(n-1,Aux,Beg,End)
Step 5: Stop
FLOWCHART:
PSEUDCODE:
BEGIN
READ n
CALCULATE move=pow(2,n)-1
FUNCTION T(n,Beg,Aux,End) Recursively until n=0
PROCEDURE
IF n=0 then,
No disk to move
ELSE
T(n,Beg,End,Aux)
T(1,Beg,Aux,End), move disk from source to destination
T(n-1,Aux,Beg,En )
END PROCEDURE
END

You might also like