0% found this document useful (0 votes)
2 views

M2 Chapter 3

Chapter 3 discusses brute force and exhaustive search methods for problem-solving, particularly in sorting algorithms like selection sort and bubble sort, as well as string matching and partition problems. It also covers exhaustive search applications in combinatorial problems such as the Traveling Salesman Problem, Knapsack Problem, and Assignment Problem, highlighting their complexities and the impracticality of brute force for larger instances. Lastly, it introduces depth-first search (DFS) and breadth-first search (BFS) algorithms for graph traversal, detailing their processes and applications.

Uploaded by

gasaja6790
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

M2 Chapter 3

Chapter 3 discusses brute force and exhaustive search methods for problem-solving, particularly in sorting algorithms like selection sort and bubble sort, as well as string matching and partition problems. It also covers exhaustive search applications in combinatorial problems such as the Traveling Salesman Problem, Knapsack Problem, and Assignment Problem, highlighting their complexities and the impracticality of brute force for larger instances. Lastly, it introduces depth-first search (DFS) and breadth-first search (BFS) algorithms for graph traversal, detailing their processes and applications.

Uploaded by

gasaja6790
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Chapter 3: Brute Force

• The subject of this class is brute force and its important special case, exhaustive search.
Brute force can be described as follows:
➢ Brute force is a straightforward approach to solving a problem, usually directly
based on the problem statement and definitions of the concepts involved.
➢ “Just do it!” would be another way to describe the prescription of the brute-force
approach. And often, the brute-force strategy is indeed the one that is easiest to
apply.
Ex: compute 𝑎𝑛 for a nonzero number ‘a’ and a nonnegative integer ‘n’.

Ex: 25 = 2 ∗ 2 ∗ 2 ∗ 2 ∗ 2 = 32

• This method suggests simply computing 𝑎𝑛 by multiplying ‘a’ ‘n’ times


L7: Selection Sort and Bubble Sort
• The application of the brute-force approach to the problem of sorting:

1. Selection Sort :

After n − 1 passes, the list is sorted.


• The analysis of selection sort is straightforward. The input size is
given by the number of elements n.
• The basic operation is the key comparison A[j ] < A[min].
• The number of times it is executed depends only on the array size and
is given by the following sum:

• Thus, selection sort is a 𝜃(𝑛2) algorithm on all inputs.


Example:
2. Bubble Sort
• Another brute-force application to the sorting problem is to compare
adjacent elements of the list and exchange them if they are out of
order.
• By doing it repeatedly, we end up “bubbling up” the largest element to
the last position on the list.
• The next pass bubbles up the second largest element, and so on, until
after n − 1 passes the list is sorted.
The number of key comparisons for the bubble-sort version given above is the same for all arrays of size n; it
is obtained by a sum that is almost identical to the sum for selection sort.
Example:
Questions:
1. List the difference between selection sort and bubble sort?
2. Write a C program to implement selection sort and bubble sort?
L9: Sequential Search, Brute-Force String Matching
What is the best, average, and worst-case time complexity of linear
search?

1. Best case: O(1)


2. Avg case: O(n/2)
3. Worst Case: O(n)
Brute-Force 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
Example:
• Thus, in the worst case, the
algorithm makes m(n − m + 1)
Executes (n-m+1) time, i.e, U-L+1 character comparisons, which
puts it in the O(nm) class.
• for random natural-language
Executes almost ‘m’ times texts, the average-case efficiency
indeed turns out to be in O(n +
m).

