Вы находитесь на странице: 1из 24

1

UNIT I 

ALGORITHMIC PROBLEM SOLVING 

Algorithms,  building  blocks  of  algorithms  (instructions/statements,  state,  control  flow, 


functions),  notation  (pseudo  code,  flow  chart,  programming  language),  algorithmic  problem 
solving,  simple  strategies  for  developing  algorithms  (iteration,  recursion).  Illustrative  problems: 
find  minimum  in  a  list,  insert  a  card  in  a  list  of sorted cards, guess an integer number in a range, 
Towers of Hanoi.  

Algorithms 
 
● Algorithm  is  an  ordered  sequence  of  finite,  well  defined,  unambiguous  instructions  for 
completing a task. 
● Step – by – step procedure to write a program. 
 
Characteristics 
● Precise and unambiguous. 
● Should not be repeated. 
● Should be written in sequence. 
● It looks like normal English 
 
Algorithm to find greatest among three numbers 
 
Step 1 :​ Start 
Step 2​ : Read the three numbers A, B, C 
Step 3​ : Compare A & B, If A is greater, perform step 4 else perform step 5. 
Step  4  :  Compare  A  &  C,  If  A  is  greater,  output  “A  is  greatest”  else  output  “C  is  greatest”. 
Perform step 6 
Step 5​ : Compare B & C, If B is greater, output “B is greatest” else output “C is greatest”. 
Step 6​ : Stop 

Building Blocks of Algorithms  

An algorithm is a sequence of simple steps that can be followed to solve a problem. 


These steps must be organized in a logical and clear manner.  

Algorithms can be designed using the following building blocks 

● Instructions/Statements 
● State 
● Control Flow 
● Functions 

GE8151-Problem Solving and Python Programming UNIT I


2

Instructions/Statements 

An algorithm is a part of the blueprint or plan for the computer program. It is also an 
effective procedure for solving a problem in a finite number of steps. 

Most algorithms have these basic parts: 

● Description of problem 
● Set up 
● Parameters 
● Execution 
● Conclusion 

The simple example below is to find the sum of the two numbers 

1. Description of problem:​ To find sum of the two numbers 


2. Set up​: Two numbers required for addition and one variable for storing the results 
3. Parameters: 
a. Read 1​st​ number 
b. Read 2​nd​ number 
4. Execution:​ Calculate the sum of two numbers 
Result=a+b 
5. Conclusion:​ The desired result is sum 

State 

A  computer  program  stores  data  in variables,  which  represent  storage  locations  in  the 
computer's memory.  The  content of these memory locations, at any given point in the program’s 
execution, is called the program's ​state. 

Sequential control 

Sequential control means that the steps of an algorithm are carried out in a sequential 
manner, where each step is executed exactly once. 

GE8151-Problem Solving and Python Programming UNIT I


3

Algorithm 
Step 1: ​Start 
Step 2: ​Input the value of A and B. 
Step 3: ​Find the sum of A and B. 
SUM=A+B 
Step 4: ​Print the value of SUM. 
Step 5: ​Stop 

Condition Control 

In  Condition  Control  only  one  flow  of  number  of  alternative steps is executed based on 
the condition. 

GE8151-Problem Solving and Python Programming UNIT I


4

True

False

Start

Stop

Algorithm 
Step 1: ​Start 
Step 2: ​Read the value of n 
Step 3: ​if (n%2==0) then  
print it is an even number  
else  
  print it is an odd number 
Step 4: ​Stop 
 
Control flow 

GE8151-Problem Solving and Python Programming UNIT I


5

In  control  flow  one  or  more  steps  are  performed  repeatedly.  This  logic  is  used  for 
producing  loops  in  program  logic,  when one or more instructions may be executed several times 
depending on some conditions. 

False

Start

Stop

True

Algorithm 
Step 1: ​Start 
Step 2: ​Read the values of n 
Step 3: ​set initial values to Fact =i=1 
Step 4: ​Is i<=n else go to step 6 
Fact=Fact*i 
i=i+1 

