DS286 AUG2016 L25-26 Algorithms
DS286 AUG2016 L25-26 Algorithms
Bangalore, India
भारतीय विज्ञान संस्थान
बं गलौर, भारत
DS286 | 2016-11-11,16
L25,26: Classes of
Algorithms
Yogesh Simmhan
simmhan@cds.iisc.ac.in
Algorithm classification
Algorithms that use a similar problem-solving
approach can be grouped together
‣ A classification scheme for algorithms
Classification is neither exhaustive nor disjoint
The purpose is not to be able to classify an
algorithm as one type or another, but to highlight
the various ways in which a problem can be
attacked
2
CDS.IISc.ac.in | Department of Computational and Data Sciences
3
CDS.IISc.ac.in | Department of Computational and Data Sciences
4
CDS.IISc.ac.in | Department of Computational and Data Sciences
algorithms ?
dead end
dead end
?
start ? ?
dead end
dead end
?
6
CDS.IISc.ac.in | Department of Computational and Data Sciences
mapColors = int[map.length];
int RED=0, GREEN=1, PINK=2, BLUE=3;
boolean explore(int country, int color) {
if (country >= map.length) return true;
if (okToColor(country, color)) {
mapColors[country] = color;
for (int i = RED; i <= BLUE; i++) {
if (explore(country + 1, i)) return
true;
} 0 1
} 4
2 3
return false;
6
} 5
8
CDS.IISc.ac.in | Department of Computational and Data Sciences
9
CDS.IISc.ac.in | Department of Computational and Data Sciences
Examples
Quicksort:
‣ Partition the array into two parts (smaller numbers in
one part, larger numbers in the other part)
‣ Quicksort each of the parts
‣ No additional work is required to combine the two
sorted parts
Mergesort:
‣ Cut the array in half, and mergesort each half
‣ Combine the two sorted arrays into a single sorted
array by merging them
10
CDS.IISc.ac.in | Department of Computational and Data Sciences
11
CDS.IISc.ac.in | Department of Computational and Data Sciences
Fibonacci numbers
ni = n(i-1) + n(i-2)
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
To find the nth Fibonacci number:
‣ If n is zero or one, return 1; otherwise,
‣ Compute fibonacci(n-1) and fibonacci(n-2)
‣ Return the sum of these two numbers
12
CDS.IISc.ac.in | Department of Computational and Data Sciences
13
CDS.IISc.ac.in | Department of Computational and Data Sciences
14
CDS.IISc.ac.in | Department of Computational and Data Sciences
Greedy algorithms
An optimization problem is one in which you want
to find, not just a solution, but the best solution
A “greedy algorithm” sometimes works well for
optimization problems
A greedy algorithm works in phases: At each phase:
‣ You take the best you can get right now, without regard
for future consequences
‣ You hope that by choosing a local optimum at each step,
you will end up at a global optimum
15
CDS.IISc.ac.in | Department of Computational and Data Sciences
16
CDS.IISc.ac.in | Department of Computational and Data Sciences
Other applications of
Greedy Algorithms
1. Shortest path problem
‣ A simple greedy strategy, Dijikstra’s greedy algorithm
‣ Greedily pick the shortest among the vertices touched
so far
2. 0/1 Knapsack problem on combinatorial optimization
‣ Pack a knapsack of weight capacity c
‣ Given n items with weight and profit, select items to
Maximize sum(pixi)
‣ Subject to constraints sum(wixi) <= c
© Keenan Pepper
http://lcm.csa.iisc.ernet.in/dsa/node187.html 20
CDS.IISc.ac.in | Department of Computational and Data Sciences
Least cost
Node Total cost
edges
http://lcm.csa.iisc.ernet.in/dsa/node187.html 22
CDS.IISc.ac.in | Department of Computational and Data Sciences
23
CDS.IISc.ac.in | Department of Computational and Data Sciences
24
CDS.IISc.ac.in | Department of Computational and Data Sciences
Randomized algorithms
A randomized algorithm uses a random number at
least once during the computation to make a
decision
‣ Example: In Quicksort, using a random number to
choose a pivot
‣ Example: Trying to factor a large number by choosing
random numbers as possible divisors
25
CDS.IISc.ac.in | Department of Computational and Data Sciences
Dynamic
Programming
Lecture 11: Dynamic Programming, Avrim Blum
https://www.cs.cmu.edu/~avrim/451f09/lectures/lect1001.pdf
26
CDS.IISc.ac.in | Department of Computational and Data Sciences
Dynamic Programming
General approach to solving problems
‣ general method like “divide-and-conquer”
Unlike divide-and-conquer, the subproblems will
typically overlap
Basic Idea (version 1): take our problem and break
it into a reasonable number of subproblems (O(n2))
that can be optimally solved to give the optimal
solution to the larger one.
Unlike divide-and-conquer (as in mergesort or
quicksort) it is OK if our subproblems overlap, so
long as there are not too many of them.
27
CDS.IISc.ac.in | Department of Computational and Data Sciences
Longest Common
Subsequence (LCS)
We are given two strings: string S of length n, and
string T of length m. Our goal is to produce their
longest common subsequence: the longest sequence
of characters that appear left-to-right (but not
necessarily in a contiguous block) in both strings.
‣ Genomics, “diff” in code repositories (edit distance)
S = ABAZDC
T = BACBAD
LCS = ABAD
http://www.columbia.edu/~cs2035/courses/csor4231.F11/lcs.pdf
28
CDS.IISc.ac.in | Department of Computational and Data Sciences
LCS
Say LCS[i,j] is the length of the LCS of S[1..i] with
T[1..j]. How can we solve for LCS[i,j] in terms of the
LCS’s of the smaller problems?
Case 1: S[i] <> T[j]
‣ The subsequence has to ignore one of S[i] or T[j]
‣ LCS[i, j] = max(LCS[i − 1, j], LCS[i, j − 1])
Case 2: S[i] = T[j]
‣ The LCS of S[1..i] and T[1..j] might as well match them
up.
‣ A common subsequence that matched S[i] to an earlier
location in T could always match it to T[j] instead
‣ LCS[i, j] = 1 + LCS[i − 1, j − 1]
29
CDS.IISc.ac.in | Department of Computational and Data Sciences
LCS
Traceback
D A B A
and reverse
A B A D
R = (GAC), and C = (AGCAT)
Memoization
Basic Idea (version 2): Suppose you have a
recurrence where many of the subproblems in the
recursion tree are the same. Then you can get a
savings only if you store your computations so that
you compute each different subproblem just once.
You can store these solutions in an array or hash
table. This is called memoizing.
31
CDS.IISc.ac.in | Department of Computational and Data Sciences
Complexity is O(mn)
(Size of array)
32
CDS.IISc.ac.in | Department of Computational and Data Sciences
Knapsack Problem
We are given a set of n items, where each item i is
specified by a size si and a value vi. We are also
given a size bound S (the size of our knapsack).
The goal is to find the subset of items of maximum
total value such that sum of their sizes is at most S
(they all fit into the knapsack).
‣ Exponential time to try all possible subsets
‣ O(n.S) using DP
33
CDS.IISc.ac.in | Department of Computational and Data Sciences
Knapsack Problem
0-1 Knapsack:
‣ n items (can be the same or different)
‣ Have only one of each
‣ Must leave or take (i.e. 0-1) each item (e.g. bars of gold)
‣ DP works, greedy does not
Fractional Knapsack:
‣ n items (can be the same or different)
‣ Can take fractional part of each item (e.g. gold dust)
‣ Greedy works and DP algorithms work
34
http://www.radford.edu/~nokie/classes/360/greedy.html
CDS.IISc.ac.in | Department of Computational and Data Sciences
Greedy Solution 1
From the remaining objects, select the object with
maximum value that fits into the knapsack
Does not guarantee an optimal solution
E.g., n=3, s=[100,10,10], v=[20,15,15], S=105
Greedy Solution 2
Select the one with minimum size that fits into the
knapsack
Also, does not guarantee optimal solution
E.g., n=2, s=[10,20], v=[5,100], c=25
Greedy Solution 3
Select the one with the maximum value density vi/si
that fits into the knapsack
E.g., n=3, s=[20,15,15], v=[40,25,25], c=30
Greedy works…if fractional items possible!
38
CDS.IISc.ac.in | Department of Computational and Data Sciences
Reading
Online resources on algorithm types
https://www.cs.cmu.edu/~avrim/451f09/lectures/l
ect1001.pdf
39