SlideShare una empresa de Scribd logo
1 de 33
Descargar para leer sin conexión
A Simple Study
On
Computer Algorithms
By
S. M. Risalat Hasan Chowdhury
Nayeem Sagor
Sumon Badaya
Divide and Conquer
Introduction:
From the beginning of creation, every creation follows its own techniques to do daily works. The techniques are
sometimes called algorithm. Algorithm is a finite set of instructions which accomplishes a particular task. An
algorithm is very effective. A person can make it with the help of a paper and pen only. Divide and Conquer is also
an algorithm to solve many problems. Divide and Conquer algorithm divide a problem into sub-problems which are
similar to the actual problem. After that recursively solve these sub-problems and combining their results. The main
problem can be solved by solving the sub-problems appropriately and combined every result. Divide and Conquer
algorithm has many appealing properties which can match them with the newly modern parallel machines. First one
is, they have much inherent parallelism. While the division step is completed, the sub problems are normally
independent and can be solved in parallel. The recursive structure of the algorithm naturally leads to recursively
generate concurrency. Both the divide and the combine steps can be executed in parallel with divide and combine
steps of other sub-problems. Second one is, divide and conquer programs also have good cache performance in the
machine. While a sub-problem goes in the cache, the program can reuse the cached data until the sub-problem will
have been completely solved or a complete result come out. Divide and conquer programs generally work well with
a well range of cache sizes and at all levels of the memory hierarchy [1]
.
Divide
Solve of
Conquer sub-problem
Combine
Figure: Divide and conquer a problem
In the figure, we divide a problem into two separate sub-problems. The machine solves the sub-problem in a parallel
way and gives individual results from it. After this combine the two results and have a solution of that problem.
We can solve a large problem with the help of this algorithm. And every sub-problem of the large problem can be
solved in parallel way in the machine and we can get a combined result of the large problem from every sub-
problem result.
There are many problems which can be solved with the help of Divide and Conquer algorithm. Two of them are
discussed here-
1. Closest pair of point:
1.1. Introduction:
Closest pair of point problem is a well known computational geometry problem. The problem is to find the smallest
distance between two dots. Given n points and their distances, we have to find the smallest distance among them.
Here is an example
Sub-problem
Problem
Sub-problem
Solution of
Sub-problem
Solution of
Sub-problem
Solution of
problem
Figure 1.1: n points and their distances.
Figure shows some points and their distances. The distances are 5, 9, 10, 11, 12, 15…. for the n points. Before
calculating the smallest distance for closest pair of point we have to divide the points into two parts to make easy in
calculation.
Figure 1.2: Divide the points into two parts to make easy in calculation.
Figure shows us two separate parts of the problem. Here we use three steps to solve the problem
1. Divide: Draw a vertical line with n/2 points on each side.
2. Conquer: Find closest pair on each side, the work will do recursively.
3. Combine: Find closest pair points from the two sub-problem solutions [2]
.
Usually, the works is not very easy to find the smallest distance between two points. We have to search the whole
distances of every two pair of points. On the left side there are 6 points, 4 distances and their values are 5,11,12,15.
On the right side there are 7 points, 3 distances and their values are 9, 10, and 15. On left side sub-problem solution
is 5 and on the right side sub-problem solution is 9. Combining 5 and 9, we get the final result and that is 5.
Figure 1.3: Closets pair of point are shown in red dots.
Figure shows the smallest distance between two points that is 5 and remarked with red dots. We divide the points in
to two parts to make the solution to be easy. If there are more points, we can divide the whole points in a binary way
(4, 8, 16 etc) for searching the minimum or smallest distance between two points.
1.2. Algorithm:
Procedure findclosest (P, int n)
// n is the number of elements in set P
Set: PLeft, PRight, Pleftmin, Prightmin, Pclosest
if (n £ 3)
return SHORTEST (P)
else
PLeft ¬ { p(1), p(2), …. , p(n/2) }
PRight ¬ { p(n/2 + 1), ……. , p(n) }
Pleftmin ¬ findclosest (PLeft, n/2)
Prightmin ¬ findclosest (PRight, n/2)
Pclosest¬ MERGEPLANES (Pleftmin, Prightmin)
return Pclosest
endif
endprocedure
Procedure MERGEPLANES (P1, P2)
D ¬ MINIMUM (P1, P2)
for (every i in P1)
for every j in P2 such that
xi < xj + D AND (yi +d > yj > yi – d)
if DISTANCE (i, j) < D
return (i, j)
endfor
endfor
endprocedure [3]
1.3. Codes in Java:
import java.util.*;
public class ClosestPair{
private double[][] points;
Point p1, p2;
public static void main(String[] args) {
System.out.println(" ********* Closest pair of points ********* ");
double[][] points = new double[100][10];
for (int i = 0; i < points.length; i++)
{
points[i][0] = Math.random() * 10;
points[i][1] = Math.random() * 10;
}
ClosestPair closestPair = new ClosestPair(points);
System.out.println("nThe shortest distance is " +
closestPair.getMinimumDistance());
System.out.print("nThe Closest pair is (" + closestPair.p1.x + ", " +
closestPair.p1.y + ") and ");
System.out.println("(" + closestPair.p2.x + ", " +
closestPair.p2.y + ")");
}
ClosestPair() {
}
public ClosestPair(double[][] points) {
setPoints(points);
}
public void setPoints(double[][] points) {
this.points = points;
}
public double getMinimumDistance() {
Point[] pointsOrderedOnX = new Point[points.length];
for (int i = 0; i < pointsOrderedOnX.length; i++)
pointsOrderedOnX[i] = new Point(points[i][0], points[i][1]);
Arrays.sort(pointsOrderedOnX);
if (checkIdentical(pointsOrderedOnX))
return 0;
Point[] pointsOrderedOnY = pointsOrderedOnX.clone();
Arrays.sort(pointsOrderedOnY, new CompareY());
return distance(pointsOrderedOnX, 0,
pointsOrderedOnX.length - 1, pointsOrderedOnY);
}
public boolean checkIdentical(Point[] pointsOrderedOnX) {
for (int i = 0; i < pointsOrderedOnX.length - 1; i++) {
if (pointsOrderedOnX[i].compareTo(pointsOrderedOnX[i + 1]) == 0) {
p1 = pointsOrderedOnX[i];
p2 = pointsOrderedOnX[i + 1];
return true;
}
}
return false;
}
public double distance(
Point[] pointsOrderedOnX, int low, int high,
Point[] pointsOrderedOnY) {
if (low >= high)
return Double.MAX_VALUE;
else if (low + 1 == high) {
p1 = pointsOrderedOnX[low];
p2 = pointsOrderedOnX[high];
return distance(pointsOrderedOnX[low], pointsOrderedOnX[high]);
}
int mid = (low + high) / 2;
Point[] pointsOrderedOnYL = new Point[mid - low + 1];
Point[] pointsOrderedOnYR = new Point[high - mid];
int j1 = 0; int j2 = 0;
for (int i = 0; i < pointsOrderedOnY.length; i++) {
if (pointsOrderedOnY[i].compareTo(pointsOrderedOnX[mid]) <= 0)
pointsOrderedOnYL[j1++] = pointsOrderedOnY[i];
else
pointsOrderedOnYR[j2++] = pointsOrderedOnY[i];
}
double d1 = distance(
pointsOrderedOnX, low, mid, pointsOrderedOnYL);
double d2 = distance(
pointsOrderedOnX, mid + 1, high, pointsOrderedOnYR);
double d = Math.min(d1, d2);
int count = 0;
for (int i = 0; i < pointsOrderedOnYL.length; i++)
if (pointsOrderedOnYL[i].x >= pointsOrderedOnX[mid].x - d)
count++;
Point[] stripL = new Point[count];
count = 0;
for (int i = 0; i < pointsOrderedOnYL.length; i++)
if (pointsOrderedOnYL[i].x >= pointsOrderedOnX[mid].x - d)
stripL[count++] = pointsOrderedOnYL[i];
count = 0;
for (int i = 0; i < pointsOrderedOnYR.length; i++)
if (pointsOrderedOnYR[i].x <= pointsOrderedOnX[mid].x + d)
count++;
Point[] stripR = new Point[count];
count = 0;
for (int i = 0; i < pointsOrderedOnYR.length; i++)
if (pointsOrderedOnYR[i].x <= pointsOrderedOnX[mid].x + d)
stripR[count++] = pointsOrderedOnYR[i];
double d3 = d;
int j = 0;
for (int i = 0; i < stripL.length; i++) {
while (j < stripR.length && stripL[i].y > stripR[j].y + d)
j++;
int k = j;
while (k < stripR.length && stripR[k].y <= stripL[i].y + d) {
if (d3 > distance(stripL[i], stripR[k])) {
d3 = distance(stripL[i], stripR[k]);
p1 = stripL[i];
p2 = stripR[k];
}
k++;
}
}
return Math.min(d, d3);
}
public static double distance(Point p1, Point p2) {
return distance(p1.x, p1.y, p2.x, p2.y);
}
public static double distance(
double x1, double y1, double x2, double y2) {
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
static class Point implements Comparable<Point> {
double x;
double y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
public int compareTo(Point p2) {
if (this.x < p2.x)
return -1;
else if (this.x == p2.x) {
if (this.y < p2.y)
return -1;
else if (this.y == p2.y)
return 0;
else
return 1;
}
else
return 1;
}
}
static class CompareY implements java.util.Comparator<Point> {
public int compare(Point p1, Point p2) {
if (p1.y < p2.y)
return -1;
else if (p1.y == p2.y) {
if (p1.x < p2.x)
return -1;
else if (p1.x == p2.x)
return 0;
else
return 1;
}
else
return 1;
}
}
} [4]
Output:
1.4. Application:
Closest pair of problem arises in a number of applications. For example: in air-traffic control, in mobile phone
connecting with network tower (BTS) problem etc. Here we discuss about the mobile phone connecting problem.
The Cellular phone or mobile phone uses Global System for Mobile communication (GSM) technique to transfer all
kind of data. The word Cell can be defined as an area of radio coverage from one Base Transceiver Station (BTS)
antenna which is the smallest building block in a mobile network. The GSM is divided into two parts and they are
switching system (SS) and Base Station System (BSS).
Mobile phone
Figure 1.4: Cell phone connection with BTS
Abbreviations of the above terms are Authentication Center (AUC), Base Station Controller (BSC), Base
Transceiver Station (BTS), Equipment Identity Register (EIR), Home Location Register (HLR), Mobile Station
(MS), Mobile services Switching Center (MSC), Visitor Location Register (VLR). The figure shows, a mobile or
cell phone is connected with a BTS. If a mobile user wants to call another user within a same city but in a different
area, the BTS transfer the data to MSC trough BSC. MSC is for the same city network providing tool. Another
user’s BSC is also connected with this MSC. So the two users of this network use the same MSC to establish a
phone call or other services. There are many BTSs in an area but the question is which BTS is going to connect with
the Mobile Station (MS). Basically cells are hexagonal. They cover a hexagon network. While a user goes to another
cell it faces some problems. The process of changing a cell during a phone call is called handover. Handover process
maintains the following steps to change a cell. They are-
1. A mobile station continuously measures the signal strength of connected cell and neighboring cells.
2. It sends the message report of measured power level to the BSC.
3. Based on these report BSC decides the importance of handover and channel allocation in new cell.
4. If BSC observes handover is essential then it assigns a new channel in new cell and informs MS to send the
message to this new channel.
5. MS leave the old cell/BTS and establish a new connection with the new cell/BTS.
With this process a cell phone can change its BTS. Now the question is which BTS is going to connect with MS.
The solution is using the technique of “Closest pair of point”. MS will follow the following criteria. One is the
signal strength and second is minimum distance between the MS and BTS. Which BTS has the following two
criteria will connect with the MS and establishes a network.
SS AUC
VLR HLR EIR
GMSC MSCOther Networks
BSS
BSC
BTS
Figure 1.5: MS connects with nearest BTS
Figure shows the connection among MS and BTSs. MS trace the distances among the BTSs but it only connects
with the nearest one. There are five BTS near by the MS and the distances are 85 feet, 95 feet, 110 feet, 165 feet and
255 feet. First the MS calculate the distance near to it and it measure all the distances. It checks 85, 95, 110, 165 and
255 in a parallel way and get the solution for which BTS is going to be connected. As we see from the figure, the 85
feet is the solution and the MS is going to connect with this BTS. Generally a cell phone connects with only a BTS.
When a cell phone changes its cell, then the handover process is needed. Cell phone’s data voice goes to the BSC
through the BTS as so MSC. If there is another cell phone user in the same city but different area, it will also use the
same MSC as the previous cell phone user use.
Figure 1.6: A conversation between two cell phone users trough a same MSC.
Figure shows us the view of a conversation between cell phone users in same city. This is what actually happened
when a phone call is being started every time.
1.5. Advantages of closest pair of point:
1. One can use the best path to travel or use.
2. After finding the smallest distance it is easy to travel to the nearest one.
3. The best path or way can save time and money too.
1.6. Disadvantages of closest pair of point:
1. It can waste some time to search the right path or nearest destination or smallest distance.
2. The measuring of smallest distance between source to destination is not easy because we have to measure all the
points or destinations [5]
.
2. Merge sort:
2.1. Introduction:
Merge sort is a technique to sort a series on numbers or a string etc. Merge sort follows Divide and conquer
algorithm to do its task. It was invented by John von Neumann in 1945. Here we are using divide-and-conquer to
sort; we need to decide what our sub-problems will be going to look like. The full problem is to sort the entire array.
The sub-problem is to sort a sub-array.
Figure 4.1: Marge sort.
The figure shows us a normal array which contain {38, 27, 43, 3, 9, 82, 10}. First we divide it into two sub-array,
after that we again divide it. Now the sub-arrays are {38, 27}, {43, 3}, {9, 82} and {10}. Every sub-array will
calculate first and then merge it with its nearest sub-array. Finally we calculate the entire two sub-arrays which are
{3, 27, 38, 43} and {9, 10, 82}. The final merged array is {3, 9, 10, 27, 38, 43, 82}.
2.2. Application:

Merge Sorts are helpful for sorting linked lists in O(n log n) time. In case of linked lists, the case is
different due to difference in memory allocation of arrays and linked lists.

It is used in inversion count problem.
 It is used in external sorting [6]
.
2.3. Algorithm:
Algorithm MergeSort(low,high)
//a[low:high] is a global array to be sorted.
//Small(P) is true if there is only one element.
//to sort. In this case list the list is already sorted.
{
if(low < high) then // If there are more than one element.
{
// Divide P into subproblems.
// Find where to split the set.
mid :=[(low+high)/2;
// Solve the subproblems.
MergeSort(low,mid);
MergeSort(mid+ I,high);
// Combinethe solutions.
Merge(lowm, id,high);
}
}[7]
.
2.4. Codes in Java:
import java.util.Scanner;
public class MergeSort
{
public static void sort(int[] a, int low, int high)
{
int N = high - low;
if (N <= 1)
return;
int mid = low + N/2;
sort(a, low, mid);
sort(a, mid, high);
int[] temp = new int[N];
int i = low, j = mid;
for (int k = 0; k < N; k++)
{
if (i == mid)
temp[k] = a[j++];
else if (j == high)
temp[k] = a[i++];
else if (a[j]<a[i])
temp[k] = a[j++];
else
temp[k] = a[i++];
}
for (int k = 0; k < N; k++)
a[low + k] = temp[k];
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
System.out.println("Merge Sort Testn");
int n, i;
System.out.println("Enter number of integer elements");
n = scan.nextInt();
int arr[] = new int[ n ];
System.out.println("nEnter "+ n +" integer elements");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
sort(arr, 0, n);
System.out.println("nElements after sorting ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
Output:
2.5. Advantages:
 It can be applied in any size of files or arrays.
 Reading of the input during the run-creation step is sequential.
 Since I/O is largely sequential, merge sort can be used.
2.6. Disadvantage:
 The disadvantage of merge sort is that it uses a lot of memory.
 It uses extra space proportional to n. This may slow it down when trying to sort very large data [8]
.
Conclusion of Divide and conquer:
The Divide and conquer algorithm has many beneficial qualities that makes it a very important algorithm. It follows
only three simple steps to make complex problems easier, and those steps are to:
(1) Divide the complex problem into two or more sub-problems,
(2) Recursively solve the sub problems,
(3) Conquer by combining the solutions from the sub problems to get a solution of the original problem.
It makes us capable to solve difficult problems, helps to find other efficient algorithms and is sometimes the best
choice than other algorithms. One of the only downfalls to this algorithm is the fact that sometimes recursion is
slow, and this fact can almost negate all the other advantages of this process. Merge Sort and Quick Sort are both
sorting algorithms that are based on this process of the divide and conquer algorithm. This basic idea of dividing and
conquering can be seen in everyday life. We can solve our daily problems by it. So, it is efficient and effective
algorithm than others.
Dynamic Programming
Introduction:
Dynamic programming is a programming method or technique that uses to solve the recursive problems in more
efficient way or manner. Dynamic programming use Recursive technique (Recursion) .
Dynamic programming recursively solve the sub-problems and store the solutions of the sub-problems this is called
Memorization (i.e. avoid solve the sub-problem again) .
Dynamic programming technique and memorization technique works together. So, we can say, dynamic
programming has two parts:-
 Recursion: recursively solve the sub-problems .
 Memorization: store the solution of sub-problems i.e. avoid solving the sub-problems that already solved.
Properties of Dynamic programming
Dynamic programming has two major properties:-
 Overlapping Sub-problems: Overlapping sub-problem means every sub-problem to be solved again and
again. In dynamic programming, using recursion, we solve those sub-problems only one time and store it
for future use.
 Optimal Substructure: If a solution of the sub-problems can be use to solve a problem that is called
Optimal Substructure Property.
Note: If any problem has both properties then we can be apply and solve this problem using dynamic programming
method.
Approaches of Dynamic Programming
Two ways can be achieved Dynamic programming-
 Bottom-Up Approach: The solution of first sub-problem can be use to solve the second sub-problem and
the solution of second sub-problem can be use to solve the third sub-problem, following these sequence or
criteria is known as Button-Up approach.
 Top-Down Approach: Break the problem into sub-problems and solves them and store the result for future
use. This is the direct fall-out of the recursive formulation of any problem [1].
Complexity of Dynamic Programming
Time Complexity:
Dynamic programming doesn't have a time complexity, because it is not a specific algorithm. It's a general approach
to constructing algorithms to solve problems that have certain properties (namely: optimal
substructure and overlapping sub-problems) [7].
Space Complexity: Dynamic programming uses recursive and memorization so that space complexity will be
occurred.
Example 1
Fibonacci Series or Sequence Using Dynamic Programming.
Introduction: The current number is the sum of previous two numbers i.e. 0 1 1 2 3 5 8 13 . . .[8].
Program Flowchart
Figure 1: Flowchart of Fibonacci series or sequence Using Dynamic Programming.
Pseudo code [2].
Main()
{
Declaration & Initialization:
Int n;
Int Fibo[n];
Boolean command = true;
While(command = true ?)
{
Input n;
Fibonacci ( n );
Print the result;
Input command = true / false;
}
}
Fibonacci(n)
{
if(n is equal to 0)
fibo[n] = return 0;
if(n is equal to 1)
fibo[n] = return 1;
if(fibo[n-2] is unsolved)
fibo[n-2] = Fibonacci(n-2);
if(fibo[n-1] is unsolved)
fibo[n-1] = Fibonacci(n-1);
fibo[n] = fibo[n-2] + fibo[n-1];
return fibo[n];
}
Code:
import java.util.Scanner;
public class FibonacciSeriesDemo {
static Scanner scanner = new Scanner(System.in);
static boolean command = true;
public static void main(String[] args) {
int n = 0;
Fibonacci fibonacci = new Fibonacci(100);
System.out.println("***Fibonacci Series Using Dynamic Programming****n");
while (command) {
System.out.print("nEnter the n'th position: ");
n = scanner.nextInt();
System.out.println("=>> Fibonacci[" + n + "] = " + fibonacci.fiboSeries(n));
System.out.print("nYou want to exit the programm(y/n): ");
getCommand();
}
System.out.println("nn******Thank You*******");
}
private static void getCommand() {
scanner.nextLine();
if (scanner.nextLine().equalsIgnoreCase("y")) {
command = false;
}
}
}
public class Fibonacci {
int[] f = { 0 };
public Fibonacci(int n) {
f = new int[n];
f[0] = 0;
f[1] = 1;
}
public int fiboSeries(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
if (f[n - 2] == 0) {
f[n - 2] = fiboSeries(n - 2);
}
if (f[n - 1] == 0) {
f[n - 1] = fiboSeries(n - 1);
}
f[n] = f[n - 2] + f[n - 1];
return f[n];
}
}
Output:
Application:
In computer science:
1. Fibonacci heaps which have better amortized running time than binomial heaps.
2. Fibonacci search which shares O(log N) running time with binary search on an ordered array.
Example 2
Maximum Sum of Contiguous Sub-Array Using Dynamic Programming [3].
Introduction: A sub-array has one number of some continuous numbers. Given an integer array with positive
numbers and negative numbers, get the maximum sum of all sub-arrays. Time complexity should be O (n) [9].
For example, in the array {1, -2, 3, 10, -4, 7, 2, -5}, its sub-array {3, 10, -4, 7, 2} has the maximum sum 18 [9].
Program Flowchart:
Figure 2: Flowchart of maximum Sum of Contiguous Sub-Array Using Dynamic Programming.
Pseudo code:
Main()
{
Declaration & Initialization:
Int array[n];
Boolean command = true;
While(command = true ?)
{
For (0 : n)
{
Input and store array elements one by one;
}
MSS ( array);
Print the result;
Input command = true / false;
}
}
MSS(array[n])
{
maxSum=subArraySum[i]= array[i]; // where i=0;
for(i=1 : n)
{
subArraySum[i] = Max( array[i], subArraySum[i-1]+array[i] );
maxSum = Max(maxSum,subArraysum[i]);
}
Return maxSum;
}
Code:
import java.util.Scanner;
public class MSSDemo { // MSSDemo.java
static boolean statement = true;
static int array[];
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("n=======Welcome to Maximum Sum Of Contiguous Sub-Array Programme========n");
while (statement) {
arrayInitialization();
System.out.println("nnMaximum Sum = "+new MaximumSumOfContiguousSuArray().MSS(array));
getStatement();
}
System.out.println("n======Thank You=======n");
}
static boolean getStatement() {
System.out.print("nYou want to exit the Program(Y/N) . . . ? ");
if ("Y".equalsIgnoreCase(new Scanner(System.in).nextLine())) {
return statement = false;
} else {
return statement = true;
}
}
public static void arrayInitialization(){
System.out.print("Enter Array Limit: ");
array=new int[sc.nextInt()];
System.out.print("nEnter Array Element:- n");
for(int i=0; i<array.length; i++){
System.out.print("nArray["+(i+1)+"] = ");
array[i]=sc.nextInt();
}
}
}
System.out.println("n=======Welcome to Maximum Sum Of Contiguous Sub-Array Programme========n");
public class MaximumSumOfContiguousSuArray {
public int MSS(int[] array) {
int subArraySum[] = new int[array.length];
int maxSum = subArraySum[0] = array[0];
for (int i = 1; i < array.length; i++) {
subArraySum[i] = Math.max(array[i], subArraySum[i - 1] + array[i]);
maxSum = Math.max(subArraySum[i], maxSum);
}
return maxSum;
}
}
Output:
Example 3
Maximum Sub-Square Matrix of a Matrix using Dynamic Programming [4].
Introduction: Given a matrix of 0’s and 1’s (binary matrix). Find out Maximum size square sub-matrix with all 1’s
[4].
Example:
Figure [3]: Maximum size square sub-matrix with all 1s Example [4].
Pseudo code [4].
Main()
{
Declare matrix[n][m];
For( i=0 : n)
{
For(j=0 : m)
{
Matrix[i][j] = input array element;
}
}
Call MSSM ( matrix );
Print the result;
}
MSSM(matrix)// Maximum Sub-Square Function
{
declare temp[matrixColumn][matrixRow];
for(i=0 : matrixColumn)
{
temp[0][i]=matrix[0][i];
}
for(i=0 : matrixRow)
{
temp[i][0]=matrix[i][0];
}
Max=1;
for(i=0 : matrixRow)
{
for(i=0 : matrixColumn)
{
If(matrix[i][j] equal 0)
{
Continue;
}
temp[i][j] = getMin(temp[i-1][j],temp[i-1][j-1],temp[i][j-1]) + 1;
if(temp[i][j] grater then max)
{
max=temp[i][j];
}
}
}
Return max;
}
getMin(a,b,c)// Finding the Minimum Value
{
Declare min;
If(a < b){
Min =a;
}
Else {
Min =b;
}
If(c < min){
Min =c;
}
Return min;
}
Program Flowchart:
Figure 3: Flowchart of Maximum Sub-Square Matrix of matrix.
Code:
import java.util.Scanner;
public class MaximumSubSquareMatrix {
public int MSSM(int matrix[][]) {
int temp[][] = new int[matrix.length][matrix[0].length];
for (int j = 0; j < matrix[0].length; j++) {
temp[0][j] = matrix[0][j];
}
for (int i = 0; i < matrix.length; i++) {
temp[i][0] = matrix[i][0];
}
int max = 1;
for (int i = 1; i < matrix.length; i++) {
for (int j = 1; j < matrix[i].length; j++) {
if (matrix[i][j] == 0)
continue;
temp[i][j] = getMin(temp[i - 1][j], temp[i - 1][j - 1], temp[i][j - 1]) + 1;
if (temp[i][j] > max)
max = temp[i][j];
}
}
return max;
}
private int getMin(int a, int b, int c) {
return Math.min(Math.min(a, b), c);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("===Maximum Sub-Square Matrix Using Dynamic Programming====");
System.out.print("nEnter number of column: ");
int m = sc.nextInt();
System.out.print("Enter number of Row: ");
int n = sc.nextInt();
int matrix[][] = new int[n][m];
System.out.println("nEnter Array Element's :");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = sc.nextInt();
}
}
MaximumSubSquareMatrix mm = new MaximumSubSquareMatrix();
System.out.println("nnMaximum Sub-Square Matrix is: " + mm.MSSM(matrix));
}
}
Output:
Application: Finding the available or free space (square) in a specific area.
The Greedy Methods
Huffman coding Algorithm
1.1 Introduction: Huffman coding is one of the most popular technique for removing coding redundancy. It has
been used in various compression applications [1].
The lossless method of compression and decompression is proposed using Huffman coding which is used on test
string as well as on different image file formats for performance analysis. The result reveals that the original image
used for coding is almost close to the decoded output image [1].
Huffman codes are prefix-free codes and decodable, that’s mean no code is the prefix of the some other code and
one can know when the last bit of the code is read and immediately recognize the code[2].
Huffman coding is base on the frequency of occurrence data item. It is used to code values according to their
probability of occurrence. Short words are assigned to highly probable values and long code words to less probable
values.
1.2 Methods:
1. Make a list of the symbols, sorted by frequencies from largest to smallest.
2. Combine the two smallest frequency values.
3. Repeat Steps 1 and 2 until the all of the frequencies have been added up.
The final number at the head of the tree should be the sum of all the frequencies. The path of each symbol’s
frequency is traced from top to the each branch of the tree.
1.3 Constructing tree:
The objective is to construct a tree such that the path from a leaf that contains the symbols s to the root has a length
as close as possible to − log( p(s)). To each leaf is associated a weight, which is basically the number of times the
symbol s has been seen in the data. Huffman algorithm’s strategy is to build a tree with weights that are as balanced
as possible.
The unusual thing is in Huffman’s algorithm is that instead of building this tree from the root down to the leaf, it is
built from the leaves up to the root. The first step is to build a list which contains only leaves. Each leaf has an
associated symbol s and a weight, p(s). We pick, out of that list, the two element with the smallest weights. We
create a new node such that its children are the two picks, and that its weight is the sum of its children’s weight. We
put back the new node in the list. We repeat this procedure until only one node is left, which will become the root of
the tree. Codes are then assigned easily. Each time we go left in the tree, we assign a ‘0’, and when we go right, we
assign a ‘1’. The code for a leaf is the path one must follow to reach it from the root.
1.4 Example: We use this sentence for the below figure "this is an example of a huffman tree".
Fig 1: Huffman tree
[Source: https://www.google.com.bd/search?q=huffman+coding+algorithm]
Figure description: In the above figure, we construct a huffman tree using the sentence "this is an example of a
huffman tree ".At first we calculate the frequency of the characters. Then we sorted them as decreasing order. After
that we combine two minimum values. Using this technique we find the root. Here is the root is 36. Each time we go
left in the tree, we assign a ‘0’, and when we go right, we assign a ‘1’.
1.5 Algorithm:
1. n=|C|
2. Q=C
3. For i=1 to n-1
4. Allocate a new node z
5. z.left=x=EXTRACT-MIN(Q)
6. z.right=y= EXTRACT-MIN(Q)
7. z.freq=x.freq+y.freq
8. INSERT(Q,z)
9. Return EXTRACT-MIN(Q) //return the root of the tree
1.6 Code:
import java.util.*;
abstract class HuffmanTree implements Comparable<HuffmanTree> {
public final int frequency; // the frequency of this tree
public HuffmanTree(int freq) { frequency = freq; }
// compares on the frequency
public int compareTo(HuffmanTree tree) {
return frequency - tree.frequency;
}
}
class HuffmanLeaf extends HuffmanTree {
public final char value; // the character this leaf represents
public HuffmanLeaf(int freq, char val) {
super(freq);
value = val;
}
}
class HuffmanNode extends HuffmanTree {
public final HuffmanTree left, right; // subtrees
public HuffmanNode(HuffmanTree l, HuffmanTree r) {
super(l.frequency + r.frequency);
left = l;
right = r;
}
}
public class HuffmanCode2 {
// input is an array of frequencies, indexed by character code
public static HuffmanTree buildTree(int[] charFreqs) {
PriorityQueue<HuffmanTree> trees = new PriorityQueue<HuffmanTree>();
// initially, we have a forest of leaves
// one for each non-empty character
for (int i = 0; i < charFreqs.length; i++)
if (charFreqs[i] > 0)
trees.offer(new HuffmanLeaf(charFreqs[i], (char)i));
assert trees.size() > 0;
// loop until there is only one tree left
while (trees.size() > 1) {
// two trees with least frequency
HuffmanTree a = trees.poll();
HuffmanTree b = trees.poll();
// put into new node and re-insert into queue
trees.offer(new HuffmanNode(a, b));
}
return trees.poll();
}
public static void printCodes(HuffmanTree tree, StringBuffer prefix) {
assert tree != null;
if (tree instanceof HuffmanLeaf) {
HuffmanLeaf leaf = (HuffmanLeaf)tree;
// print out character, frequency, and code for this leaf (which is just the prefix)
System.out.println(leaf.value + "t" + leaf.frequency + "t" + prefix);
} else if (tree instanceof HuffmanNode) {
HuffmanNode node = (HuffmanNode)tree;
// traverse left
prefix.append('0');
printCodes(node.left, prefix);
prefix.deleteCharAt(prefix.length()-1);
// traverse right
prefix.append('1');
printCodes(node.right, prefix);
prefix.deleteCharAt(prefix.length()-1);
}
}
public static void main(String[] args) {
String test = "this is an example for huffman encoding";
// we will assume that all our characters will have
// code less than 256, for simplicity
int[] charFreqs = new int[256];
System.out.println("################HUFFMAN CODING ALGORITHM###########");
// read each character and record the frequencies
for (char c : test.toCharArray())
charFreqs[c]++;
// build tree
HuffmanTree tree = buildTree(charFreqs);
// print out results
System.out.println("SYMBOLtWEIGHTtHUFFMAN CODE");
printCodes(tree, new StringBuffer());
}
}
1.7 Application:
1. It is used for image detect
2. data compression
3. pattern matching
1.8 Strength:
1. It needs less time to transmit data.
2. It needs less memory space after encoding the data.
3. It is very efficient technique.
4. Huffman is a good coding technique for compressing the data and general types of images.
1.9 Limitation:
1. It is used static coding.
2. The algorithm must see all the data before performing the compression. It is quite impractical to do so. One
might not have the sufficient space to store all the data before starting compression.
3. It needs very large memory.
4. In the ‘worst case’, the code book cannot consist more than 256 possible bytes. But in the real world, one might
need a code book for a set of several thousands or millions symbols.
5. To understand the compressed data by the decoder, the decoder must know the code book. If the decoders don’t
know the code book then one will transmit the code book by itself. By doing this, the data might be losing
completely[2].
6. It needs very large code book for large symbols.
1.10 Conclusion:
From the above discussion we have seen that the original image used for coding is almost close to the decoded
output image. The result shows that the higher Code redundancy helps to achieve more compression. The above
presented Huffman coding and decoding is used to reduce test data volume, test data compression and
decompression time. Hence we conclude that Huffman coding is efficient technique for image compression and
decompression to some extent.
The problems of adaptivity, memory management and code book transmission problems, we propose a new
algorithm for adaptive Huffman coding. The algorithm allows for very good adaptivity, efficient memory usage and
efficient decoding algorithms.
Dijkstra's Algorithm
2.1 Introduction: It is an algorithm for finding the shortest path between nodes in a graph. The algorithm is divided
into two categories. One is single source to single destination, and another is single source to multiple destinations in
the graph.For example, if the nodes are representing cities and edge represent distances between pairs of cities,
Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a result, the
shortest path algorithm is widely used in network routing protocols. We are starting from the source node or initial
node. We have to find the shortest path from source to destination node. We use some steps to find the shortest path.
Which are given bellow:
1. Set zero to initial node and infinity for all other nodes.
2. Set the initial node as current node. Mark all the nodes as unvisited. Create a set of all the unvisited nodes
as a set of unvisited set.
3. For the current node calculates the distances of unvisited neighbors. Compare the newly calculated distance
to the current assigned value and assign the smaller one.
4. When we are visited all of the neighbors of the current node, mark the current node as visited and remove it
from the unvisited set. A visited node will never check again.
5. If the destination node has been marked visited. Then the algorithm has finished.
2.2 Example:
Fig 3: Dijkstra's algorithm
From the figure ,u is the source and z is the destination, we have to reach the destination z from u using
shortest path, at first we go to v and cost is 2,we can go to the node x is cost is 1,and we can go to node w
,whose cost is 5.we can use node v to go to node w and node x whose cost is 5 and 4 respectively. But we
can go node w and x directly whose cost is 5 and 1 respectively, which is optimal from previous step. So
node v is eliminated. We can go to node w via node x , whose cost is 4 ,it is optimal than direct edge u to
w. From x we can go to node y, whose cost is 2 from source. We can go to node w via y whose cost is 3
from source which is better than previous cost from source via x. from node w we go to node z whose cost
is 8 and from via node y to node z cost is 3. So it is the optimal solution, now the shortest path is u to x and
x to y and y to z. The cost is 3.
2.3 Algorithm:
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=∅
3. Q=G.V
4. While Q≠ ∅
5. U=EXTRACT-MIN(Q)
6. S=S U {u}
7. For each vertex v ∈ G.Adj[u]
8. RELAX(u,v,w)
2.4 Code:
import java.util.*;
public class Dijkstra
{
public static void main(String args[])
{
System.out.println("************** Dijkstra's Algorithm **************n");
Scanner input=new Scanner(System.in);//Scanner
int vertex,noOfEdges=1,min=999,u=0,v=0,total=0,i,j;//initialization
System.out.print("Enter the number of vertex: ");
vertex=input.nextInt();//take input no. of vertex
int matrix[][]=new int[vertex][vertex],visited[]=new int[vertex];//initialization
//input matrix
System.out.println("Enter the values of adjacent matrix :");
for(i=0;i<vertex;i++)
{
visited[i]=0;//make all the vettexes are not visited
for(j=0;j<vertex;j++)
{
matrix[i][j]=input.nextInt();//take value of adjacent matrix
if(matrix[i][j]==0)//true if vertexes are not related
{
matrix[i][j]=999;//which vertexes are not related make the
distance 999 as like infinite between them
}
}
}
//end of taking input
//output stream
/*0 28 0 0 0 10 0
28 0 16 0 0 0 14
0 16 0 12 0 0 0
0 0 12 0 22 0 18
0 0 0 22 0 25 24
10 0 0 0 25 0 0
0 14 0 18 24 0 0*/
System.out.println();
visited[0]=1;
while(noOfEdges < vertex)
{
min=999;
for(i=0;i<vertex;i++)
{
if(visited[i]==1)
{
for(j=0;j<vertex;j++)
{
if(visited[j]!=1)
{
if(min>matrix[i][j])
{
min=matrix[i][j];
u=i;
v=j;
}
}
}
}
}
noOfEdges++;
visited[v]=1;
total+=min;
System.out.println("edge "+(u+1)+"--->"+(v+1)+" :weihgt = "+min);
}
System.out.println("nnThe cost of the minimum shortest path is = "+total);
}
}
2.5 Application:
1. Robot path planning.
2. Logistics Distribution Lines.
3. Link-state routing protocols.
4. OSPF(Open Shortest Path First)
5. IS-IS (Intermediate System to Intermediate System)
2.6 Advantage:
1. It is faster.
2. It doesn’t need to investigate all the edges.
3. It is not restricted to acyclic graph.
4. It is very efficient.
2.7 Disadvantage:
1. Expensive to compute.
2. The major disadvantage of the algorithm is the fact that it does a blind search there by consuming a lot of
time waste of necessary resources.
3. It cannot handle negative edges. This leads to acyclic graphs and most often cannot obtain the right shortest
path.
2.8 Conclusion:
From the above discussion we have seen that it is very simple algorithm to find the shortest path from
source to destination. It is very efficient and effective algorithm. We use prim’s algorithm to solve the
problem.
Contribution:
 Divide and conquer algorithm – S. M. Risalat Hasan Chowdhury.
 Dynamic Programming – Sumon Badaya.
 Greedy Method – Nayeem Sagor.
References for Divide and Conquer:
1. Radu Rugina and Martin Rinard, Laboratory for Computer Science Massachusetts Institute of Technology
Cambridge, MA 02139.
2. Paul Beame and others.
3. Ashish Sharma,Rengakrishnan Subramanian,November 28, 2001.
4. http://www.ecs.csun.edu/~cputnam/Comp%20110/Liang%208th%20ed%20PP_Slides/html/ClosestPair.ht
ml
5. GSM System Survey-1 by Ericsson.
6. http://quiz.geeksforgeeks.org/merge-sort/
7. Fundamentals of Computer Algorithms by Sartaj Sahni, Ellis Horowitz and Sanguthevar Rajasekaran.
8. http://www.answers.com/Q/What_are_the_advantages_and_disadvantages_of_merge_sort#slide=5
References for Dynamic Programming:
1. http://algorithms.tutorialhorizon.com/introduction-to-dynamic-programming.
2. http://algorithms.tutorialhorizon.com/introduction-to-dynamic-programming-fibonacci-series/
3. http://www.geeksforgeeks.org/largest-sum-contiguous-subarray/
4. http://algorithms.tutorialhorizon.com/dynamic-programming-maximum-size-square-sub-matrix-with-all-1s/
5. https://github.com/mission-peace/interview/wiki/Dynamic-Programming/
6. https://github.com/mission-peace/interview/wiki/Dynamic-Programming/
7. https://www.quora.com/What-is-the-time-complexity-of-dynamic-programming
8. http://stackoverflow.com/questions/4571670/why-are-fibonacci-numbers-significant-in-computer-science
9. http://codercareer.blogspot.com/p/dynamic-interview-questions.html
References for Greedy Method:
1. Aarti, “Performance Analysis of Huffman Coding Algorithm”, International Journal of Advanced
Research in Computer Science and Software Engineering, Volume 3, Issue 5, May 2013.
2. Steven Pigeon, Yoshua Bengio, “A Memory-Efficient Huffman Adaptive Coding Algorithm for Very
Large Sets of Symbols”
3. http://www.geeksforgeeks.org/greedy-algorithms-set-1-activity-selection-problem/

Más contenido relacionado

La actualidad más candente

Solving systems with elimination
Solving systems with eliminationSolving systems with elimination
Solving systems with elimination
Amanda Ann
 
February 13, 2015
February 13, 2015February 13, 2015
February 13, 2015
khyps13
 
A1, 6 1, solving systems by graphing (blog 1)
A1, 6 1, solving systems by graphing (blog 1)A1, 6 1, solving systems by graphing (blog 1)
A1, 6 1, solving systems by graphing (blog 1)
kstraka
 
Systems of equations by graphing by graphing sect 6 1
Systems of equations by graphing by graphing sect 6 1Systems of equations by graphing by graphing sect 6 1
Systems of equations by graphing by graphing sect 6 1
tty16922
 
A1, 6 1, solving systems by graphing (rev)
A1, 6 1, solving systems by graphing (rev)A1, 6 1, solving systems by graphing (rev)
A1, 6 1, solving systems by graphing (rev)
kstraka
 

La actualidad más candente (17)

Principle of Integration - Basic Introduction - by Arun Umrao
Principle of Integration - Basic Introduction - by Arun UmraoPrinciple of Integration - Basic Introduction - by Arun Umrao
Principle of Integration - Basic Introduction - by Arun Umrao
 
Complex Numbers
Complex NumbersComplex Numbers
Complex Numbers
 
Limit & Continuity of Functions - Differential Calculus by Arun Umrao
Limit & Continuity of Functions - Differential Calculus by Arun UmraoLimit & Continuity of Functions - Differential Calculus by Arun Umrao
Limit & Continuity of Functions - Differential Calculus by Arun Umrao
 
Matlab lab manual
Matlab lab manualMatlab lab manual
Matlab lab manual
 
Solving systems with elimination
Solving systems with eliminationSolving systems with elimination
Solving systems with elimination
 
February 13, 2015
February 13, 2015February 13, 2015
February 13, 2015
 
A1, 6 1, solving systems by graphing (blog 1)
A1, 6 1, solving systems by graphing (blog 1)A1, 6 1, solving systems by graphing (blog 1)
A1, 6 1, solving systems by graphing (blog 1)
 
Systems of equations
Systems of equationsSystems of equations
Systems of equations
 
Principle of Definite Integra - Integral Calculus - by Arun Umrao
Principle of Definite Integra - Integral Calculus - by Arun UmraoPrinciple of Definite Integra - Integral Calculus - by Arun Umrao
Principle of Definite Integra - Integral Calculus - by Arun Umrao
 
Systems of equations by graphing by graphing sect 6 1
Systems of equations by graphing by graphing sect 6 1Systems of equations by graphing by graphing sect 6 1
Systems of equations by graphing by graphing sect 6 1
 
Differential calculus maxima minima
Differential calculus  maxima minimaDifferential calculus  maxima minima
Differential calculus maxima minima
 
Elimination method Ch 7
Elimination method Ch 7Elimination method Ch 7
Elimination method Ch 7
 
Solving Systems by Substitution
Solving Systems by SubstitutionSolving Systems by Substitution
Solving Systems by Substitution
 
15.2 solving systems of equations by substitution
15.2 solving systems of equations by substitution15.2 solving systems of equations by substitution
15.2 solving systems of equations by substitution
 
A1, 6 1, solving systems by graphing (rev)
A1, 6 1, solving systems by graphing (rev)A1, 6 1, solving systems by graphing (rev)
A1, 6 1, solving systems by graphing (rev)
 
Maxima & Minima of Functions - Differential Calculus by Arun Umrao
Maxima & Minima of Functions - Differential Calculus by Arun UmraoMaxima & Minima of Functions - Differential Calculus by Arun Umrao
Maxima & Minima of Functions - Differential Calculus by Arun Umrao
 
Solving systems of Equations by Elimination
Solving systems of Equations by EliminationSolving systems of Equations by Elimination
Solving systems of Equations by Elimination
 

Destacado (9)

Pipeline Computing by S. M. Risalat Hasan Chowdhury
Pipeline Computing by S. M. Risalat Hasan ChowdhuryPipeline Computing by S. M. Risalat Hasan Chowdhury
Pipeline Computing by S. M. Risalat Hasan Chowdhury
 
Gwu 2010 intro to ergo
Gwu 2010 intro to ergoGwu 2010 intro to ergo
Gwu 2010 intro to ergo
 
I am an algorithm - workshop on understanding bias in coding
I am an algorithm - workshop on understanding bias in codingI am an algorithm - workshop on understanding bias in coding
I am an algorithm - workshop on understanding bias in coding
 
Interview tips for google hangout
Interview tips for google hangoutInterview tips for google hangout
Interview tips for google hangout
 
Digital Transmission Fundamentals
Digital Transmission FundamentalsDigital Transmission Fundamentals
Digital Transmission Fundamentals
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
NETWORK COMPONENTS
NETWORK COMPONENTSNETWORK COMPONENTS
NETWORK COMPONENTS
 
Algorithms - Introduction to computer programming
Algorithms - Introduction to computer programmingAlgorithms - Introduction to computer programming
Algorithms - Introduction to computer programming
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
 

Similar a A simple study on computer algorithms by S. M. Risalat Hasan Chowdhury

Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
Workshop03.docx lap trinh C cho người mới bắt đầu
Workshop03.docx  lap trinh C cho người mới bắt đầuWorkshop03.docx  lap trinh C cho người mới bắt đầu
Workshop03.docx lap trinh C cho người mới bắt đầu
linhtran111111111111
 
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docxCALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
RAHUL126667
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
danielgetachew0922
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 

Similar a A simple study on computer algorithms by S. M. Risalat Hasan Chowdhury (20)

Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
NPDE-TCA
NPDE-TCANPDE-TCA
NPDE-TCA
 
BCA_MATHEMATICS-I_Unit-V
BCA_MATHEMATICS-I_Unit-VBCA_MATHEMATICS-I_Unit-V
BCA_MATHEMATICS-I_Unit-V
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 
Programming workshop
Programming workshopProgramming workshop
Programming workshop
 
2.pptx
2.pptx2.pptx
2.pptx
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
Solucionario_de_Chapra_y_Canale_Quinta_E.pdf
Solucionario_de_Chapra_y_Canale_Quinta_E.pdfSolucionario_de_Chapra_y_Canale_Quinta_E.pdf
Solucionario_de_Chapra_y_Canale_Quinta_E.pdf
 
Introduction to Artificial Neural Networks
Introduction to Artificial Neural NetworksIntroduction to Artificial Neural Networks
Introduction to Artificial Neural Networks
 
Workshop03.docx lap trinh C cho người mới bắt đầu
Workshop03.docx  lap trinh C cho người mới bắt đầuWorkshop03.docx  lap trinh C cho người mới bắt đầu
Workshop03.docx lap trinh C cho người mới bắt đầu
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docxCALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 

Último

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 

Último (20)

UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 

A simple study on computer algorithms by S. M. Risalat Hasan Chowdhury

  • 1. A Simple Study On Computer Algorithms By S. M. Risalat Hasan Chowdhury Nayeem Sagor Sumon Badaya
  • 2. Divide and Conquer Introduction: From the beginning of creation, every creation follows its own techniques to do daily works. The techniques are sometimes called algorithm. Algorithm is a finite set of instructions which accomplishes a particular task. An algorithm is very effective. A person can make it with the help of a paper and pen only. Divide and Conquer is also an algorithm to solve many problems. Divide and Conquer algorithm divide a problem into sub-problems which are similar to the actual problem. After that recursively solve these sub-problems and combining their results. The main problem can be solved by solving the sub-problems appropriately and combined every result. Divide and Conquer algorithm has many appealing properties which can match them with the newly modern parallel machines. First one is, they have much inherent parallelism. While the division step is completed, the sub problems are normally independent and can be solved in parallel. The recursive structure of the algorithm naturally leads to recursively generate concurrency. Both the divide and the combine steps can be executed in parallel with divide and combine steps of other sub-problems. Second one is, divide and conquer programs also have good cache performance in the machine. While a sub-problem goes in the cache, the program can reuse the cached data until the sub-problem will have been completely solved or a complete result come out. Divide and conquer programs generally work well with a well range of cache sizes and at all levels of the memory hierarchy [1] . Divide Solve of Conquer sub-problem Combine Figure: Divide and conquer a problem In the figure, we divide a problem into two separate sub-problems. The machine solves the sub-problem in a parallel way and gives individual results from it. After this combine the two results and have a solution of that problem. We can solve a large problem with the help of this algorithm. And every sub-problem of the large problem can be solved in parallel way in the machine and we can get a combined result of the large problem from every sub- problem result. There are many problems which can be solved with the help of Divide and Conquer algorithm. Two of them are discussed here- 1. Closest pair of point: 1.1. Introduction: Closest pair of point problem is a well known computational geometry problem. The problem is to find the smallest distance between two dots. Given n points and their distances, we have to find the smallest distance among them. Here is an example Sub-problem Problem Sub-problem Solution of Sub-problem Solution of Sub-problem Solution of problem
  • 3. Figure 1.1: n points and their distances. Figure shows some points and their distances. The distances are 5, 9, 10, 11, 12, 15…. for the n points. Before calculating the smallest distance for closest pair of point we have to divide the points into two parts to make easy in calculation. Figure 1.2: Divide the points into two parts to make easy in calculation. Figure shows us two separate parts of the problem. Here we use three steps to solve the problem 1. Divide: Draw a vertical line with n/2 points on each side. 2. Conquer: Find closest pair on each side, the work will do recursively. 3. Combine: Find closest pair points from the two sub-problem solutions [2] . Usually, the works is not very easy to find the smallest distance between two points. We have to search the whole distances of every two pair of points. On the left side there are 6 points, 4 distances and their values are 5,11,12,15. On the right side there are 7 points, 3 distances and their values are 9, 10, and 15. On left side sub-problem solution is 5 and on the right side sub-problem solution is 9. Combining 5 and 9, we get the final result and that is 5.
  • 4. Figure 1.3: Closets pair of point are shown in red dots. Figure shows the smallest distance between two points that is 5 and remarked with red dots. We divide the points in to two parts to make the solution to be easy. If there are more points, we can divide the whole points in a binary way (4, 8, 16 etc) for searching the minimum or smallest distance between two points. 1.2. Algorithm: Procedure findclosest (P, int n) // n is the number of elements in set P Set: PLeft, PRight, Pleftmin, Prightmin, Pclosest if (n £ 3) return SHORTEST (P) else PLeft ¬ { p(1), p(2), …. , p(n/2) } PRight ¬ { p(n/2 + 1), ……. , p(n) } Pleftmin ¬ findclosest (PLeft, n/2) Prightmin ¬ findclosest (PRight, n/2) Pclosest¬ MERGEPLANES (Pleftmin, Prightmin) return Pclosest endif endprocedure Procedure MERGEPLANES (P1, P2) D ¬ MINIMUM (P1, P2) for (every i in P1) for every j in P2 such that xi < xj + D AND (yi +d > yj > yi – d) if DISTANCE (i, j) < D return (i, j) endfor endfor endprocedure [3] 1.3. Codes in Java: import java.util.*; public class ClosestPair{ private double[][] points; Point p1, p2; public static void main(String[] args) { System.out.println(" ********* Closest pair of points ********* "); double[][] points = new double[100][10]; for (int i = 0; i < points.length; i++) {
  • 5. points[i][0] = Math.random() * 10; points[i][1] = Math.random() * 10; } ClosestPair closestPair = new ClosestPair(points); System.out.println("nThe shortest distance is " + closestPair.getMinimumDistance()); System.out.print("nThe Closest pair is (" + closestPair.p1.x + ", " + closestPair.p1.y + ") and "); System.out.println("(" + closestPair.p2.x + ", " + closestPair.p2.y + ")"); } ClosestPair() { } public ClosestPair(double[][] points) { setPoints(points); } public void setPoints(double[][] points) { this.points = points; } public double getMinimumDistance() { Point[] pointsOrderedOnX = new Point[points.length]; for (int i = 0; i < pointsOrderedOnX.length; i++) pointsOrderedOnX[i] = new Point(points[i][0], points[i][1]); Arrays.sort(pointsOrderedOnX); if (checkIdentical(pointsOrderedOnX)) return 0; Point[] pointsOrderedOnY = pointsOrderedOnX.clone(); Arrays.sort(pointsOrderedOnY, new CompareY()); return distance(pointsOrderedOnX, 0, pointsOrderedOnX.length - 1, pointsOrderedOnY); } public boolean checkIdentical(Point[] pointsOrderedOnX) { for (int i = 0; i < pointsOrderedOnX.length - 1; i++) { if (pointsOrderedOnX[i].compareTo(pointsOrderedOnX[i + 1]) == 0) { p1 = pointsOrderedOnX[i]; p2 = pointsOrderedOnX[i + 1]; return true; } } return false; } public double distance( Point[] pointsOrderedOnX, int low, int high, Point[] pointsOrderedOnY) { if (low >= high) return Double.MAX_VALUE; else if (low + 1 == high) { p1 = pointsOrderedOnX[low]; p2 = pointsOrderedOnX[high]; return distance(pointsOrderedOnX[low], pointsOrderedOnX[high]); } int mid = (low + high) / 2; Point[] pointsOrderedOnYL = new Point[mid - low + 1]; Point[] pointsOrderedOnYR = new Point[high - mid]; int j1 = 0; int j2 = 0; for (int i = 0; i < pointsOrderedOnY.length; i++) {
  • 6. if (pointsOrderedOnY[i].compareTo(pointsOrderedOnX[mid]) <= 0) pointsOrderedOnYL[j1++] = pointsOrderedOnY[i]; else pointsOrderedOnYR[j2++] = pointsOrderedOnY[i]; } double d1 = distance( pointsOrderedOnX, low, mid, pointsOrderedOnYL); double d2 = distance( pointsOrderedOnX, mid + 1, high, pointsOrderedOnYR); double d = Math.min(d1, d2); int count = 0; for (int i = 0; i < pointsOrderedOnYL.length; i++) if (pointsOrderedOnYL[i].x >= pointsOrderedOnX[mid].x - d) count++; Point[] stripL = new Point[count]; count = 0; for (int i = 0; i < pointsOrderedOnYL.length; i++) if (pointsOrderedOnYL[i].x >= pointsOrderedOnX[mid].x - d) stripL[count++] = pointsOrderedOnYL[i]; count = 0; for (int i = 0; i < pointsOrderedOnYR.length; i++) if (pointsOrderedOnYR[i].x <= pointsOrderedOnX[mid].x + d) count++; Point[] stripR = new Point[count]; count = 0; for (int i = 0; i < pointsOrderedOnYR.length; i++) if (pointsOrderedOnYR[i].x <= pointsOrderedOnX[mid].x + d) stripR[count++] = pointsOrderedOnYR[i]; double d3 = d; int j = 0; for (int i = 0; i < stripL.length; i++) { while (j < stripR.length && stripL[i].y > stripR[j].y + d) j++; int k = j; while (k < stripR.length && stripR[k].y <= stripL[i].y + d) { if (d3 > distance(stripL[i], stripR[k])) { d3 = distance(stripL[i], stripR[k]); p1 = stripL[i]; p2 = stripR[k]; } k++; } } return Math.min(d, d3); } public static double distance(Point p1, Point p2) { return distance(p1.x, p1.y, p2.x, p2.y); } public static double distance( double x1, double y1, double x2, double y2) { return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); } static class Point implements Comparable<Point> { double x; double y; Point(double x, double y) {
  • 7. this.x = x; this.y = y; } public int compareTo(Point p2) { if (this.x < p2.x) return -1; else if (this.x == p2.x) { if (this.y < p2.y) return -1; else if (this.y == p2.y) return 0; else return 1; } else return 1; } } static class CompareY implements java.util.Comparator<Point> { public int compare(Point p1, Point p2) { if (p1.y < p2.y) return -1; else if (p1.y == p2.y) { if (p1.x < p2.x) return -1; else if (p1.x == p2.x) return 0; else return 1; } else return 1; } } } [4] Output: 1.4. Application: Closest pair of problem arises in a number of applications. For example: in air-traffic control, in mobile phone connecting with network tower (BTS) problem etc. Here we discuss about the mobile phone connecting problem. The Cellular phone or mobile phone uses Global System for Mobile communication (GSM) technique to transfer all kind of data. The word Cell can be defined as an area of radio coverage from one Base Transceiver Station (BTS) antenna which is the smallest building block in a mobile network. The GSM is divided into two parts and they are switching system (SS) and Base Station System (BSS).
  • 8. Mobile phone Figure 1.4: Cell phone connection with BTS Abbreviations of the above terms are Authentication Center (AUC), Base Station Controller (BSC), Base Transceiver Station (BTS), Equipment Identity Register (EIR), Home Location Register (HLR), Mobile Station (MS), Mobile services Switching Center (MSC), Visitor Location Register (VLR). The figure shows, a mobile or cell phone is connected with a BTS. If a mobile user wants to call another user within a same city but in a different area, the BTS transfer the data to MSC trough BSC. MSC is for the same city network providing tool. Another user’s BSC is also connected with this MSC. So the two users of this network use the same MSC to establish a phone call or other services. There are many BTSs in an area but the question is which BTS is going to connect with the Mobile Station (MS). Basically cells are hexagonal. They cover a hexagon network. While a user goes to another cell it faces some problems. The process of changing a cell during a phone call is called handover. Handover process maintains the following steps to change a cell. They are- 1. A mobile station continuously measures the signal strength of connected cell and neighboring cells. 2. It sends the message report of measured power level to the BSC. 3. Based on these report BSC decides the importance of handover and channel allocation in new cell. 4. If BSC observes handover is essential then it assigns a new channel in new cell and informs MS to send the message to this new channel. 5. MS leave the old cell/BTS and establish a new connection with the new cell/BTS. With this process a cell phone can change its BTS. Now the question is which BTS is going to connect with MS. The solution is using the technique of “Closest pair of point”. MS will follow the following criteria. One is the signal strength and second is minimum distance between the MS and BTS. Which BTS has the following two criteria will connect with the MS and establishes a network. SS AUC VLR HLR EIR GMSC MSCOther Networks BSS BSC BTS
  • 9. Figure 1.5: MS connects with nearest BTS Figure shows the connection among MS and BTSs. MS trace the distances among the BTSs but it only connects with the nearest one. There are five BTS near by the MS and the distances are 85 feet, 95 feet, 110 feet, 165 feet and 255 feet. First the MS calculate the distance near to it and it measure all the distances. It checks 85, 95, 110, 165 and 255 in a parallel way and get the solution for which BTS is going to be connected. As we see from the figure, the 85 feet is the solution and the MS is going to connect with this BTS. Generally a cell phone connects with only a BTS. When a cell phone changes its cell, then the handover process is needed. Cell phone’s data voice goes to the BSC through the BTS as so MSC. If there is another cell phone user in the same city but different area, it will also use the same MSC as the previous cell phone user use. Figure 1.6: A conversation between two cell phone users trough a same MSC. Figure shows us the view of a conversation between cell phone users in same city. This is what actually happened when a phone call is being started every time. 1.5. Advantages of closest pair of point: 1. One can use the best path to travel or use. 2. After finding the smallest distance it is easy to travel to the nearest one. 3. The best path or way can save time and money too. 1.6. Disadvantages of closest pair of point: 1. It can waste some time to search the right path or nearest destination or smallest distance. 2. The measuring of smallest distance between source to destination is not easy because we have to measure all the points or destinations [5] .
  • 10. 2. Merge sort: 2.1. Introduction: Merge sort is a technique to sort a series on numbers or a string etc. Merge sort follows Divide and conquer algorithm to do its task. It was invented by John von Neumann in 1945. Here we are using divide-and-conquer to sort; we need to decide what our sub-problems will be going to look like. The full problem is to sort the entire array. The sub-problem is to sort a sub-array. Figure 4.1: Marge sort. The figure shows us a normal array which contain {38, 27, 43, 3, 9, 82, 10}. First we divide it into two sub-array, after that we again divide it. Now the sub-arrays are {38, 27}, {43, 3}, {9, 82} and {10}. Every sub-array will calculate first and then merge it with its nearest sub-array. Finally we calculate the entire two sub-arrays which are {3, 27, 38, 43} and {9, 10, 82}. The final merged array is {3, 9, 10, 27, 38, 43, 82}. 2.2. Application:  Merge Sorts are helpful for sorting linked lists in O(n log n) time. In case of linked lists, the case is different due to difference in memory allocation of arrays and linked lists.  It is used in inversion count problem.  It is used in external sorting [6] . 2.3. Algorithm: Algorithm MergeSort(low,high) //a[low:high] is a global array to be sorted. //Small(P) is true if there is only one element. //to sort. In this case list the list is already sorted. { if(low < high) then // If there are more than one element. { // Divide P into subproblems. // Find where to split the set. mid :=[(low+high)/2; // Solve the subproblems. MergeSort(low,mid); MergeSort(mid+ I,high); // Combinethe solutions.
  • 11. Merge(lowm, id,high); } }[7] . 2.4. Codes in Java: import java.util.Scanner; public class MergeSort { public static void sort(int[] a, int low, int high) { int N = high - low; if (N <= 1) return; int mid = low + N/2; sort(a, low, mid); sort(a, mid, high); int[] temp = new int[N]; int i = low, j = mid; for (int k = 0; k < N; k++) { if (i == mid) temp[k] = a[j++]; else if (j == high) temp[k] = a[i++]; else if (a[j]<a[i]) temp[k] = a[j++]; else temp[k] = a[i++]; } for (int k = 0; k < N; k++) a[low + k] = temp[k]; } public static void main(String[] args) { Scanner scan = new Scanner( System.in ); System.out.println("Merge Sort Testn"); int n, i; System.out.println("Enter number of integer elements"); n = scan.nextInt(); int arr[] = new int[ n ]; System.out.println("nEnter "+ n +" integer elements"); for (i = 0; i < n; i++) arr[i] = scan.nextInt(); sort(arr, 0, n); System.out.println("nElements after sorting "); for (i = 0; i < n; i++) System.out.print(arr[i]+" "); System.out.println(); }
  • 12. } Output: 2.5. Advantages:  It can be applied in any size of files or arrays.  Reading of the input during the run-creation step is sequential.  Since I/O is largely sequential, merge sort can be used. 2.6. Disadvantage:  The disadvantage of merge sort is that it uses a lot of memory.  It uses extra space proportional to n. This may slow it down when trying to sort very large data [8] . Conclusion of Divide and conquer: The Divide and conquer algorithm has many beneficial qualities that makes it a very important algorithm. It follows only three simple steps to make complex problems easier, and those steps are to: (1) Divide the complex problem into two or more sub-problems, (2) Recursively solve the sub problems, (3) Conquer by combining the solutions from the sub problems to get a solution of the original problem. It makes us capable to solve difficult problems, helps to find other efficient algorithms and is sometimes the best choice than other algorithms. One of the only downfalls to this algorithm is the fact that sometimes recursion is slow, and this fact can almost negate all the other advantages of this process. Merge Sort and Quick Sort are both sorting algorithms that are based on this process of the divide and conquer algorithm. This basic idea of dividing and conquering can be seen in everyday life. We can solve our daily problems by it. So, it is efficient and effective algorithm than others.
  • 13. Dynamic Programming Introduction: Dynamic programming is a programming method or technique that uses to solve the recursive problems in more efficient way or manner. Dynamic programming use Recursive technique (Recursion) . Dynamic programming recursively solve the sub-problems and store the solutions of the sub-problems this is called Memorization (i.e. avoid solve the sub-problem again) . Dynamic programming technique and memorization technique works together. So, we can say, dynamic programming has two parts:-  Recursion: recursively solve the sub-problems .  Memorization: store the solution of sub-problems i.e. avoid solving the sub-problems that already solved. Properties of Dynamic programming Dynamic programming has two major properties:-  Overlapping Sub-problems: Overlapping sub-problem means every sub-problem to be solved again and again. In dynamic programming, using recursion, we solve those sub-problems only one time and store it for future use.  Optimal Substructure: If a solution of the sub-problems can be use to solve a problem that is called Optimal Substructure Property. Note: If any problem has both properties then we can be apply and solve this problem using dynamic programming method. Approaches of Dynamic Programming Two ways can be achieved Dynamic programming-  Bottom-Up Approach: The solution of first sub-problem can be use to solve the second sub-problem and the solution of second sub-problem can be use to solve the third sub-problem, following these sequence or criteria is known as Button-Up approach.  Top-Down Approach: Break the problem into sub-problems and solves them and store the result for future use. This is the direct fall-out of the recursive formulation of any problem [1]. Complexity of Dynamic Programming Time Complexity: Dynamic programming doesn't have a time complexity, because it is not a specific algorithm. It's a general approach to constructing algorithms to solve problems that have certain properties (namely: optimal substructure and overlapping sub-problems) [7]. Space Complexity: Dynamic programming uses recursive and memorization so that space complexity will be occurred. Example 1 Fibonacci Series or Sequence Using Dynamic Programming. Introduction: The current number is the sum of previous two numbers i.e. 0 1 1 2 3 5 8 13 . . .[8].
  • 14. Program Flowchart Figure 1: Flowchart of Fibonacci series or sequence Using Dynamic Programming. Pseudo code [2]. Main() { Declaration & Initialization: Int n; Int Fibo[n]; Boolean command = true; While(command = true ?) { Input n;
  • 15. Fibonacci ( n ); Print the result; Input command = true / false; } } Fibonacci(n) { if(n is equal to 0) fibo[n] = return 0; if(n is equal to 1) fibo[n] = return 1; if(fibo[n-2] is unsolved) fibo[n-2] = Fibonacci(n-2); if(fibo[n-1] is unsolved) fibo[n-1] = Fibonacci(n-1); fibo[n] = fibo[n-2] + fibo[n-1]; return fibo[n]; } Code: import java.util.Scanner; public class FibonacciSeriesDemo { static Scanner scanner = new Scanner(System.in); static boolean command = true; public static void main(String[] args) { int n = 0; Fibonacci fibonacci = new Fibonacci(100); System.out.println("***Fibonacci Series Using Dynamic Programming****n"); while (command) { System.out.print("nEnter the n'th position: "); n = scanner.nextInt(); System.out.println("=>> Fibonacci[" + n + "] = " + fibonacci.fiboSeries(n)); System.out.print("nYou want to exit the programm(y/n): "); getCommand(); } System.out.println("nn******Thank You*******"); } private static void getCommand() { scanner.nextLine(); if (scanner.nextLine().equalsIgnoreCase("y")) { command = false; } } } public class Fibonacci { int[] f = { 0 }; public Fibonacci(int n) { f = new int[n]; f[0] = 0; f[1] = 1; } public int fiboSeries(int n) { if (n == 0) { return 0; }
  • 16. if (n == 1) { return 1; } if (f[n - 2] == 0) { f[n - 2] = fiboSeries(n - 2); } if (f[n - 1] == 0) { f[n - 1] = fiboSeries(n - 1); } f[n] = f[n - 2] + f[n - 1]; return f[n]; } } Output: Application: In computer science: 1. Fibonacci heaps which have better amortized running time than binomial heaps. 2. Fibonacci search which shares O(log N) running time with binary search on an ordered array.
  • 17. Example 2 Maximum Sum of Contiguous Sub-Array Using Dynamic Programming [3]. Introduction: A sub-array has one number of some continuous numbers. Given an integer array with positive numbers and negative numbers, get the maximum sum of all sub-arrays. Time complexity should be O (n) [9]. For example, in the array {1, -2, 3, 10, -4, 7, 2, -5}, its sub-array {3, 10, -4, 7, 2} has the maximum sum 18 [9]. Program Flowchart: Figure 2: Flowchart of maximum Sum of Contiguous Sub-Array Using Dynamic Programming. Pseudo code: Main() { Declaration & Initialization: Int array[n]; Boolean command = true; While(command = true ?) { For (0 : n) {
  • 18. Input and store array elements one by one; } MSS ( array); Print the result; Input command = true / false; } } MSS(array[n]) { maxSum=subArraySum[i]= array[i]; // where i=0; for(i=1 : n) { subArraySum[i] = Max( array[i], subArraySum[i-1]+array[i] ); maxSum = Max(maxSum,subArraysum[i]); } Return maxSum; } Code: import java.util.Scanner; public class MSSDemo { // MSSDemo.java static boolean statement = true; static int array[]; static Scanner sc=new Scanner(System.in); public static void main(String[] args) { System.out.println("n=======Welcome to Maximum Sum Of Contiguous Sub-Array Programme========n"); while (statement) { arrayInitialization(); System.out.println("nnMaximum Sum = "+new MaximumSumOfContiguousSuArray().MSS(array)); getStatement(); } System.out.println("n======Thank You=======n"); } static boolean getStatement() { System.out.print("nYou want to exit the Program(Y/N) . . . ? "); if ("Y".equalsIgnoreCase(new Scanner(System.in).nextLine())) { return statement = false; } else { return statement = true; } } public static void arrayInitialization(){ System.out.print("Enter Array Limit: "); array=new int[sc.nextInt()]; System.out.print("nEnter Array Element:- n"); for(int i=0; i<array.length; i++){ System.out.print("nArray["+(i+1)+"] = "); array[i]=sc.nextInt(); } } } System.out.println("n=======Welcome to Maximum Sum Of Contiguous Sub-Array Programme========n");
  • 19. public class MaximumSumOfContiguousSuArray { public int MSS(int[] array) { int subArraySum[] = new int[array.length]; int maxSum = subArraySum[0] = array[0]; for (int i = 1; i < array.length; i++) { subArraySum[i] = Math.max(array[i], subArraySum[i - 1] + array[i]); maxSum = Math.max(subArraySum[i], maxSum); } return maxSum; } } Output:
  • 20. Example 3 Maximum Sub-Square Matrix of a Matrix using Dynamic Programming [4]. Introduction: Given a matrix of 0’s and 1’s (binary matrix). Find out Maximum size square sub-matrix with all 1’s [4]. Example: Figure [3]: Maximum size square sub-matrix with all 1s Example [4]. Pseudo code [4]. Main() { Declare matrix[n][m]; For( i=0 : n) { For(j=0 : m) { Matrix[i][j] = input array element; } } Call MSSM ( matrix ); Print the result; } MSSM(matrix)// Maximum Sub-Square Function { declare temp[matrixColumn][matrixRow]; for(i=0 : matrixColumn) { temp[0][i]=matrix[0][i]; } for(i=0 : matrixRow) { temp[i][0]=matrix[i][0]; } Max=1; for(i=0 : matrixRow) { for(i=0 : matrixColumn) { If(matrix[i][j] equal 0) { Continue; } temp[i][j] = getMin(temp[i-1][j],temp[i-1][j-1],temp[i][j-1]) + 1; if(temp[i][j] grater then max) { max=temp[i][j]; }
  • 21. } } Return max; } getMin(a,b,c)// Finding the Minimum Value { Declare min; If(a < b){ Min =a; } Else { Min =b; } If(c < min){ Min =c; } Return min; } Program Flowchart: Figure 3: Flowchart of Maximum Sub-Square Matrix of matrix.
  • 22. Code: import java.util.Scanner; public class MaximumSubSquareMatrix { public int MSSM(int matrix[][]) { int temp[][] = new int[matrix.length][matrix[0].length]; for (int j = 0; j < matrix[0].length; j++) { temp[0][j] = matrix[0][j]; } for (int i = 0; i < matrix.length; i++) { temp[i][0] = matrix[i][0]; } int max = 1; for (int i = 1; i < matrix.length; i++) { for (int j = 1; j < matrix[i].length; j++) { if (matrix[i][j] == 0) continue; temp[i][j] = getMin(temp[i - 1][j], temp[i - 1][j - 1], temp[i][j - 1]) + 1; if (temp[i][j] > max) max = temp[i][j]; } } return max; } private int getMin(int a, int b, int c) { return Math.min(Math.min(a, b), c); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("===Maximum Sub-Square Matrix Using Dynamic Programming===="); System.out.print("nEnter number of column: "); int m = sc.nextInt(); System.out.print("Enter number of Row: "); int n = sc.nextInt(); int matrix[][] = new int[n][m]; System.out.println("nEnter Array Element's :"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = sc.nextInt(); } } MaximumSubSquareMatrix mm = new MaximumSubSquareMatrix(); System.out.println("nnMaximum Sub-Square Matrix is: " + mm.MSSM(matrix)); } }
  • 23. Output: Application: Finding the available or free space (square) in a specific area.
  • 24. The Greedy Methods Huffman coding Algorithm 1.1 Introduction: Huffman coding is one of the most popular technique for removing coding redundancy. It has been used in various compression applications [1]. The lossless method of compression and decompression is proposed using Huffman coding which is used on test string as well as on different image file formats for performance analysis. The result reveals that the original image used for coding is almost close to the decoded output image [1]. Huffman codes are prefix-free codes and decodable, that’s mean no code is the prefix of the some other code and one can know when the last bit of the code is read and immediately recognize the code[2]. Huffman coding is base on the frequency of occurrence data item. It is used to code values according to their probability of occurrence. Short words are assigned to highly probable values and long code words to less probable values. 1.2 Methods: 1. Make a list of the symbols, sorted by frequencies from largest to smallest. 2. Combine the two smallest frequency values. 3. Repeat Steps 1 and 2 until the all of the frequencies have been added up. The final number at the head of the tree should be the sum of all the frequencies. The path of each symbol’s frequency is traced from top to the each branch of the tree. 1.3 Constructing tree: The objective is to construct a tree such that the path from a leaf that contains the symbols s to the root has a length as close as possible to − log( p(s)). To each leaf is associated a weight, which is basically the number of times the symbol s has been seen in the data. Huffman algorithm’s strategy is to build a tree with weights that are as balanced as possible. The unusual thing is in Huffman’s algorithm is that instead of building this tree from the root down to the leaf, it is built from the leaves up to the root. The first step is to build a list which contains only leaves. Each leaf has an associated symbol s and a weight, p(s). We pick, out of that list, the two element with the smallest weights. We create a new node such that its children are the two picks, and that its weight is the sum of its children’s weight. We put back the new node in the list. We repeat this procedure until only one node is left, which will become the root of the tree. Codes are then assigned easily. Each time we go left in the tree, we assign a ‘0’, and when we go right, we assign a ‘1’. The code for a leaf is the path one must follow to reach it from the root. 1.4 Example: We use this sentence for the below figure "this is an example of a huffman tree".
  • 25. Fig 1: Huffman tree [Source: https://www.google.com.bd/search?q=huffman+coding+algorithm] Figure description: In the above figure, we construct a huffman tree using the sentence "this is an example of a huffman tree ".At first we calculate the frequency of the characters. Then we sorted them as decreasing order. After that we combine two minimum values. Using this technique we find the root. Here is the root is 36. Each time we go left in the tree, we assign a ‘0’, and when we go right, we assign a ‘1’. 1.5 Algorithm: 1. n=|C| 2. Q=C 3. For i=1 to n-1 4. Allocate a new node z 5. z.left=x=EXTRACT-MIN(Q) 6. z.right=y= EXTRACT-MIN(Q) 7. z.freq=x.freq+y.freq 8. INSERT(Q,z) 9. Return EXTRACT-MIN(Q) //return the root of the tree 1.6 Code: import java.util.*; abstract class HuffmanTree implements Comparable<HuffmanTree> { public final int frequency; // the frequency of this tree public HuffmanTree(int freq) { frequency = freq; } // compares on the frequency public int compareTo(HuffmanTree tree) { return frequency - tree.frequency;
  • 26. } } class HuffmanLeaf extends HuffmanTree { public final char value; // the character this leaf represents public HuffmanLeaf(int freq, char val) { super(freq); value = val; } } class HuffmanNode extends HuffmanTree { public final HuffmanTree left, right; // subtrees public HuffmanNode(HuffmanTree l, HuffmanTree r) { super(l.frequency + r.frequency); left = l; right = r; } } public class HuffmanCode2 { // input is an array of frequencies, indexed by character code public static HuffmanTree buildTree(int[] charFreqs) { PriorityQueue<HuffmanTree> trees = new PriorityQueue<HuffmanTree>(); // initially, we have a forest of leaves // one for each non-empty character for (int i = 0; i < charFreqs.length; i++) if (charFreqs[i] > 0) trees.offer(new HuffmanLeaf(charFreqs[i], (char)i)); assert trees.size() > 0; // loop until there is only one tree left while (trees.size() > 1) { // two trees with least frequency HuffmanTree a = trees.poll(); HuffmanTree b = trees.poll(); // put into new node and re-insert into queue trees.offer(new HuffmanNode(a, b)); } return trees.poll(); }
  • 27. public static void printCodes(HuffmanTree tree, StringBuffer prefix) { assert tree != null; if (tree instanceof HuffmanLeaf) { HuffmanLeaf leaf = (HuffmanLeaf)tree; // print out character, frequency, and code for this leaf (which is just the prefix) System.out.println(leaf.value + "t" + leaf.frequency + "t" + prefix); } else if (tree instanceof HuffmanNode) { HuffmanNode node = (HuffmanNode)tree; // traverse left prefix.append('0'); printCodes(node.left, prefix); prefix.deleteCharAt(prefix.length()-1); // traverse right prefix.append('1'); printCodes(node.right, prefix); prefix.deleteCharAt(prefix.length()-1); } } public static void main(String[] args) { String test = "this is an example for huffman encoding"; // we will assume that all our characters will have // code less than 256, for simplicity int[] charFreqs = new int[256]; System.out.println("################HUFFMAN CODING ALGORITHM###########"); // read each character and record the frequencies for (char c : test.toCharArray()) charFreqs[c]++; // build tree HuffmanTree tree = buildTree(charFreqs); // print out results System.out.println("SYMBOLtWEIGHTtHUFFMAN CODE"); printCodes(tree, new StringBuffer()); } } 1.7 Application: 1. It is used for image detect 2. data compression
  • 28. 3. pattern matching 1.8 Strength: 1. It needs less time to transmit data. 2. It needs less memory space after encoding the data. 3. It is very efficient technique. 4. Huffman is a good coding technique for compressing the data and general types of images. 1.9 Limitation: 1. It is used static coding. 2. The algorithm must see all the data before performing the compression. It is quite impractical to do so. One might not have the sufficient space to store all the data before starting compression. 3. It needs very large memory. 4. In the ‘worst case’, the code book cannot consist more than 256 possible bytes. But in the real world, one might need a code book for a set of several thousands or millions symbols. 5. To understand the compressed data by the decoder, the decoder must know the code book. If the decoders don’t know the code book then one will transmit the code book by itself. By doing this, the data might be losing completely[2]. 6. It needs very large code book for large symbols. 1.10 Conclusion: From the above discussion we have seen that the original image used for coding is almost close to the decoded output image. The result shows that the higher Code redundancy helps to achieve more compression. The above presented Huffman coding and decoding is used to reduce test data volume, test data compression and decompression time. Hence we conclude that Huffman coding is efficient technique for image compression and decompression to some extent. The problems of adaptivity, memory management and code book transmission problems, we propose a new algorithm for adaptive Huffman coding. The algorithm allows for very good adaptivity, efficient memory usage and efficient decoding algorithms.
  • 29. Dijkstra's Algorithm 2.1 Introduction: It is an algorithm for finding the shortest path between nodes in a graph. The algorithm is divided into two categories. One is single source to single destination, and another is single source to multiple destinations in the graph.For example, if the nodes are representing cities and edge represent distances between pairs of cities, Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a result, the shortest path algorithm is widely used in network routing protocols. We are starting from the source node or initial node. We have to find the shortest path from source to destination node. We use some steps to find the shortest path. Which are given bellow: 1. Set zero to initial node and infinity for all other nodes. 2. Set the initial node as current node. Mark all the nodes as unvisited. Create a set of all the unvisited nodes as a set of unvisited set. 3. For the current node calculates the distances of unvisited neighbors. Compare the newly calculated distance to the current assigned value and assign the smaller one. 4. When we are visited all of the neighbors of the current node, mark the current node as visited and remove it from the unvisited set. A visited node will never check again. 5. If the destination node has been marked visited. Then the algorithm has finished. 2.2 Example: Fig 3: Dijkstra's algorithm From the figure ,u is the source and z is the destination, we have to reach the destination z from u using shortest path, at first we go to v and cost is 2,we can go to the node x is cost is 1,and we can go to node w ,whose cost is 5.we can use node v to go to node w and node x whose cost is 5 and 4 respectively. But we can go node w and x directly whose cost is 5 and 1 respectively, which is optimal from previous step. So node v is eliminated. We can go to node w via node x , whose cost is 4 ,it is optimal than direct edge u to w. From x we can go to node y, whose cost is 2 from source. We can go to node w via y whose cost is 3 from source which is better than previous cost from source via x. from node w we go to node z whose cost is 8 and from via node y to node z cost is 3. So it is the optimal solution, now the shortest path is u to x and x to y and y to z. The cost is 3.
  • 30. 2.3 Algorithm: 1. INITIALIZE-SINGLE-SOURCE(G,s) 2. S=∅ 3. Q=G.V 4. While Q≠ ∅ 5. U=EXTRACT-MIN(Q) 6. S=S U {u} 7. For each vertex v ∈ G.Adj[u] 8. RELAX(u,v,w) 2.4 Code: import java.util.*; public class Dijkstra { public static void main(String args[]) { System.out.println("************** Dijkstra's Algorithm **************n"); Scanner input=new Scanner(System.in);//Scanner int vertex,noOfEdges=1,min=999,u=0,v=0,total=0,i,j;//initialization System.out.print("Enter the number of vertex: "); vertex=input.nextInt();//take input no. of vertex int matrix[][]=new int[vertex][vertex],visited[]=new int[vertex];//initialization //input matrix System.out.println("Enter the values of adjacent matrix :"); for(i=0;i<vertex;i++) { visited[i]=0;//make all the vettexes are not visited for(j=0;j<vertex;j++) { matrix[i][j]=input.nextInt();//take value of adjacent matrix if(matrix[i][j]==0)//true if vertexes are not related { matrix[i][j]=999;//which vertexes are not related make the distance 999 as like infinite between them } } }
  • 31. //end of taking input //output stream /*0 28 0 0 0 10 0 28 0 16 0 0 0 14 0 16 0 12 0 0 0 0 0 12 0 22 0 18 0 0 0 22 0 25 24 10 0 0 0 25 0 0 0 14 0 18 24 0 0*/ System.out.println(); visited[0]=1; while(noOfEdges < vertex) { min=999; for(i=0;i<vertex;i++) { if(visited[i]==1) { for(j=0;j<vertex;j++) { if(visited[j]!=1) { if(min>matrix[i][j]) { min=matrix[i][j]; u=i; v=j; } } } } } noOfEdges++; visited[v]=1; total+=min; System.out.println("edge "+(u+1)+"--->"+(v+1)+" :weihgt = "+min); }
  • 32. System.out.println("nnThe cost of the minimum shortest path is = "+total); } } 2.5 Application: 1. Robot path planning. 2. Logistics Distribution Lines. 3. Link-state routing protocols. 4. OSPF(Open Shortest Path First) 5. IS-IS (Intermediate System to Intermediate System) 2.6 Advantage: 1. It is faster. 2. It doesn’t need to investigate all the edges. 3. It is not restricted to acyclic graph. 4. It is very efficient. 2.7 Disadvantage: 1. Expensive to compute. 2. The major disadvantage of the algorithm is the fact that it does a blind search there by consuming a lot of time waste of necessary resources. 3. It cannot handle negative edges. This leads to acyclic graphs and most often cannot obtain the right shortest path. 2.8 Conclusion: From the above discussion we have seen that it is very simple algorithm to find the shortest path from source to destination. It is very efficient and effective algorithm. We use prim’s algorithm to solve the problem.
  • 33. Contribution:  Divide and conquer algorithm – S. M. Risalat Hasan Chowdhury.  Dynamic Programming – Sumon Badaya.  Greedy Method – Nayeem Sagor. References for Divide and Conquer: 1. Radu Rugina and Martin Rinard, Laboratory for Computer Science Massachusetts Institute of Technology Cambridge, MA 02139. 2. Paul Beame and others. 3. Ashish Sharma,Rengakrishnan Subramanian,November 28, 2001. 4. http://www.ecs.csun.edu/~cputnam/Comp%20110/Liang%208th%20ed%20PP_Slides/html/ClosestPair.ht ml 5. GSM System Survey-1 by Ericsson. 6. http://quiz.geeksforgeeks.org/merge-sort/ 7. Fundamentals of Computer Algorithms by Sartaj Sahni, Ellis Horowitz and Sanguthevar Rajasekaran. 8. http://www.answers.com/Q/What_are_the_advantages_and_disadvantages_of_merge_sort#slide=5 References for Dynamic Programming: 1. http://algorithms.tutorialhorizon.com/introduction-to-dynamic-programming. 2. http://algorithms.tutorialhorizon.com/introduction-to-dynamic-programming-fibonacci-series/ 3. http://www.geeksforgeeks.org/largest-sum-contiguous-subarray/ 4. http://algorithms.tutorialhorizon.com/dynamic-programming-maximum-size-square-sub-matrix-with-all-1s/ 5. https://github.com/mission-peace/interview/wiki/Dynamic-Programming/ 6. https://github.com/mission-peace/interview/wiki/Dynamic-Programming/ 7. https://www.quora.com/What-is-the-time-complexity-of-dynamic-programming 8. http://stackoverflow.com/questions/4571670/why-are-fibonacci-numbers-significant-in-computer-science 9. http://codercareer.blogspot.com/p/dynamic-interview-questions.html References for Greedy Method: 1. Aarti, “Performance Analysis of Huffman Coding Algorithm”, International Journal of Advanced Research in Computer Science and Software Engineering, Volume 3, Issue 5, May 2013. 2. Steven Pigeon, Yoshua Bengio, “A Memory-Efficient Huffman Adaptive Coding Algorithm for Very Large Sets of Symbols” 3. http://www.geeksforgeeks.org/greedy-algorithms-set-1-activity-selection-problem/