Algorithmic Thinking and Problem Solving
Algorithmic Thinking
What will I learn?
You will be able to ▸ identify, define and analyze a problem;
▸ apply basic algorithmic thinking to solve different types of problems
Defining and stating a problem
A problem is a task to be performed. Asking some basic questions about a problem can help
define the problem. Some pertinent questions to ask are:
1. What is the problem?
2. Who has the problem?
3. What is causing the problem?
4. What are the goals?
Imp
5. What actions should be avoided?
6. Which actions are admissible?
A problem is understood and solved using available facts or data. This data is, entered in the
algorithm as input which is processed into an outcome, called the output. A problem therefore,
consists of inputs and matching outputs.
Let's define and state a real-life problem, using the above method.
Suppose your classmate is arriving late to school everyday, due to which he/she is having
difficulties in understanding the lessons. He/she has also received warning from the
headmistress. You can ask questions from your friend to understand the problem. Some
questions and their possible responses are given on the table.
66
Imp
Responses
Questions
what a the problem?
Arriving late to school
who has the problem?
what is the reason for the problem?
what are the goals?
her are the side effects to be avoided?
To arrive school on time.
Waking up late in the moming
Arsolon, my classmate has this problem
Lack of sleep in the moming is to be avoided
shutacions are admissible?
Sleeping early
wed on these responses, the problem can be defined as following:
yfiend has a problem of arriving late to school, due to which he/she is facing learning fficulties
Problem statement: "My friend Arsalan, who is my classmate, needs to have a proper cutine to
sleep early because he is facing learning difficulties due to his habit of sleeping re at night and
getting up late in the morning.
Top Tip
Think of a problem you or your friends face and frame a problem statement. You could use the
flowing template:
(Pactice)
Sajida (user) is a/an Filend wake earl early (user's needs) because (user characteristics who
needs to she is La thinsightly
Simple and complex problems
impie problems can be solved using a straightforward algorithm. Recipes and sorting jects are
examples of simple problems. However(creating an online map is a complex problem because
numerous factors will have to be considered, such as traffic situation, th hours, road blockages,
etc. Similarly, facial recognition is a complex problem because dependent on several underlying
factors such as light, changes in the expression of Deface, etc. Computational thinking is great
approach for solving all types of problems.
Examples of Simple problems:
Finding a specific book in a library
Remembering a phone number
Making a sandwich
SKFORD
Examples of Complex problems:
• Planning a city's transportation infrastructure
• Developing a new drug to treat a disease
• Controlling global warming
67
4 Algorithmic Thinking
22 Jan, Monday
Computational Thinking (CT)
CT is essentially a framework to describe a set of critical thinking and problem- solving skill CT
may be considered a precursor to learning actual coding or computer programming skills.
Computational thinking involves solving problems, designing systems, and understanding
human behaviour, by drawing on the concepts fundamental to computer science
It has the following characteristics:
• Formulating problems in a way that enables us to use a computer and other tools to help solve
them.
Logically organizing and analyzing data.
Representing data through abstractions which means removing unnecessary details to simplify
the process.
Automating solutions through algorithmic thinking (a series of ordered steps).
• Identifying, analyzing, and implementing possible solutions with the goal of achieving the most
efficient and effective combination of steps and resources.
Generalizing and transferring this problem-solving process to a wide variety of problems.
Computational thinking enables people to create technology, rather than just consuming
technology. It gives them the confidence and the persistence in working with difficult
problems. readandwite/Learm
Algorithm A
An algorithm is a precise, step-by step process to solve a problem. A problem can be solved by
using different algorithms.
By definition, something can only be called an algorithm, if it has all of the following properties.
1. It must be correct. In other words, it must compute the desired function, converting each input
to the correct output.
Neil earning
2. It is composed of a series of concrete steps. Concrete means that the action described by
that step is completely understood and doable by the person or machine that must perform the
algorithm. For example, the steps of a cookie recipe in a cookbook might be considered
sufficiently concrete for instructing a human cook, but cannot be used for programming an
automated cookie-making factory.
68
Did you Know?
Ada Lovelace, an English mathematician, wrote the first algorithm for a machine in the 1800s
and is considered the first computer programmer. However, most historians believe that the
Babylonian clay tablets (1600-1800 800 are the world's first known algorithm. The Babylonion
had developed a numerical system using cuneiform numerals to count and they preserved
those calculations on tablets.
next. The instructions hould be specific because non-specific instructions are vague and will not
help in difying the next steps. Selection (e.g. the 'if statement') is normally a part of any Inquage
for describing algorithms. Selection allows a choice for which step will be performed next.
must be composed of a finite number of steps. Most languages for describing arithms including
English and "pseudocode") provide some way to perform epeated actions, known as iteration.
Examples of iteration in programming languages clude the while loop' and 'for loop' constructs.
Iteration allows for short descriptions, with the number of steps actually performed controlled by
the input. must terminate. In other words, it may not go into an infinite loop.
What is an Algorithm?
Set of rules expected output from the given output
Algorithm
▲Fig. 4.2 Flowchart of an algorithm
Input
Output
Algorithmic thinking
panithmic thinking is a systematic way of thinking through problems and finding nitions in a way
similar to how a computer would solve a problem. comprises of the following steps:
1. Decomposition or deconstruction: Breaking down data, processes, or problems into
moller, manageable parts. There are different techniques to deconstruct a problem. A limple
problem could be subdivided into smaller parts that do not need to be further hdivided.
However, complex problems are first deconstructed into broader groups and then sub divided
into further smaller parts to understand the problem. Pattern Recognition: This pertains to
observing patterns, trends, and regularities in
data. Similar patterns could be classified together. Abstraction: patterns could be close
understandable by reducing unnecessary detall Algorithm Design: Developing the step-by-step
instructions for solving a certain
problem and simitar problems. Evaluation: Ensuring that your solution is efficient.
FORD
69
ondy 2a Jan24
Algorithmic Thinking
Finding the most efficient algorithm
The advantage of developing more than one solution to a problem is that there would be
multiple ways to solve a problem and to select the best solution,
Solution A might be more efficient than Solution B for a specific variation of the problem, or for a
specific class of inputs to the problem, while solution B might be more efficient than A for
another variation or class of inputs.
An algorithm might work for sorting a small collection of integers (which is important if you need
to do this many times); while another algorithm might work for sorting a large collection of
integers. A third might be the best for sorting a collection of variable-length strings
Designing an algorithm
(whole)
Computers rely heavily on algorithms to do useful work. Computers can neither function with
Intuitive knowledge, nor do they have their own intelligence to make decisions. They depend on
humans to provide them with the "thought pattern" or the "method" of doing work.
An algorithm should contain all the necessary inputs, outputs, instructions, decisions, etc. to
solve the problem. When designing an algorithm, consider more than one way of solving the
problem. The following aspects should be taken into account.
the big picture-What is the final goal?
⚫ the individual stages - What hurdles need to be overcome on the way to the goal?
The most important step is to analyze the problem very carefully.
Identify the functions that the solution (algorithm) should have.
Identify the required output.
Find a way to produce the required output.
Draw a proper relationship between the input and output.
Use logical reasoning
s of algorithmic thinking (whole) Benefits
1. Algorithms are a set of rules and by following these rules, one can solve a problem.
Algorithmic thinking is the best approach to solve a problem. One can decompose complicated
problems into smaller components and work on them one at a time.
2. It enables people to think logically rather than irrationally.
3. It enables people to handle a large amount of data,
4. In algorithmic thinking process, one must analyse trends and patterns, which helps to develop
analytical thinking skills. 5. Algorithmic thinking enables us to arrive at different solutions to the
same problems.
This develops efficiency and optimisation skills.
70
here w
The problems
which helps
OXFORD
▲ Fig. 44 Fist step: Adding
Step 3 Boke the pizza
Step 1 Spread tomato paste on the pizza bread
Step 2 Add other ingredients (chicken, vegies, cheese)
artingolay
Let's make a Pizza
What method would be used?
Process of writing an algorithm
How much time would be required?
For solving a problem, the steps are to be written down in the order they will be followed.
To make a quick pizzo, a readymade pizza bread could be used.
ther fuci Following a recipe is an example of solving a simple problem. If you are asked to make
a pizza, some questions to be asked could be: What ingredients would be required?
ions. Tivi
doing wast
decitinge
way of w
the goa
(The process of writing an algorithm starts by breaking down the problem step-by-step.
This is also called deconstructing a problem.mp
Add mushroom
Google: Google search engine uses an algorithm, PageRank, to determine which search results
appear and in what order, ensuring the highest-quality, most credible websites appear first. This
algorithm is run every time a query is s s searched.
whole Page
Add block olives
Real life Algorithm examples:
YouTube: YouTube uses recommendation algorithm based on the users search history.
A Fig. 43 The goal is to make a pizza
a problem decompose at a time
tomata paste
Second step: Adding Ingredients
Fig.4.5
Output: Boked pizza
71
4 Algorithmic Thinking
Computers work in the same way too. The complete process starts from identifying a problem
and ends with finding a solution using an algorithm. We can implement the algorithmic thinking
in our daily life by analyzing the problems we
are facing and planning the steps to solve it. Example/Protise
Making Tea
The step-by-step process of making tea is shown below. This is an example of a simple
problem.
Step 1: Take Kettle
Step 2: Fill Water
Step 3: Tum the stove ON
Step 4: Wait till it boils
Step 5: Add tea Leafs
Step 6: Add Milk
Step 7: Wait till it boils (repeat Step 4)
Step 8: Pour into a cup
Step 9: Tea is ready
Daily routine
Think about your daily morning routine. Your actions are usually in a sequence.
Step 1: Get up
Step 2: Brush your teeth
Step 3: take a shower
Solving a sum
Step 1: Switch ON Calculator
Step 2: Press 2
Step 3: Press'+'Sign
Step 4: Press 3
Step 5: output 5
72
Consider examples 1 till 4. To solve the problem, you need to identify the ingredients and
methods, think logically how to sequence the steps and test if the desired outcome is achieved.
The evaluation of steps can either lead to confirmation or revisiting the algorithm to improve the
steps. This process will give you confidence in decision making.
Conditional statements
Selection is a question or a decision. It allows us to find more than one path through on
algorithm. This is where If else condition is used. If the water boils, add tea leaves else wait.
Let's look into our tea making process. In step 4 we have to wait for water to boil but we haven't
disclosed any time for boiling because we are not aware of depending factors like gas pressure
or the type of pan we are using. So what we will do in this case, we will wait until water boils.
We can write this condition as below: IF Water Boils then
Move to step 5
Did you Know?
The application of rules to problem solving is called Logical Reasoning
Top Tip
Else Wait
There is one condition in this algorithm that if water boils we will proceed to the next step else
we will continue the waiting process.
Logic tells us that either a rule needs to be added, or the existing rules need to be changed.
Logical operators
For logical reasoning we will use different logical operators:
We can use AND, OR, NOT logical operators as well for making our condition more powerful
which helps us in filtering conditions more effectively.
Move to step 5
Else Wait
OXFORD
Let's use these logical operator with logical reasoning. IF Water Boils then
Condition/Selection
This is a decision within a computer program when the program decides to move, based on the
results of an event.
73
Algorithmic Thinking
Loops
Number 2:
logical operators
Number 1
ELSE Give Error
Password FIELD & Then Allow
IF Password FIELD Confirm password Then Allow ELSE Give Error
Loops are a fundamentol concept in algorithms, as they allow for efficient repetition ofa set of
instructions. Repetitive tasks are common in programming, and loops are essential to save time
and ensinimize errors. Loops in Scratch are of two types.
74
So you can see in above example that there are 2 different points where we can use
A Fig. All An example of up
Now you can see that we have used sign which is an equal operator
Example
Relational Operators
Operator Symbol
Operator Name
Equal to
Greater Than
Less Than Greater Thon or
Less Than or
Ergual To
Equal To
Not Equal Te
Javatpoint
Ban Sign Ly Far
A
A Fig 47 showing logen per for algorithms
Tests whether particular value is eat eqьої та сетил хобие
Description
Tees whether the Value is equal to the other Value
Tests whether the star is greater than certain value or Testy whether the volce is less than
certain value or not Sets whether the value is greater than or equal to certain value or not
Tets whether the value in less than or equal certain velur of nat
Finite Loop
Infinite Loop
Sequence
The sequence shows the order in which the steps are to be taken. If the order is not followed,
the desired outcome will not be achieved. Consider the example of moming routine of a student.
The student follows a certain sequence every morning.
Selection
Repetition
Exercises
Objective Type Questions
I. If the correct hardware is being used
iii. If the correct software is being used
iv. Obtain a description of the problem
OXFORD
biond
75
1. Choose the correct option. (Tick all that apply.) a. What should be considered when designing
an algorithm?
1. If there is more than one way of solving the problem
In Repetition, the algorithm repeats a certain step till a certain condition is met. For example, if
you enter a wrong password to log in to your email account, you will get o prompt to try again. If
you enter a wrong password again, the same prompt would be repeated. For security reasons,
there is a limit to the number of times to enter a password
In Selection, different sets of steps can be selected. Consider the example of making tea. If the
Water boils is an example of selection.
Sequence, Selection, and Repetition
An algorithm is made of three separate building blocks: Sequence, Selection, and Repetition.
Infinite loop also called as forever loop, allows the code (steps/instructions) to run for infinite
number of times. A music playlist on repeat mode is an example of an infinite loop. Traffic lights
are another example of infinite loop because they work round the clock to control the traffic flow.
Fierite loop also called as repeat loop, allows the steps/instructions to run for a particulor
number of times. A playlist that normally ends after the last song is an example of a finite loop