Chapter 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Algorithm

 The word algorithm comes from


the name of author, "Abu Ja'far
Mohammed Ibn Mousa Al
Khawarizmi " who wrote a
textbook on Mathematics.

https://en.wikipedia.org/wiki/Muhammad_ibn_Musa_al-Khwarizmi

Algorithms 1 Ch 01 – Introduction

al‐Khwārizmī (c. 780–850)

 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?

 An algorithm is a list that looks like


– STEP 1: Do something
– STEP 2: Do something
– STEP 3: Do something
– . .
– . .
– . .
– STEP N: Stop, you are finished

Algorithms 6 Ch 01 – Introduction

2nd-23-24 3
What’s an Algorithm?
 Formal Definition:

– An algorithm is a well-ordered collection of


unambiguous and effectively computable operations that
when executed produces a result and halts in a finite
amount of time.

 With this definition, we can identify five important


characteristics of algorithms.
– Algorithms are well-ordered.
– Algorithms have unambiguous operations.
– Algorithms have effectively computable operations.
– Algorithms produce a result.
– Algorithms halt in a finite amount of time.

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)

Each operation in an algorithm must be sufficiently clear so that it


does not need to be simplified. Given a list of numbers, you can
easily order them from largest to smallest with the simple
instruction "Sort these numbers." A computer, however, needs more
detail to sort numbers. It must be told to search for the smallest
number, how to find the smallest number, how to compare numbers
together, etc. The operation "Sort these numbers" is ambiguous to
a computer because the computer has no basic operations for
sorting. Basic operations used for writing algorithms are known as
primitive operations or primitives. When an algorithm is written in
computer primitives, then the algorithm is unambiguous and the
computer can execute it

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 Produce a Result .


The result of the algorithm must be produced after the execution
of a finite number of operations

– i.e. Can the user of the algorithm observe a result produced by


the algorithm?

– A result can be a sign, a sound, an alarm, a number, a


message of error, etc.

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”?

Step 1. Write the number 1 on the board.


Step 2. Add 1 to the number you just wrote.
Step 3. Write the result on the board.
Step 4. Repeat Step 2.
Step 5. Stop.

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:

Given a set of cities and distance


between every pair of cities, the problem
is to find the shortest possible route that
visits every city exactly once and returns
to the starting city.

Algorithms 18 Ch 01 – Introduction

2nd-23-24 9
Brute – Force Technique

 Traveling-Salesman Problem (TSP)

https://tspvprint()is.com/
Algorithms 19 Ch 01 – Introduction

Hard Problems (NP)


Can every problem be solved algorithmically?
See Pages 9 + 10 in our textbook

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

 How to Express Algorithms Clear,


Precise, and Unambiguous ?

 How to Represent 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.

• Statements describe actions.

• Focuses on the logic of the algorithm or program.


avoids language-specific elements.

• Written at a level so that the desired programming code can


be generated almost automatically from each statement.

• Steps are numbered. Subordinate numbers and/or


indentation are used for dependent statements in selection
and repetition structures.

Algorithms 29 Ch 01 – Introduction

Pseudocode Language Constructs

Algorithms 30 Ch 01 – Introduction

2nd-23-24 15
Pseudocode Language Constructs

Algorithms 31 Ch 01 – Introduction

Pseudocode Language Constructs

Algorithms 32 Ch 01 – Introduction

2nd-23-24 16
Pseudocode Example

 Express an algorithm to get two numbers


from the user (dividend and divisor), testing to
make sure that the divisor number is not zero,
and displaying their quotient using
pseudocode.

Algorithms 33 Ch 01 – Introduction

Solution

1. Get dividend, divisor


2. IF divisor = 0 THEN
1) Display error message “divisor must be non-
zero”
2) Exit algorithm
3. Compute quotient = dividend/divisor
4. Display dividend, divisor, quotient

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

1. Define the problem: State the problem you are


trying to solve in clear and concise terms.
2. List the inputs (information needed to solve the
problem) and the outputs (what the algorithm will
produce as a result)
3. Describe the steps needed to convert or
manipulate the inputs to produce the outputs.
Start at a high level first, and keep refining the steps
until they are effectively computable operations.
4. Test the algorithm: choose data sets and verify that
your algorithm works!

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

Draw a flowchart to find the


sum of the first 25 odd natural
numbers

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

 Write an algorithm with a loop that repeatedly


asks the user to enter a word (string). The
user should enter nothing (press Enter
without typing anything) to signal the end of
the loop. Once the loop ends, the algorithm
should display the average length of the
words entered, rounded to the nearest whole
number.

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

You might also like