You are on page 1of 25

Chapter 1 Introduction

Definition of Algorithm An algorithm is a finite sequence of precise


instructions for performing a computation or for solving a problem.
Example 1 Describe an algorithm for finding the maximum (largest) value in a finite
sequence of integers.
Solution
1. Set the temporary maximum equal to the first integer in the sequence.
2. Compare the next integer in the sequence to the temporary maximum, and set
the larger one to be temporary maximum.
3. Repeat the previous step if there are more integers in the sequence.
4. Stop when there are no integers left in the sequence. The temporary maximum
at this point is the maximum in the sequence.
Algorithm 1. Finding the Maximum Element in a Finite Sequence
//Input : n integers a1,a2 ,...,an
Instead of using a
// Output : max (the maximum of a1,a2 ,...,an )
particular computer
max : a1 ; language, we use a form
for i:  2 to n of pseudocode.
if max  ai then max : ai
return max
Algorithmic Problem Solving

Understand the problem

Design an algorithm and proper data structures

Analyze the algorithm

Code the algorithm

• Ascertaining the Capabilities of a Computational Device


• Choosing between Exact and Approximate Problem Solving
• Deciding on Appropriate Data Structures
• Algorithm Design
• Algorithm Analysis
• Coding
Important Problem Types
• Sorting
• Searching
• String processing (e.g. string matching)
• Graph problems (e.g. graph coloring problem)
• Combinatorial problems (e.g. maximizes a cost)
• Geometric problems (e.g. convex hull problem)
• Numerical problems (e.g. solving equations )
Fundamental Data Structures

1. Array
a[1] a[2] a[3] a[n]

2. Link List Head of list


a1 a2 a3 an

10
3. Binary Tree 6 9

4 7 17 20

1 4 13

9
Fundamental Abstract Data Structures

1. Stack
an
• Operations a stack supports: last-in-first-out (pop, push)

• Implementation: using an array or a list (need to check


a2
underflows and overflows) a1
2. Queue
• Operations a queue supports: first-in-first-out (enqueue, dequeue)
• Implementation: using an array or a list (need to check underflows
and overflows)
am am 1 am  2 an

front rear

3. Priority Queue (each elements of a priority queue has a key)


• Operations a priority queue supports: inserting an element, returning an element
with maximum/minimum key.
• Implementation: heap
Assignment
(1) Read the sections of the text book about array, linked list, stack, queue,
heap and priority queue.
(2)
(i) Implement stack S and queue Q using both of array and linked list.
(ii) Write a main function to do the following operations for S: pop, push 10
times, pop, pop, repeat push and pop 7 times. Do the following
operations for Q: dequeue, enqueue 10 times, dequeue, depueue, repeat
enqueue and dequeue 7 times. When you use array, declare the size of
the array to be 10. Printout all the elements in S and in Q.
Some Advanced Data Structure

1. Graph
• Operations a graph support: finding neighbors
• Implementation: using list or matrix

G  (V , E )
V  (a, b, c, d , e, f )
E  {(a, c ), (b, c ), (d , a), (c, e), (e, c), (b, f ), (d , e), (e, f )}

 a b c d e f
 
a c a 0 0 1 0 0 0 
a c b c f
b b 0 0 1 0 0 1 
c e  
c 0 0 0 0 1 0 
d e f d a e d1 0 0 0 1 0 
e f c  
e 0 0 1 0 0 1 
f  
 f 0 0 0 0 0 0 
Adjacent lists
Adjacent matrix
2. Binary Search Tree
Definition 1 Binary Search Tree is a Binary Tree
satisfying the following condition:
(1) Each vertex contains an item called as key 15
which belongs to a total ordering set and two 6 18
links to its left child and right child,
respectively. 3 7 17 20
(2) In each node, its key is larger than the keys of 1 4 13
all vertices in its left subtree and smaller than
the keys of all the vertices in its right subtree. 9

Operations a binary search tree support: search, insert, and delete an element
with a given key.

How to select a data structure for a given problem?


