Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

‭Notes for I - B.

Tech(CSM) - A‬ ‭1‬
‭INTRODUCTION TO PROBLEM SOLVING‬

‭INTRODUCTION‬

‭ e‬ ‭usually‬ ‭use‬ ‭the‬ ‭term‬ ‭computerisation‬ ‭to‬ ‭indicate‬ ‭the‬ ‭use‬ ‭of‬ ‭computers‬ ‭to‬ ‭develop‬
W
‭software in order to automate any routine human task efficiently.‬
‭Computers‬‭are‬‭used‬‭for‬‭solving‬‭various‬‭day-to-day‬‭problems‬‭and‬‭thus‬‭problem‬‭solving‬‭is‬‭an‬
‭essential skill that a computer science student should know.‬
‭It‬ ‭is‬ ‭pertinent‬ ‭to‬ ‭mention‬ ‭that‬ ‭computers‬ ‭themselves‬ ‭cannot‬ ‭solve‬ ‭a‬ ‭problem.‬ ‭Precise‬
‭step-by-step‬ ‭instructions‬ ‭should‬ ‭be‬ ‭given‬ ‭by‬ ‭us‬ ‭to‬ ‭solve‬ ‭the‬ ‭problem.‬ ‭Thus,‬ ‭the‬ ‭success‬ ‭of‬ ‭a‬
‭computer‬ ‭in‬ ‭solving‬ ‭a‬ ‭problem‬ ‭depends‬ ‭on‬ ‭how‬ ‭correctly‬ ‭and‬ ‭precisely‬ ‭we‬ ‭define‬ ‭the‬ ‭problem,‬
‭design a solution (algorithm) and implement the solution (program) using a programming language.‬
‭Thus,‬‭problem‬‭solving‬‭is‬‭the‬‭process‬‭of‬‭identifying‬‭a‬‭problem,‬‭developing‬‭an‬‭algorithm‬‭for‬
‭the identified problem and finally implementing the algorithm to develop a computer program.‬

‭STEPS FOR PROBLEM SOLVING‬

‭Figure: Steps for problem solving‬

‭ uppose‬‭while‬‭driving,‬‭a‬‭vehicle‬‭starts‬‭making‬‭a‬‭strange‬‭noise.‬‭We‬‭might‬‭not‬‭know‬‭how‬‭to‬
S
‭solve‬‭the‬‭problem‬‭right‬‭away.‬‭First,‬‭we‬‭need‬‭to‬‭identify‬‭where‬‭the‬‭noise‬‭is‬‭coming‬‭from?‬‭In‬‭case‬‭the‬
‭problem‬‭cannot‬‭be‬‭solved‬‭by‬‭us,‬‭then‬‭we‬‭need‬‭to‬‭take‬‭the‬‭vehicle‬‭to‬‭a‬‭mechanic.‬‭The‬‭mechanic‬‭will‬
‭analyze‬‭the‬‭problem‬‭to‬‭identify‬‭the‬‭source‬‭of‬‭the‬‭noise,‬‭make‬‭a‬‭plan‬‭about‬‭the‬‭work‬‭to‬‭be‬‭done‬‭and‬
‭finally‬ ‭repair‬ ‭the‬ ‭vehicle‬ ‭in‬‭order‬‭to‬‭remove‬‭the‬‭noise.‬‭From‬‭the‬‭above‬‭example,‬‭it‬‭is‬‭explicit‬‭that‬
‭finding the solution to a problem might consist of multiple steps.‬
‭When‬‭problems‬‭are‬‭straightforward‬‭and‬‭easy,‬‭we‬‭can‬‭easily‬‭find‬‭the‬‭solution.‬‭But‬‭a‬‭complex‬
‭problem‬‭requires‬‭a‬‭methodical‬‭approach‬‭to‬‭find‬‭the‬‭right‬‭solution.‬‭In‬‭other‬‭words,‬‭we‬‭have‬‭to‬‭apply‬
‭problem solving techniques.‬
‭Notes for I - B.Tech(CSM) - A‬ ‭2‬
‭ roblem‬ ‭solving‬ ‭begins‬ ‭with‬ ‭the‬ ‭precise‬ ‭identification‬ ‭of‬ ‭the‬ ‭problem‬ ‭and‬ ‭ends‬ ‭with‬ ‭a‬
P
‭complete‬ ‭working‬ ‭solution‬ ‭in‬ ‭terms‬ ‭of‬ ‭a‬ ‭program‬ ‭or‬ ‭software.‬ ‭Key‬ ‭steps‬ ‭required‬ ‭for‬ ‭solving‬ ‭a‬
‭problem using a computer are shown in the above figure and are discussed in the following.‬

‭1. Analyzing the problem‬


‭It‬‭is‬‭important‬‭to‬‭clearly‬‭understand‬‭a‬‭problem‬‭before‬‭we‬‭begin‬‭to‬‭find‬‭the‬‭solution‬‭for‬‭it.‬‭If‬
‭we‬ ‭are‬ ‭not‬ ‭clear‬ ‭as‬‭to‬‭what‬‭is‬‭to‬‭be‬‭solved,‬‭we‬‭may‬‭end‬‭up‬‭developing‬‭a‬‭program‬‭which‬‭may‬‭not‬
‭solve‬ ‭our‬ ‭purpose.‬ ‭Thus,‬‭we‬‭need‬‭to‬‭read‬‭and‬‭analyze‬‭the‬‭problem‬‭statement‬‭carefully‬‭in‬‭order‬‭to‬
‭list‬ ‭the‬ ‭principal‬ ‭components‬ ‭of‬ ‭the‬ ‭problem‬ ‭and‬ ‭decide‬ ‭the‬ ‭core‬‭functionalities‬‭that‬‭our‬‭solution‬
‭should‬ ‭have.‬‭By‬‭analyzing‬‭a‬‭problem,‬‭we‬‭would‬‭be‬‭able‬‭to‬‭figure‬‭out‬‭what‬‭are‬‭the‬‭inputs‬‭that‬‭our‬
‭program should accept and the outputs that it should produce.‬

‭2. Developing an Algorithm‬


