Design and Analysis of
Algorithms
Brute Force Techniques
Presented by,
Mrs. A.Jayanthi
Assistant Professor – CSE
Velammal Engineering College
2
Brute Force
A straightforward approach, usually based directly on the
problem’s statement and definitions of the concepts involved
Examples – based directly on definitions:
1. Computing an
(a > 0, n is a nonnegative integer)
2. Computing n!
3. Multiplying two matrices
4. Searching for a key of a given value in a list
3
Sorting by Brute Force
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 right 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]:
A[0]  . . .  A[i-1] | A[i], . . . , A[min], . . ., A[n-1]
in their final positions
4
Selection Sort
Example: 7 3 2 5
Solution:
Pass 1 :
 Find the Minimum element in the array : 2
 Swap the element 2 with the first element 7
2 3 7 5
Pass 2 :
 Find the Minimum element in the unsorted part of the
array (from index 1 to the end): 3
 Since 3 is already in the correct position, no swap is needed.
2 3 7 5
5
Selection Sort
Pass 2 : 2 3 7 5
Pass 3 :
 Find the Minimum element in the unsorted part of the
array (from index 2 to the end): 5
 Swap 5 with the second last element 7
2 3 5 7
Sorted Array is : 2 3 5 7
6
Algorithm of Selection Sort
7
Exhaustive Search
A brute force solution to a problem involving search for an
element with a special property, usually among
combinatorial objects such as permutations, combinations, or
subsets of a set.
Method:
• generate a list of all potential solutions to the problem in a
systematic manner
• evaluate potential solutions one by one, disqualifying
infeasible ones and, for an optimization problem, keeping
track of the best one found so far
• when search ends, announce the solution(s) found
8
Exhaustive Search – Examples
1. Traveling Salesman Problem (TSP)
2. Knapsack Problem
3. Assignment Problem
9
Example 1: Traveling Salesman Problem
 Given n cities with known distances between each pair, find
the shortest tour that passes through all the cities exactly
once before returning to the starting city
 More formally: Find shortest Hamiltonian circuit in a
weighted connected graph
 Example:
