CSE1021- Problem Solving and
Programming
Unit 1- Introduction to Computer Problem Solving
[Link] R
1
Contents
1. Problem Solving Aspects
2. Top Down Design
3. Problem Solving Strategies
4. Programs and Algorithms
5. Algorithms
6. Flowcharts
7. Algorithms and Flowcharts – Samples
8. Analysis of Algorithms
2
1. Problem Solving Aspect
3
Problem Solving Aspect
Working General
Problem Getting Use of Similarities
backwards Problem
Definition Started on Specific among
from the Solving
Phase a Problem Examples Problems
solution Strategies
4
Problem Definition Phase
5
Problem Definition Phase
Success in solving any problem comes from how to understand the
problem in a better way
What must be done rather than how to do it
Try to extract from the problem statement
A set of precisely defined tasks
From the definitions, we are led in a natural way to algorithm designs
6
Problem Definition Phase – General
What are you asked to find or show?
Can you restate the problem in your own words?
Can you think of a picture or a diagram that might help you understand the
problem?
Is there enough information to enable you to find a solution?
Do you understand all the words used in stating the problem?
Do you need to ask a question to get the answer?
7
Problem Definition Phase – Programming
Can I restate the problem in my own words?
What are the inputs that go into the problem?
What are the outputs that should come from the solution to the
problem?
Can the outputs be determined from the inputs? In other words, do I
have enough information to solve the problem?
How should I label the important pieces of data that are a part of the
problem?
8
An Example - Write a function which takes two
numbers and returns their sum.
Can I restate the problem in my own words? Maybe something like "Write a function that adds two
numbers," or "Implement addition."
What are the inputs that go into the problem? Two numbers.
This might seem like an open and shut case, but even here it's worth thinking about the structure
of the inputs.
For instance, are the numbers integers, or are floats allowed too? Should the inputs even be
numbers at all, or strings representing numbers? If the latter, the problem suddenly becomes much
harder, because you can represent arbitrarily large numbers with strings. For example,
9
An Example - Write a function which takes two numbers and returns
their sum.
100000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000 +
1000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000
is a perfectly valid mathematical sum, but if you try to add those numbers directly in C you'll get an
output of Infinity, since C can't deal with numbers of this size.
It's also worth asking whether the input always consists of just two numbers, or whether it's possible
there could be more inputs (or fewer).
10
An Example - Write a function which takes two
numbers and returns their sum.
What are the outputs that should come from the solution to the problem? One number,
representing the sum of the inputs. Here too, it's worth thinking about type: should the output
be a number, a string, or something else?
Can the outputs be determined from the inputs? It seems that way, though the answer depends a
bit on the answer to 2. For instance, if it's possible that there could be fewer than two inputs, it's
not necessarily clear what the output should be if the user doesn't pass in any arguments at all.
(Note how we're already finding examples of edge cases, before we've started writing any code!)
How should I label the important pieces of data that are a part of the problem? This is a matter of
personal preference, but it seems like there are four things that could use labels: the function,
the two inputs, and the output. Names like add, num1, num2, and sum seem suitably
descriptive.
11
Getting Started on a problem
• Many ways to solve most problems
• Many solutions to most problems
• A block often occurs at his point because people become concerned
with details of the implementation before they have completely
understood or worked out an implementation – independent solution
• The best advice here is not to be too concerned about detail.
• The sooner you start coding your program the longer it is going to
take
12
The use of Specific Examples
• A Useful strategy when we are stuck is to use some props or
heuristics (i.e., rules of thumb) to try to get a start with the problem
• E.g. if you want to find the maximum / minimum in a set of numbers, Choose
a particular set of numbers and work out the mechanism for finding the
maximum / minimum in this set.
13
The use of Specific Examples
• It is usually much easier to work out the details of a solution to a
specific problems is more clearly defined.
• A specific problem often forces us to focus on details that are not so
apparent when the problem is considered abstractly.
• Geometrical or schematic diagrams representing certain aspects of
the problem can be usefully employed in many instances
• It is very easy to fall into the trap of thinking that the solution to a
specific problem or a specific class of problems is also a solution to
the general problem.
14
Similarities among Problems
• Try to do with as much past experience as possible to bear o the
current problem
• If there are any similarities between the current problem and other
problems that we have solved or we have been solved.
• Make an effort to be aware of the similarities among problems.
• The more experience one has the more tools and techniques one can
bring to bear in talking a given problem.
• A classis case of experience blocking progress was Einstein’s discovery
of relativity.
• Newton’s theory was correct and that was all there was to it.
15
Similarities among Problems
• In trying to get a better solution to a problem, sometimes too much
study of the existing solution or a similar problem forces us down the
same reasoning path (which may not be the best) and to the same
dead end.
• In trying to get a better solution to a problem, it is usually wise, in the
first instance at least, to try to independently solve the problem.
• A skill that it is important to try to develop in problem solving is the
ability to view problem from a variety of angles
• One must be able to metaphorically turn a problem upside down,
inside out, sideways, backwards, forwards and so on.
• Once one has developed this skill it should be possible to get started
on any problem.
16
Working backwards from the solution
17
Working backwards from the solution
• We do not know where to start on a problem
• We already have the solution to the problem an then try to work
backwards to the starting conditions.
• Even a guess at the solution to the problem may be enough to give us
a foothold to start on the problem.
• Another practice that helps us develop our problem solving skills is,
once we have solved a problem, to consciously reflect back on the
way we went about discovering the solution.
• This can help us significantly – “We learn most when we have to
invent”
18
General problem – solving strategies
19
2. Top-Down Design
20
Top – Down Design
Breaking the problem into Sub
problems
Choice of Suitable Data Structure
Construction of the loops
Establishing Initial Conditions for the
loops
Finding the Iterative Construct
Termination of the loops
21
Breaking the problem into Sub problems
Split the bunch
of sticks in to
individuals and
break them one
by one
22
Lion Vs Cows
[Link] 23
Lion Vs Cows
Once upon a time, there lived four cows in the forest. Every day, they used to graze together in a
particular place.
One day a lion passed that way and saw the four cows. The lion went near the cows.
When the cows saw the lion coming near to them, all of them fought together against the lion.
The Lion ran away.
After some days, the cows quarreled with each other and began to graze in different directions and
all alone.
This was noticed by the lion. The lion thought that its turn had come and came there.
The lion killed all the four cows one by one.
Moral : Break the group of problems and Deal with them individually.
24
Choice of Suitable Data Structure
[Link]
25
Librarian Vs Books
• There is one library which contains many book racks.
• Librarian need to find individual racks to keep the books in classified
manner
• Stories, Mathematics, Bio-Science, Engineering, Medical etc.
• Books need to be numbered and racks must have name
• It helps to viewers to find the appropriate book (data) easily with less
searching time.
• Librarian can easily maintain issue and return records (push and pop)
Moral : Keep the data in efficient format and structure to deal with it.
26
Construction of Loops
27
6 Circuit Labyrinth
• One Entry point
• Six iterations
• Know the strategies to find exit
• If it not works you may fail
• You may be trapped inside loop
Have you seen any movies regarding this ?
Or any stories on Labyrinth ?
Its function is to hold the Minotaur, the monster eventually killed by the hero
Theseus. Daedalus had so cunningly made the Labyrinth that he could barely escape
it after he built it.
Moral : How to enter and exit using predefined logics
28
Establishing Initial Conditions for the Loops
29
Finding the Iterative Constructs
30
Termination of the Loops
[Link] [Link]
31
3. Problem Solving Strategies
32
Problem Solving (Algorithm) Techniques
1. Brute-force or exhaustive search
2. Divide and Conquer
3. Greedy Algorithms
4. Dynamic Programming
5. Branch and Bound Algorithm
6. Randomized Algorithm
7. Backtracking
33
1. Brute-Force
• Simple, Straight Forward
• Example: Padlock with 4 Digits, Exhaustive Search of Lost Articles in
Home.
34
2. Divide and Conquer
• Problem divided into sub problems, sub problems are solved
independently and merge the solutions of sub problem for final
solution of the problem.
• Example: Counting the coins by grouping them into denomination.
35
3. Greedy Algorithm
• Decisions are taken based on Local Optimal Solution at every stage.
• Travelling from Kanyakumari to Kashmir by visiting all the states
capital.
36
4. Dynamic Programming
• Mathematical Optimization Problem
• Breaks the problem into sub problems, solve the sub problems over
time as sequence to reach the final solution.
• Uses Recursion
• Example: Arranging the cards for rummy game.
37
5. Branch and Bound – Combinatorial
Optimization
38
6. Randomized Algorithm
• Random Numbers for Decision Making & Optimization.
• Accidently Good
• Feedback Assessments, Surprise Checks, etc.
39
7. Back Tracking
• Incremental Approach
• Uses the previous stage solution to next stage solution
• Examples: Crossword
40
3. Programs and Algorithms
41
Programs and Algorithms
The Vehicle for the computer Solution in a problem is a set of explicit and
unambiguous instructions expressed in a programming language.
This set of instructions is called a Program.
A Program may also be thought of as an algorithm expressed in a programming
language
An Algorithm therefore corresponds to a solution to a problem that is independent
of any programming language.
42
43
Problem 1
A farmer wants to cross a river and take with him a wolf, a goat, and a cabbage.
There is a boat that can fit himself plus either the wolf, the goat, or the cabbage.
If the wolf and the goat are alone on one shore, the wolf will eat the goat.
If the goat and the cabbage are alone on the shore, the goat will eat the cabbage.
How can the farmer bring the wolf, the goat, and the cabbage across the river?
[Link]
44
Solution – 1
Step 1: Farmer takes Goat across the river (leaving Wolf and Cabbage behind)
Step 2: Farmer returns alone
Step 3: Farmer takes Wolf across the river
Step 4: Farmer returns with Goat
Step 5: Farmer takes Cabbage across the river
Step 6: Farmer returns alone
Step 7: Farmer takes Goat across the river
45
46
[Link]
Problem - 2
There are 4 men who want to cross a bridge. They all begin on the same side. You have 17 minutes
to get all of them across to the other side. It is night. There is one flashlight. A maximum of two
people can cross at one time.
Any party who crosses, either 1 or 2 people, must have the flashlight with them. The flashlight must
be walked back and forth, it cannot be thrown etc.
Each man walks at a different speed. A pair must walk together at a rate of the slower man's pace.
Man 1: 1 minute to cross, Man 2: 2 minutes to cross., Man 3: 5 minutes to cross., Man 4: 10
minutes to cross.
For example, if Man 1 and Man 4 walk across first, 10 minutes have elapsed when they get to the
other side of the bridge. If Man 4 returns with the flashlight, a total of 20 minutes have passed, and
you have failed the mission.
[Link]
Solution - 2
Step 1: Men 1 and 2 travel over - Time Elapsed 15
Step 2: Man 2 brings back the flashlight – Time Elapsed 13
Step 3: Men 3 and 4 travel over and – Time Elapsed 3
Step 4: Man 1 brings back the flashlight – Time Elapsed 2
Step 5: Men 1 and 2 travel over – Time Elapsed 0
48
49
5. Algorithms
50
Algorithm – Introduction
• An algorithm describes the step by step action to solve a problem.
• An algorithm has a well defined sequence of steps, it gives you an output,
and it will eventually terminate.
• An algorithm is a self-contained step-by-step set of operations to be
performed to solve a specific problem or a class of problems.
• It finds a relationship between Input and Output.
• Algorithm is for Computational Problem.
• Algorithm is independent of programming language
• Father of Algorithm : 9th-century mathematician Muḥammad ibn Mūsā al-
Khwārizmī
51
Properties of an Algorithm
• Finiteness. An algorithm must always terminate after a finite number of
steps.
• Definiteness. Each step of an algorithm must be precisely defined; the
actions to be carried out must be rigorously and unambiguously specified
for each case.
• Input. An algorithm has zero or more inputs, i.e, quantities which are given
to it initially before the algorithm begins.
• Output. An algorithm has one or more outputs i.e, quantities which have a
specified relation to the inputs.
• Effectiveness. An algorithm is also generally expected to be effective. This
means that all of the operations to be performed in the algorithm must be
sufficiently basic that they can in principle be done exactly and in a finite
length of time.
52
Complexity of Algorithm
• 1. Time Complexity: Running time of the program as a function of the
size of input
• 2. Space Complexity: Amount of computer memory required during
the program execution, as a function of the input size.
53
Components and Flow of an Algorithm
• Components : Input, Output, Set of Operations(Process).
• Flows: Sequence, Branching, Looping and Recursion.
54
Activity: Search, Find & Present
• Search online to find any one algorithm to showcase the following.
• Sequencing
• Branching
• Looping
• Recursion
55
Algorithm Vs. Pseudo Code Vs. Flowchart Vs.
Program
• Algorithm
• Set of step-by-step instructions that perform a specific task or operation
• Natural language, NOT programming language
• Pseudocode
• Set of instructions that mimic programming language instructions
• Flowchart
• Visual program design tool
• Semantic symbols describe operations to be performed
• Program
• Implementation of Algorithm in Programming Language
56
Example : Algorithm – Linear Search
[Link] from the leftmost element of arr[] and one by one compare x with each
element of arr[].
1.2. If x matches with an element, return the index.
2.3. If x doesn’t match with any of elements, return -1.
57
Example : Pseudo Code – Linear Search
FUNCTION linearSearch(list, searchTerm):
FOR index FROM 0 -> length(list):
IF list[index] == searchTerm THEN
RETURN index
ENDIF
ENDLOOP
RETURN -1
END FUNCTION
58
Example : Flowchart– Linear Search
59
Flowchart Symbols
60
61
Complexity Analysis of Linear Search
• Array Size : n
• Parameter for evaluation: Number of Comparison
• Best Case: If element to be searched is always first element of an
array. O(1)
• Worst Case: If element to be searched is always last element of an
array or not present. O(n)
62
6. Flowcharts
63
The Flowchart
• (Dictionary) A schematic representation of
a sequence of operations, as in a
manufacturing process or computer
program.
• (Technical) A graphical representation of
the sequence of operations in an
information system or program.
• Information system flowcharts show how data
flows from source documents through the
computer to final distribution to users.
• Program flowcharts show the sequence of
instructions in a single program or subroutine.
Different symbols are used to draw each type
of flowchart.
64
The Flowchart
• Flowchart Symbols
Name Symbol Use in Flowchart
Flowchart
Oval Denotes the beginning or end of the program
Parallelogram Denotes an input operation shows logic of an
algorithm
Rectangle Denotes a process to be carried out
e.g. addition, subtraction, division etc.
emphasizes
individual steps
Diamond Denotes a decision (or branch) to be made. and their
The program should continue along one of interconnections
two routes. (e.g. IF/THEN/ELSE)
Hybrid
e.g. control flow
Denotes an output operation
from one action to
the next
Flow line Denotes the direction of logic flow in the program
65
The Flowchart: Example
START
Input
M1,M2,M3,M4
• Step 1: Input M1,M2,M3,M4
• Step 2: GRADE
GRADE(M1+M2+M3+M4)/4
(M1+M2+M3+M4)/4
• Step 3: if (GRADE <50) then
• Print “FAIL” N IS Y
• else GRADE<50
• Print “PASS”
• endif PRINT PRINT
“PASS” “FAIL”
STOP
66
The Flowchart: Example
START
• Write an algorithm and draw a
flowchart that will read the two sides
Input
of a rectangle and calculate its area. W, L
Pseudo code ALxW
• Input the width (W) and Length (L) of a
rectangle
Print
• Calculate the area (A) by multiplying L A
with W
• Print A
STOP
67
The Flowchart
Flowcharts is a graph used to depict or show a step by step
solution using symbols which represent a task.
The symbols used consist of geometrical shapes that are
connected by flow lines.
It is an alternative to pseudo coding; whereas a pseudocode
description is verbal, a flowchart is graphical in nature
68
The Flowchart Rules
• sequence control structure • selection control structure
Statement 1 No Yes
Condition
Statement 2
else- then-
statement(s) statement(s)
Statement 3
69
The Flowchart Rules
• repetition control structure
yes Loop
Condition
Statement(s)
no
70
7. Algorithms and Flowcharts – Samples
71
72
73
74
75
76
8. Analysis of Algorithms
77
Complexity Analysis of Algorithms
• Time Complexity
• Space Complexity
78
Posteriori Analysis Vs. Priori Analysis
79
Asymptotic Notations
• Performance of an Algorithm: Time, Space and other Resources.
• An algorithm may not have the same performance for different types
of inputs. With the increase in the input size, the performance will
change.
• The study of change in performance of the algorithm with the change
in the order of the input size is defined as asymptotic analysis
80
Big-O Notation (O-notation) – Worst Case-Upper
Limit
81
Omega Notation (Ω-notation) – Lower Bound-
Best Case
82
Theta Notation (Θ-notation) – Upper & Lower
Bound
83
84
85
86
Little o (o) and Little Omega (ω) – Loose Upper &
Lower Bounds
87
Examples for Little o (o) and Little Omega (ω)
• f(n) =7n+8 ∈ o(n2)
• f(n) =4n+6 ∈ ω(1)
88
Linear Search – Time Complexity Analysis
• Best Case is O(1)
• Worst Case is O(n)
Factorial – Time Complexity
• O(n)
89
Issues and Approaches
• Issues
Correctness
Time efficiency
Space efficiency
Optimality
• Approaches
Theoretical analysis
Empirical analysis
90
Measuring an Input’s Size
• Time efficiency : the number of repetitions of the basic operation as a
function of input size
• Input size is influenced by
the data representation, e.g. matrix
the operations of the algorithm, e.g. spell-checker
the properties of the objects in the problem, e.g. checking if a given integer is
a prime number
91
Examples
Problem Size of input
Find x in an array The number of the elements in
the array
Multiply two matrices The dimensions of the matrices
Sort an array The number of elements in the
array
Traverse a binary tree The number of nodes
Solve a system of linear The number of equations, or the
equations number of the unknowns, or
both
92
Units for Measuring Running Time
• Using standard time units is not appropriate
• Counting all operations in an algorithm is not appropriate
• The Approach: Identify and count the basic operation(s) in the
algorithm
93
Basic Operations
• Applied to all input items in order to carry out the algorithm
• Contribute most towards the running time of the algorithm
94
Examples
Problem Operation
Find x in an array Comparison of x with an entry
in the array
Multiplying two matrices with Multiplication of two real
real entries numbers
Sort an array of numbers Comparison of two array
entries plus moving elements
in the array
Traverse a tree Traverse an edge
95
Recursive Algorithms
• Function which calls itself is called as Recursion.
• Components
• Base Case (i.e., when to stop)
• Work toward Base Case (Convergence)
• Recursive Call (i.e., call ourselves)
96
Example1: Adding 3 Numbers
97
Example 2: 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 The mission is to move all the disks to some another
variations of the puzzle where the tower without violating the sequence of arrangement. A
number of disks increase, but the few rules to be followed for Tower of Hanoi are −
tower count remains the same. •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.
98
99
Towers of Hanoi – Algorithm
100
Mathematical Analysis of Non-recursive
Algorithms
Steps in mathematical analysis of non-recursive
algorithms
• Decide on parameter n indicating input size
• Identify algorithm’s basic operation
• Determine worst, average, and best case for input of size n
• Set up summation for C(n) reflecting algorithm’s loop structure
• Simplify summation using standard formulas
101
Example: Selection Sort
• Input: An array A[0..n-1]
• Output: Array A[0..n-1] sorted in ascending order
for i 0 to n-2 do
min i
for j = i + 1 to n – 1 do
if A[j] < A[min]
min j
swap A[i] and A[min]
102
Example: Selection Sort-Analysis
• Basic operation: comparison
Inner loop:
n-1
S(i) = 1 = (n-1) – (i + 1) + 1 = n – 1 – i
j = i+1
Outer loop:
n-2 n-2 n-2 n-2
C(n) = S(i) = (n – 1 – i) = (n – 1) – i
i=0 i=0 i=0 i=0
n
Basic formula: i = n(n+1) / 2
i=0
C(n) = (n – 1 )(n -1 ) – (n-2)(n-1)/2 = (n – 1) [2(n – 1) – (n – 2)] / 2 =
= (n – 1) n / 2 = O(n2)
103
Mathematical Analysis of Recursive
Algorithms
Steps in mathematical analysis of recursive
algorithms
• Decide on parameter n indicating input size
• Identify algorithm’s basic operation
• Determine worst, average, and best case for input of size n
• Set up a recurrence relation and initial condition(s)
• Solve the recurrence to obtain a closed form or estimate the
order of magnitude of the solution
104
Example 1: Factorial
105
Thank you
106