Note: its time complexity depends on the size of ‘P’ & ‘T’. If both P & T are of same size ‘n’, then the worst-
case complexity is O(n*n)
Partition problem using brute-force technique
• The partition problem is to determine whether a given Algorithm:
set can be partitioned into two subsets such that the
sum of elements in both subsets is the same.
1. For each recursion of the method, divide the
Examples: problem into two sub problems such that:
1. Input: arr[] = {1, 5, 11, 5} a. Create a new subset of the
array including the last element of the array if
Output: true its value does not exceed S/2 and repeat the
The array can be partitioned as {1, 5, 5} and {11} recursive step 1 again for the new subarray
2. Input: arr[] = {1, 5, 3} b. Create a new subset of the
Output: false array excluding the last element of the array
and repeat the recursive step 2 again for the
The array cannot be partitioned into equal sum new subarray
sets.
c. If the sum of any of the above subsets is
• Brute force approach: This is a recursive method in equal to S/2, return true otherwise return false
which we consider each possible subset of the array
2. If any of the above sub problems return true, then
and check if its sum is equal to total sum S/2 or not, by return true otherwise return false
eliminating the last element in the array in each turn.
L10: Exhaustive Search
• Exhaustive search is simply a brute-force approach to combinatorial
problems. It suggests generating each and every element of the problem
domain, selecting those of them that satisfy all the constraints, and then
finding a desired element (e.g., the one that optimizes some objective
function).
• We illustrate exhaustive search by applying it to three important
problems:
➢Traveling Salesman Problem
➢Knapsack Problem
➢Assignment Problem
1. Traveling Salesman Problem
Problem Statement:
• A traveler needs to visit all the cities from a list, where distances between all the cities are known and each
city should be visited just once. What is the shortest possible route that he visits each city exactly once and
returns to the origin city?
• 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.)
Solution:
• It is easy to see that a Hamiltonian circuit can also be defined as a sequence of n + 1 adjacent vertices
vi0 , vi1 ,...,vin−1 , vi0 …………………….>(Ex: a-b-c-d-a)
where the first vertex of the sequence is the same as the last one and all the other n − 1 vertices are distinct.
• Thus, we can get all the tours by generating all the permutations of n − 1 intermediate cities i.e, (n-1)!
feasible solutions (one that satisfies all defined constraints and requirements), compute the tour lengths,
and find the shortest among them (optimal).
• Consider a source node(a) and form a path
by its adjacent nodes (a-b, a-c, & a-d).

• Consider adjuacent nodes of b, c, &d, then


form all possible paths without revising any
of the node except the last one.
2. Knapsack Problem
• Given ‘n’ items of known weights w1, w2,...,wn and values v1,
v2,...,vn and a knapsack of capacity W, find the most valuable subset
of the items that fit into the knapsack.
• Ex: A transport plane that has to deliver the most valuable set of
items to a remote location without exceeding the plane’s capacity.
• The exhaustive-search approach to this problem leads to generating all
the subsets of the set of ‘n’ items given, computing the total weight of
each subset in order to identify feasible subsets (i.e., the ones with the
total weight not exceeding the knapsack capacity), and finding a
subset of the largest value among them.
Example:

𝟐𝒏

FIGURE : (a) Instance of the knapsack problem. (b) Its solution by exhaustive search. The information about the optimal selection is in bold
• Since the number of subsets of an n-element set is
𝟐𝒏 (𝑖𝑛𝑐𝑙𝑢𝑑𝑖𝑛𝑔 𝑒𝑚𝑝𝑙𝑡𝑦 𝑠𝑒𝑡), the exhaustive search leads to a Ω(2𝑛 )
algorithm, no matter how efficiently individual subsets are generated.
• Both the traveling salesman and knapsack problems are the best-
known examples of so-called NP-hard (non-deterministic
polynomial-time hard) problems.
• No polynomial-time algorithm is known for any NP-hard problem.
3. Assignment Problem
• There are ‘n’ people who need to be assigned to execute ‘n’ jobs, one
person per job. (That is, each person is assigned to exactly one job and
each job is assigned to exactly one person.)
• The cost that would result if the ith person is assigned to the jth job is a
known quantity C[i, j ] for each pair i, j = 1, 2,...,n.
• The problem is to find an assignment with the minimum total cost.
• We can describe feasible solutions to the assignment problem as n-tuples
j1,........,jn in which the ith component, i = 1,......,n, indicates the column
of the element selected in the ith row (i.e., the job number assigned to
the ith person).
For example, for the cost matrix below,

• 2, 3, 4, 1 indicates the assignment of Person 1 to Job 2, Person 2 to Job 3, Person 3 to Job


