Docsity

Algorithm Strategies for Object Oriented Programming II | CMSC 132, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC 132:
Object-Oriented Programming II
Algorithm Strategies
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Algorithm Strategies for Object Oriented Programming II | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

Object-Oriented Programming II^ CMSC 132:

Algorithm Strategies

University of Maryland, College Park^ Department of Computer Science

General Concepts

Algorithm strategy Approach to solving a problem

May combine several approaches

Algorithm structure Iterative  execute action in loop

Recursive  reapply action to subproblem(s)

Problem type

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

Recursive Algorithm

Based on reapplying algorithm to subproblem Approach

    1. Solves base case(s) directlyRecurs with a simpler subproblem
  1. May need to combine solution(s) to subproblems

Backtracking Algorithm – Reachability

1.^ Find path in graph from A to F Start with currentNode = A

    1. If currentNode has edge to F, return pathElse select neighbor node X for currentNode Recursively find path from X to F If path found, return path Return false if no path from any neighbor X^ Else repeat for different X

Backtracking Algorithm – Path Finding

Search tree A

C

E

38 D

B 6 4

F

A

A→B

A→B→E

A→C A→D

A→C→E A→D→F

A→B→C→E A→D→C→E

A→B→C A→D→C

Divide and Conquer

Based on dividing problem into subproblems Approach

  1. Divide problem into smaller subproblems Subproblems must be of same type
  2. Solve each subproblem recursively^ Subproblems do not need to overlap

Usually contains two or more recursive calls^ 3.^ Combine solutions to solve original problem

Divide and Conquer – Sorting

Quicksort Partition array into two parts around pivot

Recursively quicksort each part of array Concatenate solutions

Mergesort Partition array into two parts

Recursively mergesort each half Merge two sorted arrays into single sorted array

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(2n)

Dynamic Programming – Fibonacci

Dynamic programming version of fibonacci(n) If n is 0 or 1, return 1

Else solve fibonacci(n-1) and fibonacci(n-2) Look up value if previously computed Find their sum and store^ Else recursively compute

Dynamic programming algorithm^ Return result  O(n) time

Since solving fibonacci(n-2) is just looking up value

Greedy Algorithm

Based on trying best current (local) choice Approach

At each step of algorithm Choose best local solution

Avoid backtracking, exponential time O(2 Hope local optimum lead to global optimumn)

Example: Coin System Coins – 30 20 15 1

Find minimum number of coins for 40 Greedy Algorithm fails

Greedy Algorithm – Shortest Path (A to E)

Example Choose lowest-cost neighbor

Does not yield overall (global) shortest path

A

C

E

8 D

B 4

F

A

A→B

A→B→E Cost^ ^6

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

Brute Force Algorithm – Shortest Path

Example

Examines all paths in graph

A

C

E

38 D

B 6 4

F

A

A→B

A→B→E

A→C A→D

A→C→E A→D→F

A→B→C→E A→D→C→E

A→B→C A→D→C