19
CPSC 335 Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science University of Calgary Canada Modified from Marina’s lectures

Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

  • Upload
    others

  • View
    37

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

CPSC 335

Greedy methods and Backtracking

LECTURE 16

Jon Rokne Computer Science University of Calgary

Canada

Modified from Marina’s lectures

Page 2: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

• Optimal structure: optimal solution to problem consists of optimal solutions to subproblems.

• Overlapping subproblems: few subproblems in total, many recurring instances of each.

• Solve in “near linear” time through recursion, using a table to show state of problem at any given moment of time.

• Reduces computation time from exponential to linear in some cases.

Dynamic programming

Page 3: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

Greedy algorithm always makes the choice that is the locally optimal (best) at the moment. It can reduce complex problem to linearly solvable time, and can be much easier to code than dynamic programming solutions

3

Greedy methods

Page 4: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

Matrix Chain Multiplication - Review n  Recall how to multiply matrices. n  Given two matrices

n  The product is n  Time is

A

B

C

X =

)( ed×A )( fe×B

d

e

e

f

∑−

=

•=1

0

],[],[],[e

kjkkiji BAC

d

f

)(defO

Page 5: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

n  What about multiplying multiple matrices?

n  We know matrix multiplication is associative. Can we use this to help minimize calculations?

n  Sure, consider

n  If we multiply we do 4000 operations

n  But instead if we try we do 1575 operations

Matrix Chaining contd.

54321 AAAAAA ⋅⋅⋅⋅=

)35( ×A )1003( ×B )5100( ×C

( ) CBA ⋅⋅

)( CBA ⋅⋅

Page 6: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

How do we parenthesize

n  Brute force - try all ways to parenthesize

n  Find out which has smallest number of operations by doing multiplication, then pick the best one.

n  But how many ways can we parenthesize these? n  Equivalent to the number of ways to make a binary

tree with n nodes. This is see Catalan numbers.

nAAAAA ⋅⋅⋅⋅= …321

)2( nΩ

Page 7: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

Be greedy

n  We just learned that a greedy algorithm can sometimes work, let’s try.

n  Why not try starting with the product with the most operations.

n  Consider

n  If we do our greedy method we would compute

n  This is 2000 operations.

n  Try instead, which takes 1000 operations.

)105()510()105()510( ×××× DCBA

( ) ( )DCBA ⋅⋅⋅

( )( )DCBA ⋅⋅

Page 8: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

Another Greedy way n  Select the product with the fewest operations.

n  Consider

n  Here the new greedy method would compute

n  This is 109989+9900+108900=228789 operations

n  Try

n  Here we only need 9999+89991+89100=189090

)99100()1009()911()11101( ×××× DCBA

( )( )DCBA ⋅⋅⋅

( ) ( )DCBA ⋅⋅⋅

Page 9: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

n  Shortest-path problem and multiple instance knapsack problems can be solved using greedy approach.

n  Activity selection is another example.

9

Greedy methods

Page 10: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

Problem: Stampede midway problem. » Buy a wristband that lets you onto any ride. » Lots of rides, each starting and ending at different times. » Your goal: ride as many rides as possible. • Alternative goal that we don’t solve here: maximize time spent on rides

10

Greedy methods

Page 11: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

Let A be an optimal solution of S and let k be the minimum activity in A (i.e., the one with the earliest finish time). Then A - {k} is an optimal solution to S’ = {i ∈ S: si ≥ fk}. » In words: once activity #1 is selected, the problem reduces to finding an optimal solution for activity selection over activities in S compatible with #1.

11

Greedy algorithm

Page 12: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

The algorithm is following: » Sort the activities by finish time » Schedule the first activity » Then schedule the next activity in sorted list which starts after previous activity finishes » Repeat until no more activities

Idea: Always pick the shortest ride available at the time Greedy-choice Property. » A globally optimal solution can be arrived at by making a locally optimal (greedy) choice.

12

Greedy Algorithm

Page 13: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

n  Backtracking can reduce a NP compete problem to linear problem by only going through selected branches of the global solution.

13

Backtracking

Page 14: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

n  Graph-coloring problem n  8 queens problem n  4 knight problem n  Perfect hash function problems are examples of backtracking method

14

Backtracking

Page 15: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

15

Backtracking

n  The goal is to color vertices in a graph G={V,E} so that no 2 adjacent vertices have the same color. Partial 3-coloring problem means only 3 colors are considered.

n  Direct approach builds the tree of ALL possibilities in exponential time.

Page 16: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

16

Backtracking

n  Partial 3-coloring (3 colors) is solved by the following method: n  Color first vertex with 1st color, color next vertex with the next color,

check if those two vertices are adjacent, if not - coloring is legal, proceed to next vertex, if yes and color is the same – coloring is illegal, try next color for second vertex. If all colors tried and all colorings are illegal, backtrack, try next color for previous vertex etc.

n  Note: sometimes solution is impossible. n  Exponential O(3^n) complexity is reduced to O(n) on average.

Page 17: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

n  Finally, job scheduling problem can be solved by converting the algebraic relationship xi-xj>=c to directed graph with vertices xi, xj, direction from xj to xi, and edge cost (or time required to complete job xj) is c.

n  The problem of finding time (minimum) when the last activity can commence (i.e. all preceding activities has been completed) is then converted to longest path problem on the graph.

17

Job scheduling problem

Page 18: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

n  What are greedy methods and how multi-instance knapsack problem can be solved through them

n  How backtracking works for graph coloring

n  How job scheduling problem can be solved through directed graphs

18

Review questions

Page 19: Greedy methods and Backtracking - University of Calgarypages.cpsc.ucalgary.ca/~rokne/CPSC335/stuff/SLIDES... · Greedy methods and Backtracking LECTURE 16 Jon Rokne Computer Science

Thank You