4, and Person 4 to Job 1.
• The requirements of the assignment problem imply that there is a one-to-one
correspondence between feasible assignments and permutations of the first ‘n’ integers.
Therefore, the exhaustive-search approach to the assignment problem would require
generating all the permutations of integers 1, 2, . . . , n, (or n!) computing the total cost of
each assignment by summing up the corresponding elements of the cost matrix, and finally
selecting the one with the smallest sum.
• Since the number of permutations to be considered for the general case of the assignment problem is n!
(In the above example, we get 4!=24 possible solutions), exhaustive search is impractical for all but very
small instances of the problem. Fortunately, there is a much more efficient algorithm for this problem
called the Hungarian method.

• The Hungarian method is defined as a combinatorial optimization technique that solves the assignment
problems in polynomial time.
L11: Depth-First Search (DFS) and Breadth-First Search
(BFS).

• The term “exhaustive search” can also be applied to two very


important algorithms that systematically process all vertices and edges
of a graph. These two traversal algorithms are depth-first search (DFS)
and breadth-first search (BFS).
• These algorithms have proved to be very useful for many applications
involving graphs in artificial intelligence and operations research. In
addition, they are indispensable for efficient investigation of
fundamental properties of graphs such as connectivity and cycle
presence.
1. Depth-First Search (DFS)
• Depth-first search starts a graph’s traversal at an arbitrary vertex by marking it as
visited.
• On each iteration, the algorithm proceeds to an unvisited vertex that is adjacent to
the one it is currently in. (If there are several such vertices, a tie can be resolved
arbitrarily. As a practical matter, which of the adjacent unvisited candidates is
chosen is dictated by the data structure representing the graph. In our examples,
we always break ties by the alphabetical order of the vertices.)
• This process continues until a dead end—a vertex with no adjacent unvisited
vertices— is encountered. At a dead end, the algorithm backs up one edge to the
vertex it came from and tries to continue visiting unvisited vertices from there.
• The algorithm eventually halts after backing up to the starting vertex, with the
latter being a dead end. By then, all the vertices in the same connected component
as the starting vertex have been visited.
• If unvisited vertices still remain, the depth-first search must be restarted at any one
of them.
• It is convenient to use a stack (LIFO) to trace the operation of depth-
first search. We push a vertex onto the stack when the vertex is reached
for the first time (i.e., the visit of the vertex starts), and we pop a vertex
off the stack when it becomes a dead end (i.e., the visit of the vertex
ends).
• It is also very useful to accompany a depth-first search traversal by
constructing the so-called depth-first search forest.
• The starting vertex of the traversal serves as the root of the first tree in
such a forest.
• Whenever a new unvisited vertex is reached for the first time, it is
attached as a child to the vertex from which it is being reached. Such an
edge is called a tree edge because the set of all such edges forms a
forest.
• The algorithm may also encounter an edge leading to a previously
visited vertex other than its immediate predecessor (i.e., its parent in the
tree). Such an edge is called a back edge because it connects a vertex to
its ancestor, other than the parent, in the depth-first search forest.
The time complexity of the DFS
algorithm is represented in the
form of O(V + E), where V is
the number of nodes and E is
the number of edges.
2. Breadth-First Search (BFS)
• The breadth-first search (BFS) algorithm is used to search a tree or
graph data structure for a node that meets a set of criteria. It starts at the
tree’s root or graph and searches/visits all nodes at the current depth
level before moving on to the nodes at the next depth level. Breadth-
first search can be used to solve many problems in graph theory.
• It is convenient to use a queue (FIFO) to trace the operation of
breadth-first search. The queue is initialized with the traversal’s starting
vertex, which is marked as visited. On each iteration, the algorithm
identifies all unvisited vertices that are adjacent to the front vertex,
marks them as visited, and adds them to the queue; after that, the front
vertex is removed from the queue.
• Visit a node and add to Q
• add all its adjacent nodes to Q
and remove it.
• Like a DFS traversal, it is useful to accompany a BFS traversal by
constructing the so-called breadth-first search forest.
• The traversal’s starting vertex serves as the root of the first tree in such a
forest.
• Whenever a new unvisited vertex is reached for the first time, the vertex
is attached as a child to the vertex it is being reached from with an edge
called a tree edge.
• If an edge leading to a previously visited vertex other than its immediate
predecessor (i.e., its parent in the tree) is encountered, the edge is noted
as a cross edge.
The time complexity of the BFS
algorithm is represented in the form
of O(V + E), where V is the number
of nodes and E is the number of
edges.
Difference between DFS and BFS
• DFS Applications
1. DFS of a graph produces the minimum spanning tree and pair of all shortest-path trees.
2. Detects a cycle in a graph; A graph contains cycle if there exists a back edge for a node. This can be done
with a depth-first search algorithm because we when we can maintain the backtrack in DFS, we can maintain
the back edge.
3. To find the path between cities: The DFS algorithm finds the path between two vertices(u and v)
4. Topological sorting: In this sorting, we schedule jobs from the given dependencies among jobs. In computer
science, applications of this type arise in instruction scheduling, ordering formula cell evaluation when
recomputing formula values in spreadsheets, logic synthesis, determining the order of compilation tasks to
perform in makefiles, data serialization, and resolving symbol dependencies in linkers.
5. DFS algorithm also checks if a graph is bipartite or not.
6. Using DFS, we can find the strongly connected components in a graph. Strongly connected components are
the ones that are connected to every node in the graph.
7. We can solve maze puzzles with the DFS algorithm’s help as it can be implemented to find all possible paths
to a maze end by only including nodes on the visited set’s current path.
8. The bridges in a graph can be found using DFS.
9. It is also used in planarity testing and finding biconnectivity in graphs.
10.It is also used to generate words in order to plot the limit set of a group.
• BFS Applications
1. Shortest Path and MST for unweighted graph: The path with the least number of edges is the shortest path. With the
help of BFS, we can reach a vertex from a given source using the minimum number of edges. We can also use DFS or BFS
to find a spanning tree in the unweighted graph.
2. To find all neighbor nodes BFS can be a great help. It is useful in finding the peer to peer networks. Example - BitTorrent
3. The crawlers in the search engine use the BFS approach. The idea used behind it is to start from the source page and repeat
it. DFS can also be used for crawlers, but it has some limits.
4. DFS is also used in social networking websites where we see a mutual friend in the friend list, and we can search anyone
via mutual connections.
5. BFS is also helpful in navigation as it is easier to find the neighboring locations on maps.
6. In broadcasting, a packet uses the BFS algorithm to reach all the destinations.
7. It is also used in Chenney’s algorithm in garbage collection. Due to better locality of reference, we use BFS over DFS.
8. The cycle detection can be done using BFS or DFS. For weighted graphs, BFS helps in cycle detection.
9. The BFS algorithm is beneficial in the ford-Fulkerson algorithm to find the maximum flow because it reduces O’s time
complexity(VE^2).
10.The BFS algorithm also checks the bipartiteness of a graph.
11.To find all the reachable nodes from a given node BFS is much beneficial.
Lab 4. BRUTE FORCE TECHNIQUE - II
1. Write a program to implement Knapsack problem using brute-force design
technique and analyze its time efficiency. Obtain the experimental result of order of
growth and plot the result. Knapsack Problem: Given n items of known weights
w1, w2, ..wn values v1, v2,...vn and a knapsack of capacity B, find the most
valuable subset of items that fit into the knapsack.
2.Write a program for assignment problem by brute-force technique and analyze its
time efficiency. Obtain the experimental result of order of growth and plot the result.
3.Write a program for depth-first search (DFS) of a graph. Identify the push and pop
order of vertices.
4.Write a program for breadth-first search (BFS) of a graph.
Tutorial:
1. Solve TSP(travelling salesman problem)
2. Solve Knapsack problem for,
Knapsack Max weight : W = 10 (units) Total
items : N = 4
Values of items : v[] = {10, 40, 30, 50} Weight of
items : w[] = {5, 4, 6, 3}

3. Solve the assignment problem for the matrix


(The given problem is balanced with 5 job and 5 men. )
Solve DFS & BFS for the below graphs?

By:
Dr. Geeta S Hukkeri

You might also like