The document discusses brute force algorithms. It defines brute force as a straightforward approach to solving problems directly based on definitions without optimizations. Brute force algorithms examine all possible solutions exhaustively and verify each one. The document provides examples of using brute force for tasks like sorting, searching, and string matching. It notes that brute force is simple to implement but not very efficient for large problems. Characteristics, examples, strengths, and weaknesses of brute force algorithms are covered.
2. OBJECTIVE
To learn the definition of brute force
To learn the characteristic of brute force
To learn how brute force can solve the problem
3. DEFINITION
“Brute force is a straight-forward approach to solving a problem,
usually directly based on the problem statement and definitions of the
concepts involved.”
“This algorithm solves problems very simply,
directly and in obvious way”
4. CHARACTERISTIC
Brute force algorithms are generally not "smart" and not fast, becaus
e they require many steps in progress
The word "force" indicates “power" rather than "brain“
Sometimes called by naïve algorithm
Suit for small problem because of its simplicity and easy to implemen
t
5. CHARACTERISTIC (cont.. )
Brute-force algorithms are often used as comparative bases with faste
r algorithms
Although not a fast method, almost every problem can be solved with
this algorithm
It’s difficult to show problem that can not be solved by the brute force
method. In fact, there are problems that can only be solved by the brut
e force
6. EXAMPLE (1)
• Computing an (a > 0, n a non-negative integer)
an = a x a x … x a (n times) , if n > 0
= 1 , if n = 0
Algorithm : multiplying 1 by a as much as n times
Complexity : O(n)
7. EXAMPLE (2)
• Computing n!
n! = 1 × 2 × 3 × … × n , jika n > 0
= 1 , jika n = 0
Algorithm : multiplying n number (1,2,3. . .,n) together
Complexity : O(n)
8. EXAMPLE (3)
• Multiplying two matrices, A and B
For example :
C = A x B and matrix elements are expressed as cij, aij, and bij
Algorithm : calculate each element, multiplication one by one, by
multiplying two vectors of length n.
n
k
kjiknjinjijiij babababac
1
2211
9. SORTING PROBLEM
What is the most straightforward approach to solving a sorting
problem ??
Bubble sort and selection sort
These algorithm show the use of brute force obviously
10. CONTOH PENDEKATAN BRUTE FORCE
Jika diberikan sekumpulan data angka sebanyak 30 buah (misal 8, 4, 67, 20 48, 0, dst),
kemudian diminta untuk mengurutkan secara ascending, bagaimana pendekatan Brute Fo
rce melakukannya?
Pendekatan Brute Force mengusulkan algoritma yang membandingkan isi list data angka p
ertama dengan kedua, yang lebih kecil akan diletakkan pada susunan pertama. Berikutnya d
ata kedua dibandingkan dengan data ketiga, yang lebih kecil diletakkan disusunan kedua, d
st. Dibandingkan satu persatu, kemudian ditukar, diualng sampai urutan sesuai.
Bubble Sort adalah algoritma tersebut.
11. BUBBLE SORT
• Compare adjacent elements of the list and exchange them if they are
out of order
• Do it repeatedly, we end up “bubbling up” the largest element to the
last position on the list
Example : https://www.youtube.com/watch?v=Vpvgxm4hlGM
12. 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 righ
t 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]:
Example : https://www.youtube.com/watch?v=R_f3PJtRqUQ
14. CONTOH PENDEKATAN BRUTE FORCE
Jika diberikan sekumpulan data nama mahasiswa sebanyak 1000 buah (missal Udin, Marn
i, Kirana, dll), kemudian diminta untuk mencari nama “Budi”, bagaimana pendekatan Brute
Force melakukannya?
Pendekatan Brute Force mengusulkan algoritma yang mencari kata “Budi” dengan
membandingkan isi list nama mulai dari data pertama, kedua, ketiga dan seterusnya. Di
bandingkan satu persatu, sampai dengan ditemukan kata yang dicari. Pencarian berurutan a
tau Sequential search adalah algoritma tersebut.
Apakah ada pendekatan pencarian lain selain dicari satu persatu? Binary Search
15. STRING MATCHING
• Pattern: a string of (m) characters to search for
• Text: a long string of (n) characters to search in
• Brute force algorithm:
1. Align pattern at beginning of text
2. Moving from left to right, compare each character of pattern to the correspondin
g character in text until
a. All characters are found to match (successful search); or
b. 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.
• Example : https://www.youtube.com/watch?v=i2XBayzzkao
16. STRING MATCHING (example)
Pattern: GATTTCG (length = m = 7)
Text:
GATTTCG
GATTTCATCAGATTTCGATACAGAT
GATTTCGGATTTCGGATTTCGGATTTCGGATTTCGGATTTCGGATTTCGGATTTCGGATTTCGGATTTCG
Text length = n = 25
Best Case: ?
Worst Case: ?
17. BRUTE FORCE - STRING MATCHING
• Pattern length = m;
• Text length = n;
• Best Case:
GATTTCATCAGATTTCGATACAGAT
GATTTCA
O(m) The pattern is found right away, but you still have to do
m comparison to verify that is was found.
18. TRY BY YOUR SELF
• Solve this string matching problem
• Pattern : 001011
• Text : 10010101101001100101111010
• How many step to solve this problem ?
19. 1. Pattern length = m;
2. Text length = n;
3. Average Case:
4. Observation: If the number of symbols is relatively large (26 for e
xample) then
Most shifts will happen after very few comparisons.
5. In fact, there are very few pattern-text combinations that will cau
se (m) comparisons for each shift.
6. Note: There are always (n) shifts if the pattern is not found
BRUTE FORCE - STRING MATCHING
20. KEKUATAN
Metode brute force dapat digunakan untuk memecahkan hampir
sebagian besar masalah (wide applicability).
Metode brute force sederhana dan mudah dimengerti.
Metode brute force menghasilkan algoritma yang layak untuk
beberapa masalah penting seperti pencarian, pengurutan, pencocoka
n string, perkalian matriks.
Metode brute force menghasilkan algoritma baku (standard) untuk
tugas-tugas komputasi seperti penjumlahan/perkalian n buah bilanga
n, menentukan elemen minimum atau maksimum di dalam tabel (list)
.
21. KELEMAHAN
Metode brute force jarang menghasilkan algoritma yang mangkus.
Beberapa algoritma brute force lambat sehingga tidak dapat diterima.
Tidak sekonstruktif/sekreatif teknik pemecahan masalah lainnya.
Ken Thompson (salah seorang penemu Unix) mengatakan: “When in
doubt, use brute force”, faktanya kernel Unix yang asli lebih menyukai
algoritma yang sederhana dan kuat (robust) daripada algoritma yang
cerdas tapi rapuh.
22. REFFERENCE
Munir, Rinaldi. Diktat Kuliah “Kompleksitas Algoritma”, Departemen T
eknik Informatika ITB
Levitin, Anany. 2012. Introduction to the Design and Analysis of A
lgorithms, 3rd Edition.Addison Wesley
Brute force adalah sebuah pendekatan yang langsung (straightforward) untuk memecahkan suatu masalah, biasanya didasarkan pada pernyataan masalah (problem statement) dan definisi konsep yang dilibatkan.
Algoritma ini menyelesaikan masalah dengan sangat sederhana, langsung dan dengan cara yang jelas
Algoritma brute force umumnya tidak“cerdas” dan tidak cepat, karena ia membutuhkan jumlah langkah yang besar dalam penyelesaiannya.
Kata“force” mengindikasikan“tenaga” ketimbang“otak”
Kadang-kadang algoritma brute force disebut juga algoritma naif (naïve algorithm).
Algoritma brute force lebih cocok untuk masalah yang berukuran kecil karena kesederhanaan dan kemudahan dalam implementasinya
Algoritma brute force sering digunakan sebagai basis pembanding dengan algoritma yang lebih cepat.
Meskipun bukan metode yang cepat, hampir semua masalah dapat diselesaikan dengan algoritma brute force
Sukar menunjukkan masalah yang tidak dapat diselesaikan dengan metode brute force. Bahkan, ada masalah yang hanya dapat diselesaikan dengan metode brute force