Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

An overview of various algorithm strategies, including recursive, backtracking, divide and conquer, dynamic programming, greedy, brute force, and heuristic algorithms. Each strategy is explained with examples and use cases. Recursive algorithms reapply the algorithm to subproblems, backtracking algorithms use depth-first recursive search, divide and conquer algorithms divide the problem into smaller subproblems, dynamic programming algorithms remember past results, greedy algorithms make the best local choice at each step, brute force algorithms try all possible solutions, and heuristic algorithms try to guide the search for a solution. The document also covers specific algorithms such as quicksort, mergesort, dijkstra’s shortest path algorithm, kruskal’s minimal spanning tree algorithm, and the traveling salesman problem.

Typology: Exams

Pre 2010

Uploaded on 07/29/2009

koofers-user-fox
koofers-user-fox 🇺🇸

10 documents

1 / 29

Toggle sidebar

Related documents


Partial preview of the text

Download Algorithm Strategies: Recursive, Backtracking, Divide & Conquer, Dynamic Programming and more Exams 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 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. 5 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 6 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 combine solution(s) to subproblems 7 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 10 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+1 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 11 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 12 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 15 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 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 16 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 17 Greedy Algorithm Based on trying best current (local) choice Approach At each step of algorithm Choose best local solution Avoid backtracking, exponential time O(2n) 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 20 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 21 Brute Force Algorithm – Shortest Path Example Examines all paths in graph A C E D 3 8 5 1 72 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 22 Brute Force Algorithm – TSP 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 Complexity O(n!) 25 Branch and Bound – TSP 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(2n) 26 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 27 Heuristic – Shortest Path Example Try only edges with cost < 5 Worked…in this case A C E D 3 8 5 1 72 B 6 4 F A A→B A→C A→C→E
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved