Documente Academic
Documente Profesional
Documente Cultură
Brute force
Divide-and-conquer
Decrease-and-conquer
Transform-and-conquer
Space-and-time tradeoffs
Dynamic programming
Greedy techniques
Important examples:
Selection sort, String matching, Convex-hull problem, and Exhaustive search.
Scan the list repeatedly to find the elements, one at a time, in an non-
decreasing order.
On the ith pass through the list, search for the smallest item among the last
n - i elements and swap it with A[i]. After n - 1 passes, the list is sorted.
n2 n1 n2 n2
(n 1)n
C(n) = 1 = [(n 1) (i + 1) + 1] = (n 1 i) = (n 2 )
i= 0 j= i+1 i= 0 i= 0
2
CSC 3102 0.6 B.B. Karki, LSU
Brute-Force String Matching
Align the pattern against the first m characters of the text and start matching the corresponding
pairs of characters from left to right until all m pairs match. But, if a mismatching pair is found,
then the pattern is shift one position to the right and character comparisons are resumed.
The goal is to find i - the index of the leftmost character of the first matching substring in the text
such that ti = p0,.ti+j = pj, .. Ti+m-1 = pm-1
In the worst case, the algorithm is (mn). But the average-case efficiency is shown to
be linear, i.e., (m + n) = (n) for searching in random texts.
CSC 3102 0.7 B.B. Karki, LSU
Convex-Hull Problem
P6
The convex hull of any P7
set S of n points is a
convex polygon with the P2
P8
vertices at some of the
points (called extreme P5
points) of S so that the P3
polygon contains all
points (either inside or on P4
its boundary)
A set of points on the
P1
plane is called convex if
for any two points in the
set, the entire line The convex hull for the set of eight points is the
segment with the end convex polygon with its vertices at P1, P5, P6,
points at P and Q P7 and P3
belongs to the set.
Find the equation of the straight line through two points (x1, y1) and (x2, y2)
ax + by = c, where a = y2 - y1, b = x1 - x2 and c = x1y2 - y1x2.
Examples:
Traveling salesman problem
Finding the shortest tour through a given set of n cities that visits each city
exactly once before returning to the city where it started.
Knapsack problem
Finding the most valuable list of out-of n items that fit into the knapscak.
Assignment problem
Finding an assignment of n people to execute n jobs with the smallest total
cost.
c d
Finding the shortest Hamiltonian circuit of 1
the graph
A cycle that passes through all the vertices of
the graph exactly once.
A sequence of n +1 adjacent vertices v0, v1, Tour Length
v2, vn-1, v0.
Get all tours by generating all the abcda 2+8+1+7 = 18
permutations of n - 1 intermediate cities, abdca 2+3+1+5 = 11
compute the tour lengths, and find the
shortest among them. acbda 23
acdba 11
adbca 23
Efficiency:
adcba 18
Total number of permutations = (n - 1)!/2
Impractical but ok very small values of n.
More than one optimal solutions.
CSC 3102 0.11 B.B. Karki, LSU
Knapsack Problem
Permutation cost
<2 1 3 4> 2+6+1+4 = 13
Generate all the permutations of integers 1, <2 1 4 3> 2+6+8+9 = 25
2, n, computing the total cost of each .
assignment by summing up the <3 1 2 4> 7+6+8+4 = 25
.
corresponding elements of the cost matrix, <4 1 2 3> 8+6+8+9 = 31
and finally select the one with smallest sum.
The optimal solution is <2 1 3 4> with the minimum cost
of 13. So person 1 to job 2, person 2 to job 1, person 3 to
Complexity: n! operations. job 3 and person 4 to job 4.
Selection sort: n2
String matching: mn
Exhaustive search
Traveling salesman problem: (n-1)!/2
Knapsack problem: 2n
Assignment problem: n!