GE8151-Problem Solving and Python Programming UNIT I


6

Step 5: ​go to step 4. 


Step 6: ​Print Fact. 
Step 7: ​Stop 
 
Functions 

For  complex  problems our goal is to divide the task into smaller and simpler tasks during 


algorithm  design.  A  function  is  a  block  of  organized,  reusable  code  that  is  used  to  perform  a 
single,  related  action.  Functions  provide better modularity for your application and a high degree 
of code reusing. 

Algorithm 
Step 1: Start 
Step 2: Read n1, n2, n3 
Step 3: Call the function Average and pass the parameters 
Step 4: Print the value of average 
Step 5: Stop 
Function Average 
Step 1: Calculate sum=a+b+c 
Step 2: Calculate result=sum/3 
Step 3: Return the value of result to main function 
 
Notation 
Pseudocode 
✓ Pseudocode is written in normal English and cannot be understood by the computer. 
✓ Only  concentrate  on  logic  of  the  program  without  worrying  the  syntax  of  the 
instructions. 
 
Rules for writing Pseudocode 

GE8151-Problem Solving and Python Programming UNIT I


7

✓ Write one statement per line. 


✓ Capitalize initial keyword. 
✓ Indent to show hierarchy. 
✓ End multiline structure. 
✓ Keep statement language independent. 
 
Example: Pseudocode to find maximum of any three numbers 
READ values of A, B, C  
IF A is greater than B THEN 
ASSIGN A to MAX 
ELSE 
ASSIGN B to MAX 
END IF 
IF MAX is greater than C THEN 
PRINT MAX is greatest 
ELSE 
PRINT C is greatest 
END IF 
STOP 
 
Advantages 
✓ Easily modified. 
✓ Written easily. 
✓ Read and understood easily. 
✓ Converting Pseudocode to programming language is very easy. 
 
Disadvantages 
✓ It is not visual. 
✓ We do not get picture of design. 
✓ No standardize style or format. 
 

Flowchart 
● A flowchart is a diagrammatic representation of the logic for solving a task. 
 
Characteristics 
● Program preparation can be simplified. 
● Easier to understand. 
● Easy to analyze and compare. 
● Assist in reviewing and debugging 
● Provide effective programming documentation. 
 

GE8151-Problem Solving and Python Programming UNIT I


8

Symbols 
Symbol Name  Symbol  Use 

Flow lines    Flow of control of instruction 

 
Stop
Terminal  Start symbol and End symbol 
Start

Input/Output    To read and write 

Calculations and initialization of 


Process   
memory location 

Represents a decision to be made. 


Decision symbol    Branch to one of two or more 
alternative point. 

Connectors    Used to join different flow lines. 

 
Rules for drawing flowchart 
∗ The standard symbols should only be used 
∗ Arrow head represents the direction of flow of control. 
∗ Usual direction – top to bottom or left to right 
∗ The flowchart should be clear, neat and easy to follow. 
∗ The flowchart must have a logical start and finish. 
∗ Only one flow line should come out from a process symbol. 
 
 
 
 
 
 
∗ Only  one flow line should enter a decision symbol. However, two or three flow lines may 
leave the decision symbol. 
 
 

GE8151-Problem Solving and Python Programming UNIT I


9

 
 
 
 
∗ Only one flow line is used with a terminal symbol. 
 

 
 
 
 
∗ The flow lines should not cross each other. 
∗ Be consistent in using names and variables. 
∗ Keep the flowchart as simple as possible. 
 
Advantages 
✓ Understand logic clearly 
✓ Better communication 
✓ Effective analysis 
✓ Effective coding 
✓ Effective program maintenance 
 
Disadvantages 
✓ Complex logic 
✓ Alterations and Modifications. 
✓ Reproduction. 
✓ Cost. 
 

Programming Languages 

A ​programming  language​ is  a formal  language that specifies a set  of  instructions that 


can  be  used  to  produce  various  kinds  of output.  Programming  languages  generally  consist 
of instructions for  a computer.  Programming  languages  can  be  used  to  create programs that 
implement specific algorithms. 

