Decrease and Conquer - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Decrease and Conquer

Description:

Reduce problem instance to smaller instance of the same problem ... Euclid's algorithm. Selection by partition. Design and Analysis of Algorithms - Chapter 5 ... – PowerPoint PPT presentation

Number of Views:146
Avg rating:3.0/5.0
Slides: 24
Provided by: Alek170
Category:

less

Transcript and Presenter's Notes

Title: Decrease and Conquer


1
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

2
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
  • Euclids algorithm
  • Selection by partition

3
Whats the difference?
  • Consider the problem of exponentiation Compute
    an
  • Brute Force
  • Divide and conquer
  • Decrease by one
  • Decrease by constant factor

4
Decrease by one
5
Decrease by constant factor
6
Insertion sort
7
Insertion sort
8
Graph Traversal
  • Many problems require processing all graph
    vertices in systematic fashion
  • Graph traversal algorithms
  • Depth-first search
  • Breadth-first search

9
Depth-first search
10
Depth-first search
11
Depth-first search
DFS(v) mark v visited define an empty stack S
S.push(v) while S is not empty u ? top of
S scan the neighbors of u looking for an
unvisited neighbor if an unvisited neighbor
(called x) is found then S.push(x) else
S.pop()
12
(No Transcript)
13
Breadth-first search
14
(No Transcript)
15
Depth-first search Notes
  • DFS can be implemented with graphs represented
    as
  • Adjacency matrices T(V2)
  • Adjacency linked lists T(VE)
  • 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

16
Breadth-first search
  • Explore graph moving across to all the neighbors
    of last visited vertex
  • Instead of a stack, breadth-first uses queue
  • Applications same as DFS, but can also find
    paths from a vertex to all other vertices with
    the smallest number of edges

17
Breadth-first search Notes
  • BFS has same efficiency as DFS and can be
    implemented with graphs represented as
  • Adjacency matrices T(V2)
  • Adjacency linked lists T(VE)
  • Yields single ordering of vertices (order
    added/deleted from queue is the same)

18
Directed acyclic graph (dag)
  • A directed graph with no cycles
  • Arise in modeling many problems, eg
  • prerequisite structure
  • food chains
  • Imply partial ordering on the domain

19
Topological sorting
C1, C2, C3, C4, C5 - courses
means A is a prerequisite for B
A
B
20
Topological sorting
21
Topological sorting
  • Problem find a total order consistent with a
    partial order
  • Example

tiger
Order them from lower to higher, consistent with
food chain)
human
fish
sheep
shrimp
Note problem is solvable iff graph is dag
wheat
plankton
22
Topological sorting
  • Problem find a total order consistent with a
    partial order
  • Example

tiger
Order them so that they dont 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
wheat
plankton
23
Topological sorting Algorithms
  • 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
  • Both T(VE) using adjacency linked lists
Write a Comment
User Comments (0)
About PowerShow.com