You are on page 1of 10

Analysis, Design of Algorithms

CS3610

Dr. Islam Hegazy


Associate Professor

F2023 Analysis, Design of Algorithms 1


Objectives

By the end of this lecture, you will:

Solve examples

F2023 Analysis, Design of Algorithms 2


Complexity
1. Depth-First Search

F2023 Analysis, Design of Algorithms 3


Brute-force

Describe an O(n log n) time algorithm to test whether a sequence a1…an of


integers contains any duplicates.
1. Depth-First Search

F2023 Analysis, Design of Algorithms 4


Brute-force

Design a brute-force algorithm for computing the value of a polynomial

p(x) = anxn + an−1xn−1 + ... + a1x + a0


1. Depth-First Search

F2023 Analysis, Design of Algorithms 5


Recursive algorithm

Consider the following recursive algorithm

Algorithm LoopAndRecurse(A[1..n]);
int i;
if n > 1 then
1. Depth-First Search

for i := n-1 downto 1 do


A[n] := A[n] + A[i];
call LoopAndRecurse(A[1..n-1])

Analyze the algorithm to obtain a tight asymptotic bound on T(n2) using a


recurrence relation.

F2023 Analysis, Design of Algorithms 6


Graph traversal

Describe an O(m) algorithm for computing single-source shortest paths in a


connected undirected graph with edges of weight 1 or 2.
1. Depth-First Search

F2023 Analysis, Design of Algorithms 7


Exhaustive search

A magic square of order n is an arrangement of the numbers from 1 to n2 in an


n-by-n matrix, with each number occurring exactly once, so that each row, each
column, and each main diagonal has the same sum.
1. Depth-First Search

Prove that if a magic square of order n exists, the sum in question must be
equal to n(n2 + 1)/2.
Describe an exhaustive search algorithm to solve this problem.

F2023 Analysis, Design of Algorithms 8


Recurrence relation

Solve the following recurrence relation


1. Depth-First Search

F(n) = 5F(n-1) – 6F(n-2), where F(0) = 1, F(1) = 4

F(n) = 10F(n-1) -25F(n-2), where F(0) = 3, F(1) =17

F2023 Analysis, Design of Algorithms 9


F2023 Analysis, Design of Algorithms 10

You might also like