GE8151-Problem Solving and Python Programming UNIT I


10

  
List  of  some  of  the  different  fields  of  programming,  as  well as the languages to consider 
for each is explained below.  
Applications and Program development 
Application  and program development involves programs you work with on a daily basis. 
For  example,  the  Internet  browser  you  are  using  to  view this web page is considered a program. 
If you are interested in developing a program, you should consider the following languages. 
● C 
● C# 
● C++ 
● D 
● Java 
● Tcl 
● Visual Basic 

Artificial Intelligence development 


Artificial  Intelligence or  related  fields  involve  creating  the character interactions in computer 
games,  portions  of  programs  that  make  decisions, chatbots,  and  more.  If  you're  interested  in 
developing an AI, you should consider the following languages. 
● AIML 
● C 
● C# 
● C++ 

GE8151-Problem Solving and Python Programming UNIT I


11

● Prolog 
● Python 

Database development 
Database  developers  create  and  maintain ​databases​.  If  you're  interested  in  creating  or 
maintaining a database, you should consider any of the following languages. 
● DBASE 
● FoxPro 
● MySQL 
● SQL 
● Visual FoxPro 

Game development 
Game  development  involves  the  development  of  computer  games  or  other 
entertainment  software.  If  you're  interested  in  developing  a  game,  you  should  consider  the 
following languages. 
● C 
● C# 
● C++ 
● DarkBASIC 
● Java 

Computer drivers or other hardware interface development 


Computer  drivers  and  programming  hardware  interface  support  are  a  necessity  for 
hardware  functionality.  If  you're  interested  in  developing  drivers  or  software  interfaces  for 
hardware devices, you should consider the following languages. 
● Assembly 
● C 

Internet and web page development 


Internet  and  web  page  development  are  the essence of the Internet. Without developers, 
the  Internet  would  not  exist.  If  you're  interested  in  creating  web  pages,  developing  Internet 
applications, or Internet related tasks, you should consider the following languages. 
● HDML 
● HTML 
● Java 
● JavaScript 
● Perl 
● PHP 
● Python 
● XML 

Script development 
Although  it  is  not  likely  to  become  a  career,  knowing  how  to  create  and  develop scripts 
can  increase  productivity  for  you  or  your  company,  saving  you  countless  hours.  If  you're 
interested in developing scripts, consider the following languages. 

GE8151-Problem Solving and Python Programming UNIT I


12

● Autohotkey 
● awk 
● bash 
● Batchfile 
● Perl 
● Python 
● Tcl 
 
Algorithmic Problem Solving 

Algorithmic problem solving is about the formulation and solution of problems where 
the solution involves, possibly implicitly, the principles and techniques that have been developed 
to assist in the construction of correct algorithms. 

The  Problem  Solving  process  consists  of  a  sequence  of  sections  that  fit  together 
depending on the type of problem to be solved. These are: 

● Problem Definition. 
● Problem Analysis. 
● Generating possible Solutions. 
● Analyzing the Solutions. 
● Selecting the best Solution(s). 
● Planning the next course of action (Next Steps) 

The  process  is  only  a  guide  for  problem solving. It is useful to have a structure to follow 


to  make  sure  that  nothing is overlooked. Nothing here is likely to be brand new to anyone, but it 
is  the  pure  acknowledgement  and  reminding  of  the  process  that  can  help  the  problems  to  be 
solved. 

1. Problem Definition 

The  normal  process  for  solving  a  problem  will  initially involve defining the problem you 


