0% found this document useful (0 votes)
24 views22 pages

Lecture 03 - Using Brute Force

The document discusses different brute force algorithms and problem solving techniques including selection sort, bubble sort, sequential search, string matching, traveling salesman problem and knapsack problem. It provides pseudocode to illustrate the brute force approach for these problems and explains that while brute force is a simple general technique, it can be highly inefficient for problems like TSP and knapsack.

Uploaded by

waheedgx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views22 pages

Lecture 03 - Using Brute Force

The document discusses different brute force algorithms and problem solving techniques including selection sort, bubble sort, sequential search, string matching, traveling salesman problem and knapsack problem. It provides pseudocode to illustrate the brute force approach for these problems and explains that while brute force is a simple general technique, it can be highly inefficient for problems like TSP and knapsack.

Uploaded by

waheedgx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Algorithms and Problem

Solving
Lecture 03 – Problem Solving
using Brute Force Strategy

AL-Zaiem AL-Azhari University


Faculty of Computer Sciences and Information
Technology
Mohammed Mahmoud
Overview
• Brute-force or exhaustive search is a
very general problem-solving technique
and algorithmic paradigm that consists of
systematically enumerating all possible
candidates for the solution and checking
whether each candidate satisfies the
problem's statement.
Brute Force Algorithms
• Sorting
• Selection Sort
• Bubble Sort
• Searching
• Sequential Search
• String Matching
• Exhaustive Search
• Travel Salesman Problem (TSP)
• Knapsack Problem
Brute Force Approach for
Sorting
• Given a list of n orderable items (e.g.,
numbers, characters from some
alphabet, character strings), rearrange
them in nondecreasing order.
Selection Sort
• Scan the array to find its smallest
element
• 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. repeat the
process to the last element in the list.
Selection Sort
begin

Find a smallest value in the list

Exchange it with current element

yes
move to next element ?

no

done
Selection Sort
ALGORITHM SelectionSort(A[0..n - 1])
//The algorithm sorts a given array by selection sort
//Input: An array A[0..n - 1] of orderable elements
//Output: Array A[0..n - 1] sorted in ascending order
for i  0 to n - 2 do
min  i
for j  i + 1 to n - 1 do
if A[j ]<A[min] min  j
swap A[i] and A[min]
Bubble Sort
• Compare adjacent elements of the list
• and exchange them if they are out of
order
• Then we repeat the process
• By doing it repeatedly, we end up
‘bubbling up’ the largest element to the
last position on the list
Bubble Sort
ALGORITHM BubbleSort(A[0..n - 1])
//The algorithm sorts array A[0..n - 1] by bubble sort
//Input: An array A[0..n - 1] of orderable elements
//Output: Array A[0..n - 1] sorted in ascending order
for i  0 to n - 2 do
for j  0 to n - 2 - i do
if A[j + 1]<A[j ] swap A[j ] and A[j + 1]
Searching – Sequential Search

• Compares successive elements of a given


list with a given search key until either a
match is encountered (successful search)
or the list is exhausted without finding a
match (unsuccessful search).
Searching – Sequential Search
ALGORITHM SequentialSearch2(A[0..n], K)
//The algorithm implements sequential search with a search key as a //
sentinel
//Input: An array A of n elements and a search key K
//Output: The position of the first element in A[0..n - 1] whose value is
// equal to K or -1 if no such element is found
A[n]  K
i0
while A[i] = K do
ii+1
if i < n return i
else return -1
String Matching
• Given a string of n characters called the
text and a string of m characters (m =
n) called the pattern, find a substring of
the text that matches the pattern. To put
it more precisely, we want to find i—the
index of the leftmost character of the
first matching substring in the text—such
that
String Matching
String Matching
ALGORITHM BruteForceStringMatch(T [0..n - 1], P[0..m - 1])
//The algorithm implements brute-force string matching.
//Input: An array T [0..n - 1] of n characters representing a text;
// an array P[0..m - 1] of m characters representing a pattern.
//Output: The position of the first character in the text that starts the first
// matching substring if the search is successful and -1 otherwise.
for i  0 to n - m do
j0
while j <m and P[j ]= T [i + j ] do
jj+1
if j = m return i
return -1
Exhaustive Search
• A brute-force approach to combinatorial
problem.
• It suggests generating each and every
element of the problem’s domain,
selecting those of them that satisfy the
problem’s constraints, and then finding a
desired element
Exhaustive Search
• There are two well – known optimization
problem:
• Traveling Salesman Problem
• Knapsack Problem
• Both the traveling salesman and knapsack
problems, exhaustive search leads to algorithms
that are extremely inefficient on every input.
• In fact, these two problems are the best-known
examples of so-called NP-hard problems. No
polynomial-time algorithm is known for any NP-
hard problem.
Travel Salesman Problem (TSP)
• To find the shortest tour through a
given set of n cities that visits each
city exactly once before returning to
the city where it started.

• The problem can be conveniently


modeled by a weighted graph, with
the graph’s vertices representing the
cities and the edge weights specifying
the distances. Then the problem can
be stated as the problem of finding
the shortest Hamiltonian circuit of
the graph.

• A Hamiltonian circuit is defined as a


cycle that passes through all the
vertices of the graph exactly once.
Travel Salesman Problem (TSP)
Knapsack Problem
• Given n items of known weights w1, . . . , wn and values v1, . . . ,
vn and a knapsack of capacity W, find the most valuable subset
of the items that fit into the knapsack.
• The 0/1 knapsack problem is a problem that deals with putting
the best items out of a set into a backpack without exceeding the
backpack’s capacity.
• To better explain this, consider packing a backpack for a day .
Your backpack has a certain size, its capacity. Now, there are
many different items that you could put into your backpack but
you want to take items that are appropriate for the trip and leave
the not so important things behind.
Knapsack Problem
• The exhaustive search approach to this
problem leads to considering all the
subsets of the set of n items given,
computing the total weight of each
subset in order to identify feasible
subsets and finding a subset of the
largest value among them.
Knapsack Problem
END!

You might also like