a b
c d
8
2
7
5 3
4
10
TSP by Exhaustive Search
Tour Cost
a→b→c→d→a 2+3+7+5 = 17
a→b→d→c→a 2+4+7+8 = 21
a→c→b→d→a 8+3+4+5 = 20
a→c→d→b→a 8+7+4+2 = 21
a→d→b→c→a 5+4+3+8 = 20
a→d→c→b→a 5+7+3+2 = 17
Therefore, the optimal Solution is,
a→b→c→d→a
a→d→c→b→a Total Cost : 17
11
Example 2: Knapsack Problem
Given n items:
• weights: w1 w2 … wn
• values: v1 v2 … vn
• a knapsack of capacity W
Find most valuable subset of the items that fit into the knapsack
Example: Knapsack capacity W=10
item weight value
1 7 $42
2 3 $12
3 4 $40
4 5 $25
12
Knapsack: Exhaustive Search
Subset Total weight Total value
{0} 0 0
{1} 7 $42
{2} 3 $12
{3} 4 $40 Feasible Solution is :
{4} 5 $25 {3,4} – Total Profit : 65
{1,2} 10 $54
{1,3} 11 not feasible
{1,4} 12 not feasible
{2,3} 7 $52
{2,4} 8 $37
{3,4} 9 $65
{1,2,3} 14 not feasible
{1,2,4} 15 not feasible
{1,3,4} 16 not feasible
{2,3,4} 12 not feasible
{1,2,3,4} 19 not feasible
13
Example 3: The Assignment Problem
There are n people who need to be assigned to n jobs, one
person per job. The cost of assigning person i to job j is C[i,j].
Find an assignment that minimizes the total cost.
Job 1 Job 2 Job 3 Job 4
Person 1 9 2 7 8
Person 2 6 4 3 7
Person 3 5 8 1 8
Person 4 7 6 9 4
Algorithmic Plan:
Generate all legitimate assignments
Compute costs
Select cheapest
14
9 2 7 8
6 4 3 7
5 8 1 8
7 6 9 4
Iteration 1 :
Assignment (col.#s) Total Cost
Step 1: 1, 2, 3, 4 9+4+1+4=18
Step 2: 1, 2, 4, 3 9+4+8+9=30
Step 3: 1, 3, 2, 4 9+3+8+4=24
Step 4: 1, 3, 4, 2 9+3+8+6=26
Step 5: 1, 4, 2, 3 9+7+8+9=33
Step 6: 1, 4, 3, 2 9+7+1+6=23
Minimum Cost : 18
Assignment Problem: Exhaustive Search
C =
15
9 2 7 8
6 4 3 7
5 8 1 8
7 6 9 4
Iteration 2 :
Assignment (col.#s) Total Cost
Step 1: 2, 1, 3, 4 2+6+1+4=13
Step 2: 2, 1, 4, 3 2+6+8+9=25
Step 3: 2, 3, 1, 4 2+3+5+4=14
Step 4: 2, 3, 4, 1 2+3+8+7=20
Step 5: 2, 4, 1, 3 2+7+5+9=23
Step 6: 2, 4, 3, 1 2+7+1+7=17
Minimum Cost : 13
Assignment Problem: Exhaustive Search
C =
16
9 2 7 8
6 4 3 7
5 8 1 8
7 6 9 4
Iteration 3 :
Assignment (col.#s) Total Cost
Step 1: 3, 1, 2, 4 7+6+8+4=25
Step 2: 3, 1, 4, 2 7+6+8+6=27
Step 3: 3, 2, 1, 4 7+4+5+4=20
Step 4: 3, 2, 4, 1 7+4+8+7=26
Step 5: 3, 4, 1, 2 7+7+5+6=25
Step 6: 3, 4, 2, 1 7+7+8+7=29
Minimum Cost : 20
Assignment Problem: Exhaustive Search
C =
17
9 2 7 8
6 4 3 7
5 8 1 8
7 6 9 4
Iteration 4 :
Assignment (col.#s) Total Cost
Step 1: 4, 1, 2, 3 8+6+8+9=31
Step 2: 4, 1, 3, 2 8+6+1+6=21
Step 3: 4, 2, 1, 3 8+4+5+9=26
Step 4: 4, 2, 3, 1 8+4+1+7=20
Step 5: 4, 3, 1, 2 8+3+5+6=22
Step 6: 4, 3, 2, 1 8+3+8+7=26
Minimum Cost : 20
Assignment Problem: Exhaustive Search
C =
18
The Minimum Cost from each iterations is,
Iteration 1 : Minimum Cost = 18
Iteration 2 : Minimum Cost = 13
Iteration 3 : Minimum Cost = 20
Iteration 4 : Minimum Cost = 20
Optimal Solution : Person 1 – Job 2
Person 2 – Job 1 Minimum Cost : 13
Person 3 – Job 3
Person 4 – Job 4
Assignment Problem: Exhaustive Search
19
THANK YOU

INTERNET PROGRAMMING UNIT 5 NOTES DAA.pptx

  • 1.
    Design and Analysisof Algorithms Brute Force Techniques Presented by, Mrs. A.Jayanthi Assistant Professor – CSE Velammal Engineering College
  • 2.
    2 Brute Force A straightforwardapproach, usually based directly on the problem’s statement and definitions of the concepts involved Examples – based directly on definitions: 1. Computing an (a > 0, n is a nonnegative integer) 2. Computing n! 3. Multiplying two matrices 4. Searching for a key of a given value in a list
  • 3.
    3 Sorting by BruteForce 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 right 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]: A[0]  . . .  A[i-1] | A[i], . . . , A[min], . . ., A[n-1] in their final positions
  • 4.
    4 Selection Sort Example: 73 2 5 Solution: Pass 1 :  Find the Minimum element in the array : 2  Swap the element 2 with the first element 7 2 3 7 5 Pass 2 :  Find the Minimum element in the unsorted part of the array (from index 1 to the end): 3  Since 3 is already in the correct position, no swap is needed. 2 3 7 5
  • 5.
    5 Selection Sort Pass 2: 2 3 7 5 Pass 3 :  Find the Minimum element in the unsorted part of the array (from index 2 to the end): 5  Swap 5 with the second last element 7 2 3 5 7 Sorted Array is : 2 3 5 7
  • 6.
  • 7.
    7 Exhaustive Search A bruteforce solution to a problem involving search for an element with a special property, usually among combinatorial objects such as permutations, combinations, or subsets of a set. Method: • generate a list of all potential solutions to the problem in a systematic manner • evaluate potential solutions one by one, disqualifying infeasible ones and, for an optimization problem, keeping track of the best one found so far • when search ends, announce the solution(s) found
  • 8.
    8 Exhaustive Search –Examples 1. Traveling Salesman Problem (TSP) 2. Knapsack Problem 3. Assignment Problem
  • 9.
    9 Example 1: TravelingSalesman Problem  Given n cities with known distances between each pair, find the shortest tour that passes through all the cities exactly once before returning to the starting city  More formally: Find shortest Hamiltonian circuit in a weighted connected graph  Example: a b c d 8 2 7 5 3 4
  • 10.
    10 TSP by ExhaustiveSearch Tour Cost a→b→c→d→a 2+3+7+5 = 17 a→b→d→c→a 2+4+7+8 = 21 a→c→b→d→a 8+3+4+5 = 20 a→c→d→b→a 8+7+4+2 = 21 a→d→b→c→a 5+4+3+8 = 20 a→d→c→b→a 5+7+3+2 = 17 Therefore, the optimal Solution is, a→b→c→d→a a→d→c→b→a Total Cost : 17
  • 11.
    11 Example 2: KnapsackProblem Given n items: • weights: w1 w2 … wn • values: v1 v2 … vn • a knapsack of capacity W Find most valuable subset of the items that fit into the knapsack Example: Knapsack capacity W=10 item weight value 1 7 $42 2 3 $12 3 4 $40 4 5 $25
  • 12.
    12 Knapsack: Exhaustive Search SubsetTotal weight Total value {0} 0 0 {1} 7 $42 {2} 3 $12 {3} 4 $40 Feasible Solution is : {4} 5 $25 {3,4} – Total Profit : 65 {1,2} 10 $54 {1,3} 11 not feasible {1,4} 12 not feasible {2,3} 7 $52 {2,4} 8 $37 {3,4} 9 $65 {1,2,3} 14 not feasible {1,2,4} 15 not feasible {1,3,4} 16 not feasible {2,3,4} 12 not feasible {1,2,3,4} 19 not feasible
  • 13.
    13 Example 3: TheAssignment Problem There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person i to job j is C[i,j]. Find an assignment that minimizes the total cost. Job 1 Job 2 Job 3 Job 4 Person 1 9 2 7 8 Person 2 6 4 3 7 Person 3 5 8 1 8 Person 4 7 6 9 4 Algorithmic Plan: Generate all legitimate assignments Compute costs Select cheapest
  • 14.
    14 9 2 78 6 4 3 7 5 8 1 8 7 6 9 4 Iteration 1 : Assignment (col.#s) Total Cost Step 1: 1, 2, 3, 4 9+4+1+4=18 Step 2: 1, 2, 4, 3 9+4+8+9=30 Step 3: 1, 3, 2, 4 9+3+8+4=24 Step 4: 1, 3, 4, 2 9+3+8+6=26 Step 5: 1, 4, 2, 3 9+7+8+9=33 Step 6: 1, 4, 3, 2 9+7+1+6=23 Minimum Cost : 18 Assignment Problem: Exhaustive Search C =
  • 15.
    15 9 2 78 6 4 3 7 5 8 1 8 7 6 9 4 Iteration 2 : Assignment (col.#s) Total Cost Step 1: 2, 1, 3, 4 2+6+1+4=13 Step 2: 2, 1, 4, 3 2+6+8+9=25 Step 3: 2, 3, 1, 4 2+3+5+4=14 Step 4: 2, 3, 4, 1 2+3+8+7=20 Step 5: 2, 4, 1, 3 2+7+5+9=23 Step 6: 2, 4, 3, 1 2+7+1+7=17 Minimum Cost : 13 Assignment Problem: Exhaustive Search C =
  • 16.
    16 9 2 78 6 4 3 7 5 8 1 8 7 6 9 4 Iteration 3 : Assignment (col.#s) Total Cost Step 1: 3, 1, 2, 4 7+6+8+4=25 Step 2: 3, 1, 4, 2 7+6+8+6=27 Step 3: 3, 2, 1, 4 7+4+5+4=20 Step 4: 3, 2, 4, 1 7+4+8+7=26 Step 5: 3, 4, 1, 2 7+7+5+6=25 Step 6: 3, 4, 2, 1 7+7+8+7=29 Minimum Cost : 20 Assignment Problem: Exhaustive Search C =
  • 17.
    17 9 2 78 6 4 3 7 5 8 1 8 7 6 9 4 Iteration 4 : Assignment (col.#s) Total Cost Step 1: 4, 1, 2, 3 8+6+8+9=31 Step 2: 4, 1, 3, 2 8+6+1+6=21 Step 3: 4, 2, 1, 3 8+4+5+9=26 Step 4: 4, 2, 3, 1 8+4+1+7=20 Step 5: 4, 3, 1, 2 8+3+5+6=22 Step 6: 4, 3, 2, 1 8+3+8+7=26 Minimum Cost : 20 Assignment Problem: Exhaustive Search C =
  • 18.
    18 The Minimum Costfrom each iterations is, Iteration 1 : Minimum Cost = 18 Iteration 2 : Minimum Cost = 13 Iteration 3 : Minimum Cost = 20 Iteration 4 : Minimum Cost = 20 Optimal Solution : Person 1 – Job 2 Person 2 – Job 1 Minimum Cost : 13 Person 3 – Job 3 Person 4 – Job 4 Assignment Problem: Exhaustive Search
  • 19.