Académique Documents
Professionnel Documents
Culture Documents
Complexity
ASSIGNMENT
M.Tech CSE
1st Semester
Submitted By: Sounak Banerjee
1.3 Program:
The algorithm has been implemented using JAVA
programming language.
File: Partition.java
import java.util.Scanner;
public class Partition
{
static int count=0;
static void printArray(int p[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(p[i] + " ");
System.out.println("");
}
static void printAllUniqueParts(int n)
{
int[] p = new int[100];
int k=0;
p[k]=n;
while(true)
{
3 | Page
printArray(p,k+1);
count++;
int rem_val = 0;
while (k >= 0 && p[k] == 1)
{
rem_val += p[k];
k--;
}
if (k < 0)
return;
p[k]--;
rem_val++;
while (rem_val > p[k])
{
p[k+1] = p[k];
rem_val = rem_val - p[k];
k++;
}
p[k+1] = rem_val;
k++;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number : ");
int number=sc.nextInt();
4 | Page
System.out.println(" ");
System.out.println("List of Partitions of "+number+"
are: ");
printAllUniqueParts(number);
System.out.println(" ");
System.out.print("number of partitions are: "+count);
}
}
1.4 Output:
5 | Page
6 | Page
Assignment 2
2.3 Program:
The algorithm has been implemented using JAVA
programming language.
File: GrahamScan.java
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Stack;
10 | P a g e
public double distanceTo(Point2D that)
{
double dx = this.x - that.x;
double dy = this.y - that.y;
return Math.sqrt(dx * dx + dy * dy);
}
// defensive copy
int N = pts.length;
Point2D[] points = new Point2D[N];
for (int i = 0; i < N; i++)
points[i] = pts[i];
Arrays.sort(points);
Arrays.sort(points, 1, N, points[0].POLAR_ORDER);
assert isConvex();
}
// test client
public static void main(String[] args)
{
System.out.println("Graham Scan Test");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of points");
int N = sc.nextInt();
18 | P a g e
Assignment 3
The Brute force solution is O(n^2), compute the distance between each
pair and return the smallest. We can calculate the smallest distance in
O(nLogn) time using Divide and Conquer strategy.
19 | P a g e
Algorithm:
Input: An array of n points P[]
Output: The smallest distance between two points in the given array.
As a pre-processing step, input array is sorted according to x coordinates.
Step 1: Find the middle point in the sorted array, we can take P[n/2] as
middle point.
Step 2: Divide the given array in two halves. The first subarray contains
points from P[0] to P[n/2]. The second subarray contains points from
P[n/2+1] to P[n-1].
Step 3: Recursively find the smallest distances in both subarrays. Let the
distances be dl and dr. Find the minimum of dl and dr. Let the minimum
be d.
20 | P a g e
Step 5: Sort the array strip[] according to y coordinates. This step is
O(nLogn). It can be optimized to O(n) by recursively sorting and merging.
Step 6: Find the smallest distance in strip[]. This is tricky. From first look,
it seems to be a O(n^2) step, but it is actually O(n). It can be proved
geometrically that for every point in strip, we only need to check at most 7
points after it (note that strip is sorted according to Y coordinate).
See this for more analysis.
Step 7: Finally return the minimum of d and distance calculated in above
step (step 6)
3.3 Program:
The algorithm has been implemented using JAVA
programming language.
File: ClosestPair.java
import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.Arrays;
import java.util.Scanner;
21 | P a g e
public class ClosestPair {
// Each row in points represents a point
private double[][] points;
Point p1, p2;
public static void main(String[] args) {
int number=0;
Scanner scan = new Scanner( System.in );
System.out.println( "Enter number of points: " );
number = scan.nextInt();
double[][] points = new double[number][2];
for (int i = 0; i < number; i++) {
System.out.println( "Enter the x-coordinate for point "
+ ( i + 1 ) );
points[i][0] = scan.nextInt();
System.out.println( "Enter the y-coordinate for point "
+ ( i + 1 ) );
points[i][1] = scan.nextInt();
}
ClosestPair closestPair = new ClosestPair(points);
System.out.println("shortest distance is " +
closestPair.getMinimumDistance());
System.out.print("(" + closestPair.p1.x + ", " +
closestPair.p1.y + ") to ");
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];
22 | P a g e
for (int i = 0; i < pointsOrderedOnX.length; i++)
pointsOrderedOnX[i] = new Point(points[i][0], points[i]
[1]);
Arrays.sort(pointsOrderedOnX);
// Locate the identical points if exists
if (checkIdentical(pointsOrderedOnX))
return 0; // The distance between the identical points is
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) // Zero or one point in the set
return Double.MAX_VALUE;
else if (low + 1 == high) {
// Two points in the set
p1 = pointsOrderedOnX[low];
p2 = pointsOrderedOnX[high];
23 | P a g e
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];
}
// Recursively find the distance of the closest pair in the
left
// half and the right half
double d1 = distance(
pointsOrderedOnX, low, mid, pointsOrderedOnYL);
double d2 = distance(
pointsOrderedOnX, mid + 1, high, pointsOrderedOnYR);
double d = Math.min(d1, d2);
// stripL: the points in pointsOrderedOnYL within the strip
d
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];
24 | P a g e
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];
// Find the closest pair for a point in stripL and
// a point in stripR
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; // Start from r1 up in stripR
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) {
25 | P a g e
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) {
// Secondary order on y-coordinates
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) {
// Secondary order on x-coordinates
if (p1.x < p2.x)
return -1;
else if (p1.x == p2.x)
26 | P a g e
return 0;
else
return 1;
}
else
return 1;
}
}
}
3.4 Output:
27 | P a g e
28 | P a g e
Assignment 4
Fn = Fn-1 + Fn-2
Fibonacci iterative algorithm:
Procedure Fibonacci(n)
declare f0, f1, fib, loop
set f0 to 0
set f1 to 1
display f0, f1
for loop 1 to n
fib f0 + f1
f0 f1
f1 fib
display fib
29 | P a g e
end for
end procedure
Any whole number which is greater than 1 and has only
two factors that is 1 and the number itself, is called a
prime number. Other than these two number it has no
positive divisor.
Prime number testing algorithm:
START
Step 1 Take integer variable A
Step 2 Divide the variable A with (A-1 to 2)
Step 3 If A is divisible by any value (A-1 to 2) it is not
prime
Step 4 Else it is prime
STOP
Algorithm for finding prime fibonacci:
Procedure PrimeFibonacci(n)
declare f0, f1, fib, loop
set f0 to 0
set f1 to 1
display f0, f1
for loop 1 to n
fib f0 + f1
f0 f1
f1 fib
if fib is prime then
display fib
end for
end procedure
30 | P a g e
4.3 Program:
The algorithm has been implemented using JAVA
programming language.
File: PrimeFibo.java
import java.util.Scanner;
public class PrimeFibo
{
static int n1=0,n2=1,n3=0;
static void printFibonacci(long number)
{
long a = -1;
long b = 1;
long c = 0;
for(long i=1; i<=number; i++)
{
c = a + b;
long temp = a;
a = b;
b = c;
long t=isPrime(c);
if(t==0 && (c != 0 && c != 1))
System.out.println("T("+i+") :"+c+" is
prime");
else
System.out.println("T("+i+") :"+c);
}
}
static long isPrime(long n)
{
long i,m=0,flag=0;
31 | P a g e
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
return flag;
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the uppermost Nth term : ");
long limit = Integer.parseInt(scan.nextLine());
System.out.println("The Fibonacci Series are : ");
printFibonacci(limit);
}
}
32 | P a g e
4.4 Output:
33 | P a g e
34 | P a g e
35 | P a g e
36 | P a g e