


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























Brute Force ApproachPrime (n)
for
i
to
n-
if
n
0 mod i
then
“number is composite”
else
“number is prime”
The computational cost is
(n)
The computational cost in terms of binary operationis
n^ )
n-
Statement•^
If n
N, n > 1 is not prime then n is divisible by
some prime number p
square root of n.
Proof•^
Since n is not prime hence it can be factored as
n = x.y
where 1 <x
y <n
x or y is a prime, if not it can be further factored out.
-^
Also suppose without loss of generality that x
y
Now our claim is that x
sq(n)
This is because otherwise x.y > n, a contradiction
-^
We only require to check till sqr(n) for primality test.
Prime (n)
for
i
to
sqr(n)
if
n
0 mod i
then
“number is composite”
else
“number is prime”
The computational cost is
(sqr(n)), much faster
The computational cost in terms of binary operationis
squareroot(n)
), still exponential
Computation cost can be decreased using numbertheoretic concepts, which will be discussed later on.
n
2
1
n
2
2
(^1) Sorting Algorithm
Sort the array [2, 4, 1, 3] in increasing order s1 = [4,3,2,1],
s2 = [4,3,1,2],
s3 = [4,1,2,3]
s4 = [4,2,3,1],
s5 = [4,1,3,2],
s6 = [4,2,1,3]
s7 = [3,4,2,1],
s8 = [3,4,1,2],
s9 = [3,1,2,4]
s10 = [3,2,4,1],
s11 = [3,1,4,2],
s12 = [3,2,1,4]
s13 = [2,3,4,1], s14 = [2,3,1,4], s15 = [2,1,4,3]s16 = [2,4,3,1], s17 = [2,1,3,4], s18 = [2,4,1,3] s19 = [1,3,2,4],
s20 = [1,3,1,4],
s21 = [1,4,2,3]
s22 = [1,2,3,4],
s23 = [1,4,3,2],
s24 = [1,2,4,3]
There are 4! = 24 number of permutations.For n number of elements there will be n! number of
permutations. Hence cost of order n! for sorting.
Theorem •^
Prove, by mathematical induction, that computationalcost of generating permutations is n!. Proof •^
If n = 1, then the statement is true, because 1! =
-^
If there are k elements in set then no. of permutation = k!
-^
If we add one more element in any of the permutations,there will be k+1 number of ways to add it, resulting k+1no. of permutations.
-^
Now total no. of permutations = k!(k+1) = (k+1)!
-^
Hence true for all n.
Input•^
Given
n
items each
-^
weight w
i
-^
value v
i
-^
Knapsack of capacity
W
Output: Find most valuable items that fit into the knapsackExample: item
weight
value
knapsack capacity W =
^16
1
2
20
2
5
30
3
10
50
4
5
10
Subset
Total weight
Total value
1.
0
0
#^
W
V
{1}
2
20
1
2
20
{2}
5
30
2
5
30
{3}
10
50
3
10
50
{4}
5
10
4
5
10
{1,2}
7
50
{1,3}
12
70
{1,4}
7
30
{2,3}
15
80
{2,4}
10
40
{3,4}
15
60
{1,2,3}
17
not feasible
{1,2,4}
12
60
{1,3,4}
17
not feasible
{2,3,4}
20
not feasible
22
not feasible
Approach•^
In brute force algorithm, we go through allcombinations and find the one with maximumvalue and with total weight less or equal to
Complexity•^
Cost of computing subsets O(
n^ ) for n elements
Cost of computing weight = O(
n^ )
Cost of computing values = O(
n^ )
Total cost in worst case: O(
n^ )