want  to  solve.  You  need  to  decide  what  you  want  achieve and write it down. Often people keep 
the  problem  in  their  head  as  a  vague  idea  and  can  so  often  get  lost  in  what  they  are  trying  to 
solve  that  no  solution  seems  to  fit.  Merely  writing  down  the  problem  forces  you  to  think about 
what  you  are  actually  trying  to  solve  and  how  much  you  want  to  achieve.  The  first  part  of  the 
process  not  only  involves  writing  down  the  problem  to  solve,  but  also  checking  that  you  are 
answering  the  right  problem.  It  is  a  check-step  to  ensure  that  you  do  not  answer  a  side issue or 
only  solve  the  part  of  the  problem  that  is  most  easy  to  solve.  People  often  use  the  most 
immediate  solution  to  the  first  problem definition that they find without spending time checking 
the problem is the right one to answer. 

2. Problem Analysis 

The  next  step  in  the  process  is often to check where we are, what is the current situation 


and  what  is  involved  in  making  it  a  problem.  For  example,  what  are  the  benefits  of  the  current 
product/service/process?  And  why  did  we  decide  to  make it like that? Understanding where the 

GE8151-Problem Solving and Python Programming UNIT I


13

problem  is  coming  from,  how  it  fits  in  with  current  developments  and  what  the  current 
environment  is,  is  crucial  when  working  out  whether  a  solution  will  actually  work  or  not. 
Similarly  you  must  have  a  set  of  criteria  by  which  to  evaluate  any  new  solutions  or  you  will  not 
know  whether  the  idea  is  workable  or  not.  This  section  of  the  problem  solving  process ensures 
that  time  is  spent  in  stepping  back  and  assessing the current situation and what actually needs to 
be changed. 

After  this  investigation,  it  is  often  good  to  go  back  one  step  to  reconfirm  that  your 
problem  definition  is  still  valid.  Frequently  after  the  investigation  people  discover  that  the 
problem they really want to answer is very different from their original interpretation of it. 

3. Generating possible Solutions 

When  you  have discovered the real problem that you want to solve and have investigated 


the  climate  into  which  the  solution  must  fit,  the  next  stage  is  to  generate  a  number  of  possible 
solutions.  At  this  stage  you  should  concentrate  on  generating  many  solutions  and  should  not 
evaluate  them  at  all.  Very  often  an  idea,  which  would  have  been  discarded  immediately,  when 
evaluated  properly,  can  be  developed  into  a  superb  solution.  At  this  stage,  you  should  not 
pre-judge  any  potential  solutions  but  should  treat  each  idea  as  a  new  idea  in  its  own  right  and 
worthy of consideration. 

4. Analyzing the Solutions 

This  section  of  the  problem  solving  process  is  where  you  investigate  the  various  factors 
about  each  of  the  potential  solutions.  You  note  down  the  good and bad points and other things 
which  are  relevant  to  each  solution.  Even  at  this  stage  you  are  not  evaluating  the  solution 
because  if  you  do  so  then  you  could  decide  not  to  write  down  the  valid  good  points  about  it 
because  overall  you  think  it  will  not  work.  However you might discover that by writing down its 
advantages  that  it  has  a  totally  unique  advantage.  Only  by  discovering  this  might  you  choose  to 
put the effort in to develop the idea so that it will work. 

5. Selecting the best Solution(s) 

This  is  the  section  where  you  look  through  the  various  influencing  factors  for  each 
possible  solution  and  decide  which  solutions  to  keep  and  which  to  disregard.  You  look  at  the 
solution  as  a  whole  and  use  your  judgement  as  to  whether  to  use  the  solution  or  not.  In 
Innovation  Toolbox,  you  can  vote  using  either  a  Yes/No/Interesting  process  or  on  a  sliding 
scale  depending  on  how  good  the  idea  is.  Sometimes  pure  facts  and  figures  dictate  which  ideas 
will  work  and  which  will  not.  In  other  situations,  it  will  be  purely  feelings  and  intuition  that 
decides.  Remember  that  intuition  is  really  a  lifetimes experience and judgement compressed into 
a single decision. 

By  voting  for  the  solutions  you  will  end  up  with  a  shortlist  of  potential  solutions.  You 
may  want  to  increase  the  depth  in  the  analysis  of  each  idea  and  vote  again  on  that  shortlist  to 
further refine your shortlist. 

