Docsity

Algorithm Strategies: Recursive, Backtracking, Divide & Conquer, Dynamic Programming, Exams of Computer Science

Partial preview of the text

Download Algorithm Strategies: Recursive, Backtracking, Divide & Conquer, Dynamic Programming and more Exams Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Algorithm Strategies

Department of Computer Science

University of Maryland, College Park

Course Evaluations The CourseEvalUM system will be open for student participation Tuesday,April 28th, through Wednesday, May 13th. Please as soon as possible complete the evaluations for this course. We consider them extremely important. WHAT YOU GET FROM COMPLETING THE EVALUATION (READ) One hundred undergraduates who submit all of their Spring 2009 CourseEvalUM evaluations will be randomly selected to register first in the fall for Spring 2010 courses. Here is the flyer with this information: https://www.irpa.umd.edu/Assessment/...romo_flyer.pdf WHERE TO GO TO COMPLETE THE EVALUATION https://www.courseevalum.umd.edu/portal As always, any students who submit all of their evaluations through CourseEvalUM will be able to see the student-view results posted at Testudo.

Problem Type

Satisfying

Find any satisfactory solution Example → Find path from A to F

Optimization

Find best solution (vs. cost metric) Example → Find shortest path from A to E

A

C

E

D

B 6 4

F

Some Algorithm Strategies

Recursive algorithms

Backtracking algorithms

Divide and conquer algorithms

Dynamic programming algorithms

Greedy algorithms

Brute force algorithms

Branch and bound algorithms

Heuristic algorithms

Backtracking Algorithm

Based on depth-first recursive search

Approach

  1. Tests whether solution has been found
  2. If found solution, return it
  3. Else for each choice that can be made a) Make that choice b) Recur c) If recursion returns a solution, return it
  4. If no choices remain, return failure

Tree of alternatives → search tree

Backtracking Algorithm – Reachability

Find path in graph from A to F

  1. Start with currentNode = A
  2. If currentNode has edge to F, return path
  3. Else select neighbor node X for currentNode Recursively find path from X to F If path found, return path Else repeat for different X Return false if no path from any neighbor X

Backtracking Algorithm – Map Coloring Color a map using four colors so adjacent regions do not share the same color. Coloring map of countries If all countries have been colored return success Else for each color c of four colors and country n If country n is not adjacent to a country that has been colored c Color country n with color c Recursively color country n+ If successful, return success Return failure Map from wikipedia – http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Map_of_USA_with_state_names.svg/650px- Map_of_USA_with_state_names.svg.png

Divide and Conquer

Based on dividing problem into subproblems

Approach

  1. Divide problem into smaller subproblems Subproblems must be of same type Subproblems do not need to overlap
  2. Solve each subproblem recursively
  3. Combine solutions to solve original problem

Usually contains two or more recursive calls

Dynamic Programming Algorithm

Based on remembering past results

Approach

  1. Divide problem into smaller subproblems Subproblems must be of same type Subproblems must overlap
  2. Solve each subproblem recursively May simply look up solution (if previously solved)
  3. Combine solutions into to solve original problem
  4. Store solution to problem

Generally applied to optimization problems

Fibonacci Algorithm

Fibonacci numbers

fibonacci(0) = 1 fibonacci(1) = 1 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)

Recursive algorithm to calculate fibonacci(n)

If n is 0 or 1, return 1 Else compute fibonacci(n-1) and fibonacci(n-2) Return their sum

Simple algorithm  exponential time O(

n

Dynamic Programming – Shortest Path Djikstra’s Shortest Path Algorithm S = 

C[X] = 0

C[Y] =  for all other nodes

while ( not all nodes in S )

find node K not in S with smallest C[K]

add K to S

for each node M not in S adjacent to K

C[M] = min ( C[M] , C[K] + cost of (K,M) )

Stores results of

smaller subproblems

Greedy Algorithm

Based on trying best current (local) choice

Approach

At each step of algorithm Choose best local solution

Avoid backtracking, exponential time O(

n

Hope local optimum lead to global optimum

Example: Coin System

Coins – 30 20 15 1 Find minimum number of coins for 40 Greedy Algorithm fails

Greedy Algorithm – MST Kruskal’s Minimal Spanning Tree Algorithm

sort edges by weight (from least to most)

tree = 

for each edge (X,Y) in order

if it does not create a cycle

add (X,Y) to tree

stop when tree has N – 1 edges

Picks best

local solution

at each step

Brute Force Algorithm

Based on trying all possible solutions

Approach

Generate and evaluate possible solutions until Satisfactory solution is found Best solution is found (if can be determined) All possible solutions found Return best solution Return failure if no satisfactory solution

Generally most expensive approach