CPSC 335 Greedy methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada Dynamic programming 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 • • • • • Greedy methods 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 3 Greedy methods Shortest-path problem and multiple instance knapsack problems can be solved using greedy approach Activity selection is another example 4 Greedy methods 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 5 Greedy algorithm 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 6 Greedy Algorithm 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. 7 Backtracking Backtracking can reduce a NP compete problem to linear problem by only going through selected branches of the global solution. 8 Backtracking Graph-coloring problem 8 queens problem 4 knight problem Perfect hash function problems are examples of backtracking method 9 Backtracking 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. Direct approach builds the tree of ALL possibilities in exponential time. 10 Backtracking Partial 3-coloring (3 colors) is solved by the following method: 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. Note: sometimes solution is impossible. Exponential O(3^n) complexity is reduced to O(n) on average. 11 Job scheduling problem Finally, job scheduling problem can be solved by converting the algebraic relationship xixj>=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. 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. 12 Review questions What are greedy methods and how multi-instance knapsack problem can be solved through them How backtracking works for graph coloring How job scheduling problem can be solved through directed graphs 13 Thank You