Example: Select a data structure for supporting Dynamic
Dictionary
Definition 2
A Dynamic Dictionary is a data structure of item
with keys that support the following basic operations:
(1) Insert a new item
(2) Remove an item with a given key
(3) Search an item with a given key

What data structure


is the best?
Chapter 2 Fundamentals of the Analysis of Algorithm
Implementation and Empirical Analysis
Challenge in empirical analysis:
• Develop a correct and complete implementation.
• Determine the nature of the input data and other factors
influencing on the experiment. Typically, There are three
choices: actual data, random data, or perverse data.
• Compare implementations independent to programmers,
machines, compilers, or other related systems.
• Consider performance characteristics of algorithms, especially
for those whose running time is big.
Can we analyze algorithms that haven’t run yet?
Example 1 Describe an algorithm for finding an element x in a
list of distinct elements a1 , a2 ,..., an .

Algothm 1 The linear Search Algorithm.


Procedure linear search ( x : integer, a1,a2 ,..., an : distinct integers)
i:  1;
while (i  n and x  ai )
i:  i  1;
if i  n then location:  i
else locatiton:  0;
{location is the subscript of
term that equals x, or is 0 if x is not found}
Algorithm 2 The Binary Search Algorithm.
Procedure binary search ( x : integer, a1,a2 ,..., an : increasing integers)
i:  1;
j:  n;
while (i  j )
begin
m:  (i  j)/ 2;
if x  am then i:  m  1
else j:  m;
end;
if x  ai then location:  i
else location:  0;
{location is the subscript of
term that equals x, or is 0 if x is not found}
Linear Search and Binary Search written in C++

1. Linear search
int lsearch(int a[], int v, int l, int r)
{
for (int i=l; i<=n; i++)
if (v==a[i]) return i;
return -1;
}

2. Binary search
int bsearch(int a[], int v, int l, int n)
{
while (r>=l)
{int m=(l+r)/2;
if (v==a[m]) return m;
if (v<a[m]) r=m-1; else l=m+1;
}
return -1;
}
Complexity of Algorithms

Assume that both algorithms A and B solve the problem P. Which


one is better?
• Time complexity: the time required to solve a problem of
a specified size.
• Space complexity: the computer memory required to
solve a problem of a specified size.

The time complexity is expressed in terms of the


number of operations used by the algorithm.
• Worst case analysis: the largest number of operations
needed to solve the given problem using this algorithm.
• Average case analysis: the average number of
operations used to solve the problem over all inputs.
Example 2 Analyze the time complexities of linear search
algorithm and binary search algorithm
Linear Search Algorithm.
Procedure linear search ( x : integer, a1,a2 ,..., an : distinct integers)
i:  1; 1
while (i  n and x  ai ) 2(n+1)
i:  i  1; n
if i  n then location:  i
else locatiton:  0; 2
{location is the subscript of
3n+5
term that equals x, or is 0 if x is not found}
Binary Search Algorithm.
Procedure binary search ( x : integer, a1,a2 ,..., an : increasing integers)
i:  1; 1
j:  n; 1
while (i  j ) 1
begin
m:  (i  j)/ 2; 1 Let n  2 k.
if x  am then i:  m  1
2 It repeats at most k (k  log 2 n) times.
else j:  m;
end;
if x  ai then location:  i
2
else location:  0;
{location is the subscript of
term that equals x, or is 0 if x is not found}

Number of operations = 4 log n+4


2
Orders of Growth
6
Running time for a problem with size n  10

Running necessary operations


Time
2
Operation
lg n n n 2n
Per second
6 instant 1 second 11.5 days Never end
10 259350 days
12 instant Instant 1 second Never end
10
259340 days

Using silicon computer, no matter how fast CPU will be you can
never solve the problem whose running time is exponential !!!
Asymptotic Notations: O-notation

Definition 2.1 A function t(n) is said to be O(g(n)) if there


c0  0 and n0  0 such that t (n) c 0 g (n) for all n  n0 .
exist some constant

c0 g ( n )

