ALGORITHMS ANALYSIS

A computer implementation of a number of problems covering the area:

1. Divide and conquer

2. Dynamic programming and

3. Greedy algorithms

The student should also submit evidence that the program performs the required tasks correctly. And demonstrate a good grasp of the factors involved in the design of efficient algorithms.

You should submit a report with following included:

objective of the report

propsed soloution (flowchart)

Sample screen display (program run)

## Expert Answer

//Sample programming example of Implementing binary search using Divide and Conquer

//Explanation

A binary search or half-interval search algorithm finds the position of a specified value (the input “key”) within a sorted array.In each step, the algorithm compares the input key value with the key value of the middle element of the array. If the keys match, then a matching element has been found so its index, or position, is returned. Otherwise, if the sought key is less than the middle element’s key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the input key is greater, on the sub-array to the right. If the remaining array to be searched is reduced to zero, then the key cannot be found in the array and a special “Not found” indication is returned.

public class MyBinarySearch {

public int binarySearch(int[] inputArr, int key) {

int start = 0;

int end = inputArr.length – 1;

while (start <= end) {

int mid = (start + end) / 2;

if (key == inputArr[mid]) {

return mid;

}

if (key < inputArr[mid]) {

end = mid – 1;

} else {

start = mid + 1;

}

}

return -1;

}

public static void main(String[] args) {

MyBinarySearch mbs = new MyBinarySearch();

int[] arr = {2, 4, 6, 8, 10, 12, 14, 16};

System.out.println(“Key 14’s position: “+mbs.binarySearch(arr, 14));

int[] arr1 = {6,34,78,123,432,900};

System.out.println(“Key 432’s position: “+mbs.binarySearch(arr1, 432));

}

}

Output: |

Key 14's position: 6 Key 432's position: 4 Dynamic Programming is nothing but an simple recursion. simple example will illustrate below The classic example to explain dynamic programming is the fibonacci computation The definition of the fibonacci number of a number is clearly a recursive one:
This means that the sequence of the first 10 fibonacci numbers would go:
You might also find it defined as:
And so the sequence would be:
public static long fibonacci(int n) { if (n < 3) return 1; return fibonacci(n-2) + fibonacci(n-1); } Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. Greedy algorithms are used for optimization problems. An optimization problem can be solved using Greedy if the problem has the following property: There are standard algorithm that are greedy algorithm such as krushkal mimimum spanning tree,prim minimum spanning tree, dijkshtra shortest p[ath,huffman coding
/ The following implementation assumes that the activities // are already sorted according to their finish time import java.util.*; import java.lang.*; import java.io.*; class ActivitySelection { // Prints a maximum set of activities that can be done by a single // person, one at a time. // n –> Total number of activities // s[] –> An array that contains start time of all activities // f[] –> An array that contains finish time of all activities public static void printMaxActivities(int s[], int f[], int n) { int i, j;
System.out.print(“Following activities are selected : n”);
// The first activity always gets selected i = 0; System.out.print(i+” “);
// Consider rest of the activities for (j = 1; j < n; j++) { // If this activity has start time greater than or // equal to the finish time of previously selected // activity, then select it if (s[j] >= f[i]) { System.out.print(j+” “); i = j; } } }
// driver program to test above function public static void main(String[] args) { int s[] = {1, 3, 0, 5, 8, 5}; int f[] = {2, 4, 6, 7, 9, 9}; int n = s.length;
printMaxActivities(s, f, n); }
} Following activities are selected 0 1 3 4 |