Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Branch and Bound

Job Selection: O(m*n)

Algorithm Reduce(p. w, n, m, /1, /2)


/ Variables are as described in the discussion.
// p(il/w(i] >= p[i + 1]/w[i + 1], 1 <i<n.
{
I1:= I2 := 0;
q := Lbb(0,0);
k:= largest j such that w[1] + ... + w[j] < m;
for i:= 1 to k do
{
if (Ubb(theta, (i)) < q) then I1 := I1 union {i};
else if (Lbb(0, (i}) > 4) then q := Lbb(theta, {i));
}
for i = k+ 1 to n do
if (Ubb({i), theta) < q) then I2 := I2U {i};
else if (Lbb((i),theta) > q) then q:= Lbb({i}, 0);

}
}

Job Sequencing: O(m*n)

1. Sort jobs by profit in descending order.


2. max_deadline = maximum deadline among all jobs
3. Create a schedule array of length max_deadline, initialized with -1
indicating empty slot.
4. max_profit = 0
5. for each job in the sorted array:
a. for i = job.deadline - 1 down to 0:
if schedule[i] is empty:
schedule[i] = job.id
max_profit += job.profit
break
b. end for
6. end for
7. Output the scheduled jobs and maximum profit.

0/1 Knapsack : O(N)

Data: W,01, 02, 03, ..., Un , WI, W2, W3, ..., Wn


Result: M [n, W]
M(0, w] <- 0, for all w 0 to W
M [i, 0] <-0, for all i 0 to n
for i<-1 to n do
for w<-1 to W do
if wi < = w then
M[i, w] = max(M [i - 1, w – Wi ]+ vi, M[i - 1,w])
else
M[i, w] = M[i - 1, w]
end
end
end
return M [n, W]
String matching algorithms

Naive String Matching (T,P)


n=T.length
m=P.length
for s=0 to n-m
if P[1..m]==T[s+1….s+m]
print “Pattern occur with shift” s

Rabin Karp
Network Flow
Ford Fulkerson

Edmonds Karp

inputs
C[n x n] : Capacity Matrix
E[n x n] : Adjacency Matrix
s : source
t : sink
output
f : maximum flow
Edmonds-Karp:
f=0 // Flow is initially 0
F = [n x n] // residual capacity array
while true:
m, P = Breadth-First-Search(C, E, s, t, F)
if m = 0:
break
f=f+m
v=t
while v != s:
u = P[v]
F[u, v] = F[u, v] - m //This is reducing the residual capacity of the au
gmenting path
F[v, u] = F[v, u] + m //This is increasing the residual capacity of the
reverse edges
v=u
return f
Geometric Algorithms
Graham’s Scan : O(n log n)

Jarvis March: O(nh) time complexity


Randomized algorithms
Randomized Quick Sort: O(n log n)

Approximation Algorithm
Travelling Salesman :O(N^3)
Algorithm: Traveling-Salesman-Problem
C ({1}, 1) = 0
for s = 2 to n do
for all subsets S Є {1, 2, 3, … , n} of size s and containing 1
C (S, 1) = ∞
for all j Є S and j ≠ 1
C (S, j) = min {C (S – {j}, i) + d(i, j) for i Є S and i ≠ j}
Return minj C ({1, 2, 3, …, n}, j) + d(j, i)

You might also like