‭It‬ ‭is‬ ‭essential‬‭to‬‭devise‬‭a‬‭solution‬‭before‬‭writing‬‭a‬‭program‬‭code‬‭for‬‭a‬‭given‬‭problem.‬‭The‬
‭solution‬‭is‬‭represented‬‭in‬‭natural‬‭language‬‭and‬‭is‬‭called‬‭an‬‭algorithm.‬‭We‬‭can‬‭imagine‬‭an‬‭algorithm‬
‭like‬‭a‬‭very‬‭well-written‬‭recipe‬‭for‬‭a‬‭dish,‬‭with‬‭clearly‬‭defined‬‭steps‬‭that,‬‭if‬‭followed,‬‭one‬‭will‬‭end‬
‭up‬ ‭preparing‬ ‭the‬ ‭dish.‬ ‭We‬ ‭start‬ ‭with‬ ‭a‬ ‭tentative‬ ‭solution‬ ‭plan‬ ‭and‬ ‭keep‬‭on‬‭refining‬‭the‬‭algorithm‬
‭until‬ ‭the‬ ‭algorithm‬ ‭is‬ ‭able‬ ‭to‬ ‭capture‬ ‭all‬ ‭the‬‭aspects‬‭of‬‭the‬‭desired‬‭solution.‬‭For‬‭a‬‭given‬‭problem,‬
‭more than one algorithm is possible and we have to select the most suitable solution.‬

‭3. Coding‬
‭After‬ ‭finalizing‬‭the‬‭algorithm,‬‭we‬‭need‬‭to‬‭convert‬‭the‬‭algorithm‬‭into‬‭the‬‭format‬‭which‬‭can‬
‭be‬‭understood‬‭by‬‭the‬‭computer‬‭to‬‭generate‬‭the‬‭desired‬‭solution.‬‭Different‬‭high‬‭level‬‭programming‬
‭languages‬ ‭can‬ ‭be‬ ‭used‬ ‭for‬ ‭writing‬ ‭a‬ ‭program.‬ ‭It‬ ‭is‬ ‭equally‬ ‭important‬ ‭to‬ ‭record‬ ‭the‬ ‭details‬ ‭of‬ ‭the‬
‭coding‬ ‭procedures‬ ‭followed‬ ‭and‬ ‭document‬ ‭the‬ ‭solution.‬ ‭This‬ ‭is‬ ‭helpful‬ ‭when‬ ‭revisiting‬ ‭the‬
‭programs at a later stage. Coding is explained in detail in the upcoming section.‬

‭4. Testing and Debugging‬


‭The‬‭program‬‭created‬‭should‬‭be‬‭tested‬‭on‬‭various‬‭parameters.‬‭The‬‭program‬‭should‬‭meet‬‭the‬
‭requirements‬‭of‬‭the‬‭user.‬‭It‬‭must‬‭respond‬‭within‬‭the‬‭expected‬‭time.‬‭It‬‭should‬‭generate‬‭correct‬‭output‬
‭for‬‭all‬‭possible‬‭inputs.‬‭In‬‭the‬‭presence‬‭of‬‭syntactical‬‭errors,‬‭no‬‭output‬‭will‬‭be‬‭obtained.‬‭In‬‭case‬‭the‬
‭output generated is incorrect, then the program should be checked for logical errors, if any.‬
‭Software‬ ‭industry‬ ‭follows‬ ‭standardized‬ ‭testing‬ ‭methods‬ ‭like‬ ‭unit‬ ‭or‬ ‭component‬ ‭testing,‬
‭integration‬ ‭testing,‬ ‭system‬ ‭testing,‬ ‭and‬‭acceptance‬‭testing‬‭while‬‭developing‬‭complex‬‭applications.‬
‭This‬ ‭is‬‭to‬‭ensure‬‭that‬‭the‬‭software‬‭meets‬‭all‬‭the‬‭business‬‭and‬‭technical‬‭requirements‬‭and‬‭works‬‭as‬
‭expected.‬ ‭The‬ ‭errors‬ ‭or‬ ‭defects‬ ‭found‬ ‭in‬ ‭the‬ ‭testing‬ ‭phases‬ ‭are‬ ‭debugged‬ ‭or‬ ‭rectified‬ ‭and‬ ‭the‬
‭program‬ ‭is‬‭again‬‭tested.‬‭This‬‭continues‬‭till‬‭all‬‭the‬‭errors‬‭are‬‭removed‬‭from‬‭the‬‭program.‬‭Once‬‭the‬
‭software‬‭application‬‭has‬‭been‬‭developed,‬‭tested‬‭and‬‭delivered‬‭to‬‭the‬‭user,‬‭still‬‭problems‬‭in‬‭terms‬‭of‬
‭functioning‬ ‭can‬ ‭come‬ ‭up‬ ‭and‬ ‭need‬ ‭to‬ ‭be‬ ‭resolved‬ ‭from‬ ‭time‬ ‭to‬ ‭time.‬ ‭The‬ ‭maintenance‬ ‭of‬ ‭the‬
‭solution,‬‭thus,‬‭involves‬‭fixing‬‭the‬‭problems‬‭faced‬‭by‬‭the‬‭user,‬‭answering‬‭the‬‭queries‬‭of‬‭the‬‭user‬‭and‬
‭even serving the request for addition or modification of features.‬
‭ALGORITHM‬
‭In‬ ‭our‬ ‭day-to-day‬ ‭life‬ ‭we‬ ‭perform‬ ‭activities‬ ‭by‬ ‭following‬ ‭a‬ ‭certain‬ ‭sequence‬ ‭of‬ ‭steps.‬
‭Examples‬‭of‬‭activities‬‭include‬‭getting‬‭ready‬‭for‬‭school,‬‭making‬‭breakfast,‬‭riding‬‭a‬‭bicycle,‬‭wearing‬
‭a‬‭tie,‬‭solving‬‭a‬‭puzzle‬‭and‬‭so‬‭on.‬‭To‬‭complete‬‭each‬‭activity,‬‭we‬‭follow‬‭a‬‭sequence‬‭of‬‭steps.‬‭Suppose‬
‭following are the steps required for an activity ‘riding a bicycle’:‬
‭●‬ ‭remove the bicycle from the stand,‬
‭●‬ ‭sit on the seat of the bicycle,‬
‭●‬ ‭start pedaling,‬
‭●‬ ‭use breaks whenever needed and‬
‭Notes for I - B.Tech(CSM) - A‬ ‭3‬
‭●‬ ‭stop on reaching the destination.‬

