1 / 30

# Decrease and Conquer - PowerPoint PPT Presentation

Decrease and Conquer. Reduce problem instance to smaller instance of the same problem 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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Decrease and Conquer' - hera

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

• Reduce problem instance to smaller instance of the same problem

• Solve smaller instance

• Extend solution of smaller instance to obtain solution to original problem

• Also referred to as inductive or incremental approach

Design and Analysis of Algorithms - Chapter 5

• 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

Design and Analysis of Algorithms - Chapter 5

Consider the problem of exponentiation: Compute an

• Brute Force:

• Divide and conquer:

• Decrease by one:

• Decrease by constant factor:

Design and Analysis of Algorithms - Chapter 5

Algorithm InsertionSort(A[0..n-1])

for i1 to n-1 do

vA[i]; ji-1;

while j>=0 and A[j]>v do

A[j+1]A[j]; jj-1

A[j+1]v

Improvements: sentinel, binary insertion, Shellsort

Efficiency

Design and Analysis of Algorithms - Chapter 5

• Many problems require processing all graph vertices in systematic fashion

• Graph traversal algorithms:

• Depth-first search

Design and Analysis of Algorithms - Chapter 5

• Explore a graph G=(V,E) always moving away from last visited vertex

• Similar to preorder tree traversals

• Algorithm DFS(G)

count :=0

mark each vertex with 0 (unvisited)

for each vertex v∈V do

if v is marked with 0

dfs(v)

dfs(v)

count  count + 1

mark v with count

for each vertex w adjacent to vdo

if w is marked with 0

dfs(w)

Design and Analysis of Algorithms - Chapter 5

b

c

d

e

f

g

h

Example – undirected graph

• Depth-first traversal:

Design and Analysis of Algorithms - Chapter 5

• Tree edges: edges comprising forest

• Back edges: edges to ancestor nodes

• Forward edges: edges to descendants (digraphs only)

• Cross edges: none of the above

Design and Analysis of Algorithms - Chapter 5

• Depth-first traversal:

a

b

c

d

e

f

g

h

Design and Analysis of Algorithms - Chapter 5

• DFS can be implemented with graphs represented as:

• 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 (Hopcroft-Tarjan):

• checking connectivity, finding connected components

• checking acyclicity, articulation points

• searching state-space of problems for solution (AI)

Design and Analysis of Algorithms - Chapter 5

• Explore graph moving across to all the neighbors of last visited vertex

• Similar to level-by-level tree traversals

• Applications: same as DFS, but can also find paths from a vertex to all other vertices with the smallest number of edges

Design and Analysis of Algorithms - Chapter 5

• Algorithm BFS(G)

• count 0

• mark each vertex with 0

• for each vertex v ∈ V do bfs(v)

bfs(v)

count  count + 1

mark v with count

initialize queue with v

while queue is not empty do

a front of queue

for each vertex w adjacent to a do

if w is marked with 0

count  count + 1

mark w with count

add w to the end of the queue

remove a from the front of the queue

Design and Analysis of Algorithms - Chapter 5

b

c

d

e

f

g

h

Example – undirected graph

Design and Analysis of Algorithms - Chapter 5

a

b

c

d

e

f

g

h

Design and Analysis of Algorithms - Chapter 5

• BFS has same efficiency as DFS and can be implemented with graphs represented as:

• Yields single ordering of vertices (order added/ deleted from queue is the same)

Design and Analysis of Algorithms - Chapter 5

• A directed graph with no cycles

• Arise in modeling many problems, eg:

• prerequisite structure

• food chains

• Imply partial ordering on the domain

Design and Analysis of Algorithms - Chapter 5

• Problem: find a total order consistent with a partial order

tiger

Order them so that they don’t have to wait for any of their food (i.e., from lower to higher, consistent with food chain)

human

fish

sheep

shrimp

NB: problem is solvable iff graph is dag

plankton

wheat

Design and Analysis of Algorithms - Chapter 5

• DFS-based algorithm:

• DFS traversal noting order vertices are popped off stack

