Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.

Slides:



Advertisements
Similar presentations
Algorithm Design Techniques
Advertisements

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin Introduction to the Design & Analysis of Algorithms, 2 nd ed., Ch. 1 Chapter 2.
Limitation of Computation Power – P, NP, and NP-complete
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.
Lecture 21 Paths and Circuits CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Chapter 15 Graph Theory © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 4 sec. 2.  A famous and difficult problem to solve in graph theory.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
S. J. Shyu Chap. 1 Introduction 1 The Design and Analysis of Algorithms Chapter 1 Introduction S. J. Shyu.
The Design and Analysis of Algorithms
Theory of Algorithms: Brute Force James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August.
COSC 3100 Brute Force and Exhaustive Search Instructor: Tanvir 1.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Analysis & Design of Algorithms (CSCE 321)
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
1 -1 Chapter 1 Introduction Why Do We Need to Study Algorithms? To learn strategies to design efficient algorithms. To understand the difficulty.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
Homework page 102 questions 1, 4, and 10 page 106 questions 4 and 5 page 111 question 1 page 119 question 9.
Ch 13 – Backtracking + Branch-and-Bound
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
RAIK 283: Data Structures & Algorithms
Chapter 12 Coping with the Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
IT 60101: Lecture #201 Foundation of Computing Systems Lecture 20 Classic Optimization Problems.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Theory of Algorithms: Brute Force. Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
5-1-1 CSC401 – Analysis of Algorithms Chapter 5--1 The Greedy Method Objectives Introduce the Brute Force method and the Greedy Method Compare the solutions.
Chapter 3 Brute Force. A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples:
LIMITATIONS OF ALGORITHM POWER
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 UNIT-I BRUTE FORCE ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 3:
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Section 3 - Slide 1 Copyright © 2009 Pearson Education, Inc. AND.
MA/CSSE 473 Day 12 Amortization (growable Array) Knuth interview Brute Force Examples.
Algorithm Complexity By: Ashish Patel and Alex Golebiewski.
Ch3 /Lecture #4 Brute Force and Exhaustive Search 1.
Exhaustive search Exhaustive search is simply a brute- force approach to combinatorial problems. It suggests generating each and every element of the problem.
Introduction to Algorithms: Brute-Force Algorithms.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Brute Force II.
Limitation of Computation Power – P, NP, and NP-complete
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Brute Force Algorithms
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 3 String Matching.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples – based directly.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 3 String Matching.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Heuristics Definition – a heuristic is an inexact algorithm that is based on intuitive and plausible arguments which are “likely” to lead to reasonable.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Recall Some Algorithms And Algorithm Analysis Lots of Data Structures
3. Brute Force Selection sort Brute-Force string matching
3. Brute Force Selection sort Brute-Force string matching
CSC 380: Design and Analysis of Algorithms
Euler circuit Theorem 1 If a graph G has an Eulerian path, then it must have exactly two odd vertices. Theorem 2 If a graph G has an Eulerian circuit,
CSC 380: Design and Analysis of Algorithms
Graphs CS 2606.
Analysis and design of algorithm
Algorithms CSCI 235, Spring 2019 Lecture 36 P vs
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing a n (a > 0, n a nonnegative integer) 2. Computing n! 3. Multiply two n by n matrices 4. Selection sort 5. Sequential search

Design and Analysis of Algorithms - Chapter 32 String matching b pattern: a string of m characters to search for b text: a (long) string of n characters to search in b Brute force algorithm: 1.Align pattern at beginning of text 2.moving from left to right, compare each character of pattern to the corresponding character in text until –all characters are found to match (successful search); or –a mismatch is detected 3.while pattern is not found and the text is not yet exhausted, realign pattern one position to the right and repeat step 2.

Design and Analysis of Algorithms - Chapter 33 Brute force string matching – Examples: 1. Pattern: Text: Pattern: happy Text: It is never too late to have a happy childhood. Number of comparisons: Efficiency:

Design and Analysis of Algorithms - Chapter 34 Brute force polynomial evaluation b Problem: Find the value of polynomial p(x) = a n x n + a n-1 x n-1 +… + a 1 x 1 + a 0 at a point x = x 0 b Algorithm: b Efficiency: p := 0.0 for i := n down to 0 do power := 1 power := 1 for j := 1 to i do for j := 1 to i do power := power * x power := power * x p := p + a[i] * power p := p + a[i] * power return p