‭ et us now find the Greatest Common Divisor (GCD) of two numbers 45 and 54.‬
L
‭Note: GCD is the largest number that divides both the given numbers.‬
‭Step 1: Find the numbers (divisors) which can divide the given numbers‬
‭Divisors of 45 are: 1, 3, 5, 9, 15, and 45‬
‭Divisors of 54 are: 1, 2, 3, 6, 9, 18, 27, and 54‬
‭Step 2: Then find the largest common number from these two lists.‬
‭Therefore, GCD of 45 and 54 is 9‬
‭Hence,‬‭it‬‭is‬‭clear‬‭that‬‭we‬‭need‬‭to‬‭follow‬‭a‬‭sequence‬‭of‬‭steps‬‭to‬‭accomplish‬‭the‬‭task.‬‭Such‬‭a‬
‭finite‬‭sequence‬‭of‬‭steps‬‭required‬‭to‬‭get‬‭the‬‭desired‬‭output‬‭is‬‭called‬‭an‬‭algorithm.‬‭It‬‭will‬‭lead‬‭to‬‭the‬
‭desired‬ ‭result‬‭in‬‭a‬‭finite‬‭amount‬‭of‬‭time,‬‭if‬‭followed‬‭correctly.‬‭Algorithm‬‭has‬‭a‬‭definite‬‭beginning‬
‭and a definite end, and consists of a finite number of steps.‬

‭Why do we need an Algorithm?‬


‭A‬‭programmer‬‭writes‬‭a‬‭program‬‭to‬‭instruct‬‭the‬‭computer‬‭to‬‭do‬‭certain‬‭tasks‬‭as‬‭desired.‬‭The‬
‭computer‬ ‭then‬ ‭follows‬ ‭the‬ ‭steps‬ ‭written‬ ‭in‬ ‭the‬ ‭program‬ ‭code.‬ ‭Therefore,‬ ‭the‬ ‭programmer‬ ‭first‬
‭prepares‬ ‭a‬ ‭roadmap‬ ‭of‬ ‭the‬ ‭program‬ ‭to‬ ‭be‬ ‭written,‬ ‭before‬ ‭actually‬ ‭writing‬ ‭the‬ ‭code.‬ ‭Without‬ ‭a‬
‭roadmap,‬ ‭the‬ ‭programmer‬ ‭may‬ ‭not‬ ‭be‬ ‭able‬ ‭to‬ ‭clearly‬ ‭visualise‬ ‭the‬ ‭instructions‬ ‭to‬ ‭be‬ ‭written‬‭and‬
‭may end up developing a program which may not work as expected.‬
‭Such‬ ‭a‬ ‭roadmap‬ ‭is‬ ‭nothing‬ ‭but‬ ‭the‬ ‭algorithm‬ ‭which‬ ‭is‬ ‭the‬ ‭building‬ ‭block‬ ‭of‬ ‭a‬ ‭computer‬
‭program.‬ ‭For‬ ‭example,‬ ‭searching‬ ‭using‬ ‭a‬ ‭search‬ ‭engine,‬ ‭sending‬ ‭a‬ ‭message,‬ ‭finding‬ ‭a‬ ‭word‬ ‭in‬ ‭a‬
‭document,‬‭booking‬‭a‬‭taxi‬‭through‬‭an‬‭app,‬‭performing‬‭online‬‭banking,‬‭playing‬‭computer‬‭games,‬‭all‬
‭are based on algorithms.‬
‭Writing‬‭an‬‭algorithm‬‭is‬‭mostly‬‭considered‬‭as‬‭a‬‭first‬‭step‬‭to‬‭programming.‬‭Once‬‭we‬‭have‬‭an‬
‭algorithm‬ ‭to‬ ‭solve‬ ‭a‬ ‭problem,‬ ‭we‬ ‭can‬ ‭write‬ ‭the‬ ‭computer‬ ‭program‬ ‭for‬ ‭giving‬ ‭instructions‬ ‭to‬ ‭the‬
‭computer‬ ‭in‬ ‭high‬ ‭level‬ ‭language.‬ ‭If‬ ‭the‬ ‭algorithm‬ ‭is‬ ‭correct,‬ ‭computer‬ ‭will‬ ‭run‬ ‭the‬ ‭program‬
‭correctly,‬‭every‬‭time.‬‭So,‬‭the‬‭purpose‬‭of‬‭using‬‭an‬‭algorithm‬‭is‬‭to‬‭increase‬‭the‬‭reliability‬‭,‬‭accuracy‬
‭and‬‭efficiency of obtaining solutions‬‭.‬

‭Characteristics of a good algorithm‬