• Reverse order solves topological sorting

• Back edges encountered?→ NOT a dag!

• Source removal algorithm

• Repeatedly identify and remove a source vertex, ie, a vertex that has no incoming edges

Design and Analysis of Algorithms - Chapter 5

Useful in exhaustive search algorithms

Minimal-change requirement

Given: a permutation for k-1 objects

Method: Generate a new permutation by inserting the k-th object in all possible places, moving in one direction (e.g. left to right). Then, insert the (k+1)-th object by alternating the direction (right to left)

Example

Design and Analysis of Algorithms - Chapter 5

Concept: use arrows to show the moving direction of an object. An object is called mobile if it points to a smaller object. No need to create smaller instances.

Algorithm Johnson-Trotter

Initialize the first permutation 1 2 3 … n

while there exist a mobile integer k do

find the largest mobile integer k

swap k and the adjacent integer its arrow points to

reverse the direction of all integers that are larger than k

Example 123, 132, 312, 321, 231, 213

Efficiency

Design and Analysis of Algorithms - Chapter 5

The Johnson-Trotter algorithm does not produce permutations in lexicographical order

Example 123, 132, 213, 231, 312, 321

Idea: start form the end and check an-1<an. Then consider the an-2 position and put the smallest out of the three digits, the remaining two in increasing order. Then backtrack.

Design and Analysis of Algorithms - Chapter 5

Useful in the knapsack problem (exhaustive solution).

Power set : a set of 2n sets

(Decrease-by-one) Idea: create the powerset with {1,2,…, n-1} and then insert n in each set off them.

Example:

Design and Analysis of Algorithms - Chapter 5

Use of bitstring to represent sets: 101 ≈ {a1, a3}

Example: for 3 objects, we have 8 sets represented with 000, 001, 010, …, 110, 111.

Extensions

• no lexicographical order in terms of objects; squashed order

• minimal change order; Gray code;000, 001, 011, 010, 110, 111, 101, 100

Design and Analysis of Algorithms - Chapter 5

• Among n identical looking coins, one is fake.Assume that the fake is lighter.

• Divide the coins in two piles of n/2 coins (plus 1 if n is odd)

• Recurrence relation:

• What if we divide in three piles ?

Design and Analysis of Algorithms - Chapter 5

• Or the Russian peasant method

• Let n and m two positive integers.

• If n is even, then : n . m = n/2 . 2m

• If n is odd, then : n . m = (n-1)/2 . 2m + m

• Example : 50 . 65

Design and Analysis of Algorithms - Chapter 5

• Historic problem

• N men are going to perish one by one, where each man dispatches his neighbor. Find the last one who survives.

• Example for N = 6 , 7

• Recurrence for the position of the survivor

Design and Analysis of Algorithms - Chapter 5

• Order statistics: find the k-th number

• One solution is to sort and pick the k-th

• Better to partition ala quicksort

• Example : 4, 1, 10, 9, 7, 12, 8, 2, 15

• Efficiency in best, worst and average case

Design and Analysis of Algorithms - Chapter 5

• Binary search is not used as panacea

• Given a sorted array A, with A[l] the minimum, A[r] the maximum. Find the position of value v.

• Solution : try at position x = l + (v-A[l])(r-l)/(A[r]-A[l])

• Efficiency in best, worst and average case

Design and Analysis of Algorithms - Chapter 5

• Arrange keys in a binary tree with the binary search tree property:

Example 1: 5, 10, 3, 1, 7, 12, 9

Example 2: 4, 5, 7, 2, 1, 3, 6

k

<k

>k

Design and Analysis of Algorithms - Chapter 5

• Searching – straightforward

• Insertion – search for key, insert at leaf where search terminated

• All operations: worst case # key comparisons = h+1

• lgn ≤ h ≤ n–1 with average (random files) 1.41 lg n

• Thus all operations have:

• worst case: Θ(n)

• average case: Θ(lgn)

• Bonus: inorder traversal produces sorted list (treesort)

Design and Analysis of Algorithms - Chapter 5