Design and Analysis of Algorithms - Chapter 35 Polynomial evaluation: improvement b We can do better by evaluating from right to left: b Algorithm: b Efficiency: p := a[0] power := 1 for i := 1 to n do power := power * x power := power * x p := p + a[i] * power p := p + a[i] * power return p

Design and Analysis of Algorithms - Chapter 36 More brute force algorithm examples: b Closest pair Problem: find closest among n points in k-dimensional spaceProblem: find closest among n points in k-dimensional space Algorithm: Compute distance between each pair of pointsAlgorithm: Compute distance between each pair of points Efficiency:Efficiency: b Convex hull Problem: find smallest convex polygon enclosing n points on the planeProblem: find smallest convex polygon enclosing n points on the plane Algorithm: For each pair of points p 1 and p 2 determine whether all other points lie to the same side of the straight line through p 1 and p 2Algorithm: For each pair of points p 1 and p 2 determine whether all other points lie to the same side of the straight line through p 1 and p 2 Efficiency:Efficiency:

Design and Analysis of Algorithms - Chapter 37 Brute force strengths and weaknesses b Strengths: wide applicabilitywide applicability simplicitysimplicity yields reasonable algorithms for some important problemsyields reasonable algorithms for some important problems –searching –string matching –matrix multiplication yields standard algorithms for simple computational tasksyields standard algorithms for simple computational tasks –sum/product of n numbers –finding max/min in a list b Weaknesses: rarely yields efficient algorithmsrarely yields efficient algorithms some brute force algorithms unacceptably slowsome brute force algorithms unacceptably slow not as constructive/creative as some other design techniquesnot as constructive/creative as some other design techniques

Design and Analysis of Algorithms - Chapter 38 Exhaustive search b A brute force solution to a problem involving search for an element with a special property, usually among combinatorial objects such a permutations, combinations, or subsets of a set. b Method: construct a way of listing all potential solutions to the problem in a systematic mannerconstruct a way of listing all potential solutions to the problem in a systematic manner –all solutions are eventually listed –no solution is repeated Evaluate solutions one by one, perhaps disqualifying infeasible ones and keeping track of the best one found so farEvaluate solutions one by one, perhaps disqualifying infeasible ones and keeping track of the best one found so far When search ends, announce the winnerWhen search ends, announce the winner

Design and Analysis of Algorithms - Chapter 39 Example 1: Traveling salesman problem b Given n cities with known distances between each pair, find the shortest tour that passes through all the cities exactly once before returning to the starting city. b Alternatively: Find shortest Hamiltonian circuit in a weighted connected graph. b Example: ab cd

Design and Analysis of Algorithms - Chapter 310 Traveling salesman by exhaustive search b Tour Cost. b a→b→c→d→a = 17 b a→b→d→c→a = 21 b a→c→b→d→a = 20 b a→c→d→b→a = 21 b a→d→b→c→a = 20 b a→d→c→b→a = 17 b Efficiency:

Design and Analysis of Algorithms - Chapter 311 Example 2: Knapsack Problem Given n items: weights: w 1 w 2 … w nweights: w 1 w 2 … w n values: v 1 v 2 … v nvalues: v 1 v 2 … v n a knapsack of capacity Wa knapsack of capacity W Find the most valuable subset of the items that fit into the knapsack Example: item weight value Knapsack capacity W= $ $ $ $10

Design and Analysis of Algorithms - Chapter 312 Knapsack by exhaustive search Subset Total weight Total value {1} 2 $20 {1} 2 $20 {2} 5 $30 {2} 5 $30 {3} 10 $50 {3} 10 $50 {4} 5 $10 {4} 5 $10 {1,2} 7 $50 {1,2} 7 $50 {1,3} 12 $70 {1,3} 12 $70 {1,4} 7 $30 {1,4} 7 $30 {2,3} 15 $80 {2,3} 15 $80 {2,4} 10 $40 {2,4} 10 $40 {3,4} 15 $60 {3,4} 15 $60 {1,2,3} 17 not feasible {1,2,3} 17 not feasible {1,2,4} 12 $60 {1,2,4} 12 $60 {1,3,4} 17 not feasible {1,3,4} 17 not feasible {2,3,4} 20 not feasible {2,3,4} 20 not feasible {1,2,3,4} 22 not feasible Efficiency:

Design and Analysis of Algorithms - Chapter 313 Final comments: b Exhaustive search algorithms run in a realistic amount of time only on very small instances b In many cases there are much better alternatives! Euler circuitsEuler circuits shortest pathsshortest paths minimum spanning treeminimum spanning tree assignment problemassignment problem b In some cases exhaustive search (or variation) is the only known solution