Week 02 - Lecture 03 - Problem Solving and Algorithms
Week 02 - Lecture 03 - Problem Solving and Algorithms
1
Last Lecture Summary
What is Data?
Computer Program
What are Data Structures?
Basic Data Structures
Selection of Data Structures
Data Structure Operations
Abstract Data Type (ADT)
Types of Data Structures
2
Today’s Lecture
What is Problem Solving?
Introduction to Algorithms
What is an Algorithm?
Computer Algorithm and Program
Favorite vs. Efficient Algorithm
Measuring Efficiency
Running Time of an Algorithm
Analyzing an Algorithm with Simple Example
Algorithm History and Applications
3
Computer Program
• To exactly know, what is data structure? We must know:
- What is a computer program?
Computer Program Solution
Problem
4
Data Structures & Algorithms
Data Structures is about how data can be stored in
different structures
Algorithms is about how to solve different
problems, often by searching and manipulating
data structures
5
Data Structures & Algorithms
6
What is Problem Solving?
Some examples of problems:
- A farmer trying to get his fields cropped before the storm
comes.
- A student trying to improve his gpa to improve his grades.
- A man trying to push his car in the middle or road with no
petrol.
- Parents trying to make budget with increase in tuition fees
- A covid patient trying to wait for this test/medications.
- ……etc.,
7
What is Problem Solving?
A dictionary defines a problem as a question raised
for inquiry, consideration, or solution.
In mathematics, a problem is usually a situation to resolve
with well-defined mathematical principles.
Solving is defined as finding a solution for something such
as a problem.
Problem Solving is “the act of finding a solution to a
perplexing, distressing, vexing and unsettled question”.
We will focus on problem solving using computers also
called computational problem solving
8
Problem Solving using computers?
A computer can do nothing without being explicitly told
what to do. Computers are dummy devices…
It cannot specify or analyze the problem and come up with
a solution.
Steps for problem solving with computers include:
1. The entity (the client) with the problem must specify
(outline) the problem to be solved.
2. A human(the programmer) must analyze the problem
develop the instructions (the program) for solving the
problem and have the computer carry out the instructions.
9
Introduction to Algorithms
Algorithms are fundamental
10
What is an Algorithm?
Algorithm in simple words is a set of step-by-step
instructions to complete a task. For example
Task: To make a cup of tea.
Algorithm:
11
Several definitions of algorithm
An algorithm is a definite procedure for solving a problem in
finite number of steps
12
What is a Computer algorithm?
A computer algorithm is an orderly step-by-step procedure
(instructions) to the computer to solve a specific problem.
The main features of computer algorithm are:
5. It is language independent.
13
Algorithm Specification
Algorithms are often specified either in Natural Language or
Pseudo Code Specification
1. Natural Language Specification (NLS)
In this form of specification, English language words, phrases and
terminology are used to describe the algorithm important steps.
NLS highlight the algorithm design features.
14
Algorithm Specification – Cont…
2. Pseudo Code Specification (PCS)
• In this form of specification symbol and phrases of some high level
programming (a combination of English words and mathematical
symbols) are used to describe the working of an algorithm.
• It is more useful for analysis purpose.
• There is no standardized notation used for the pseudo code.
− However, the following notation is normally followed.
Procedures: Name followed by input parameters enclosed in
parentheses. For example, FINDMAX (A, n).
Assignments to variables: left arrows (←) e.g., j← k ← p
Variables comparison: symbols x ≤ y, x ≥ y, x ≠ y, x = y, x > y, x < y
Logical expressions: connectives exp1 AND exp2 , exp1 OR exp2,
NOT exp.
Computations: arithmetic symbols +, -, *, /
15
Algorithm Specification – Cont…
Exchanges (Swapping): symbol ↔ e.g., A[i] ↔ A[k]
Loops and iterations: Words for, do, repeat, while, until are used to
describe loops, such as: for ----, do ----, while ----, do ---- while, do ----
- until etc.
Conditionals: Words if and then are used to specify conditional
statements, such as: if ---- then-----else----
Block structure: Using indentation i.e., a block with in another block.
For example a block is illustrated as below.
for --- (start of outer most block)
for --- (start of next / inner block)
if --- else ---- ( start of a new block)
Array: Name followed by size in brackets, e.g., A[1..n]
16
Algorithm Specification – Cont…
Pseudo Code for displaying even and odd numbers
for i ← 1 to n
if i%2 = 0
print “even number”
else
print “odd number”
17
Computer Program – Steps
Steps involved in writing a computer program:
2. Design of solution
3. Implementation
4. Testing
5. Documentation
18
Favorite Algorithm
Favorite algorithm
19
Efficient Algorithm
Consumes lesser amount of resources while solving a
problem of size n
Memory
Time
20
Measuring Efficiency
The efficiency of an algorithm is a measure of the amount of
resources (time and memory) consumed in solving a problem of size
n.
It would seem that the most obvious way to measure the efficiency
21
Running Time of an Algorithm
Factors affecting running time:
- Nature of input
- Number of input
- Number of steps / primitive operations
22
Analyzing an Algorithm
Calculating Running time of an Algorithm
Running time is measured by number of steps / primitive operations
performed
23
Simple Example
// Input: int A[N], array of N integers
// Output: Sum of all numbers in array A
24
Simple Example – Cont…
1,2,8: Once
3: N+1 iteration
4,5,6,7: Once per each iteration
of FOR loop, i.e., N iteration
Total: 1+1+1+(N+1)+N+N+N+N = 5N + 4
The complexity function of the
algorithm is : f(N) = 5N +4
25
Growth of 5N+4
Estimated running time for different values of N:
N = 10 => 54 steps
N = 100 => 504 steps
N = 1,000 => 5004 steps
N = 1,000,000 => 5,000,004 steps
26
Algorithm History
The word algorithm is derived from the title “Khwarizmi”
27
Algorithm Applications
To solve variety of problems in the field of
Searching Algorithms
Sorting Algorithms
Strings Processing
28
Algorithm Applications
Optimization Algorithms (Shortest routes, minimum cost).
Image Processing
operations, etc.)
29
Summary
Introduction to Algorithms
What is an Algorithm?
Computer Algorithm and Program
Favorite and Efficient Algorithm
Measuring Efficiency
Running Time of an Algorithm
Analyzing an Algorithm with Simple Example
Algorithm History
Algorithm Applications
30
THANK YOU
31