Professional Documents
Culture Documents
widely applicable
easy
good for small problem sizes
Con:
Selection sort
Bubble sort
Sequential search:
Depth-first search:
TSP Example
2
a
5
8
c
b
7
3
d
a,b,c,d,a -> 18
a,b,d,c,a -> 11
a,c,b,d,a -> 23
a,d,b,c,a -> 23
a,d,c,b,a -> 18
TSP Applications
tsp.gatech.edu/apps/index.html
CSCE 411, Spring 2013: Set 2
8
iris.gmu.edu/~khoffman/papers/trav_salesman.html
Knapsack Problem:
Knapsack Applications
10
Knapsack Example
item 1: 7
item 2: 3
item 3: 4
item 4: 5
W = 10
lbs,
lbs,
lbs,
lbs,
$42
$12
$40
$25
need to check 16
possibilities
subset
total
weight
total
value
$0
{1}
$42
{2}
$12
{3}
$40
{4}
$25
{1,2}
10
$54
{1,3}
11
infeasible
{1,4}
12
infeasible
{2,3}
$52
{2,4}
$37
etc.
CSCE 411, Spring 2013: Set 2
11
Closest-Pair Problem:
Applications:
12
Convex Hull Problem: Given a set of points in 2D, find the smallest convex polygon s.t. each
point in the set is enclosed by the polygon
polygon: sequence of line segments that ends
where it begins
convex: all points on a line segment between
2 points in the polygon are also in the polygon
14
15
The 2 pts are on the convex hull iff all other pts
are on same side of this line:
16
17
Problem: Compute an
Solution: Multiply a by itself n-1 times
Takes (n) time, assuming each
multiplication takes constant time.
Problem: multiply two nxn matrices A and B
to create product matrix C
Solution: Follow the definition, which says
the (i,j) entry of C is aik*bkj , k = 1 to n
18
Brute Force/Exhaustive
Search Summary
19
Applications of DFS
topological sort
finding strongly connected components of
a graph
20
Depth-First Search
Input: G = (V,E)
for each vertex u in V do
mark u as unvisited
parent[u] := nil
time := 0
for each unvisited
vertex u in V do
parent[u] := u // a root
call recursive DFS(u)
recursiveDFS(u):
mark u as visited
time++
disc[u] := time
for each unvisited
neighbor v of u do
parent[v] := u
call recursiveDFS(v)
time++
fin[u] := time
21
Nested Intervals
Corollary: v is a descendant of u in
the DFS forest if and only if v's
interval is inside u's interval.
CSCE 411, Spring 2013: Set 2
22
Classifying Edges
23
Example of Classifying
Edges
a tree
in DFS
forest
not in DFS
forest
forward
tree
tree
cross
back
tree
back
24
DFS Application:
Topological Sort
25
Precedence Example
26
Precedence Example
get glass
pour juice
get bowl
pour cereal
pour milk
get spoon
eat breakfast
Order: glass, juice, bowl, cereal, milk, spoon, eat.
CSCE 411, Spring 2013: Set 2
27
Why Acyclic?
28
1 2 3 4 5
eat
juice
glass
7 8 9 10
11 12
13 14
milk
cereal
spoon
bowl
29
30
Correctness of T.S.
Algorithm
Show that if (u,v) is an edge, then v finishes
before u finishes. Thus the algorithm
correctly orders u before v.
disc(u)
v
u
31
Correctness of T.S.
Algorithm
Show that if (u,v) is an edge, then v finishes
before u finishes. Thus the algorithm
correctly orders u before v.
disc(u)
fin(u)
fin(v)
v
u
32
Correctness of T.S.
Algorithm
33
34
35
SCC Example
h
four SCCs
36
37
38
SCC Algorithm
input: directed graph G = (V,E)
1. call DFS(G) to compute finishing
times
2. compute GT // transpose graph
3. call DFS(GT), considering vertices in
decreasing order of finishing times
4. each tree from Step 3 is a separate
SCC of G
CSCE 411, Spring 2013: Set 2
39
40
1 2 3 4 5
c
d
b
fin(f)
fin(g)
fin(h)
fin(e)
fin(a)
fin(b)
fin(c)
fin(d)
After Step 1
7 8 9 10 11 12 13 14 15 16
e
h
g
f
41
After Step 2
h
42
After Step 3
1 2 3 4 5
g
h
f
7 8 9 10 11 12 13 14 15 16
e
a
c
b
43
44
Correctness of SCC
Algorithm
45
Example of Component
Graph
{a,e}
{f,h,g}
{d}
{b,c}
46
47
48
Proof of Lemma
C'
49
Proof of Lemma
C'
50
51
52
G:
u C
u C
z
z
C'
C'
53
u C
z
w
54