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

Algorithm Design Techniques KnapsackProblem (1)

The document outlines various algorithm design techniques, including Brute Force, Divide and Conquer, and Decrease and Conquer. It provides examples of algorithms such as Linear Search, Selection Sort, Quick Sort, and Insertion Sort, explaining their methodologies and applications. Additionally, it discusses specific problems like the Knapsack Problem and the Fake Coin Problem, highlighting their solutions and complexities.

Uploaded by

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

Algorithm Design Techniques KnapsackProblem (1)

The document outlines various algorithm design techniques, including Brute Force, Divide and Conquer, and Decrease and Conquer. It provides examples of algorithms such as Linear Search, Selection Sort, Quick Sort, and Insertion Sort, explaining their methodologies and applications. Additionally, it discusses specific problems like the Knapsack Problem and the Fake Coin Problem, highlighting their solutions and complexities.

Uploaded by

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

Algorithm Design

Techniques
Algorithm Design Techniques
• Brute First
• Linear search, Selection Sort, Bubble Sort, Knapsack Problem
• Divide and Conquer
• Quick Sort, Merge Sort
• Decrease and conquer
Brute Force Algorithms
• Brute Force Algorithms refers to a programming style that does not
include any shortcuts to improve performance, but instead relies on
sheer computing power to try all possibilities until the solution to a
problem is found.
Examples: Linear Search
Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering
ascending order) from unsorted part and putting it at the beginning.
Bubble Sort
Bubble Sort compares each pair of array element unless the whole array is completely sorted in an
ascending order. ... If no swap has occurred, i.e. the array requires no more processing to
be sorted, it will come out of the loop.
Knapsack Problem
Knapsack Problem
Knapsack Problem
int knapSack(int W, int[] weights, int[] val, int n) {
if (n == 0 || W == 0)
return 0;

/*
If weight of the nth item is more than Knapsack
capacity W,then this item cannot be included
in the optimal solution
*/
if (weights[n] > W)
return knapSack(W, weights, val, n - 1);

/* Consider two cases, including item and excluding item.*/


else return max((val[n]+ knapSack(W - weights[n], weights, val, n - 1)),(knapSack(W, weights, val, n - 1)));
}

int[] val = {60, 100, 120};


int[] wt = {10, 20, 30};
int W = 5
knapSack(W, weights, val, n )
Divide and Conquer
• A divide-and-conquer algorithm works by recursively breaking down
a problem into two or more sub-problems of the same or related
type, until these become simple enough to be solved directly.
Merge Sort
Merge Sort is a divide and conquer algorithm. It works by continually splitting a list in half until both halves
are sorted, then the operation merge is performed to combine two lists into one sorted new list.
Quick Sort
Quick sort is one of the most famous sorting algorithms based on divide and conquers strategy which results in
an O(n log n) complexity. So, the algorithm starts by picking a single item which is called pivot and moving all
smaller items before it, while all greater elements in the later portion of the list.
Decrease-and-Conquer
1. Reduce problem instance to smaller instance of the same problem
2. Solve smaller instance
3. Extend solution of smaller instance to obtain solution to original
instance

• Can be implemented either top-down or bottom-up


• Also referred to as inductive or incremental approach
Types of Decrease and Conquer
• Decrease by a constant (usually by 1):
• Insertion Sort

• Decrease by a constant factor (usually by half)


• Binary Search
• Fake Coin Problem
• Variable-size decrease
• Searching in Binary Search Tree
• Interpolation Search
Insertion Sort
Fake coin Algorithm
Searching in Binary Search Tree
Interpolation Search
Binary Search always checks the value at middle index. But,
interpolation search may check at different locations based on the
value of element being searched.
Example: Interpolation Search

Time complexity?

You might also like