Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
Dynamic Programming 25-Mar-17.
Advertisements

Algorithm Design Techniques
Greedy Algorithms.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Types of Algorithms.
Greedy Algorithms Greed is good. (Some of the time)
Lecture 24 Coping with NPC and Unsolvable problems. When a problem is unsolvable, that's generally very bad news: it means there is no general algorithm.
Anany Levitin ACM SIGCSE 1999SIG. Outline Introduction Four General Design Techniques A Test of Generality Further Refinements Conclusion.
Algorithms + L. Grewe.
Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng
Dynamic Programming.
Branch & Bound Algorithms
Introduction to Algorithms
15-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
1 Greedy Algorithms. 2 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
E.G.M. PetrakisAlgorithms and Complexity1 The sphere of algorithmic problems.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Graphs & Graph Algorithms 2
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 10: Algorithm Design Techniques
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Ch 13 – Backtracking + Branch-and-Bound
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Algorithm Design Techniques Greedy algorithms Divide and conquer Dynamic programming Randomized algorithms Backtracking.
Backtracking.
Instructor: Dr. Sahar Shabanah Fall Lectures ST, 9:30 pm-11:00 pm Text book: M. T. Goodrich and R. Tamassia, “Data Structures and Algorithms in.
Analysis of Algorithms
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
SPANNING TREES Lecture 21 CS2110 – Spring
Minimum Spanning Trees
Exploring Algorithms Traveling Salesperson Problem I: Brute Force, Greedy, and Heuristics Except as otherwise noted, the content of this presentation is.
Fundamentals of Algorithms MCS - 2 Lecture # 7
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
Design and Analysis of Algorithms - Chapter 111 How to tackle those difficult problems... There are two principal approaches to tackling NP-hard problems.
State-Space Searches. 2 State spaces A state space consists of A (possibly infinite) set of states The start state represents the initial problem Each.
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
Honors Track: Competitive Programming & Problem Solving Optimization Problems Kevin Verbeek.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
CSE 589 Part VI. Reading Skiena, Sections 5.5 and 6.8 CLR, chapter 37.
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
Algorithm Design Methods (II) Fall 2003 CSE, POSTECH.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
CSE 340: Review (at last!) Measuring The Complexity Complexity is a function of the size of the input O() Ω() Θ() Complexity Analysis “same order” Order.
Ch3 /Lecture #4 Brute Force and Exhaustive Search 1.
Data Structures Lab Algorithm Animation.
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Types of Algorithms.
Short paths and spanning trees
DYNAMIC PROGRAMMING.
Graphs & Graph Algorithms 2
Types of Algorithms.
CIT Nov-18.
Advanced Analysis of Algorithms
Principles of Computing – UFCFA3-30-1
Lecture 3: Environs and Algorithms
Backtracking and Branch-and-Bound
Types of Algorithms.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Major Design Strategies
Major Design Strategies
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Presentation transcript:

Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

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)

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) directly 2. Recurs with a simpler subproblem 3. May need to convert solution(s) to subproblems

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 Some times called “search tree”

Backtracking Algorithm – Example Find path through maze Start at beginning of maze If at exit, return true Else for each step from current location Recursively find path Return with first successful step Return false if all steps fail

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+1 If successful, return success Return failure

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

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

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

Dynamic Programming – Example 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 Else recursively compute Find their sum and store Return result Dynamic programming algorithm  O(n) time Since solving fibonacci(n-2) is just looking up value

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

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

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

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 – Example Traveling Salesman Problem (TSP) Given weighted undirected graph (map of cities) Find lowest cost path visiting all nodes (cities) once No known polynomial-time general solution Brute force approach Find all possible paths using recursive backtracking Calculate cost of each path Return lowest cost path Requires exponential time O(2 n )

Branch and Bound Algorithm Based on limiting search using current solution Approach Track best current solution found Eliminate partial solutions that can not improve upon best current solution Reduces amount of backtracking Not guaranteed to avoid exponential time O(2 n )

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 )

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

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

Summary Wide range of strategies Choice depends on Properties of problem Expected problem size Available resources