‭●‬ C ‭ lear‬ ‭and‬ ‭Unambiguous:‬ ‭The‬ ‭algorithm‬ ‭should‬ ‭be‬ ‭unambiguous.‬ ‭Each‬ ‭of‬ ‭its‬ ‭steps‬
‭should be clear in all aspects and must lead to only one meaning.‬
‭●‬ ‭Well-Defined‬ ‭Inputs:‬ ‭If‬ ‭an‬ ‭algorithm‬ ‭says‬ ‭to‬ ‭take‬ ‭inputs,‬ ‭it‬ ‭should‬ ‭be‬ ‭well-defined‬
‭inputs. It may or may not take input.‬
‭●‬ ‭Well-Defined‬ ‭Outputs:‬ ‭The‬ ‭algorithm‬ ‭must‬ ‭clearly‬ ‭define‬‭what‬‭output‬‭will‬‭be‬‭yielded‬
‭and it should be well-defined as well. It should produce at least 1 output.‬
‭●‬ ‭Finite-ness:‬‭The algorithm must be finite, i.e. it‬‭should terminate after a finite time.‬
‭●‬ ‭Feasible:‬ ‭The‬ ‭algorithm‬ ‭must‬ ‭be‬ ‭simple,‬ ‭generic,‬ ‭and‬ ‭practical,‬ ‭such‬ ‭that‬ ‭it‬ ‭can‬ ‭be‬
‭executed‬ ‭with‬ ‭the‬ ‭available‬ ‭resources.‬ ‭It‬ ‭must‬ ‭not‬ ‭contain‬ ‭some‬ ‭future‬ ‭technology‬ ‭or‬
‭anything.‬
‭●‬ ‭Language‬‭Independent:‬‭The‬‭Algorithm‬‭designed‬‭must‬‭be‬‭language-independent,‬‭i.e.‬‭it‬
‭must‬ ‭be‬ ‭just‬ ‭plain‬ ‭instructions‬ ‭that‬ ‭can‬ ‭be‬ ‭implemented‬ ‭in‬ ‭any‬ ‭language,‬ ‭and‬ ‭yet‬ ‭the‬
‭output will be the same, as expected.‬
‭●‬ ‭Input:‬‭An‬‭algorithm‬‭has‬‭zero‬‭or‬‭more‬‭inputs.‬‭Each‬‭that‬‭contains‬‭a‬‭fundamental‬‭operator‬
‭must accept zero or more inputs.‬
‭●‬ ‭Output:‬ ‭An‬ ‭algorithm‬ ‭produces‬ ‭at‬ ‭least‬ ‭one‬ ‭output.‬ ‭Every‬ ‭instruction‬ ‭that‬ ‭contains‬ ‭a‬
‭fundamental operator must accept zero or more inputs.‬
‭Notes for I - B.Tech(CSM) - A‬ ‭4‬
‭●‬ D ‭ efiniteness:‬‭All‬‭instructions‬‭in‬‭an‬‭algorithm‬‭must‬‭be‬‭unambiguous,‬‭precise,‬‭and‬‭easy‬‭to‬
‭interpret.‬ ‭By‬ ‭referring‬ ‭to‬ ‭any‬ ‭of‬ ‭the‬ ‭instructions‬ ‭in‬ ‭an‬ ‭algorithm‬ ‭one‬ ‭can‬ ‭clearly‬
‭understand‬ ‭what‬ ‭is‬ ‭to‬ ‭be‬ ‭done.‬ ‭Every‬ ‭fundamental‬ ‭operator‬ ‭in‬ ‭instruction‬ ‭must‬ ‭be‬
‭defined without any ambiguity.‬
‭●‬ ‭Finiteness:‬ ‭An‬ ‭algorithm‬ ‭must‬‭terminate‬‭after‬‭a‬‭finite‬‭number‬‭of‬‭steps‬‭in‬‭all‬‭test‬‭cases.‬
‭Every‬ ‭instruction‬ ‭which‬ ‭contains‬ ‭a‬ ‭fundamental‬ ‭operator‬ ‭must‬ ‭be‬ ‭terminated‬ ‭within‬ ‭a‬
‭finite‬ ‭amount‬ ‭of‬ ‭time.‬ ‭Infinite‬ ‭loops‬ ‭or‬ ‭recursive‬ ‭functions‬ ‭without‬ ‭base‬ ‭conditions‬ ‭do‬
‭not possess finiteness.‬
‭●‬ ‭Effectiveness:‬‭An‬‭algorithm‬‭must‬‭be‬‭developed‬‭by‬‭using‬‭very‬‭basic,‬‭simple,‬‭and‬‭feasible‬
‭operations so that one can trace it out by using just paper and pencil.‬

‭Properties of Algorithm:‬
‭‬
● I‭ t should terminate after a finite time.‬
‭●‬ ‭It should produce at least one output.‬
‭●‬ ‭It should take zero or more input.‬
‭●‬ ‭It should be deterministic means giving the same output for the same input case.‬
‭●‬ ‭Every step in the algorithm must be effective i.e. every step should do some work.‬

‭While writing an algorithm, it is required to clearly identify the following:‬


‭‬T
● ‭ he input to be taken from the user‬
‭●‬ ‭Processing or computation to be performed to get the desired result‬
‭●‬ ‭The output desired by the user‬

‭REPRESENTATION OF ALGORITHMS‬
‭ sing‬ ‭their‬ ‭algorithmic‬ ‭thinking‬‭skills,‬‭the‬‭software‬‭designers‬‭or‬‭programmers‬‭analyse‬‭the‬
U
‭problem‬‭and‬‭identify‬‭the‬‭logical‬‭steps‬‭that‬‭need‬‭to‬‭be‬‭followed‬‭to‬‭reach‬‭a‬‭solution.‬‭Once‬‭the‬‭steps‬
‭are‬ ‭identified,‬ ‭the‬ ‭need‬ ‭is‬ ‭to‬ ‭write‬ ‭down‬ ‭these‬ ‭steps‬ ‭along‬ ‭with‬ ‭the‬ ‭required‬ ‭input‬ ‭and‬ ‭desired‬
‭output.‬‭There‬‭are‬‭two‬‭common‬‭methods‬‭of‬‭representing‬‭an‬‭algorithm‬‭—flowchart‬‭and‬‭pseudocode.‬
‭Either of the methods can be used to represent an algorithm while keeping in mind the following:‬
‭●‬ ‭it showcases the logic of the problem solution, excluding any implementational details‬
‭●‬ ‭it clearly reveals the flow of control during execution of the program‬

‭Flowchart — Visual Representation of Algorithms‬


‭ ‬‭flowchart‬‭is‬‭a‬‭visual‬‭representation‬‭of‬‭an‬‭algorithm.‬‭A‬‭flowchart‬‭is‬‭a‬‭diagram‬‭made‬‭up‬‭of‬
A
‭boxes,‬ ‭diamonds‬ ‭and‬ ‭other‬ ‭shapes,‬ ‭connected‬ ‭by‬ ‭arrows.‬ ‭Each‬ ‭shape‬ ‭represents‬ ‭a‬ ‭step‬ ‭of‬ ‭the‬
‭solution‬‭process‬‭and‬‭the‬‭arrow‬‭represents‬‭the‬‭order‬‭or‬‭link‬‭among‬‭the‬‭steps.‬‭There‬‭are‬‭standardised‬
‭symbols to draw flowcharts. Some are given in below table‬

‭Shapes or symbols to draw flow charts‬


‭Flowchart Symbol‬ ‭Function‬ ‭Description‬

‭Start/End‬ ‭Also‬ ‭called‬ ‭“Terminator”‬ ‭symbol.‬ ‭It‬ ‭indicates‬


‭ here the flow starts and ends.‬
w

‭Process‬ ‭ lso‬ ‭called‬ ‭“Action‬ ‭Symbol,”‬ ‭it‬ ‭represents‬ ‭a‬


A
‭process, action, or a single step.‬
‭Notes for I - B.Tech(CSM) - A‬ ‭5‬

‭Decision‬ ‭ ‬ ‭decision‬ ‭or‬ ‭branching‬ ‭point,‬ ‭usually‬ ‭a‬ ‭yes/no‬


