0% found this document useful (0 votes)
5 views

Lecture01-Introduction to Algorithms

The document provides an introduction to algorithms, detailing their definition, characteristics, and methods for analysis. It emphasizes the importance of efficiency, clarity, generality, and correctness in good algorithms and outlines techniques for evaluating algorithms' performance, including experimental and theoretical analysis. Additionally, it discusses the frequency count method for analyzing time complexity through basic operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture01-Introduction to Algorithms

The document provides an introduction to algorithms, detailing their definition, characteristics, and methods for analysis. It emphasizes the importance of efficiency, clarity, generality, and correctness in good algorithms and outlines techniques for evaluating algorithms' performance, including experimental and theoretical analysis. Additionally, it discusses the frequency count method for analyzing time complexity through basic operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Algorithm Design and Analysis

Lecture 01
Introduction to Algorithms

Mohammed Alqmase
Outlines
• What is algorithm?
• How to describe problem?
• How to describe algorithm?
• Characteristics of a Good Algorithm
• How to analyze algorithm?
What is an Algorithm?
What is an Algorithm?

A set of steps to solve a problem


Problem → Algorithm → Implementation
Algorithm Implementation

Problem Algorithm Implementation


. .
. .
. .

Binary search Algorithm


Searching problem
Java
Linear search Algorithm
Python
Algorithm Implementation

Problem Algorithm Implementation


. .
1. Natural Language .
2. Mathematical notations
.
. .
3. Modeled as template
1. Natural Language
2. Pseudocode
3. Flowchart
How to describe sorting problem?

13 1 6 2
How to describe sorting problem?

1. Natural Language
13 1 6 2
2. Mathematical Notation
3. Template
How to describe sorting problem?

1. Natural Language
13 1 6 2
2. Mathematical Notation
3. Template

Consider the problem of reordering a set of elements 𝑨 such


that all elements in 𝑨 are reordered increasing or decreasing.
How to describe sorting problem?

1. Natural Language
13 1 6 2
2. Mathematical Notation
3. Template

Given a sequence of 𝒏 elements 𝑨 = {𝒂𝟏 , 𝒂𝟐 , … , 𝒂𝒏 }.


Reorder the given input such that {𝒂, 𝟏 ≥ 𝒂, 𝟐 ≥ ⋯ ≥ 𝒂, 𝒏 } for
decreasing order or {𝒂, 𝟏 ≤ 𝒂, 𝟐 ≤ ⋯ ≤ 𝒂, 𝒏 } for increasing
order.
How to describe sorting problem?

1. Natural Language
13 1 6 2
2. Mathematical Notation
3. Template Input: A set of 𝒏 sortable elements 𝑨 = {𝒂𝟏 , 𝒂𝟐 , … , 𝒂𝒏 }.
Output: A set of sorted elements 𝑨, = {𝒂, 𝟏 , 𝒂, 𝟐 , … , 𝒂, 𝒏 }
Variant:
• 𝑨 sorted in decreasing order.
• 𝑨 sorted in non-decreasing order.
Requirement: providing an algorithm to permute (reorder) the
given input set such that {𝒂, 𝟏 ≥ 𝒂, 𝟐 ≥ ⋯ ≥ 𝒂, 𝒏 } for decreasing
order or {𝒂, 𝟏 ≤ 𝒂, 𝟐 ≤ ⋯ ≤ 𝒂, 𝒏 } for increasing order.
How to describe selection sort Algorithm?

13 1 6 2
How to describe selection sort Algorithm?
1. Natural Language
2. Pseudocode
3. Flowchart

13 1 6 2
How to describe selection sort Algorithm?
1. Natural Language
Let 𝑨[𝟏. . 𝒏] be an array of 𝒏 elements. First,
2. Pseudocode we find the minimum element and interchange
it with the element in position 𝑨[𝟏]. Next, we
3. Flowchart
find the minimum element of the remaining
𝒏 − 𝟏 elements and interchange it with
element in position 𝑨[𝟐]. We continue this way
13 1 6 2 until the second largest element is stored in
𝑨[𝒏 − 𝟏].
How to describe selection sort Algorithm?
1. Natural Language
Algorithm 1.4 SELECTIONSORT
2. Pseudocode Input: An array 𝑨[𝟏. . 𝒏] of 𝒏 elements
3. Flowchart Output: 𝑨[𝟏. . 𝒏] sorted in non-decreasing order.

1. for 𝑖 ← 1 to 𝑛 − 1
2. 𝑘←𝑖
13 1 6 2 3. for 𝑗 ← 𝑖 + 1 to 𝑛
4. if 𝐴[𝑗] < 𝑘 then 𝑘 ← 𝑗
5. end for
6. if 𝑘 ≠ 𝑖 then interchange 𝐴[𝑖] and 𝐴[𝑗]
7. end for
How to describe selection sort Algorithm?
1. Natural Language
2. Pseudocode
3. Flowchart

13 1 6 2
Definitions

❖ Pseudocode: A human-readable version of the steps in


the algorithm.

❖ Flowcharts: Graphical representation of the steps in the


algorithm.
Characteristics of a Good Algorithm
Characteristics of a Good Algorithm

❖ Efficiency: Performs the task with minimal resources.


❖ Clarity: Steps are easily understandable.
❖ Generality: Can be applied to a wide range of problems.
❖ Correctness: Always produces the correct output.
Introduction to Algorithm Analysis
Algorithm Analysis

Evaluating the efficiency of algorithms in terms of time and


space.

❖ Key Metrics:
• Time complexity: Measures how the time to run an algorithm
increases with input size.
• Space complexity: Measures how much memory the algorithm
uses with input size.
Why Analyze Algorithms?

❖ To determine how fast and efficient an algorithm is.


❖ Helps in comparing different algorithms for the same
task.
Methods of Analyzing Algorithms
Methods of Analyzing Algorithms

• Experimental Analysis: Measure performance with actual data.


Methods Example: Run algorithm with different input sizes and measure runtime.
• Advantages: Real results.
• Disadvantages: Depends on hardware.

• Theoretical Analysis: Predict performance using mathematical models.


Methods Example:
• Frequency Count Analysis
• Asymptotic Analysis (Big O Notation)
Advantages: Independent of hardware.
Disadvantages: May be difficult for complex algorithms.
Analyzing Algorithms Using the Frequency Count
Frequency Count Method

Definition: Count the number of times basic operations


are executed.
Frequency Count Method

Name Operations Time


Arithmetic Operations +, -, *, / one unit of time
Assignment Operators ≔, ← one unit of time
Comparisons Operations =, ≠, <, ≤, >, ≥ one unit of time
Control Statements return, break, continue one unit of time
Frequency Count Method

Function sum(a, b):


sum = a + b
return sum
EndFunction

Time Function f(n): 3 units of time


Frequency Count Method
Function sumArray(arr, n):
sum = 0
For i = 0 to n-1:
sum = sum + arr[i]
EndFor
return sum
EndFunction

Time Function f(n): 2n + 3


Frequency Count Method
Function findMax(arr, n):
max = arr[0]
For i = 1 to n-1:
If arr[i] > max:
max = arr[i]
EndFor
return max
EndFunction

Time Function f(n): 2n + 3 In the worst case

Time Function f(n): n+4 In the best case


Frequency Count Method
Function findMax(arr, n):
max = arr[0]
For i = 1 to n-1:
If arr[i] > max:
max = arr[i]
EndFor
return max
EndFunction

Time Function f(n): 2n + 1 In the worst case

Time Function f(n): n+3 In the best case

You might also like