German University in Cairo
Department of Computer Science
Dr. Amr Desouky
Eng. Nourhan Ehab
Analysis and Design of Algorithms, Winter 2014
Practice Assignment 5 Solutions
Discussion: 8.11.2014  12.11.2014
Exercise 51
So far in this course, you have stud
DivideAndConquer Sorting
Small instance.
n <= 1 elements.
n <= 10 elements.
Well use n <= 1 for now.
Large instance.
Divide into k >= 2 smaller instances.
k = 2, 3, 4, ?
What does each smaller instance look like?
Sort smaller instances recursively.
Shortest Path Problems
Directed weighted graph.
Path length is sum of weights of edges on path.
The vertex at which the path begins is the
source vertex.
The vertex at which the path ends is the
destination vertex.
Example
1
6
8
2
16
3
7
6
3
5
4
10
1
Overflow Handling
An overflow occurs when the home bucket for a
new pair (key, element) is full.
We may handle overflows by:
Search the hash table in some systematic fashion for a
bucket that is not full.
Linear probing (linear open addressing).
Quad
Hard Problems
Some problems are hard to solve.
No polynomial time algorithm is known. E.g., NPhard problems such as machine scheduling, bin packing, 0/1 knapsack.
Is this necessarily bad? Data encryption relies on difficult to solve problems.
Cryptogr
SingleSource AllDestinations
Shortest Paths With Negative Costs
Directed weighted graph.
Edges may have negative cost.
No cycle whose cost is < 0.
Find a shortest path from a given source vertex
s to each of the n vertices of the digraph.
SingleSource
Dynamic Programming
Steps.
View the problem solution as the result of a sequence
of decisions.
Obtain a formulation for the problem state.
Verify that the principle of optimality holds.
Set up the dynamic programming recurrence
equations.
Solve these equ
Tournament Trees
Winner trees.
Loser Trees.
Winner Trees
Complete binary tree with n external
nodes and n  1 internal nodes.
External nodes represent tournament
players.
Each internal node represents a match
played between its two children;
the winner of
Growth of Functions
Asymptotic Notation
The notation used to describe the asymptotic
running time of an algorithm is defined in terms of
functions whose domains are the set of natural
numbers.
This notation refers to how the problem scales as
the proble
Trees
Nature Lovers View Of A Tree
leaves
branches
root
Computer Scientists View
root
leaves
branches
nodes
Linear Lists And Trees
Linear lists are useful for serially ordered data.
(e0, e1, e2, , en1)
Days of week.
Months in a year.
Students in this cl
Graph Search Methods
A vertex u is reachable from vertex v iff there is a path from v to u.
2 1 4 5 3 8
9 10
6
7
11
Graph Search Methods
A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v.
2 1 4 5 3
AllPairs Shortest Paths
Given an nvertex directed weighted graph,
find a shortest path from vertex i to vertex j
for each of the n2 vertex pairs (i,j).
1
7
2
5
1
9
3
7
6
9
5
4
1
8
4
2
5
4
16
1
2
4
7
Dijkstras Single Source Algorithm
Use Dijkstras algo
UnionFind Problem
Given a set cfw_1, 2, ., n of n elements. Initially each element is in a different set.
cfw_1, cfw_2, ., cfw_n
An intermixed sequence of union and find operations is performed. A union operation combines two sets into one.
Each of t
Stacks
public interface Stack
cfw_
public boolean empty();
public Object peek();
public void push(Object theObject);
public Object pop();
Derive From A Linear List Class
ArrayLinearList
Chain
Derive From ArrayLinearList
a
b c
0
1
2
d
e
3 4
5 6
stack t
Backtracking And Branch And Bound
Subset & Permutation Problems
Subset problem of size n.
Nonsystematic search of the space for the answer takes O(p2n) time, where p is the time needed to evaluate each member of the solution space.
Permutation problem
Binary Tree Traversal Methods
In a traversal of a binary tree, each element of
the binary tree is visited exactly once.
During the visit of an element, all action (make
a clone, display, evaluate the operator, etc.)
with respect to this element is taken
Rank
Rank of an element is its position in ascending key
order.
[2,6,7,8,10,15,18,20,25,30,35,40]
rank(2) = 0
rank(15) = 5
rank(20) = 7
Selection Problem
Given n unsorted elements, determine the
kth smallest element. That is, determine the
element whose
Algorithms CS305
Lab3 Solution
Exercise 36
Consider the following graph apply the following
algorithm to find the shortest path
u (Visited) Distance
(d[u])
a

4
a
h
8
a
c
4+8 = 12
b
g
8+1 = 9
h
i
8+7 = 15
h
f
4+4+8 =
16
c
d
BFS can only be used to find
DESIGN & ANALYSIS OF
ALGORITHMS
Dr: Mahmood Abdel Moneim
Winter 2014
Topics covered
Insertion Sort
Invariants
Analyzing Algorithms
Dr. Mahmood A.Moneim
2
Problem Definition
Given a sequence of
cards in one hand,
Produce
a
sorted
sequence of the same
ca
Cairo Univeristy
Faculty of Science
Department of Computer Science
Dr.Mahmood Abdel Moneim Mahmood
Eng. Hend Dawood
Analysis and Design of Algorithms, Winter 2014
Exercise 31
Give tight asymptotic bounds for the following recurrence. Justify your answers
DESIGN & ANALYSIS OF
ALGORITHMS (CS305)
Dr: Mahmood Abdel Moneim
Winter 2014
Asymptotic Notations
Dr. Mahmood A.Moneim
2
 notation
Dr. Mahmood A.Moneim
3
Example
Dr. Mahmood A.Moneim
4
Dr. Mahmood A.Moneim
5
Onotation
Dr. Mahmood A.Moneim
6
Example
Dr.
Cairo University
Faculty of Science
Department of Computer Science
Dr.Mahmood Abdel Moneim Mahmood
Eng. Hend Dawood
CS305  Analysis and Design of Algorithms, Winter 2014
Assignment 1 Solutions
Exercise 11 Warm Up
In each of the following scenarios, choo
Design & Analysis of Algorithms
(CS305)
Dr: Mahmood Abdel Moneim
Winter 2014
The Master Method
Dr. Mahmood A.Moneim
2
Dr. Mahmood A.Moneim
3
Dr. Mahmood A.Moneim
4
Dr. Mahmood A.Moneim
5
Dr. Mahmood A.Moneim
6
Dr. Mahmood A.Moneim
7
Level #
Dr. Mahmood A.
Design & Analysis of Algorithms
(CS305)
Dr: Mahmood Abdel Moneim
Winter 2014
GREEDY ALGORITHMS
We have already seen two general problemsolving
techniques: divideandconquer and dynamicprogramming. In this section we introduce a third basic
technique: th