A
‭or‬‭true/‬‭false‬‭question‬‭is‬‭asked,‬‭and‬‭based‬‭on‬‭the‬
‭answer, the path gets split into two branches.‬

‭Input/Output‬ ‭Also‬ ‭called‬ ‭data‬ ‭symbol,‬ ‭this‬ ‭parallelogram‬


s‭ hape is used to input or output data‬

‭Arrow‬ ‭Connector‬ ‭to‬ ‭show‬ ‭order‬ ‭of‬ ‭flow‬ ‭between‬


s‭ hapes.‬

‭Example 1: Write an algorithm and flowchart to find the average of five numbers.‬

‭Before developing the algorithm, let us first identify the input, process and output:‬
‭●‬ ‭Input: Five numbers is required to calculate average‬
‭●‬ ‭Process:‬ ‭Calculate‬ ‭the‬ ‭sum‬ ‭of‬ ‭all‬ ‭numbers‬ ‭and‬ ‭divide‬ ‭the‬ ‭sum‬‭with‬‭the‬‭value‬‭5‬‭(since‬‭we‬
‭find average of 5 numbers)‬
‭●‬ ‭Output: Average of five numbers‬

‭Algorithm to find average of 5 numbers.‬


‭Step 1: Input 5 numbers and store them in variables num1,num2,num3,num4,num5‬
‭Step 2: Compute (num1+num2+num3+num4+num5) / 5 and store it in average‬
‭Step 3: Print average‬

‭ he‬ ‭algorithm‬ ‭to‬ ‭find‬ ‭average‬ ‭of‬ ‭five‬ ‭numbers‬ ‭can‬ ‭be‬ ‭represented‬ ‭pictorially‬ ‭using‬ ‭flowchart‬ ‭as‬
T
‭shown below figure:‬
‭Notes for I - B.Tech(CSM) - A‬ ‭6‬
‭Example 2: Draw a flowchart to test the eligibility of a person to vote in Indian elections.‬

‭Pseudocode‬
‭ ‬ ‭pseudocode‬ ‭(pronounced‬ ‭Soo-doh-kohd)‬‭is‬‭another‬‭way‬‭of‬‭representing‬‭an‬‭algorithm.‬‭It‬
A
‭is considered as a non-formal language that helps programmers to write algorithm.‬
‭It‬‭is‬‭a‬‭detailed‬‭description‬‭of‬‭instructions‬‭that‬‭a‬‭computer‬‭must‬‭follow‬‭in‬‭a‬‭particular‬‭order.‬
‭It is intended for human reading and cannot be executed directly by the computer.‬
‭No‬‭specific‬‭standard‬‭for‬‭writing‬‭a‬‭pseudocode‬‭exists.‬‭The‬‭word‬‭“pseudo”‬‭means‬‭“not‬‭real,”‬
‭so “pseudocode” means “not real code”.‬
‭Following are some of the frequently used keywords while writing pseudocode:‬
‭• INPUT‬
‭• COMPUTE‬
‭• PRINT‬
‭• INCREMENT‬
‭• DECREMENT‬
‭• IF/ELSE‬
‭• WHILE‬
‭• TRUE/FALSE‬

‭ xample‬ ‭1:‬ ‭Write‬ ‭an‬ ‭algorithm‬ ‭to‬ ‭display‬ ‭the‬ ‭sum‬ ‭of‬ ‭two‬ ‭numbers‬ ‭entered‬ ‭by‬ ‭user,‬ ‭using‬
E
‭both pseudocode and flowchart.‬

‭ seudocode for the sum of two numbers will be:‬


P
‭INPUT num1‬
‭INPUT num2‬
‭COMPUTE Result = num1 + num2‬
‭PRINT Result‬

‭Note: Draw the flowchart on your own as a practice with reference to above flowchart notes‬
‭Notes for I - B.Tech(CSM) - A‬ ‭7‬
‭ xample‬ ‭2:‬ ‭Write‬ ‭an‬ ‭algorithm‬ ‭to‬ ‭calculate‬ ‭area‬ ‭and‬ ‭perimeter‬ ‭of‬ ‭a‬ ‭rectangle,‬ ‭using‬ ‭both‬
E
‭pseudocode and flowchart.‬

‭ seudocode for calculating area and perimeter of a rectangle.‬


P
‭INPUT length‬
‭INPUT breadth‬
‭COMPUTE Area = length * breadth‬
‭PRINT Area‬
‭COMPUTE Perim = 2 * (length + breadth)‬
‭PRINT Perim‬

‭Note: Draw the flowchart on your own as a practice with reference to above flowchart notes‬

‭Top-Down Approach and Bottom-up Approach‬

‭ lassification of Algorithm by Design Approaches :‬


C
‭There are two approaches for designing an algorithm. these approaches include‬
‭❖‬ ‭Top-Down approach‬
‭❖‬ ‭Bottom-up approach‬
‭●‬ ‭Top-Down‬ ‭Approach:‬ ‭In‬ ‭the‬ ‭top-down‬ ‭approach,‬ ‭a‬ ‭large‬ ‭problem‬ ‭is‬ ‭divided‬ ‭into‬ ‭small‬
‭sub-problem.‬ ‭and‬ ‭keep‬ ‭repeating‬ ‭the‬ ‭process‬ ‭of‬ ‭decomposing‬ ‭problems‬ ‭until‬‭the‬‭complex‬
‭problem is solved.‬
‭●‬ ‭Bottom-up‬ ‭approach:‬ ‭The‬ ‭bottom-up‬ ‭approach‬ ‭is‬ ‭also‬ ‭known‬ ‭as‬ ‭the‬ ‭reverse‬ ‭of‬ ‭top-down‬
‭approaches.‬
‭In‬ ‭approach‬ ‭different,‬‭part‬‭of‬‭a‬‭complex‬‭program‬‭is‬‭solved‬‭using‬‭a‬‭programming‬‭language‬
‭and then this is combined into a complete program.‬

‭Top-Down Approach:‬

I‭ n‬ ‭the‬ ‭top-down‬ ‭model,‬ ‭an‬ ‭overview‬‭of‬‭the‬‭system‬‭is‬‭formulated‬‭without‬‭going‬‭into‬‭detail‬


