Algorithm classification

Similar documents
Data Structures, Algorithms & Data Science Platforms

CAD Algorithms. Categorizing Algorithms

CMSC Introduction to Algorithms Spring 2012 Lecture 16

CS 206 Introduction to Computer Science II

Algorithms Interview Questions

CS 206 Introduction to Computer Science II

Lecture 14: General Problem-Solving Methods

CS 206 Introduction to Computer Science II

Algorithm Design Techniques part I

Algorithm Design Techniques. Hwansoo Han

Lecture 3, Review of Algorithms. What is Algorithm?

QuickSort

CS 380 ALGORITHM DESIGN AND ANALYSIS

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Design and Analysis of Algorithms

So far... Finished looking at lower bounds and linear sorts.

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

LECTURE 3 ALGORITHM DESIGN PARADIGMS

Heuristic Optimisation

Algorithm Design Paradigms

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

6. Algorithm Design Techniques

Backtracking and Branch-and-Bound

15.4 Longest common subsequence

Merge Sort Goodrich, Tamassia Merge Sort 1

D.K.M.COLLEGE FOR WOMEN (AUTONOMOUS), VELLORE-1.

CSCE 321/3201 Analysis and Design of Algorithms. Prof. Amr Goneid. Fall 2016

We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real

1 Greedy algorithms and dynamic programming

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Algorithms Dr. Haim Levkowitz

CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees

CS 171: Introduction to Computer Science II. Quicksort

Data Structures and Algorithms

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Catie Baker Spring 2015

Question 7.11 Show how heapsort processes the input:

Algorithmic patterns

Computer Sciences Department 1

CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK UNIT-III. SUB NAME: DESIGN AND ANALYSIS OF ALGORITHMS SEM/YEAR: III/ II PART A (2 Marks)

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

looking ahead to see the optimum

16 Greedy Algorithms

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Data Structures and Algorithms Week 8

Algorithm Design Techniques (III)

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Sorting Goodrich, Tamassia Sorting 1

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

CSci 231 Final Review

CS301 - Data Structures Glossary By

Lecture 6: Divide-and-Conquer

CS6402-DESIGN AND ANALYSIS OF ALGORITHM. TWO MARK QUESTION WITH ANSWERS Unit-I. Introduction

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS

Programming II (CS300)

QB LECTURE #1: Algorithms and Dynamic Programming

CSCE 411 Design and Analysis of Algorithms

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: CS6402- Design & Analysis of Algorithm Year/Sem : II/IV UNIT-I INTRODUCTION

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017

Sorting. Data structures and Algorithms

V Advanced Data Structures

Dynamic Programming Group Exercises

10/24/ Rotations. 2. // s left subtree s right subtree 3. if // link s parent to elseif == else 11. // put x on s left

Sorting is a problem for which we can prove a non-trivial lower bound.

Recursion & Dynamic Programming. Algorithm Design & Software Engineering March 17, 2016 Stefan Feuerriegel

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

For 100% Result Oriented IGNOU Coaching and Project Training Call CPD: ,

Lecture 19 Sorting Goodrich, Tamassia

Quick Sort. CSE Data Structures May 15, 2002

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Module 2: Classical Algorithm Design Techniques

Geometric data structures:

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

CSC 427: Data Structures and Algorithm Analysis. Fall 2006

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

Anany Levitin 3RD EDITION. Arup Kumar Bhattacharjee. mmmmm Analysis of Algorithms. Soumen Mukherjee. Introduction to TllG DCSISFI &

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

V Advanced Data Structures

Lecture 5: Sorting Part A

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Lecture Summary CSC 263H. August 5, 2016

15.4 Longest common subsequence

Algorithms. Ch.15 Dynamic Programming

CPE702 Algorithm Analysis and Design Week 7 Algorithm Design Patterns

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

We ve done. Now. Next

CMPSCI 187: Programming With Data Structures. Lecture #34: Efficient Sorting Algorithms David Mix Barrington 3 December 2012

Better sorting algorithms (Weiss chapter )

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Greedy Algorithms and Huffman Coding

CS F-11 B-Trees 1

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Transcription:

Types of Algorithms

Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We ll talk about a classification scheme for algorithms This classification scheme is neither exhaustive nor disjoint The purpose is not to be able to classify an algorithm as one type or another, but to highlight the various ways in which a problem can be attacked 2

A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide and conquer algorithms Dynamic programming algorithms Greedy algorithms Branch and bound algorithms Brute force algorithms Randomized algorithms 3

Simple recursive algorithms I A simple recursive algorithm: Solves the base cases directly Recurs with a simpler subproblem Does some extra work to convert the solution to the simpler subproblem into a solution to the given problem I call these simple because several of the other algorithm types are inherently recursive 4

Example recursive algorithms To count the number of elements in a list: If the list is empty, return zero; otherwise, Step past the first element, and count the remaining elements in the list Add one to the result To test if a value occurs in a list: If the list is empty, return false; otherwise, If the first thing in the list is the given value, return true; otherwise Step past the first element, and test whether the value occurs in the remainder of the list 5

