Design Analysis and Algorithm
Design Analysis and Algorithm
Design Analysis and Algorithm
Algorithms Analysis
Techniques & Design
Technique
Efficiency of Algorithms,
Zope Chaitali
Krushna
Roll no: 15
Efficiency of Algorithms,
Definition
Fundamental techniques which are used to design an
algorithm efficiently:
1. Divide-and-Conquer
2. Greedy method
3. Dynamic Programming
4. Backtracking
5. Branch-and-Bound
Advanced MIS
Advanced MIS
Advanced MIS
Advanced MIS
&
conquer
technique
is
top-down
(or
Solve)
every
sub-problem
individually, recursive.
3. Combine the solutions of these sub problems to
get the solution of original problem.
Advanced MIS
Binary search
e.g. 2 4 5 6 7 8 9
Algorithm binary-search
Input: A sorted sequence of n elements stored in an array
Output: The position of x (to be searched).
Step 1: If only one element remains in the array, solve it
directly.
Step 2: Compare x with the middle element of the array.
Step 2.1: If x = middle element, then output it and stop.
Step 2.2: If x < middle element, then recursively solve the
problem with x and the left half array.
Step 2.3: If x > middle element, then recursively solve the
problem with x and the right half array.
Two-way merge
Merge two sorted sequences into a single
one.
[25
37
48
[12
25
33
57][12 33
merge
37 48 57
86
92]
86
92]
4 -11
Merge sort
Sort into nondecreasing order
[25][57][48][37][12][92][86][33]
pass 1
[25 57][37 48][12 92][33 86]
pass 2
[25 37 48 57][12 33 86 92]
pass 3
[12 25 33 37 48 57 86 92]
Algorithm Merge-Sort
Input: A set S of n elements.
Output: The sorted sequence of the inputs in
nondecreasing order.
Step 1: If |S|2, solve it directly.
Step 2: Recursively apply this algorithm to solve the left
half part and right half part of S, and the results are
stored in S1 and S2, respectively.
Step 3: Perform the two-way merge scheme on S1 and S2.
C11
B21
C12
B22
C21
B21
C22
B22
Time complexity
7 multiplications and
subtractions
b
Time complexity:
,n2
T(n) =
7T(n/2)+an2 , n > 2
18
additions
T ( n ) an 2 7T ( n / 2)
an 2 7( a ( n2 ) 2 7T ( n / 4)
an 2 74 an 2 7 2 T ( n / 4)
an 2 (1
7
4
( 74 ) 2 ( 74 ) k 1 ) 7 k T (1)
or
Maximum numbers
finding the maximum of a set S of n
numbers
4 -16
Greedy Algorithm
Includes
Definition
A Minimum Spanning Tree (MST) is a
subgraph of an undirected graph
such that the subgraph spans
(includes) all nodes, is connected, is
acyclic, and has minimum total edge
weight
Algorithm Characteristics
Both Prims and Kruskals Algorithms
work with undirected graphs
Both work with weighted and
unweighted graphs but are more
interesting when edges are weighted
Both are greedy algorithms that
produce optimal solutions
Prims Algorithm
Similar to Dijkstras Algorithm except
that dv records edge weights, not
path lengths
Walk-Through
2
10
C
3
18
10
25
2
3
Initialize
array
dv
pv
10
C
3
18
10
25
2
3
dv
pv
A
B
C
D
E
F
G
H
10
3
18
10
25
2
3
Update distances of
adjacent, unselected
nodes
dv
pv
25
18
A
B
C
D
10
H
3
K
3
18
10
25
2
7
dv
pv
25
18
A
B
C
D
G
H
10
3
18
10
25
2
3
Update distances of
adjacent, unselected
nodes
dv
pv
18
A
B
C
D
G
H
10
H
3
C
3
18
10
25
2
7
dv
pv
18
A
B
G
H
10
3
18
10
25
2
3
Update distances of
adjacent, unselected
nodes
dv
pv
A
B
C
G
H
10
H
3
K
3
18
10
25
2
7
dv
pv
A
B
C
E
F
10
3
18
10
25
2
3
Update distances of
adjacent, unselected
nodes
dv
pv
10
E
F
10
H
3
K
3
18
10
25
2
7
dv
pv
10
10
3
18
10
25
2
3
Update distances of
adjacent, unselected
nodes
dv
pv
10
TableH entries 3
unchanged
10
H
3
K
3
18
10
25
2
7
dv
pv
10
10
3
18
10
25
2
3
Update distances of
adjacent, unselected
nodes
dv
pv
10
H
3
C
3
18
10
25
2
7
dv
pv
B
C
10
3
18
10
25
2
3
Update distances of
adjacent, unselected
nodes
A
dv
pv
B
C
Table entries
unchanged
10
H
3
C
3
18
10
25
2
7
dv
pv
2
3
F
A
4
2
3
21
Cost of Minimum
Spanning Tree = dv =
K
dv
pv
Done
Kruskals Algorithm
Work with edges, rather than nodes
Two steps:
Sort edges by increasing edge weight
Select the first |V| 1 edges that do not
generate a cycle
Walk-Through
F
10
C
3
6
4
1
2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edge
dv
(D,E)
(D,G)
10
3
6
4
1
2
3
edge
dv
(B,E)
(B,F)
(E,G)
(B,H)
(C,D)
(G,H)
(A,H)
(C,F)
(D,F)
(B,C)
(A,B)
(A,F)
10
edge
dv
(D,E)
(D,G)
10
3
6
4
1
2
3
edge
dv
(B,E)
(B,F)
(E,G)
(B,H)
(C,D)
(A,H)
(G,H)
(C,F)
(D,F)
(B,C)
(A,B)
(A,F)
10
edge
dv
(D,E)
(D,G)
10
3
6
4
1
2
3
edge
dv
(B,E)
(B,F)
(E,G)
(B,H)
(C,D)
(A,H)
(G,H)
(D,F)
(C,F)
(B,C)
(A,B)
(A,F)
10
edge
dv
(D,E)
(D,G)
F
A
C
3
2
3
D
1
edge
dv
(B,E)
(B,F)
(E,G)
(B,H)
(C,D)
(A,H)
(G,H)
(D,F)
(C,F)
(B,C)
(A,B)
(A,F)
10
edge
dv
(D,E)
(D,G)
Done
Total Cost =
21
dv =
not
consider
ed
Knapsack problem
Includes
Advanced MIS
max bi subject to wi W
iT
iT
Dynamic Programming
Dynamic Programming is an algorithm design technique for
optimization problems: often minimizing or maximizing.
Like divide and conquer, DP solves problems by combining
solutions to subproblems.
Unlike divide and conquer, subproblems are not
independent.
Subproblems may share subsubproblems,
However, solution to one subproblem may not affect the
solutions to other subproblems of the same problem. (More on
this later.)
DP reduces computation by
Solving subproblems in a bottom-up fashion.
Storing solution to a subproblem the first time it is solved.
Looking up the solution when subproblem is encountered again.
6
1
1 0 4 3
0
6 5 1 0
5
2
D(k-1)[i,k]
i
D(k-1)[k,j]
D(k-1)[i,j]
j
Initial
condition?
1
3
D(2)
2
7
0
7
1
0
D(1)
0
2
0
7
3
5
0
9
1
0
D(0)
0
2
0
2
9
6
0
7
3
5
0
9
1
0
D(3)
0
2
9
6
10
0
7
16
3
5
0
9
4
6
1
0
D(4)
0
2
7
6
10
0
7
16
3
5
0
9
4
6
1
0
0
1
0
0
0
0
0
1
1
0
0
0
0
1
0
0
0
1
0
1
0
1
0
1
1
1
0
1
0
1
0
1
Warshalls Algorithm
Constructs transitive closure T as the last matrix in the sequence of nby-n matrices R(0), , R(k), , R(n) where
R(k)[i,j] = 1 iff there is nontrivial path from i to j with only the first
k vertices allowed as intermediate
Note that R(0) = A (adjacency matrix), R(n) = T (transitive closure)
3
0
1
0
0
1
4
2
R(0)
0 1
0 0
0 0
1 0
0
1
0
0
1
4
1
4
0
1
0
1
R(3)
0 1
0 1
0 0
1 1
0
1
0
1
Backtracking
Suppose you have to make a series of
decisions, among various choices, where
You dont have enough information to know
what to choose
Each decision leads to a new set of choices
Some sequence of choices (possibly more
than one) may be a solution to your
problem
Backtracking is a methodical way of trying out
N-Queens Problem
Try to place N queens on an N * N
board such that none of the queens
can attack another queen.
Remember that queens can move
horizontally, vertically, or diagonally
any distance.
Lets consider the 8 queen
example
0
0
1
2
3
4
5
6
7
A t ( 4 ,4 ) a t t a c k f r o m ( 0 ,0 )
A t ( 5 ,4 ) a t t a c k f r o m ( 2 , 1 )
0
0
1
2
3
4
5
6
7
A t ( 6 , 4 ) a t t a c k f r o m ( 4 ,2 )
4
A t ( 7 ,4 ) s u c c e s s
Terminology I
A tree is composed of nodes
Terminology II
Each non-leaf node in a tree is a parent of
one or more other nodes (its children)
Each node in the tree, other than the root,
has exactly one parent
parent
Usually, however,
we draw our trees
downward, with
the root at the top
parent
children
children
Sum-of-Subsets problem
Sum-of-Subsets problem
Example
Say that our weight values are 5, 3,
2, 4, 1
W is 8
We could have
5+3
5+2+1
4+3+1
Hamiltonian Circuits
Problem
Hamiltonian circuit (tour) of a graph
is a path that starts at a given
vertex, visits each vertex in the
graph exactly once, and ends at the
starting vertex.
An enhancement of backtracking
Applicable to optimization problems
Uses a lower bound for the value of
the objective function for each node
(partial solution) so as to:
guide the search through state-space
rule out certain branches as
unpromising
Traveling salesman
example:
lb =
[(1+3)+(3+6)+(1+2)+(3+4)+(2+3)]/2
= 14
Example
18
C
3
15
12
A
6
B
5
4
5
22
10
19
H
4
THANK YOU