‭for‬‭any‬‭part‬‭of‬‭it.‬‭Each‬‭part‬‭of‬‭it‬‭then‬‭refined‬‭into‬‭more‬‭details,‬‭defining‬‭it‬‭in‬‭yet‬‭more‬‭details‬‭until‬
‭the‬‭entire‬‭specification‬‭is‬‭detailed‬‭enough‬‭to‬‭validate‬‭the‬‭model.‬‭if‬‭we‬‭glance‬‭at‬‭a‬‭haul‬‭as‬‭a‬‭full,‬‭it’s‬
‭going‬ ‭to‬ ‭appear‬ ‭not‬ ‭possible‬ ‭as‬ ‭a‬ ‭result‬ ‭of‬ ‭it’s‬‭so‬‭complicated‬‭For‬‭example:‬‭Writing‬‭a‬‭University‬
‭system‬‭program,‬‭writing‬‭a‬‭word‬‭processor.‬‭Complicated‬‭issues‬‭may‬‭be‬‭resolved‬‭victimization‬‭high‬
‭down style, conjointly referred to as Stepwise refinement where,‬
‭Breaking‬ ‭down‬ ‭a‬ ‭complex‬ ‭problem‬ ‭into‬ ‭smaller,‬ ‭more‬ ‭manageable‬ ‭sub-problems‬ ‭and‬
‭solving each sub-problem individually.‬
‭Designing‬ ‭a‬ ‭system‬ ‭starting‬ ‭from‬ ‭the‬ ‭highest‬ ‭level‬ ‭of‬‭abstraction‬‭and‬‭moving‬‭towards‬‭the‬
‭lower levels.‬

‭ e break the problem into parts,‬


W
‭Then break the parts into parts soon and now each of parts will be easy to do.‬

‭Advantages:‬
‭●‬ ‭Breaking problems into parts help us to identify what needs to be done.‬
‭●‬ ‭At‬ ‭each‬ ‭step‬ ‭of‬ ‭refinement,‬ ‭new‬ ‭parts‬ ‭will‬ ‭become‬ ‭less‬ ‭complex‬ ‭and‬ ‭therefore‬ ‭easier‬ ‭to‬
‭solve.‬
‭●‬ ‭Parts of the solution may turn out to be reusable.‬
‭●‬ ‭Breaking problems into parts allows more than one person to solve the problem.‬
‭Notes for I - B.Tech(CSM) - A‬ ‭8‬
‭Bottom-Up Approach:‬
‭In‬‭this‬‭design,‬‭individual‬‭parts‬‭of‬‭the‬‭system‬‭are‬‭specified‬‭in‬‭detail.‬‭The‬‭parts‬‭are‬‭linked‬‭to‬
‭form‬ ‭larger‬ ‭components,‬ ‭which‬ ‭are‬ ‭in‬ ‭turn‬ ‭linked‬ ‭until‬ ‭a‬ ‭complete‬ ‭system‬ ‭is‬ ‭formed.‬
‭Object-oriented‬ ‭language‬ ‭such‬ ‭as‬ ‭C++‬ ‭or‬ ‭java‬ ‭use‬ ‭a‬ ‭bottom-up‬ ‭approach‬ ‭where‬ ‭each‬ ‭object‬ ‭is‬
‭identified first. .‬
‭Building‬ ‭a‬ ‭system‬ ‭by‬ ‭starting‬ ‭with‬ ‭the‬ ‭individual‬ ‭components‬ ‭and‬ ‭gradually‬ ‭integrating‬
‭them to form a larger system.‬
‭Solving‬‭sub-problems‬‭first‬‭and‬‭then‬‭using‬‭the‬‭solutions‬‭to‬‭build‬‭up‬‭to‬‭a‬‭solution‬‭of‬‭a‬‭larger‬
‭problem.‬

‭Advantage:‬
‭●‬ ‭Make‬‭decisions‬‭about‬‭reusable‬‭low-level‬‭utilities‬‭then‬‭decide‬‭how‬‭there‬‭will‬‭be‬‭put‬‭together‬
‭to create high-level construct.‬

‭ ote:‬‭Both‬‭approaches‬‭have‬‭their‬‭own‬‭advantages‬‭and‬‭disadvantages‬‭and‬‭the‬‭choice‬‭between‬‭them‬
N
‭often depends on the specific problem being solved.‬

‭Difference between Top-Down Approach and Bottom-up Approach‬


‭ .‬ ‭Top-Down Approach‬
S ‭Bottom-up Approach‬
‭No‬
‭1‬ I‭ n this approach We focus on breaking up‬ I‭ n bottom up approach, we solve smaller‬
‭the problem into smaller parts.‬ ‭problems and integrate it as whole and‬
‭complete the solution.‬

‭2‬ ‭ ainly used by structured programming‬


M ‭ ainly used by object oriented programming‬
M
‭language such as COBOL, Fortran, C, etc.‬ ‭language such as C++, C#, Python.‬

‭3‬ ‭ ach part is programmed separately‬


E ‭ edundancy is minimized by using data‬
R
‭therefore contain redundancy.‬ ‭encapsulation and data hiding.‬

‭4‬ I‭ n this the communications is less among‬ ‭In this module must have communication.‬
‭modules.‬

‭5‬ I‭ t is used in debugging, module‬ ‭It is basically used in testing.‬


‭documentation, etc.‬

‭6‬ I‭ n top down approach, decomposition takes‬ I‭ n bottom up approach composition takes‬
‭place.‬ ‭place.‬

‭7‬ I‭ n this top function of system might be hard‬ I‭ n this sometimes we can not build a‬
‭to identify.‬ ‭program from the piece we have started.‬

‭8‬ ‭In this implementation details may differ.‬ ‭This is not natural for people to assemble.‬

‭9‬ ‭Pros-‬ ‭Pros-‬

‭‬ E
● ‭ asier isolation of interface errors‬ ‭‬ E
● ‭ asy to create test conditions‬
‭●‬ ‭It benefits in the case error occurs‬ ‭●‬ ‭Test results are easy to observe‬
‭towards the top of the program.‬ ‭●‬ ‭It is suited if defects occur at the‬
‭●‬ ‭Defects in design get detected early‬ ‭bottom of the program.‬
‭and can be corrected as an early‬
‭working module of the program is‬
‭available.‬
‭Notes for I - B.Tech(CSM) - A‬ ‭9‬

