Chapter 1
Chapter 1
https://en.wikipedia.org/wiki/Muhammad_ibn_Musa_al-Khwarizmi
Algorithms 1 Ch 01 – Introduction
Father of algebra
– The Compendious
Book on Calculation by
Completion and
Balancing (c. 830)
– Linear & quadratic
equations: some of the
first algorithms
Algorithms 2 Ch 01 – Introduction
2nd-23-24 1
Algorithm Analysis and Design
Algorithms 3 Ch 01 – Introduction
Notion of an Algorithm
Algorithms 4 Ch 01 – Introduction
2nd-23-24 2
Phases
Algorithms 5 Ch 01 – Introduction
What’s an Algorithm?
Algorithms 6 Ch 01 – Introduction
2nd-23-24 3
What’s an Algorithm?
Formal Definition:
Algorithms 7 Ch 01 – Introduction
Algorithm Properties
Algorithms are well-ordered.
Since an algorithm is a collection of operations or
instructions, we must know the correct order in which
to execute the instructions. If the order is unclear, we
may perform the wrong instruction or we may be
uncertain which instruction should be performed next.
This characteristic is especially important for
computers. A computer can only execute an
algorithm if it knows the exact order of steps to
perform.
Algorithms 8 Ch 01 – Introduction
2nd-23-24 4
Algorithm Properties
Algorithms have unambiguous operations. Primitive (operation)
Algorithms 9 Ch 01 – Introduction
Exercise
Which of the following are primitive operations
for a computer?
– Add x and y to get the sum z
– See whether x is greater than, equal to, or less
than y
– Sort a list of names into alphabetical order
– Factor an arbitrary integer into all of its prime
factors
– Make a cherry pie
Algorithms 10 Ch 01 – Introduction
2nd-23-24 5
Algorithm Properties
Algorithms have effectively computable operations
Each operation in an algorithm must be doable, that is, the
operation must be something that is possible to do. Suppose you
were given an algorithm for planting a garden where the first step
instructed you to remove all large stones from the soil. This
instruction may not be doable if there is a four ton rock buried just
below ground level. For computers, many mathematical operations
such as division by zero or finding the square root of a negative
number are also impossible. These operations are not effectively
computable so they cannot be used in writing algorithms.
Algorithms 11 Ch 01 – Introduction
Example
Example of not effectively computable
operations:
– Write all the real numbers between 0 and 1.
Algorithms 12 Ch 01 – Introduction
2nd-23-24 6
Algorithm Properties
Algorithms 13 Ch 01 – Introduction
Algorithm Properties
It halts in a finite amount of time.
– Infinite loop
o The algorithm has no provisions to terminate
o A common error in the designing of algorithms
o Do not confuse, "not finite" with "very, very large".
Algorithms 14 Ch 01 – Introduction
2nd-23-24 7
Example
Q : Is the following loop “finite”, “infinite”, or “very,
very large”?
Algorithms 15 Ch 01 – Introduction
Operations in an Algorithm
Sequential operations.
A sequential operation carries out a single well-defined task. When that task is
finished, the algorithm moves on to the next operation.
Conditional operation.
A conditional operation is the “question-asking” instructions of an algorithm.
It asks a question and then select the next operation to be executed according
to the question answer
Iterative operations.
An Iterative operation is a “looping” instruction of an algorithm. It tells us not to go
on to the next instruction, but, instead, to go back and repeat the execution of a
pervious block of instructions
Algorithms 16 Ch 01 – Introduction
2nd-23-24 8
Why We Study Algorithms?
The programming process is a complicated one. You
must first understand the program specifications, of
course, and then you need to organize your thoughts
and create the program. This is a difficult task when the
program is not trivial (i.e. easy). You must break the
main tasks that must be accomplished into smaller ones
in order to be able to eventually write fully developed
code. Writing an algorithm WILL save you time later
during the construction & and testing phase of a
program's development.
Algorithms 17 Ch 01 – Introduction
Problem (TSP)
Suggest a solution (an algorithm) for the
following problem:
Algorithms 18 Ch 01 – Introduction
2nd-23-24 9
Brute – Force Technique
https://tspvprint()is.com/
Algorithms 19 Ch 01 – Introduction
Algorithms 20 Ch 01 – Introduction
2nd-23-24 10
Exercise
Write an algorithm to find the largest number in an
(unsorted) list of numbers.
Algorithms 21 Ch 01 – Introduction
Representing
Algorithm
Algorithms 22 Ch 01 – Introduction
2nd-23-24 11
Representing Algorithms
Algorithms 23 Ch 01 – Introduction
Example
Algorithms 24 Ch 01 – Introduction
2nd-23-24 12
(1) In Terms of Natural Language
Advantages Disadvantages
Familiar Verbose
Imprecise -> Ambiguity
Rarely used for complex or
technical algorithms
Algorithms 25 Ch 01 – Introduction
(2) In Terms of
Formal Programming Language
Advantages Disadvantages
Precise Unambiguous Can be too low-level for algorithm
design.
Will ultimately program in these
languages It has syntactic details which are not
important at the algorithm design
phase.
Not familiar for the person who is not
interested in this programming
language.
Algorithms 26 Ch 01 – Introduction
2nd-23-24 13
(3) In Terms of Pseudocode
1. Get array list and its size n
2. Assign max = list[1]
3. For i = 2 to n Do
1. IF (list[i] > max) THEN
1. max = list[i]
2. End If
4. End For
5. Display max
Advantages Disadvantages
A middle-ground compromise.
Resembles many popular programming
languages.
Relatively free of grammatical rules.
Only well-defined statements are included “A
Programming language without details”.
Algorithms 27 Ch 01 – Introduction
What is a Pseudocode?
Algorithms 28 Ch 01 – Introduction
2nd-23-24 14
Pseudocode
• Consists of natural language-like statements that precisely
describe the steps of an algorithm or program.
Algorithms 29 Ch 01 – Introduction
Algorithms 30 Ch 01 – Introduction
2nd-23-24 15
Pseudocode Language Constructs
Algorithms 31 Ch 01 – Introduction
Algorithms 32 Ch 01 – Introduction
2nd-23-24 16
Pseudocode Example
Algorithms 33 Ch 01 – Introduction
Solution
Algorithms 34 Ch 01 – Introduction
2nd-23-24 17
Flowcharts
A flowchart is a graphical representation of an algorithm. Once the
flowchart is drawn, it becomes easy to write the program in any
high-level language.
Algorithms 35 Ch 01 – Introduction
Flowchart Constructs
Sequence Decision
Algorithms 36 Ch 01 – Introduction
2nd-23-24 18
Flowchart Constructs
Switch Case
Algorithms 37 Ch 01 – Introduction
Flowchart Constructs
Loop
Algorithms 38 Ch 01 – Introduction
2nd-23-24 19
Method for Developing an Algorithm
Algorithms 39 Ch 01 – Introduction
Example 1
Draw flowchart to
represent the process of
reading two numbers,
dividing them, and then
displaying the result..
Algorithms 40 Ch 01 – Introduction
2nd-23-24 20
Example 2
Draw a flowchart to find
the sum of first 100 natural
numbers. This means that
we want to find sum where
sum is given by:
Sum = 1 + 2 + 3 + … + 99 +
100.
Algorithms 41 Ch 01 – Introduction
Example 3
Algorithms 42 Ch 01 – Introduction
2nd-23-24 21
Example 4
Algorithms 43 Ch 01 – Introduction
Example 5
Draw a flowchart to find the largest of three numbers A, B, and C.
Algorithms 44 Ch 01 – Introduction
2nd-23-24 22
Example
Algorithms 45 Ch 01 – Introduction
Solution
Compute Length_sum = 0
Compute word_counter = 0
Display “enter a word:”
Input word
While length( word ) > 0 do
Compute Length_sum = Length_sum+ length(word)
Compute word_counter = word_counter +1
Display “enter a word (or press enter to exit):”
Input word
End while
Compute Average_length = round(Length_sum/ Word_counter)
Display “the average is”, Average_length
Algorithms 46 Ch 01 – Introduction
2nd-23-24 23
Assignment #1(Teams)
Pseudocode-Flowchart-Python
Algorithms 47 Ch 01 – Introduction
Exercise
Algorithms 48 Ch 01 – Introduction
2nd-23-24 24
Exercise
Algorithms 49 Ch 01 – Introduction
Exercise
Algorithms 50 Ch 01 – Introduction
2nd-23-24 25
Exercise
Algorithms 51 Ch 01 – Introduction
Exercise
Algorithms 52 Ch 01 – Introduction
2nd-23-24 26