1 / 14

CPSC 335

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,

brucep
Télécharger la présentation

CPSC 335

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPSC 335 Greedy methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada

  2. 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

  3. 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

  4. Greedy methods • Shortest-path problem and multiple instance knapsack problems can be solved using greedy approach • Activity selection is another example

  5. 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

  6. 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

  7. 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.

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

  9. Backtracking • Graph-coloring problem • 8 queens problem • 4 knight problem • Perfect hash function problems are examples of backtracking method

  10. 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.

  11. 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.

  12. Job scheduling problem • 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. • 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.

  13. 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

  14. Thank You

More Related