‭10‬ ‭Cons-‬ ‭Cons-‬

‭●‬ D ‭ ifficulty in observing the output of‬ ‭●‬ T ‭ here is no representation of the‬
‭test case.‬ ‭working model once several modules‬
‭●‬ ‭Stub writing is quite crucial as it‬ ‭have been constructed.‬
‭leads to setting of output‬ ‭●‬ ‭There is no existence of the program‬
‭parameters.‬ ‭as an entity without the addition of‬
‭●‬ ‭When stubs are located far from the‬ ‭the last module.‬
‭top level module, choosing test‬ ‭●‬ ‭From a partially integrated system,‬
‭cases and designing stubs become‬ ‭test engineers cannot observe‬
‭more challenging.‬ ‭system-level functions. It can be‬
‭possible only with the installation of‬
‭the top-level test driver.‬

‭Time Complexity and Space Complexities of Algorithms‬

‭ enerally,‬‭there‬‭is‬‭always‬‭more‬‭than‬‭one‬‭way‬‭to‬‭solve‬‭a‬‭problem‬‭in‬‭computer‬‭science‬‭with‬
G
‭different‬ ‭algorithms.‬ ‭Therefore,‬ ‭it‬ ‭is‬ ‭highly‬ ‭required‬ ‭to‬ ‭use‬ ‭a‬ ‭method‬‭to‬‭compare‬‭the‬‭solutions‬‭in‬
‭order to judge which one is more optimal. The method must be:‬

‭ ‬ I‭ ndependent of the machine and its configuration, on which the algorithm is running on.‬

‭●‬ ‭Shows a direct correlation with the number of inputs.‬
‭●‬ ‭Can distinguish two algorithms clearly without ambiguity.‬

‭ here‬ ‭are‬ ‭two‬ ‭such‬ ‭methods‬ ‭used,‬ ‭time‬ ‭complexity‬ ‭and‬ ‭space‬ ‭complexity‬ ‭which‬ ‭are‬
T
‭discussed below:‬

‭Time Complexity:‬
‭The‬‭time‬‭complexity‬‭of‬‭an‬‭algorithm‬‭quantifies‬‭the‬‭amount‬‭of‬‭time‬‭taken‬‭by‬‭an‬‭algorithm‬‭to‬
‭run‬‭as‬‭a‬‭function‬‭of‬‭the‬‭length‬‭of‬‭the‬‭input.‬‭Note‬‭that‬‭the‬‭time‬‭to‬‭run‬‭is‬‭a‬‭function‬‭of‬‭the‬‭length‬‭of‬
‭the input and not the actual execution time of the machine on which the algorithm is running on.‬

‭Definition:‬
‭The‬ ‭valid‬ ‭algorithm‬ ‭takes‬ ‭a‬ ‭finite‬ ‭amount‬ ‭of‬‭time‬‭for‬‭execution.‬‭The‬‭time‬‭required‬‭by‬‭the‬
‭algorithm‬ ‭to‬ ‭solve‬ ‭given‬ ‭problem‬‭is‬‭called‬‭time‬‭complexity‬‭of‬‭the‬‭algorithm.‬‭Time‬‭complexity‬‭is‬
‭very useful measure in algorithm analysis.‬

I‭ t‬‭is‬‭the‬‭time‬‭needed‬‭for‬‭the‬‭completion‬‭of‬‭an‬‭algorithm.‬‭To‬‭estimate‬‭the‬‭time‬‭complexity,‬‭we‬‭need‬
‭to‬ ‭consider‬ ‭the‬ ‭cost‬ ‭of‬ ‭each‬ ‭fundamental‬ ‭instruction‬ ‭and‬ ‭the‬ ‭number‬ ‭of‬ ‭times‬ ‭the‬ ‭instruction‬ ‭is‬
‭executed.‬

‭Example‬‭: Addition of two scalar variables.‬

‭ lgorithm ADD SCALAR(A, B)‬


A
‭//Description: Perform arithmetic addition of two numbers‬
‭//Input: Two scalar variables A and B‬
‭//Output: variable C, which holds the addition of A and B‬
‭C <- A + B‬
‭return C‬
‭Notes for I - B.Tech(CSM) - A‬ ‭10‬
‭ he‬ ‭addition‬ ‭of‬ ‭two‬ ‭scalar‬ ‭numbers‬ ‭requires‬ ‭one‬ ‭addition‬ ‭operation.‬ ‭the‬ ‭time‬ ‭complexity‬ ‭of‬ ‭this‬
T
‭algorithm is constant, so T(n) = O(1) .‬

I‭ n‬‭order‬‭to‬‭calculate‬‭time‬‭complexity‬‭on‬‭an‬‭algorithm,‬‭it‬‭is‬‭assumed‬‭that‬‭a‬‭constant‬‭time‬‭c‬‭is‬‭taken‬
‭to execute one operation, and then the total operations for an input length on N are calculated.‬

‭ pace Complexity:‬
S
‭Definition:‬
‭Problem-solving‬ ‭using‬ ‭computer‬ ‭requires‬ ‭memory‬ ‭to‬ ‭hold‬ ‭temporary‬ ‭data‬ ‭or‬ ‭final‬ ‭result‬
‭while‬‭the‬‭program‬‭is‬‭in‬‭execution.‬‭The‬‭amount‬‭of‬‭memory‬‭required‬‭by‬‭the‬‭algorithm‬‭to‬‭solve‬‭given‬
‭problem is called‬‭space complexity‬‭of the algorithm.‬

‭ he‬‭space‬‭complexity‬‭of‬‭an‬‭algorithm‬‭quantifies‬‭the‬‭amount‬‭of‬‭space‬‭taken‬‭by‬‭an‬‭algorithm‬
T
‭to run as a function of the length of the input.‬

‭It is the amount of memory needed for the completion of an algorithm.‬

‭To estimate the memory requirement we need to focus on two parts:‬

(‭ 1)‬ ‭A‬ ‭fixed‬ ‭part:‬ ‭It‬ ‭is‬ ‭independent‬ ‭of‬ ‭the‬ ‭input‬ ‭size.‬ ‭It‬ ‭includes‬ ‭memory‬‭for‬‭instructions‬‭(code),‬
‭constants, variables, etc.‬

