Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch3 /Lecture #4 Brute Force and Exhaustive Search 1.

Similar presentations


Presentation on theme: "Ch3 /Lecture #4 Brute Force and Exhaustive Search 1."— Presentation transcript:

1 ch3 /Lecture #4 Brute Force and Exhaustive Search 1

2 (Algorithm strategies( approaches Brute Force and Exhaustive Search Divide –and –conquer algorithms. Binary- search Dynamic programming Greedy algorithms, 2

3 Brute force Brute force : is a straightforward approach to solving a problem, usually directly based on the problem statement and definitions of the concepts. 3

4 applications of the brute-force approach problem of sorting :(given a list of n orderable items (e.g., numbers, characters from some alphabet, character strings), rearrange them in nondecreasing order.  Tow sorting algorithms:  Selection Sort and  Bubble Sort 4

5 Selection Sort Idea: ◦ Find the smallest element in the array ◦ Exchange it with the element in the first position ◦ Find the second smallest element and exchange it with the element in the second position ◦ Continue until the array is sorted ◦ Time complexity :  (n 2 ) 5

6 Selection Sort ALGORITHM SelectionSort(A[0..n − 1]) //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 nondecreasing 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] 6

7 Bubble Sort Idea: ◦ Repeatedly pass through the array ◦ Swaps adjacent elements that are out of order ◦ Time complexity :  (n 2 ) 7

8 Bubble Sort ALGORITHM Bubble Sort(A[0..n − 1]) //Sorts a given array by bubble sort //Input: An array A[0..n − 1] of orderable elements //Output: Array A[0..n − 1] sorted in nondecreasing 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] 8

9 Exhaustive search Exhaustive search is simply a brute- force approach. 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). 9

10 Tow examples on Exhaustive search: Traveling Salesman Problem Knapsack Problem 10

11 Traveling Salesman Problem Problem statement : The problem asks 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. We 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. 11

12 Example on Traveling Salesman Problem 12

13 Knapsack Problem Here is another well-known problem in algorithmic. 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 13

14 Example on Knapsack Problem Problem Using Brute Force and Exhaustive Search to Solve the following knapsack problem with 4 items {I1,I2,I3,I4}and the items weight is {w1=7,w2= 3,w3=4,w4=5} and there values {v1=42$,v2=12$,v3=40$ v4=25$}, The knapsack weight is 10 14

15 15

16 Exercise : Using Brute Force and Exhaustive Search to Solve the following knapsack problem with 4 items The knapsack weight is 8. 16 w 1 =2 v 1 =3 w 2 =4 v 2 =5 w 3 =5 v 3 =8 w 4 =3 v 4 =4


Download ppt "Ch3 /Lecture #4 Brute Force and Exhaustive Search 1."

Similar presentations


Ads by Google