BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
Design and Analysis of Algorithm Brute Force 1.ppt
1. Department of Computer Science
COMSATS University, Islamabad
Design and Analysis of Algorithm
Tanveer Ahmed Siddiqui
2. Department of Computer Science
Algorithm Design and Analysis Process
Understand the problem
Decide on : algorithm
design techniques etc.
Design an algorithm
Prove correctness
Analyze efficiency etc
Code the algorithm
Decide on : algorithm
design techniques etc.
Recap and Today Covered
3. Department of Computer Science
Reading Material
Read Chapter 3
Brute Force and Exhaustive Search
4. Department of Computer Science
How to Design Algorithm using Brute Force(BF) Approach
Swapping two numbers
Computing an (a > 0, n a nonnegative integer)
Computing n!
Add n numbers
Decimal to Binary conversion
Finding Maximum/Minimum Element in a list
Multiply two n by n Matrices
Selection Sort
Bubble Sort
Sequential Search/Linear Search
Conversion of a 2D array into 1D array
Find the value of polynomial
Objectives
5. Department of Computer Science
Designing an algorithm is essentially a
creative effort containing all the ingredients
of a thriller:
Adventure
Excitement
Challenge
Suspense.
Designing an Algorithm
6. Department of Computer Science
Designing an algorithm is easy if you
know
design
techniques
7. Department of Computer Science
An algorithm design techniques (or “strategy”
or “paradigm”) is a general approach to solve
problems algorithmically
It can be applicable to a variety of problems
from different area of computing.
The design approach depends mainly on the
model chosen.
What is algorithm designing technique?
8. Department of Computer Science
They provide us guidance in designing
algorithms for new problems
They represent a collection of tools useful for
applications
Why do we need to know such techniques?
9. Department of Computer Science 9
Brute force
Decrease and conquer
Divide and conquer
Greedy technique
Dynamic programming
Backtracking
What are the most used techniques?
10. Department of Computer Science
How you solve this puzzle:
Given a 3×3 board with 8 tiles (every tile has
one number from 1 to 8) and one empty space.
The objective is to place the numbers on tiles to
match the final configuration using the empty
space. We can slide four adjacent (left, right,
above, and below) tiles into the empty space.
Initial state : any configuration
Goal state : tiles in a specific order
10
Motivation Discussion
12. Department of Computer Science
What is Brute Force?
A straightforward approach to solving a problem,
usually directly based on the problem’s statement
and definitions of the concepts involved.
“Brute Force” means “Just do it!”
Generally, it involved iterating through all possible
solutions until a valid one is found.
Other Names
Generate and Test
Exhaustive Search
Can you name some problems that can be solved
with BF
Examples:
12
What is Brute Force?
13. Department of Computer Science
What is Brute Force?
How many examples do you know?
Swapping two numbers
Computing an (a > 0, n a nonnegative integer)
Computing n!
Add n numbers
Decimal to Binary conversion
Finding Maximum/Minimum Element in a list
Multiply two n by n Matrices
Selection Sort
Bubble Sort
Sequential Search/Linear Search
Conversion of a 2D array into 1D array
Find the value of polynomial
13
Known examples of Brute Force
15. Department of Computer Science
Problem: Design an algorithm that Exchange the
values of two variables say x and y.
Solution:
What is input
Two numbers say x and y.
What should be the output
Interchange the value of x and y
Which designing technique?
Brute-Force
Logic/Idea
Example-1
16. Department of Computer Science
Logic/Idea
Take a temporary variable temp to swap the
numbers.
The contents of the first variable is copied into the
temp variable.
Then, the contents of second variable is copied to the
first variable.
Finally, the contents of the temp variable is copied
back to the second variable which completes the
swapping process.
Example-1
Do you have another
idea to solve
exchange problem?
17. Department of Computer Science
Do you have another idea to solve exchange
problem?
Example-1
18. Department of Computer Science
Brute Force Example
In RSA (Ron Rivest, Adi Shamir, and Leonard
Adleman) encryption algorithm we need to
compute an mod m for a > 1 and large n.
Design an algorithm that computes an using BF
Solution:
What is input
Two numbers say x and n.
What should be the output
Interchange the value of xn
Which designing technique?
Brute-Force
Logic/Idea
18
Example-2: Computing an
19. Department of Computer Science
Brute Force Example
Logic/Idea: x n =
First response: Multiply 1 by x n times which is
the “Brute Force” approach.
19
Computing an
x × x × … … × x
n times
20. Department of Computer Science
Induction Examples (4/4)
3.3 Mathematical Induction
Example 3: Factorial of positive integer
We want to compute n! = 1 × 2 × … … × n
Is there a
connection?
21. Department of Computer Science
Problem: Design an algorithm for finding the
binary representation of a positive decimal
integer.
Solution:
What is input
Positive integer n.
What should be the output
Binary string bk bk bk-1 bk-2………….. b1 b0
Which designing technique?
Brute-Force
Logic/Idea
Example 4: Decimal to Binary Conversion
22. Department of Computer Science
Problem: Design an algorithm for finding the
binary representation of a positive decimal
integer.
Representation
Example 4: Decimal to Binary Conversion
23. Department of Computer Science
Problem: Design an algorithm that count
numbers of bits in a binary representation of a
given number n.
Representation
Your Turn
25. Department of Computer Science
Problem: Design an algorithm that find the
maximum number in a given array of n elements.
Solution:
What is input
An array A of n numbers e.g.
What should be the output
The value of largest element in A. e.g., 10
Which designing technique?
Brute-Force
Logic/Idea
4 1 8 9 3 7 10 2 3 1
Example-6: Largest element
26. Department of Computer Science
Problem: Design an algorithm that find maximum
number in a given 2D array of nxn elements.
Solution:
Your Turn
27. Department of Computer Science
Problem: Design an algorithm that multiply two matrices
A and B
Solution:
What is input
Two 2D array A and B of compatible size.
What should be the output
A matrix C such that C = AB.
Which designing technique?
Brute-force
Logic/Idea?
55 99 6 29 1 7 3
Example-7
30. Department of Computer Science
Problem: Design an algorithm that find a key in a
given array of n elements.
Solution:
What is input
An array A of n numbers and a key say K
What should be the output
The location of key if found otherwise return false
Which designing technique?
Brute-Force
Logic/Idea
Example-8: Searching
31. Department of Computer Science
Brute-Force Polynomial Evaluation
Problem: Design an algorithm that compute the
value of polynomial
p(x) = anxn + an-1xn-1 +… + a1x1 + a0
at a point x = x0
Example-9
32. Department of Computer Science
Brute-Force Polynomial Evaluation
Problem: Find the value of polynomial
p(x) = anxn + an-1xn-1 +… + a1x1 + a0
at a point x = x0
Brute-force algorithm
p 0.0
for i n downto 0 do
power 1
for j 1 to i do //compute xi
power power x
p p + a[i] power
return p
Example-9
33. Department of Computer Science
Polynomial Evaluation: Improvement
We can do better by evaluating from right to left:
Better brute-force algorithm
p a[0]
power 1
for i 1 to n do
power power x
p p + a[i] power
return p
Example-9
34. Department of Computer Science
Problem: Design an algorithm convert a 2D array
into 1D array
Solution:
What is input
An 2D-array A[i][j]. e. g. int [5][5]
What should be the output
You could picture the conversion
to the corresponding 1-D array
like this:
Example-10
:
35. Department of Computer Science
Which designing technique?
Brute-Force
Logic/Idea
A 1-D array looks like this:int [5] :
You could picture the conversion to the corresponding 1-D array
like this
Example-10
:
36. Department of Computer Science
Logic/Idea
But an alternative way of thinking about it is to
picture the original array, but re-labelled - like this
Example-10
:
2-D array index [i][j] => 1-D array index [i*5 + j]
37. Department of Computer Science
Example-10
ALGORITHM Convert2d_into_1D(arr2D[i][j], arr1D [n*m])
Input:
OutPut:
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{ // mapping 2D array to 1D array
arr1D[i * m + j] = arr2D[i][j];
}
}
2-D array index [i][j] => 1-D array index [i*5 + j]
38. Department of Computer Science
What is Brute Force?
Numerical problems, searching, sorting, etc.
Acceptable efficiency
Can be used for large problem instances
Combinatorial problems
Exhaustive search
Set of candidate solutions grows very fast
Used only for reduced size instances.
Let us apply Brute force approach for solving
sorting problem.
38
Where to Apply?
40. Department of Computer Science
What is Brute Force?
The (most) straightforward approach for solving
a problem.
“Brute Force” means “Just do it!”
Directly based on
The problem statement
The definitions involved
Generally, it involved iterating through all possible
solutions until a valid one is found.
Other Names
Generate and Test
Exhaustive Search
40
What is Brute Force?
41. Department of Computer Science
What is Brute Force?
Strengths
Simplicity
Applicable to different kinds of problems
Weaknesses
(Very!) Low efficiency in some cases
Useful only for instances of (relatively) small size!
41
Strength and Weakness of Brute Force
42. Department of Computer Science 42
Algorithms should be learn at a higher level
with linkages to prior knowledge and with each
other so as to see how one algorithm is different
from the rest and what it does and does not
perform, and why?
In this integrative approach of learning the
initial investment is large but it is essential for
meaningful learning
How we do it?
43. Department of Computer Science 43
Intuition First & Formalism Later
Common & Small Building Blocks
Design of Cruder versions providing a ladder
Visual patterns or puzzles
We do not tell the story of an algorithm?
The interesting story connects all characters and
provides a panoramic picture
Platforms for Comparisons
Ladder
How we do it?