(‭ 2)‬ ‭A‬ ‭variable‬ ‭part:‬ ‭It‬ ‭is‬ ‭dependent‬ ‭on‬ ‭the‬ ‭input‬ ‭size.‬ ‭It‬ ‭includes‬ ‭memory‬ ‭for‬ ‭recursion‬ ‭stack,‬
‭referenced variables, etc.‬

‭Example‬‭: Addition of two scalar variables‬

‭ lgorithm ADD SCALAR(A, B)‬


A
‭//Description: Perform arithmetic addition of two numbers‬
‭//Input: Two scalar variables A and B‬
‭//Output: variable C, which holds the addition of A and B‬
‭C <— A+B‬
‭return C‬

‭ he‬‭addition‬‭of‬‭two‬‭scalar‬‭numbers‬‭requires‬‭one‬‭extra‬‭memory‬‭location‬‭to‬‭hold‬‭the‬‭result.‬‭Thus‬‭the‬
T
‭space complexity of this algorithm is constant, hence S(n) = O(1).‬

‭Time Complexity vs. Space Complexity‬

‭ ime‬ ‭Complexity‬ ‭and‬ ‭Space‬ ‭Complexity‬ ‭are‬ ‭two‬ ‭key‬ ‭concepts‬ ‭in‬ ‭algorithm‬ ‭analysis‬ ‭that‬
T
‭measure‬ ‭different‬ ‭aspects‬ ‭of‬ ‭an‬ ‭algorithm's‬ ‭performance.‬ ‭Here‬ ‭is‬ ‭a‬ ‭comparison‬ ‭between‬ ‭Time‬
‭Complexity and Space Complexity:‬

‭Time Complexity:‬

‭‬
● ‭ easures the amount of time an algorithm takes to run.‬
M
‭●‬ ‭It focuses on analyzing the growth rate of the running time as the input size increases.‬
‭●‬ ‭Time complexity is denoted using Big O notation.‬
‭●‬ ‭The‬‭time‬‭complexity‬‭is‬‭determined‬‭by‬‭analyzing‬‭the‬‭number‬‭of‬‭operations‬‭performed‬‭by‬‭the‬
‭algorithm.‬
‭Notes for I - B.Tech(CSM) - A‬ ‭11‬
‭●‬ I‭ t‬ ‭considers‬ ‭the‬ ‭worst-case‬ ‭scenario,‬ ‭indicating‬ ‭the‬ ‭maximum‬ ‭time‬ ‭required‬ ‭for‬ ‭any‬‭input‬
‭size.‬
‭●‬ ‭Optimizing time complexity aims to reduce the overall running time of the algorithm.‬
‭●‬ ‭Time‬ ‭complexity‬ ‭is‬ ‭crucial‬ ‭for‬ ‭optimizing‬ ‭algorithm‬ ‭efficiency,‬ ‭especially‬ ‭for‬ ‭large‬ ‭input‬
‭sizes.‬

‭Space Complexity:‬

‭‬
● ‭ stimates the amount of memory space an algorithm requires.‬
E
‭●‬ ‭It focuses on analyzing the growth rate of the memory usage as the input size increases.‬
‭●‬ ‭Space complexity is also denoted using Big O notation.‬
‭●‬ ‭The‬ ‭space‬ ‭complexity‬ ‭considers‬ ‭the‬ ‭memory‬ ‭used‬ ‭by‬ ‭variables,‬ ‭inputs,‬ ‭outputs,‬ ‭and‬ ‭any‬
‭auxiliary data structures.‬
‭ ‬ ‭It‬ ‭provides‬ ‭an‬ ‭understanding‬ ‭of‬ ‭how‬ ‭the‬ ‭algorithm's‬ ‭memory‬ ‭requirements‬ ‭scale‬ ‭with‬

‭increasing input size.‬
‭●‬ ‭Optimizing space complexity aims to minimize the memory usage of the algorithm.‬
‭●‬ ‭Space‬ ‭complexity‬ ‭is‬ ‭important‬ ‭for‬ ‭optimizing‬ ‭memory‬ ‭efficiency,‬ ‭especially‬ ‭in‬
‭resource-constrained environments.‬

‭Key Differences:‬

‭●‬ M ‭ easure:‬ ‭Time‬ ‭complexity‬ ‭measures‬ ‭the‬ ‭running‬ ‭time‬ ‭of‬ ‭an‬ ‭algorithm,‬ ‭while‬ ‭space‬
‭complexity estimates the memory space required.‬
‭●‬ ‭Growth‬ ‭Rate:‬ ‭Time‬ ‭complexity‬ ‭analyzes‬ ‭the‬ ‭growth‬ ‭rate‬ ‭of‬ ‭running‬ ‭time,‬ ‭while‬ ‭space‬
‭complexity analyzes the growth rate of memory usage.‬
‭●‬ ‭Notation:‬‭Both complexities are expressed using Big O notation.‬
‭●‬ ‭Analysis:‬ ‭Time‬ ‭complexity‬ ‭considers‬ ‭the‬ ‭number‬ ‭of‬ ‭operations‬ ‭performed,‬ ‭while‬ ‭space‬
‭complexity considers the memory used by variables and data structures.‬
‭●‬ ‭Worst-case‬ ‭Scenario:‬ ‭Time‬ ‭complexity‬ ‭analyzes‬ ‭the‬ ‭worst-case‬ ‭scenario,‬ ‭while‬ ‭space‬
‭complexity provides an estimation for all input sizes.‬
‭●‬ ‭Optimization‬ ‭Focus:‬ ‭Time‬ ‭complexity‬ ‭optimization‬ ‭aims‬ ‭to‬ ‭reduce‬ ‭running‬ ‭time,‬ ‭while‬
‭space complexity optimization aims to minimize memory usage.‬

I‭ n‬‭summary,‬‭time‬‭complexity‬‭focuses‬‭on‬‭analyzing‬‭the‬‭running‬‭time‬‭of‬‭an‬‭algorithm,‬‭while‬
‭space‬‭complexity‬‭focuses‬‭on‬‭analyzing‬‭the‬‭memory‬‭requirements.‬‭Both‬‭complexities‬‭are‬‭important‬
‭considerations‬ ‭for‬ ‭optimizing‬ ‭algorithm‬ ‭performance‬ ‭and‬ ‭efficiency.‬ ‭By‬ ‭understanding‬ ‭these‬
‭concepts, developers can design algorithms that are both time and space efficient.‬

You might also like