You  will  then  end  up  with  one,  many  or  no  viable solutions. In the case where you have 
no  solutions  that  work,  you  will  need  to  repeat  the  generation  of  solutions  section  to  discover 

GE8151-Problem Solving and Python Programming UNIT I


14

more  potential  solutions.  Alternatively  you  might  consider  re-evaluating  the  problem  again  as 
sometimes  you  may  not  find  a  solution  because  the  problem  definition  is  not  well  defined  or 
self-contradictory. 

6. Planning the next course of action (Next Steps) 

This  section  of  the process is where you write down what you are going to do next. Now 


that  you  have  a  potential  solution  or  solutions  you  need  to  decide  how  you  will  make  the 
solution  happen.  This  will  involve  people  doing  various  things at various times in the future and 
then  confirming  that  they  have  been  carried  out  as  planned.  This stage ensures that the valuable 
thinking  that  has  gone  into  solving  the  problem  becomes reality. This series of Next Steps is the 
logical step to physically solving the problem. 

Simple Strategies for Developing Algorithms (Iteration, Recursion) 

Sequential Control Structure 

Sequential control means that the steps of an algorithm are carried out in a sequential 
manner, where each step is executed exactly once. 

Algorithm 
Step 1: ​Start 
Step 2: ​Input the value of A and B. 
Step 3: ​Find the sum of A and B. 
SUM=A+B 
Step 4: ​Print the value of SUM. 
Step 5: ​Stop 

Selection Control Structure 

In  Selection  control  only  one  flow  of  number  of  alternative  steps  is  executed  based  on 
the condition. 

GE8151-Problem Solving and Python Programming UNIT I


15

True

False

Start

Stop

Algorithm 
Step 1: ​Start 
Step 2: ​Read the value of n 
Step 3: ​if (n%2==0) then  
print it is an even number  
else  
  print it is an odd number 
Step 4: ​Stop 
 
Iteration Control Structure (Looping) 

GE8151-Problem Solving and Python Programming UNIT I


16

In  control  flow  one  or  more  steps  are  performed  repeatedly.  This  logic  is  used  for 
producing  loops  in  program  logic,  when one or more instructions may be executed several times 
depending on some conditions. 

False

Start

Stop

True

Algorithm 
Step 1: ​Start 
Step 2: ​Read the values of n 
Step 3: ​set initial values to Fact =i=1 
Step 4: ​Is i<=n else go to step 6 
Fact=Fact*i 
i=i+1 

GE8151-Problem Solving and Python Programming UNIT I


17

Step 5: ​go to step 4. 


Step 6: ​Print Fact. 
Step 7: ​Stop 
 

Recursion 

