0% found this document useful (0 votes)
9 views18 pages

Module 2

Uploaded by

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

Module 2

Uploaded by

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

MODULE-2

BRUTE FORCE
APPROACHES(conti…)
Exhaustive Search
• a brute-force approach to combinatorial problems.
• It suggests generating each and every element of the problem's
domain, selecting those of them that satisfy all the constraints, and
then finding a desired element.
• Examples for the Exhaustive Search
1.Traveling salesman problem
2. The knapsack problem
Traveling Salesman Problem (TSP)
Problem Statement: 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.
• Modeled by a weighted graph, with the graph's vertices representing the
cities and the edge weights specifying the distances.
• 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.
• A Hamiltonian circuit can also be defined as a sequence of 𝑛+1 adjacent
vertices 𝑣𝑖0,𝑣𝑖1,…,𝑣𝑖(𝑛−1),𝑣𝑖0 where the first vertex of the sequence is the
same as the last one, and all the other 𝑛−1 vertices are distinct.
Example problem
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, such that the total weight does not
exceed 𝑊and the total value is maximized.
• 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 to identify feasible subsets
Decrease-and-Conquer
• technique is based on exploiting the relationship between a solution
to a given instance of a problem and a solution to a smaller instance
of the same problem.
• Once such a relationship is established, it can be exploited either top
down (recursively) or bottom up (without a recursion).
• There are three major variations of decrease-and-conquer
1. Decrease by a constant
2. Decrease by a constant factor
3. Variable size Decrease
Decrease by CONSTANT(ONE)
• The size of an instance is reduced by the same constant on each
iteration of the algorithm. Typically, this constant is equal to one

Ex: n! ,𝑎^𝑛,Topological sorting


Decrease-by-a-constant-factor
• Technique suggests reducing a problem's instance by the same
constant factor on each iteration of the algorithm.
variable-size-decrease
• variety of decrease-and-conquer, a size reduction pattern varies from
one iteration of an algorithm to another.
ex: Euclid's algorithm for computing the greatest common divisor
INSERTION SORT
• we consider sorting an array 𝐴[0..𝑛−1] using the decrease-by-one
technique.
• We assume the smaller problem of sorting the array 𝐴[0..𝑛−2] has
already been solved, giving us a sorted array of size 𝑛−1:
𝐴[0]≤...≤𝐴[𝑛−2]
• To solve the original problem, we need to find the appropriate
position for 𝐴[𝑛−1] in the sorted subarray and insert it there.
• This involves locating the correct insertion point for 𝐴[𝑛−1] among
the sorted elements and inserting it into its correct position.
To insert 𝐴[𝑛−1]] into its correct position in the sorted subarray
𝐴[0..𝑛−2] ,we have three reasonable alternatives:
1.Left-to-Right Scan:
1. Method: Scan the sorted subarray from left to right.
2. Process: Start at the beginning of the sorted subarray and move rightward
until you find the first element that is greater than or equal to 𝐴[𝑛−1]
3. Insertion: Insert 𝐴[𝑛−1] right before this element.
2.Right-to-Left Scan:
• Method: Scan the sorted subarray from right to left.
• Process: Start at the end of the sorted subarray and move leftward
until you find the first element that is smaller than or equal to 𝐴[𝑛−1].
• Insertion: Insert 𝐴[𝑛−1] right after this element.
3.Binary Search:
• Method: Use binary search to find the correct insertion point.
• Process: Perform a binary search on the sorted subarray to find the
position where 𝐴[𝑛−1]A[n−1] should be inserted.
ALGORITHM InsertionSort(A[0 .. n-1])
// Sorts a given array by insertion sort
// Input: An array A[0 .. n-1] of n orderable elements
// Output: Array A[0 .. n-1] sorted in nondecreasing order

for i <- 1 to n-1 do


v <- A[i]
j <- i-1
while j >= 0 and A[j] > v do
A[j + 1] <- A[j]
j <- j-1
A[j + 1] <- v
Topological Sorting
• . A directed graph( digraph) is a graph with directions specified for all
its edges
• There are only two notable differences between undirected and
directed graphs in representing them:
(1) the adjacency matrix of a directed graph does not have to be
symmetric
(2) An edge in a directed graph has just one corresponding nodes in
the digraph's adjacency lists.
• Topological sorting(ordering) for Directed Acyclic Graph (DAG) G = (V,
E), is a linear ordering of vertices such that for every directed edge( u
,v), vertex u comes before v in the ordering.
• Topological sorting(ordering) can be done by using 2 algorithms
1. DFS traversal
2. Source removal method
Topological ordering by DFS traversal
• perform a DFS traversal and note the order in which vertices become
dead ends (i.e., are popped off the traversal stack). Reversing this
order yields a solution to the topological sorting problem
Source removal method

• The second algorithm is based on a direct implementation of the


decrease (by one )-and-conquer technique
1. Repeatedly ,Identify in a digraph a source, which is a vertex with no
incoming edges
2. delete it along with all the edges outgoing from it.
3. The order in which the vertices are deleted yields a solution to the
topological sorting problem.

You might also like