lect12

advertisement
Now, Chapter 5: Decrease and Conquer
Reduce problem instance to smaller instance of the same
problem and extend solution
Solve smaller instance
Extend solution of smaller instance to obtain solution to
original problem
Also referred to as inductive or incremental approach
Examples of Decrease and Conquer
Decrease by one:
• Insertion sort
• Graph search algorithms:
– DFS
– BFS
– Topological sorting
• Algorithms for generating permutations, subsets
Decrease by a constant factor
• Binary search
• Fake-coin problems
• multiplication à la russe
• Josephus problem
Variable-size decrease
• Euclid’s algorithm
• Selection by partition
What’s the difference?
problem of size n
Consider the problem of
exponentiation: Compute an
Decrease-by-a-constant
 f (n  1)  a if n  1
f (n)  
a
if n  1

subproblem
of size n-1
solution to
the subproblem
solution to
the original problem
problem
of size n
Decrease-by-a-constant-factor
subproblem
of size n/2
solution to
the subproblem
 [ f (n / 2)]2
if n is e ve nandpositive

f (n )  [ f ((n  1) / 2)]2  a if n is odd andgre ate rthan1

a
if n  1

solution to
the original problem
Variable-size-decrease
A size reduction pattern varies from one iteration of an
algorithm to another
Example: Euclid’s algorithm for computing the greatest
common divisor
gcd(m,n) = gcd(n, m mod n)
The arguments on the right-hand side are always smaller than
those on the left-hand side
But they are not smaller neither by a constant nor by a
constant factor
Insertion Sort
We have talked about this algorithm before
This is a typical decrease-by-one technique
Assume A[0..i-1] has been sorted, how to achieve the sorted
A[0..i]?
Solution: insert the last element A[i] to the right position
A[0]
...
A[j]
smaller than or equal toA[i]
Algorithm complexity:
A[j+1]
...
A[i-1]
A[i]
...
A[n-1]
greater than A[i]
Tworst (n)  (n2 ), Tbest (n)  (n)
Graph Traversal
Many problems require processing all graph vertices in
systematic fashion
Graph traversal algorithms:
• Depth-first search (DFS)
• Breadth-first search (BFS)
They can be treated as decrease-by-one strategy.
Depth-first search (DFS)
Explore graph
always moving
away from last
visited vertex,
similar to preorder
tree traversals
ALGO RITHMDFS(G )
mark each vertexin V with 0 // label as unvisited
count  0
for each vertexv in V do
if v is markedwith 0
Pseudocode for
dfs(v )
Depth-first-search
of graph G=(V,E) /////////////////////////////////////////////////////
dfs(v )
count  count 1; mark v with count
for each vertexw in V ajacent tov do
if w is markedwith 0
dfs( w)
Example – Undirected Graph
g
h
a
e6, 2
e
c
d
j
f
d 3,1
b
i
Input Graph
(Adjacency matrix /
linked list
b5,3
f 4,4
j10 ,7
i9 ,8
c2 ,5
h8,9
a1,6
g 7 ,10
Stack push/pop
a
g
c
h
d
f
i
b
j
e
DFS forest
(Tree edge /
Back edge)
Example – Directed Graph (Digraph)
a
b
c
d
e
f
g
h
DFS forest may also contain forward edges: edges to descendants
(digraphs only) and cross edges (all the edges that are not
tree/back/forward edges)
DFS Forest and Stack
a
f 3,1
b2, 4
a1,6
h5, 2
g 4,3
c
b
e
f
g
e6,5 d 8,7
c7,8
Stack push/pop
h
d
DFS: Notes
DFS can be implemented with graphs represented as:
• Adjacency matrices: Θ(V2)
• Adjacency linked lists: Θ(V+E)
Yields two distinct ordering of vertices:
• preorder: as vertices are first encountered (pushed onto stack)
• postorder: as vertices become dead-ends (popped off stack)
Applications:
• checking connectivity, finding connected components
• checking acyclicity
• searching state-space of problems for solution (AI)
Problem 3
Design a decrease-by-one algorithm for generating power set
of a set of n elements.
{a, b, c}
{} {a} {b} {c} {ab} {ac} {bc} {a b c}
Download