The  process  in  which  a  function  calls  itself  directly  or  indirectly  is  called  recursion  and 
the  corresponding  function  is  called  as  recursive  function.  Using  recursive  algorithm,  certain 
problems  can  be  solved  quite  easily.  Examples  of  such  problems  are Towers  of  Hanoi 
(TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. 

 
 

Algorithm: 

Step 1: Start 
Step 2: Read input value n 
Step 3: Call and print function fact(n) 
Step 4: Stop 
Function fact 
Step 1: Function fact start 
Step 2: if n=0 return 1 else goto step 3 
Step 3: return (n*fact(n-1) 
 
Illustrative Problems: 
Find Minimum and Maximum In A List 

Algorithm: 

Step 1: Start 
Step 2: Read the upper limit of the list ‘n’ 
Step 3: Read an list a[ ] of n numbers 

GE8151-Problem Solving and Python Programming UNIT I


18

Step 4: Assign first list value to MAX=a[0] and MIN=a[0] 


Step 5: for i=2 to n 
Step 6: if a[i]>MAX then MAX=a[i] 
Step 7: if a[i]<MIN then MIN=a[i] 
Step 8: Repeat step 5 to step 7 until loop reached to ‘n’ 
Step 9: Print MAX, MIN 
Step 10: Stop 
 
Pseudocode: 
Set initial i 
READ n 
READ an list a[ ] of ‘n’ numbers 
Set MAX=a[0], MIN=a[0] 
FOR i=2 to n 
IF a[i]>MAX then MAX =a[i] 
IF a[i]<MIN then MIN=a[i] 
END FOR 
PRINT MAX, MIN 
Stop 
 

Flowchart 

GE8151-Problem Solving and Python Programming UNIT I


19

 
 
Insert A Card In A List Of Sorted Cards 

Algorithm 

Step 1: Start 
Step 2: if it is the first card, it is already sorted. Return 1 
Step 3: Pick next card 
Step 4: Compare with all cards in the sorted list of cards 
Step 5: Shift all the cards in the sorted list of cards that is greater than the card to be  

GE8151-Problem Solving and Python Programming UNIT I


20

inserted. 
Step 6: Insert the card 
Step 7: Repeat until entire list is sorted 
Step 8: Stop 
 

Flowchart 

 
 

Pseudocode 

FOR i=1 to length(A) inclusive 


do: 
n=A[i] 
Position=i 
WHILE Position>0 and A[Position-1]>n 
do: 
A[Position]=A[Position-1] 
Position=Position-1 
END WHILE 
A[Position]=n 
END FOR  

GE8151-Problem Solving and Python Programming UNIT I


21

 
Guess An Integer Number In A Range 
Algorithm 

Step 1: ​start 
Step 2: Read guess number and random number n 
Step 3: Check if guess less than random number 
Step 3.1: print “too low” 
Step 4: check if guess greater than random number 
Step 4.1: Print “too high” 
Step 5: if guess equal to random number 
Step 5.1: print “Good job” 
Step 6: else print “You Lose” 
Step 7: Stop 

Flowchart 

Pseudocode: 

READ GUESS and random number n 


IF GUESS>n 
PRINT “Too High” 

GE8151-Problem Solving and Python Programming UNIT I


22

ELSE IF GUESS<n 
PRINT “Too Low” 
ELSE IF GUESS==n 
PRINT “Good job” 
ELSE 
PRINT “You Lose” 
 
Towers Of Hanoi 

Tower  of  Hanoi,  is  a  mathematical  puzzle  which  consists  of  three  towers  (pegs)  and 
more than one rings is as depicted − 

 
These  rings  are  of  different  sizes  and  stacked  upon  in  an  ascending  order,  i.e.  the  smaller  one 
sits  over  the  larger  one.  There  are  other  variations  of  the  puzzle  where  the  number  of  disks 
increase, but the tower count remains the same. 

Rules 
The  mission  is  to  move  all  the  disks  to  some  another  tower  without  violating  the  sequence  of 
arrangement. A few rules to be followed for Tower of Hanoi are − 

● 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. 


Following is an animated representation of solving a Tower of Hanoi puzzle with three disks. 

GE8151-Problem Solving and Python Programming UNIT I


23

 
Tower  of  Hanoi  puzzle  with  n  disks  can  be  solved  in  minimum ​2​ −1​ steps.  This  presentation 
n​

shows that a puzzle with 3 disks has taken ​23​​  - 1 = 7​ steps. 

Tower of Hanoi Algorithm: 

Step 1: Start 
Step 2: Procedure Hanoi(disk, source, dest, aux) 
Step 3: if disk==0 
Step 3.1: move disk from source to dest 
Step 4: else 
Step 4.1: Hanoi(disk-1, source, aux, dest) 
Step 4.2: move disk from source to dest 
Step 4.3: Hanoi(disk-1, aux, dest, source) 
Step 5: end if 
Step 6: Stop 

Flowchart 

GE8151-Problem Solving and Python Programming UNIT I


24

Pseudocode 

Procedure Hanoi(disk, source, dest, aux) 


IF disk==0, THEN 
move disk from source to dest 
ELSE 
Hanoi(disk-1, source, aux, dest) 
move disk from source to dest 
Hanoi(disk-1, aux, dest, source) 
END IF 
END PROCEDURE 

GE8151-Problem Solving and Python Programming UNIT I

Вам также может понравиться