Professional Documents
Culture Documents
Other approaches:
• proof by cases/enumeration
• proof by chain
• proof by contradiction
• proof by contrapositive
• For any algorithm, we must prove that it always returns
the desired output for all legal instances of the
problem. For sorting, this means even if the input is
already sorted or it contains repeated elements.
Proof by Counterexample
• Searching for counterexamples is the best way to
disprove the correctness of some things.
• Identify a case for which something is NOT true
• If the proof seems hard or tricky, sometimes a
counterexample works
• Sometimes a counterexample is just easy to see, and
can shortcut a proof
• If a counterexample is hard to nd, a proof might be
easier
Proof by Induction
• Failure to find a counterexample to a given
algorithm does not mean “it is obvious" that the
algorithm is correct.
• Mathematical induction is a very useful method
for proving the correctness of recursive
algorithms.
• 1. Prove base case
• 2. Assume true for arbitrary value n
• 3. Prove true for case n + 1
Proof by Loop Invariant
A:0
0 1
B : 10
C : 110 A
0 1
D : 111 B
0 1
C D
Observe:
1. This is a prefix code, since each of the
leaves has a path ending in it, without
continuation.
2. If the tree is full then we are not
“wasting” bits.
3. If we make sure that
0 1
the more frequent
symbols are closer to A
0 1
the root then they will B
have a smaller code. 0 1
C D
Greedy Algorithm:
3. Iterate.
Greedy Algorithm Example:
Alphabet: A, B, C, D, E, F
Frequency table:
A B C D E F
10 20 30 40 50 60
A 10 B 20
Algorithm Run:
Y 60 D 40 E 50 F 60
X 30 C 30
A 10 B 20
Algorithm Run:
D 40 E 50 Y 60 F 60
X 30 C 30
A 10 B 20
Algorithm Run:
Z 90 Y 60 F 60
D 40 E 50 X 30 C 30
A 10 B 20
Algorithm Run:
Y 60 F 60 Z 90
X 30 C 30 D 40 E 50
A 10 B 20
Algorithm Run:
W 120 Z 90
Y 60 F 60 D 40 E 50
X 30 C 30
A 10 B 20
Algorithm Run:
Z 90 W 120
D 40 E 50 Y 60 F 60
X 30 C 30
A 10 B 20
Algorithm Run:
V 210
0 1
Z 90 W 120
0 1 1
0
D 40 E 50 Y 60 F 60
0 1
X 30 C 30
0 1
A 10 B 20
The Huffman encoding:
A: 1000 0
V 210
1
B: 1001
C: 101 Z 90
1
W 120
0 0 1
D: 00
E: 01 D 40 E 50 Y 60 F 60
0 1
F: 11
X 30 C 30
0 1
A 10 B 20
• Thus:
T(n) = 2T(n/2) + O(n)
T(n) = O(n log n)
Dynamic Programming
• Dynamic Programming is a technique of breaking down
a problem into sub problems, solving these sub
problems once, and storing their solutions.
• The main use of dynamic programming is to solve
optimization problems. Here, optimization problems
mean that when we are trying to find out the minimum
or the maximum solution of a problem.
• Dynamic Programming, what we are ultimately hoping
to achieve is a significantly faster computation time at
the expense of a modest increase in space used.
Approaches
• There are essentially two ways you can go
about storing solutions to problems:
• Memoization (top-down approach)
• Tabulation (bottom-up approach)
Assembly line Scheduling using
Dynamic Programming
• In an product industry, products are produced
using assembly lines. Multiple lines are
worked together to produce a useable product
and completed useable product exits at the
end of the line.
• For making a product in the optimal time we
should choose the optimised assembly line
from a station so that a company can make
product in the best utilized time.
Problem Statement
• M [1, 3] =264
• As Comparing both output 264 is minimum in both cases so
we insert 264 in table and ( M1 x M2) + M3 this combination
is chosen for the output making.
• M [2, 4] = M2 M3 M4
• There are two cases by which we can solve this
multiplication: (M2x M3)+M4, M2+(M3 x M4)
• After solving both cases we choose the case in which
minimum output is there.
• M [2, 4] = 1320
• As Comparing both output 1320 is minimum in both
cases so we insert 1320 in table and M2+(M3 x M4) this
combination is chosen for the output making.
• M [3, 5] = M3 M4 M5
• There are two cases by which we can solve
this multiplication: ( M3 x M4) + M5, M3+ (
M4xM5)
• After solving both cases we choose the case in
which minimum output is there.
• As Comparing both output 1140 is minimum
in both cases so we insert 1140 in table and (
M3 x M4) + M5this combination is chosen for
the output making.
• Now Product of 4 matrices:
• M [1, 4] = M1 M2 M3 M4
• There are three cases by which we can solve
this multiplication:
• ( M1 x M 2 x M 3 ) M4
• M1 x(M2 x M3 x M4)
• (M1 xM2) x ( M3 x M4)
• After solving these cases we choose the case
in which minimum output is there
As comparing the output of different cases then '1080' is minimum output, so we
insert 1080 in the table and (M1 xM2) x (M3 x M4) combination is taken out in
output making,
• There are three cases by which we can solve
this multiplication:
• (M2 x M3 x M4)x M5
• M2 x( M3 x M4 x M5)
• (M2 x M3)x ( M4 x M5)
• After solving these cases we choose the case
in which minimum output is there
• Now Product of 5 matrices:
• M [1, 5] = M1 M2 M3 M4 M5
• There are five cases by which we can solve this
multiplication:
• (M1 x M2 xM3 x M4 )x M5
• M1 x( M2 xM3 x M4 xM5)
• (M1 x M2 xM3)x M4 xM5
• M1 x M2x(M3 x M4 xM5)
• After solving these cases we choose the case
in which minimum output is there
Longest common subsequence