Algorithms Strategies - Objected Oriented Program II - Notes | CMSC 132, Study notes of Computer Science

Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-08y
koofers-user-08y 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
CMSC 132:
Object-Oriented Programming II
Algorithm Strategies
Department of Computer Science
University of Maryland, College Park
2
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
Satisfying find any satisfactory solution
Optimization find best solutions (vs. cost metric)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Algorithms Strategies - Objected Oriented Program II - Notes | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

1

CMSC 132:

Object-Oriented Programming II

Algorithm Strategies

Department of Computer Science

University of Maryland, College Park

2

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

Satisfying ⇒ find any satisfactory solution Optimization ⇒ find best solutions (vs. cost metric)

3

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

4

Recursive Algorithm

Based on reapplying algorithm to subproblem

Approach

  1. Solves base case(s) directly
  2. Recurs with a simpler subproblem
  3. May need to convert solution(s) to subproblems

7

Backtracking Algorithm – Example

Color a map with no more than four colors

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

8

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

9

Divide and Conquer – Examples

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

10

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

13

Dynamic Programming – Example

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

14

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 n^ )

Hope local optimum lead to global optimum

15

Greedy Algorithm – Example

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

16

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

19

Branch and Bound – Example

Branch and bound algorithm for TSP

Find possible paths using recursive backtracking Track cost of best current solution found Stop searching path if cost > best current solution Return lowest cost path

If good solution found early, can reduce search

May still require exponential time O(2 n^ )

20

Heuristic Algorithm

Based on trying to guide search for solution

Heuristic ⇒ “rule of thumb”

Approach

Generate and evaluate possible solutions Using “rule of thumb” Stop if satisfactory solution is found

Can reduce complexity

Not guaranteed to yield best solution

21

Heuristic Algorithm – Example

Heuristic algorithm for TSP

Find possible paths using recursive backtracking Search 2 lowest cost edges at each node first Calculate cost of each path Return lowest cost path from first 100 solutions

Not guaranteed to find best solution

Heuristics used frequently in real applications

22

Summary

Wide range of strategies

Choice depends on

Properties of problem Expected problem size Available resources