t (n)

n
n0
t ( n)
If lim n  c (c  0 is a constant ) , then t(n)  O(g(n)).
g ( n)
Example 3 Prove 2n+1=O(n)

Example 4 Prove 10n 2  12n  5  O(n 2 )

Example 5
List the following function in O-notation in increasing order:
lg n, n, n 2 , n lg n, n 3 , n!,2 n.

Example 6 What is the big - oh of the following functions?


5n 5  100n 2 1000,
n lg n 2  100n lg n  1000n,
0.0001 2 n  n
Asymptotic Analysis of algorithms (using O-notation)

Example 7 Analyze the time complexities of linear search


algorithm and binary search algorithm asymptotically.
Linear Search Algorithm.
Procedure linear search ( x : integer, a1,a2 ,..., an : distinct integers)
i:  1;
while (i  n and x  ai ) It repeats n times.
i:  i  1;
if i  n then location:  i
else locatiton:  0;
{location is the subscript of
term that equals x, or is 0 if x is not found} Totally(addition): O(n)
Binary Search Algorithm.
Procedure binary search ( x : integer, a1,a2 ,..., an : increasing integers)
i:  1;
j:  n;
while (i  j )
begin
m:  (i  j)/ 2; Let n  2 k.
if x  am then i:  m  1
It repeats at most k (k  log 2 n) times.
else j:  m;
end;
if x  ai then location:  i
else location:  0;
{location is the subscript of
term that equals x, or is 0 if x is not found} Totally(comparison): O(log n)
Example 8 Analyze the time complexities of following algorithm
asymptotically.

Matrix addition algorithm


Procedure MatricAddition(A[0..n-1,0..n-1],B[0..n-1,0..n-1])
for i=0 to n-1 do
Repeat n times
for j=0 to n-1 do
C[i,j] = A[i,j] + B[i,j]; Repeat n times
2
return C; Totally(addition): O (n )
Recursive Algorithms

Example 9 Computing the factorial function F(n)=n!.


F(n) can be defined recursively as follows:
 F (0)  1

 F (n)  F (n  1)  n

Factorial Algorithm
Procedure factorial(n) Algorithm factorial calls itself
if n = 0 return 1 in its body!

else return factorial(n-1) * n;

Time complexity(multiplication): T(0) = 0 recurrence


T(n) = T(n-1) +1 when n>0
Basic Recurrences
Example 10 Example 11
Solve the following recurrence
Solving the following recurrence
Tn  2Tn / 2  n n 1
T(0) = 1
T1  c'
T(n) = T(n-1) + 1 n>0
T (n)  2T (n / 2)  cn
T(n) = T(n-1) + 1  2(2T (n / 2 2 )  c(n / 2))  cn  2 2 T (n / 2 2 )  cn  cn
2 3 2 3 3
= T(n-2) + 1 + 1 = T(n-2) + 2  2 (2T (n / 2 )  c(n / 2 ))  cn  cn  2 T (n / 2 )  3cn
......
= T(n-3) + 1 + 2 = T(n-3) + 3
 2i T (n / 2i )  icn

......
= T(n-i) + i  2 k T (n / 2 k )  kcn (k  log n)
…  nT1  cn log n  c' cn log n
= T(n-n) + n  O(n log n)

=n
Example 12 Solve the recurrence Example13 Solve the recurrence
T (n)  T (n  1)  n n  1 T (n)  T (n / 2)  1 n 1
T1  1 T (1)  1
T (n)  T (n  1)  n (Assume that n is a power of 2.
 T (n  2)  (n  1)  n That is, n  2 k , where k  log n).
 T (n  3)  (n  2)  (n  1)  n T (n)  T (n / 2)  1
......  T (n / 2 2 )  1  1
T 12  3  ...  (n  2)  (n  1)  n  T (n / 23 )  1  1  1
 1  2  3  ...  n ......
 n(n  1) / 2  T (n / 2i )  i
......
 T (n / 2 k )  k (k  log n)
 1  log n

You might also like