BFAI-Agorithms-Lecture01
BFAI-Agorithms-Lecture01
Algorithms
Lecture 01
Dr. Eman Monir
Faculty of Computers and Artificial Intelligence
Benha University
Spring 2024
Basic Course Information
• Course code:
• Course name: Analysis and Design of Algorithms
• Level: 2nd Year / B.Sc.
• Course Credit: 3 credits
• Instructor: Dr. Eman Monir
• Teaching Assistant:
Course Material
Book
T. H. Cormen, C. E. Leiserson, R. L.Rivest,
C. Stein. Introduction to Algorithms , 3rd
Edition, 2009.
Slides
- Should be available after the class
1/26
Table of Contents
1. Foundations
- The Role of Algorithms in Computing
- Design and Analysis algorithms
- Growth of Functions
2. Sorting
- Heapsort
- Quicksort
- Sorting in Linear Time
3. Graph Algorithms
- Elementary Graph Algorithms
- Minimum Spanning Trees
4. Advanced Design and Analysis Techniques
- Dynamic Programming
- Greedy Algorithms
5. Selected Topics
- Linear Programming
- String Matching
3/24
Mathematical background needed to understand algorithms
4/24
Ch1: Foundations
5/24
Algorithms
6/24
What is an algorithm?
An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of
time.
problem
algorithm
1-9
Algorithm
7/24
What kinds of problems are solved by algorithms?
• Graph algorithms
- find an efficient path through some kind of network — be it a computer network, a
road network
8/24
Algorithms Task
9/24
Algorithms Task (cont.)
10/
Time Complexity & Space Complexity
11/
24
Some Well-known Computational Problems
• Sorting
• Searching
• Shortest paths in a graph
• Minimum spanning tree
• Primality testing
• Traveling salesman problem
• Knapsack problem
• Chess
• Towers of Hanoi
• Program termination
Some of these problems don’t have efficient algorithms, or
algorithms at all!
1-7
Basic Issues Related to Algorithms
• How to design algorithms
• Proving correctness
– Empirical analysis
• Optimality
1-17
Analysis of Algorithms
• How good is the algorithm?
– Correctness
– Time efficiency
– Space efficiency
1-18
Algorithm Design Strategies
Greedy approach
Dynamic programming
1-19
What is an Algorithm?
1-20
Comparing between two Algorithms
Algorithm B
o If I change the model of
computation, the time
Exec_Time = 2msec
and space may change
RAM = 10KB
12/
Sorting Problem
❑ Sort a sequence of numbers into non-decreasing order
Example:
Input:
8 2 4 9 3 6
Output:
2 3 4 6 8 9
- Input sequence is called an instance of the sorting problem.
13/
Sorting Problem (cont)
❑ Efficiency
o Different algorithms devised to solve the same problem often differ
dramatically in their efficiency.
o These differences can be much more significant than differences due
to hardware and software
o In this chapter, we will see two algorithms for sorting problem
o The first, known as insertion sort, takes time roughly equal to 𝒄𝟏𝒏𝟐 to sort 𝑛 items,
where 𝒄𝟏 is a constant that does not depend on 𝒏.
o The second, merge sort, takes time roughly equal to 𝒄𝟐𝒏𝒍𝒐𝒈𝟐n, where 𝒄𝟐 is
another constant that also does not depend on 𝒏.
14/
Insertion Sort
15/
Insertion Sort (cont.)
keys: The numbers
that we wish to
sort
Example of Insertion Sort
17/
Analysis of Algorithms
18/
Analysis of Algorithms
2 3 5 4 1 7 6
19/
Analysis of Algorithms
2. Average-case: (sometimes)
• We take all possible inputs and calculate computing time for all of the inputs
• 𝑇(𝑛) =expected time of algorithm over all inputs of size 𝑛
3. Best-case
• Calculate lower bound on running time of an algorithm.
• The case that causes minimum number of operations to be executed.
Example: Search for number 2
2 3 5 4 1 7 6
Analysis of Insertion Sort
21/
Analysis of Insertion Sort
❑ In insertion sort, the best case occurs if the array is already sorted.
23/24
Analysis of Insertion Sort
❑ Best Case
❑ If 𝐴 is sorted: 𝑂(𝑛) comparisons
❑ Worst Case
❑ If 𝐴 is reversed sorted: 𝑂(𝑛2) comparisons
❑ Average Case
❑ If A is randomly sorted: 𝑂(𝑛2) comparisons
24/24
Example of computational problem: sorting
• Statement of problem:
– Input: A sequence of n numbers <a1, a2, …, an>
• Algorithms:
– Selection sort
– Insertion sort
– Merge sort
1-34
Selection Sort
• Input: array a[1],…,a[n]
• Algorithm:
for i=1 to n
swap a[i] with smallest of a[i],…,a[n]
1-35
Euclid’s Algorithm
Problem: Find gcd(m,n), the greatest common divisor of
two nonnegative, not both zero integers m and n
Pseudo code:
while n ≠ 0 do
r ← m mod n r= m % n;
m← n m = n;
n←r
return m
1-37
other Description for computing gcd(m,n)
for p ← 2 to n do A[p] ← p
for p ← 2 to n do
if A[p] 0 //p hasn’t been previously eliminated from the list
j ← p* p
while j ≤ n do
A[j] ← 0 //mark element as eliminated
j←j+p
Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20
Time complexity: O(n)
1-40
Thank You