Final Paper
Final Paper
MS150400809
Greedy Algorithm for Optimal Solution:
• OPTIMAL-BST(p, q, n)
• for i ← 1 to n + 1
• do e[i, i- 1] ← qi-1.
• w[i, i- 1] ← qi-1.
• for l ← 1 to n
• do for i ← 1 to n-l + 1
• do j ←i + l-1
• e[i, j ]←∞
• w[i, j ] ← w[i, j-1] + pj + qj
• for r ←i to j
• do t ← e[i, r-1] + e[r + 1, j ] + w[i, j ]
• if t < e[i, j ]
• then e[i, j ] ← t
• root[i, j ] ←r
• return e and root
Algorithm: Fractional Knapsack problem:
Fractional-Knapsack (W, v[n], w[n])
1. While w > 0 and as long as there are items remaining
2. pick item with maximum vi/wi
3. xi ¬ min (1, w/wi)
4. remove item i from list
5. w ¬ w – xiwi
• w the amount of space remaining in the knapsack (w = W)
• Running time: Q(n) if items already ordered; else Q(nlgn)
Pseudo Code For Road Trip Problem:
Greedy algorithm
1. for i = 1 to n do
2. if di - di-1 > k then “do not use this car”
3. S = d0
4. last = d0 (the distance of the last item in S)
5. dn+1 =∞ (forces dn to be in S)
6. for i = 1 to n do
7. if di+1 - last > k then
8. S := S È di
9. last := di
Breath First Search Algorithm:
BFS(G, s)
1 for each vertex u e V [G] – {s}
2 do color [u] ← WHITE
3 d [u] ← ∞
4 π[u] ← NIL
5 color[s] ← GRAY
6 d [s] ← 0
7 π[s] ← NIL
8 Q←Ø /* Q always contains the set of GRAY vertices */
9 ENQUEUE (Q, s)
10 while Q ≠ Ø
11 do u ← DEQUEUE (Q)
12 for each v e Adj [u]
13 do if color [v] = WHITE /* For undiscovered vertex. */
14 then color [v] ← GRAY
15 d [v] ← d [u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color [u] ← BLACK
Breath First Search Algorithm(Shortest Path):
BFS-Shortest-Paths (G, s)
1 AveV
2 d [v] ← ∞
3 d [s] ← 0
4 ENQUEUE (Q, s)
5 while Q ≠ φ
6 do v ← DEQUEUE(Q)
7 for each w in Adj[v]
8 do if d [w] = ∞
9 then d [w] ← d [v] +1
10 ENQUEUE (Q, w)
Kruskal's Algorithm:
MST-KRUSKAL (G, w)
1 A←Ø
2 for each vertex vÎ V[G]
3 do MAKE-SET(v)
4 sort edges in non-decreasing order by weight w
5 for each (u, v) in non-decreasing order by weight
6 do if FIND-SET(u) ≠ FIND-SET(v)
7 then A ← A U {(u, v)}
8 UNION (u, v)
9 return A
Total Running time = O (E lg V),
Prim's Algorithm:
MST-PRIM (G, w, r)
1 for each u e V [G]
2 do key[u] ← ∞
3 π[u] ← NIL
4 key[r] ← 0
5 Q ← V [G]
6 while Q ≠ Ø
7 do u ← EXTRACT-MIN(Q)
8 for each v e Adj[u]
9 do if v e Q and w(u, v) < key[v]
10 then π[v] ← u
11 key[v] ← w(u, v)
Dijkstra's Algorithm:
DIJKSTRA(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2 S←Ø
3 Q ← V[G]
4 while Q ≠ Ø
5 do u ← EXTRACT-MIN(Q)
6 S ← S U {u}
7 for each vertex v e Adj[u]
8 do RELAX (u, v, w)
For any integers a, b, and p, if both gcd(a, p) = 1 and gcd(b, p) = 1, then gcd(ab, p) = 1.
Proof
• As, gcd(a, p) = 1, there exist integers x, y such that
ax + py = 1 (1)
• gcd(b, p) = 1, there exist integers x’, y’ such that bx’ + py’ = 1 (2)
• Multiplying equations (1) and (2) and rearranging, ab(x x′) + p(ybx′ + y′ax + pyy′) = 1, abx’’ + py’’
=1
• Since 1 is a positive linear combination of ab and p,
• Hence gcd(ab , p) = 1,which completes the proof
RABIN-KARP-MATCHER(T, P, d, q)
1 n ← length[T]
2 m ← length[P]
3 h ← dm-1 mod q
4 p←0
5 t0 ← 0
6 for i ← 1 to m t Preprocessing.
7 do p ← (dp + P[i]) mod q
8 t0 ← (dt0 + T[i]) mod q
9 for s ← 0 to n - m t Matching.
10 do if p = ts
11 then if P[1 .. m] = T [s + 1 .. s + m]
12 then print "Pattern occurs with shift" s
13 if s < n - m
14 then ts+1 ← (d(ts - T[s + 1]h) + T[s + m + 1]) mod q
SAT is NP-complete.
Proof:
• SAT belongs to NP.
– Given a satisfying assignment
– Verifying algorithm replaces each variable with its value, and evaluates formula in
polynomial time.
• SAT is NP-hard
– Sufficient to show that CIRCUIT-SAT£p SAT
• CIRCUIT-SAT£p SAT, i.e., any instance of circuit satisfiability can be reduced in polynomial time
to an instance of formula satisfiability.
• Intuitive induction:
– Look at the gate that produces the circuit output.
– Inductively express each of gate’s inputs as formulas.
Formula for circuit is obtained by writing an expression that applies gate’s function to its input
formulas.
• Unfortunately, this is not a polynomial time reduction
• This is because the gate whose output is fed to 2 or more inputs
of other gates, cause size to grow exponentially.
3-CNF-SAT is NP Complete
Proof:
3-CNF-SAT Î NP
• 3-CNF-SAT is NP-hard.
• SAT £p3-CNF-SAT?
– Suppose f is any boolean formula, Construct a binary ‘parse’ tree, with literals as leaves
and connectives as internal nodes.
– Introduce yi for output of each internal node.
– Rewrite formula to f‘: AND of root and conjunction of clauses describing operation of
each node.
– In f', each clause has at most three literals.
• Change each clause into conjunctive normal form as:
– Construct a true table, (at most 8 by 4)
– Write disjunctive normal form for items evaluating 0
– Using DeMorgan law to change to CNF.
• Result: f'' in CNF but each clause has 3 or less literals.
• Change 1 or 2-literal clause into 3-literal clause as:
– Two literals:
(l1Ú l2), change it to (l1Ú l2 Úp) Ù (l1Ú l2 ÚØp).
– If a clause has one literal l, change it to (lÚpÚq)Ù(lÚpÚØq)Ù (lÚØpÚq)Ù (lÚØpÚØq).
1 if v = s
2 then print s
3 else if π[v] = NIL
4 then print “no path from s to v exists
5 else PRINT-PATH (G, s, π[v])
6 print v