Professional Documents
Culture Documents
Introduction To Problem Solving: Notes For I - B.Tech (CSM) - A
Introduction To Problem Solving: Notes For I - B.Tech (CSM) - A
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.
Computersareusedforsolvingvariousday-to-dayproblemsandthusproblemsolvingisan
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,problemsolvingistheprocessofidentifyingaproblem,developinganalgorithmfor
the identified problem and finally implementing the algorithm to develop a computer program.
upposewhiledriving,avehiclestartsmakingastrangenoise.Wemightnotknowhowto
S
solvetheproblemrightaway.First,weneedtoidentifywherethenoiseiscomingfrom?Incasethe
problemcannotbesolvedbyus,thenweneedtotakethevehicletoamechanic.Themechanicwill
analyzetheproblemtoidentifythesourceofthenoise,makeaplanabouttheworktobedoneand
finally repair the vehicle inordertoremovethenoise.Fromtheaboveexample,itisexplicitthat
finding the solution to a problem might consist of multiple steps.
Whenproblemsarestraightforwardandeasy,wecaneasilyfindthesolution.Butacomplex
problemrequiresamethodicalapproachtofindtherightsolution.Inotherwords,wehavetoapply
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.
3. Coding
After finalizingthealgorithm,weneedtoconvertthealgorithmintotheformatwhichcan
beunderstoodbythecomputertogeneratethedesiredsolution.Differenthighlevelprogramming
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.
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,itisclearthatweneedtofollowasequenceofstepstoaccomplishthetask.Sucha
finitesequenceofstepsrequiredtogetthedesiredoutputiscalledanalgorithm.Itwillleadtothe
desired resultinafiniteamountoftime,iffollowedcorrectly.Algorithmhasadefinitebeginning
and a definite end, and consists of a finite number of steps.
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.
REPRESENTATION OF ALGORITHMS
sing their algorithmic thinkingskills,thesoftwaredesignersorprogrammersanalysethe
U
problemandidentifythelogicalstepsthatneedtobefollowedtoreachasolution.Oncethesteps
are identified, the need is to write down these steps along with the required input and desired
output.Therearetwocommonmethodsofrepresentinganalgorithm—flowchartandpseudocode.
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
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 sumwiththevalue5(sincewe
find average of 5 numbers)
● Output: Average of five numbers
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)isanotherwayofrepresentinganalgorithm.It
A
is considered as a non-formal language that helps programmers to write algorithm.
Itisadetaileddescriptionofinstructionsthatacomputermustfollowinaparticularorder.
It is intended for human reading and cannot be executed directly by the computer.
Nospecificstandardforwritingapseudocodeexists.Theword“pseudo”means“notreal,”
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.
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.
Note: Draw the flowchart on your own as a practice with reference to above flowchart notes
Top-Down Approach:
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:
Inthisdesign,individualpartsofthesystemarespecifiedindetail.Thepartsarelinkedto
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.
Solvingsub-problemsfirstandthenusingthesolutionstobuilduptoasolutionofalarger
problem.
Advantage:
● Makedecisionsaboutreusablelow-levelutilitiesthendecidehowtherewillbeputtogether
to create high-level construct.
ote:Bothapproacheshavetheirownadvantagesanddisadvantagesandthechoicebetweenthem
N
often depends on the specific problem being solved.
4 I n this the communications is less among In this module must have communication.
modules.
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.
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
● 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.
enerally,thereisalwaysmorethanonewaytosolveaproblemincomputersciencewith
G
different algorithms. Therefore, it is highly required to use a methodtocomparethesolutionsin
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:
Thetimecomplexityofanalgorithmquantifiestheamountoftimetakenbyanalgorithmto
runasafunctionofthelengthoftheinput.Notethatthetimetorunisafunctionofthelengthof
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 oftimeforexecution.Thetimerequiredbythe
algorithm to solve given problemiscalledtimecomplexityofthealgorithm.Timecomplexityis
very useful measure in algorithm analysis.
I tisthetimeneededforthecompletionofanalgorithm.Toestimatethetimecomplexity,weneed
to consider the cost of each fundamental instruction and the number of times the instruction is
executed.
I nordertocalculatetimecomplexityonanalgorithm,itisassumedthataconstanttimecistaken
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
whiletheprogramisinexecution.Theamountofmemoryrequiredbythealgorithmtosolvegiven
problem is calledspace complexityof the algorithm.
hespacecomplexityofanalgorithmquantifiestheamountofspacetakenbyanalgorithm
T
to run as a function of the length of the input.
( 1) A fixed part: It is independent of the input size. It includes memoryforinstructions(code),
constants, variables, etc.
( 2) A variable part: It is dependent on the input size. It includes memory for recursion stack,
referenced variables, etc.
headditionoftwoscalarnumbersrequiresoneextramemorylocationtoholdtheresult.Thusthe
T
space complexity of this algorithm is constant, hence S(n) = O(1).
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.
● Thetimecomplexityisdeterminedbyanalyzingthenumberofoperationsperformedbythe
algorithm.
Notes for I - B.Tech(CSM) - A 11
● I t considers the worst-case scenario, indicating the maximum time required for anyinput
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 nsummary,timecomplexityfocusesonanalyzingtherunningtimeofanalgorithm,while
spacecomplexityfocusesonanalyzingthememoryrequirements.Bothcomplexitiesareimportant
considerations for optimizing algorithm performance and efficiency. By understanding these
concepts, developers can design algorithms that are both time and space efficient.