Problem Solving: Brute Force Approaches

Slides:



Advertisements
Similar presentations
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
Advertisements

Eight queens puzzle. The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture.
CS420 lecture ten BACKTRACK. Solution vectors In optimization problems, or more general in search problems, a set of choices are to be made to arrive.
Backtracking.
Algorithm Fundamentals Shane Carr CSE 232, Fall 2015.
Brought to you by Max (ICQ: TEL: ) March 12, 2005 Recursion and Exhaustion.
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Game playing Types of games Deterministic vs. chance
Problem Solving: Brute Force Approaches
Sort & Search Algorithms
More Mathematical Techniques
BackTracking CS255.
Depth-First Search N-Queens Problem Hamiltonian Circuits
CSSE 230 Day 25 Skip Lists.
Algorithmic complexity: Speed of algorithms
CSCI 104 Backtracking Search
Backtracking And Branch And Bound
Basic Data Structures.
CS1371 Introduction to Computing for Engineers
Sit-In Lab 1 Ob-CHESS-ion
Algorithm Analysis CSE 2011 Winter September 2018.
C ODEBREAKER Class discussion.
Topics Introduction to Repetition Structures
Using a Stack Chapter 6 introduces the stack data type.
Types of Algorithms.
Recursion Copyright (c) Pearson All rights reserved.
Some Basics for Problem Analysis and Solutions
Optimizing Malloc and Free
Mathematics Common Math Approaches
Algorithms Chapter 3 With Question/Answer Animations
Algorithm design and Analysis
Dynamic Programming.
Dynamic Programming.
Recursion Chapter 11.
Back Tracking.
Analysis and design of algorithm
More Mathematical Techniques
Using a Stack Chapter 6 introduces the stack data type.
Types of Algorithms.
Lesson 15: Processing Arrays
Brute Force Approaches
Encryption and Decryption
Mathematics Basic Math and Combinatorics
Coding Concepts (Basics)
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
adapted from Recursive Backtracking by Mike Scott, UT Austin
Algorithmic complexity: Speed of algorithms
3. Brute Force Selection sort Brute-Force string matching
The N-queens problem Team: 404 brain not found
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Pruning 24-Feb-19.
Quicksort.
3. Brute Force Selection sort Brute-Force string matching
Programming Logic and Design Fifth Edition, Comprehensive
Algorithmic complexity: Speed of algorithms
Types of Algorithms.
Topics Introduction to Repetition Structures
More Mathematical Techniques
Backtracking, Search, Heuristics
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Quicksort.
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

Problem Solving: Brute Force Approaches

Brute Force Approaches (aka Complete Search) Basic idea: try out everything to find the best. Sometimes, this is the only reasonable solution Given an unsorted list of numbers, find the max/min. Only option is to look at each number. Sometimes, the brute-force approach is good enough Especially when sizes are not too large, or time is not an issue e.g. if checking n! different options, if n is 3 or 4, it might be fine to enumerate them. Often, the brute-force approach is the easiest to implement Unless you expect time to be an issue (which it often is…), it’s a good way to start Solving via brute force ensures you understand the problem and know a way to find a solution If you know a brute force solution is correct, you can use it as a check on your faster solution

Basic ways to approach brute force Iterative vs. recursive Iterative: loop through to generate all possible combinations Sometimes will need many loops Recursive: Make choices in order, generating next state Then use “backtracking” to return to previous state and pick next choice Generative vs. filtered Generators: generate the possible answers – the good ones Often a recursive definition is better, here Filtered: generate all answers and choose valid ones

Common Themes to Brute-Force Approaches Pruning When exploring an option, quit that example as soon as you know it can’t work. Bounds Limitations Check the limitations on sizes of input, or other factors This can drastically reduce the search space of possible options

Iterative approaches Loop through all options, testing each Example: find all pairs of numbers where X is c times Y, and X and Y are 5-digit numbers, using all digits from 0-9 Multiple loops Example: enumerate all subsets of 6 numbers from a larger collection; use 6 nested loops Limit range of search If x2+y2+z2 = C, then x, y, z must have absolute value < sqrt(C), etc. Keep flag to stop loops early (pruning) Generating all permutations Example: For 8 moviegoers, what seating options in a row meet all of 20 conditions (setting limits about distance apart/near) Subset-sum Generate all subsets, then check those n items, 2n possible subsets; possibly viable for n<=20

Example Read numbers A, B C, each <= 10000. Find distinct x, y, z so that: x+y+z = A x*y*z = B x2+y2+z2 = C We can limit loops to [-100,100] for each, due to C We can limit one of these to cube root of B, or [-22,22] at most We can do fast checks to make sure x, y, z are distinct before doing calculations Then, just have loops, and check answers.

Recursive search Recursively search for all possible solutions to a problem Key is to prune away invalid solutions quickly Classical problem: 8 queens Find the positions possible for arranging 8 queens on a chessboard so they can’t attack each other First: note each column must have one queen, so can recurse that way Check each row for that column to see if valid – if so, place and recurse (for each valid position) Valid position means not at the same row or on same diagonal as a prior queen Helps prune away large sections of search space. Expanded problem: n-queens on larger board Need faster (e.g. bitfield) storage to identify valid/invalid rows/diagonals. Notice: 2n-1 left and 2n-1 right diagonals on an nxn board.

Tips to improve performance of brute force Use short-circuiting of if statements to limit checks Symmetries: Can drastically reduce the search space. But, can involve a lot of overhead and complicate the code Pre-compute: When some values can be known ahead of time, precompute and store. Solve backward: Look at whether the inverse problem is faster Optimize source code: First, try to identify the “critical” code (see next page)

Optimizing source code Use a faster language: C++>Java>Python Faster I/O: C++: scanf/printf vs. cin/cout (scanf/printf don’t flush other) Java: BufferedReader/BufferedWriter vs. Scanner/PrintStream Use built-in fast sort (e.g. C++ algorithm::sort) Access 2D arrays in row-major order (for cache coherency) Use bit manipulations whenever possible Use low-level structures when you know you don’t need advanced functionality e.g. arrays vs. vectors, char[] vs. string Do single allocation of array as a global Locals avoid passing/copying Dynamic allocation will take time Iterative instead of recursive code when possible Avoid repeated array access – store the data in a variable

Brute Force in Book Book references – section 3.2 READ THIS CHAPTER!!! Examples of basic problem solutions Generating all permutations Use the C++ next_permutation algorithm (iterative approach to generating permutations!). Generating all subsets Form an n-bit bitmask (integer), then iterate through numbers