Backtracking algorithms Backtracking algorithms are based on a depth-first recursive search A backtracking algorithm: Tests to see if a solution has been found, and if so, returns it; otherwise For each choice that can be made at this point, Make that choice Recur If the recursion returns a solution, return it If no choices remain, return failure 6

Example backtracking algorithm To color a map with no more than four colors: color(country n): If all countries have been colored (n > number of countries) return success; otherwise, For each color c of four colors, 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 If loop exits, return failure 7

Divide and Conquer A divide and conquer algorithm consists of two parts: Divide the problem into smaller subproblems of the same type, and solve these subproblems recursively Combine the solutions to the subproblems into a solution to the original problem Traditionally, an algorithm is only called divide and conquer if it contains at least two recursive calls 8

Examples Quicksort: Partition the array into two parts (smaller numbers in one part, larger numbers in the other part) Quicksort each of the parts No additional work is required to combine the two sorted parts Mergesort: Cut the array in half, and mergesort each half Combine the two sorted arrays into a single sorted array by merging them 9

Binary tree lookup Here s how to look up something in a sorted binary tree: Compare the key to the value in the root If the two values are equal, report success If the key is less, search the left subtree If the key is greater, search the right subtree This is not a divide and conquer algorithm because, although there are two recursive calls, only one is used at each level of the recursion 10

Fibonacci numbers To find the n th Fibonacci number: If n is zero or one, return one; otherwise, Compute fibonacci(n-1) and fibonacci(n-2) Return the sum of these two numbers This is an expensive algorithm It requires O(fibonacci(n)) time This is equivalent to exponential time, that is, O(2 n ) 11

Dynamic programming algorithms A dynamic programming algorithm remembers past results and uses them to find new results Dynamic programming is generally used for optimization problems Multiple solutions exist, need to find the best one Requires optimal substructure and overlapping subproblems Optimal substructure: Optimal solution contains optimal solutions to subproblems Overlapping subproblems: Solutions to subproblems can be stored and reused in a bottom-up fashion This differs from Divide and Conquer, where subproblems generally need not overlap 12

Fibonacci numbers again To find the n th Fibonacci number: If n is zero or one, return one; otherwise, Compute, or look up in a table, fibonacci(n-1) and fibonacci(n-2) Find the sum of these two numbers Store the result in a table and return it Since finding the n th Fibonacci number involves finding all smaller Fibonacci numbers, the second recursive call has little work to do The table may be preserved and used again later 13

Greedy algorithms An optimization problem is one in which you want to find, not just a solution, but the best solution A greedy algorithm sometimes works well for optimization problems A greedy algorithm works in phases: At each phase: You take the best you can get right now, without regard for future consequences You hope that by choosing a local optimum at each step, you will end up at a global optimum 14

Example: Counting money Suppose you want to count out a certain amount of money, using the fewest possible bills and coins A greedy algorithm would do this would be: At each step, take the largest possible bill or coin that does not overshoot Example: To make $6.39, you can choose: a $5 bill a $1 bill, to make $6 a 25 coin, to make $6.25 A 10 coin, to make $6.35 four 1 coins, to make $6.39 For US money, the greedy algorithm always gives the optimum solution 15

A failure of the greedy algorithm In some (fictional) monetary system, krons come in 1 kron, 7 kron, and 10 kron coins Using a greedy algorithm to count out 15 krons, you would get A 10 kron piece Five 1 kron pieces, for a total of 15 krons This requires six coins A better solution would be to use two 7 kron pieces and one 1 kron piece This only requires three coins The greedy algorithm results in a solution, but not in an optimal solution 16

Branch and bound algorithms Branch and bound algorithms are generally used for optimization problems As the algorithm progresses, a tree of subproblems is formed The original problem is considered the root problem A method is used to construct an upper and lower bound for a given problem At each node, apply the bounding methods If the bounds match, it is deemed a feasible solution to that particular subproblem If bounds do not match, partition the problem represented by that node, and make the two subproblems into children nodes Continue, using the best known feasible solution to trim sections of the tree, until all nodes have been solved or trimmed 17

Example branch and bound algorithm Traveling salesman problem: A salesman has to visit each of n cities (at least) once each, and wants to minimize total distance traveled Consider the root problem to be the problem of finding the shortest route through a set of cities visiting each city once Split the node into two child problems: Shortest route visiting city A first Shortest route not visiting city A first Continue subdividing similarly as the tree grows 18

Brute force algorithm A brute force algorithm simply tries all possibilities until a satisfactory solution is found Such an algorithm can be: Optimizing: Find the best solution. This may require finding all solutions, or if a value for the best solution is known, it may stop when any best solution is found Example: Finding the best path for a traveling salesman Satisficing: Stop as soon as a solution is found that is good enough Example: Finding a traveling salesman path that is within 10% of optimal 19

Improving brute force algorithms Often, brute force algorithms require exponential time Various heuristics and optimizations can be used Heuristic: A rule of thumb that helps you decide which possibilities to look at first Optimization: In this case, a way to eliminate certain possibilities without fully exploring them 20

Randomized algorithms A randomized algorithm uses a random number at least once during the computation to make a decision Example: In Quicksort, using a random number to choose a pivot Example: Trying to factor a large number by choosing random numbers as possible divisors 21

The End 22