3. Brute Force Selection sort Brute-Force string matching

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin Introduction to the Design & Analysis of Algorithms, 2 nd ed., Ch. 1 Chapter 2.
Advertisements

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.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
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.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
Analysis & Design of Algorithms (CSCE 321)
Dynamic Programming Reading Material: Chapter 7..
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.
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
GRAPH Learning Outcomes Students should be able to:
IT 60101: Lecture #201 Foundation of Computing Systems Lecture 20 Classic Optimization Problems.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
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.
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
Excursions in Modern Mathematics Sixth Edition
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.
© Ronaldo Menezes, Florida Tech
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Lecture on Design and Analysis of Computer Algorithm
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
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.
Chapter 11 Limitations of Algorithm Power
Recall Some Algorithms And Algorithm Analysis Lots of Data Structures
3. Brute Force Selection sort Brute-Force string matching
Backtracking and Branch-and-Bound
Hamilton Paths and Hamilton Circuits
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Analysis and design of algorithm
Algorithms CSCI 235, Spring 2019 Lecture 36 P vs
Invitation to Computer Science 5th Edition
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

3. Brute Force Selection sort Brute-Force string matching Polynomial Evaluation Closest pair problem by brute force Exhaustive search Traveling salesman problem Knapsack problem Assignment problem

Brute Force Brute force is straightforward approach to solving a problem, usually directly based on the problem’s statement and definitions of the concepts involved Examples: Computing an (a > 0, n a nonnegative integer) Computing n! Multiplying two matrices Searching for a key of a given value in a list

3.1 Brute-Force Sorting Algorithm We consider the application of the brute-force approach to the problem of sorting: Selection Sort: Scan the array to find its smallest element and swap it with the first element. Then, starting with the second element, scan the elements to the right of it to find the smallest among them and swap it with the second elements. Generally, on pass i (0  i  n-2), find the smallest element in A[i..n-1] and swap it with A[i]: A[0]  . . .  A[i-1] | A[i], . . . , A[min], . . ., A[n-1] in their final positions

Analysis of Selection Sort After n-1 passes, the list is sorted. Here is pseu-docode of this algorithm, which for simplicity, assumes that the list is implemented as an array. Time efficiency: Space efficiency:

Analysis of Selection Sort As an example, the action of algorithm on the list 89,45,68,90,29,34,17 is illustrated as below: The analysis of selection sort is straightforward. The input’s size is given by the number of elements n; the algorithm basic operation is the key comparison A[j]<A[min]. So the number of times its executed as C(n) = n(n-1)/2 Thus selection sort is Θ(n2)

Bubble Sort The algorithm [page 100] The time efficiency Compare to selection sort: number of comparisons, number of swaps The number of times its executed : C(n) = n(n-1)/2 Thus Bubble sort is Θ(n2) So the performances of the bubble sort and the selection sort are approximately equivalent. However, on the average, the bubble sort performs much better than the selection sort because it can finish the sorting without doing all N-1 passes.

3.2 Brute-Force String Matching Given a string of n characters called the text and a string of m characters called the pattern, find a substring of the text that matches the pattern. Brute-force algorithm Step1: Align pattern at beginning of text Step2: 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 Step3: While pattern is not found and the text is not yet exhausted, realign pattern one position to the right and repeat Step 2

Pseudocode and Efficiency

An operation of algorithm is illustrated below: Efficiency is an order of Θ(n). There are several more efficient algorithms for string searching will discuss in chapter 7

3.3 Closest-Pair Problem The closest-pair problem calls for finding the two closest points in a set of n points. For simplicity we consider the two-dimensional Cartesian plane. The distance between two points Pi = (xi, yi) and Pj = (xj, yj) is the standard Euclidean distance Brute-force algorithm Compute the distance between every pair of distinct points and return the indexes of the points for which the distance is the smallest. Draw example.

Closest-Pair Brute-Force Algorithm The basic operation of the algorithm is computing the Euclidean distance between two points. The square root is a complex operation who’s result is often irrational, therefore the results can be found only approximately. Computing such operations are not trivial. One can avoid computing square roots by comparing distance squares instead. The basic operation of algorithm will be squaring a number . The number of time it will be executed can be computed as: C(n) = (n-1)n  Θ(n2)

Brute-Force Strengths and Weaknesses wide applicability simplicity yields reasonable algorithms for some important problems (e.g., matrix multiplication, sorting, searching, string matching) Weaknesses rarely yields efficient algorithms some brute-force algorithms are unacceptably slow not as constructive as some other design techniques

3.4 Exhaustive Search A brute force solution to a problem involving search for an element with a special property, usually among combinatorial objects such as permutations, combinations, or subsets of a set. Method: generate a list of all potential solutions to the problem in a systematic manner evaluate potential solutions one by one, disqualifying infeasible ones and, for an optimization problem, keeping track of the best one found so far then search ends, announce the solution(s) found

Example 1: Traveling Salesman Problem 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. The problem can be modeled by a weighted graph, vertices representing cities and edge weight’s specifying the distances. Alternatively: Find shortest Hamiltonian circuit in a weighted connected graph.

Example 2: Knapsack Problem Given n items of known: weights: w1 w2 … wn values: v1 v2 … vn a knapsack of capacity W Find most valuable subset of the items that fit into the knapsack

Example 3: The Assignment Problem There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person i to job j is C[i,j]. Find an assignment that minimizes the total cost. Job 0 Job 1 Job 2 Job 3 Person 0 9 2 7 8 Person 1 6 4 3 7 Person 2 5 8 1 8 Person 3 7 6 9 4 Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one.

Pose the problem as the one about a cost matrix: How many assignments are there?

Final Comments on Exhaustive Search Exhaustive-search algorithms run in a realistic amount of time only on very small instances In some cases, there are much better alternatives! Euler circuits shortest paths minimum spanning tree assignment problem In many cases, exhaustive search or its variation is the only known way to get exact solution