You are on page 1of 13

JOB SEQUENCING

import java.util.*;

import java.util.Scanner;

class Main {

public static void main(String[] args) {

int max = 100;

Scanner scn = new Scanner(System.in);

System.out.print("Enter number of jobs -->");

int job_count = scn.nextInt();

char[] j = new char[job_count];

int[] p = new int[job_count];

int[] d = new int[job_count];

System.out.print("Enter job details:\n\n");

for (int i = 0; i < job_count; i++) {

System.out.print("Job name: ");

j[i] = scn.next().charAt(0);

System.out.print("Job profit: ");

p[i] = scn.nextInt();

System.out.print("Job deadline: ");

d[i] = scn.nextInt();

System.out.print("\n--------------\n");

max = d[0];
for (int i = 1; i < job_count; i++) {

if (d[i] >= max)

max = d[i];

char[] seq = new char[max];

for (int i = 0; i < max; i++) {

seq[i] = '0';

int n = d.length;

for (int i = 0; i < n - 1; i++) {

for (int k = 0; k < n - i - 1; k++) {

if (p[k] > p[k + 1]) {

int temp1 = p[k];

p[k] = p[k + 1];

p[k + 1] = temp1;

int temp2 = d[k];

d[k] = d[k + 1];

d[k + 1] = temp2;

char temp3 = j[k];

j[k] = j[k + 1];

j[k + 1] = temp3;

}
int m;

int profit = 0;

//

for (int i = job_count - 1; i >= 0; i--) {

m = d[i];

while (m > 0) {

if (seq[m - 1] == '0') {

seq[m - 1] = j[i];

profit = profit + p[i];

break;

} else {

m--;

System.out.println("Profit is " + profit);

System.out.println("Sequence is ");

for (int i = 0; i < job_count; i++) {

if (seq[i] != '0') {

System.out.print(seq[i]);

DIJKSTRA

import java.util.*;

public class HelloWorld

{
public static void main(String[] args)

int graph[][] = new int[][] { { 0, 0, 1, 2, 0, 0, 0 }, { 0, 0, 2, 0, 0, 3, 0 }, { 1, 2, 0, 1, 3, 0, 0 },

{ 2, 0, 1, 0, 0, 0, 1 }, { 0, 0, 3, 0, 0, 2, 0 }, { 0, 3, 0, 0, 2, 0, 1 }, { 0, 0, 0, 1, 0, 1, 0 }};

int count=graph.length;

Scanner sc=new Scanner(System.in);

System.out.println("Enter value of source::");

int source=sc.nextInt();

boolean visited[]=new boolean[count];

int distance[]=new int[count];

// make all distance from source to all other node infinite at the beginning

// also mark all node as unvisited that is false;

for(int i=0;i<count;i++)

distance[i]=Integer.MAX_VALUE;

visited[i]=false;

// make distance of source as zero

distance[source]=0;

// traverse through all vertices

for(int i=0;i<count;i++)

int res=-1;

int min=Integer.MAX_VALUE;

// find minimum distance to vertex

for(int j=0;j<count;j++)

if(visited[j]==false && min>distance[j])

min=distance[j];

res=j;
}

// mark the minimum vertex as visited

visited[res]=true;

for(int k=0;k<count;k++)

if((visited[k]!=true) && (graph[res][k]!=0) && ((distance[res]+graph[res][k])<distance[k]))

distance[k]=distance[res]+graph[res][k];

for (int i = 0; i <count; i++)

System.out.println(String.format("Distance from %s to %s is %s", source, i, distance[i]));

FLOYD WARSHALL

import java.util.*;

class Main {

public static void main(String[] args) {

Scanner scn = new Scanner(System.in);

int n = 4;

int max = 1000;

int A[][] = new int[5][5];

for (int i = 1; i <= n; i++) {


for (int j = 1; j <= n; j++) {

A[i][j] = scn.nextInt();

for (int k = 1; k <= n; k++) {

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= n; j++) {

A[i][j] = Math.min(A[i][j], (A[i][k] + A[k][j]));

System.out.println("Intermediate node is " + k);

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= n; j++) {

System.out.print(A[i][j] + " ");

System.out.println();

System.out.println();

int min;

int p = 1;

int path_sum = 0;

System.out.println("Shortest path ");

for (int i = 1; i <= n; i++) {

if (A[i][1] == 0) {

min = A[i][2];
} else {

min = A[i][1];

for (int j = 1; j <= n; j++) {

if (A[i][j] <= min && A[i][j] != 0) {

min = A[i][j];

p = j;

path_sum = path_sum + A[i][p];

System.out.print(p + " ");

System.out.print("\nSum of shortest path is " + path_sum);

0 3 100 7

8 0 2 100

5 100 0 1

2 100 100 0

Knights TOUR

// Java program for Knight Tour problem

class Main {

static int N = 8;

static boolean isSafe(int x, int y, int sol[][]) {

return (x >= 0 && x < N && y >= 0 && y < N

&& sol[x][y] == -1);


}

static void printSolution(int sol[][]) {

for (int x = 0; x < N; x++) {

for (int y = 0; y < N; y++)

System.out.print(sol[x][y] + " ");

System.out.println();

static boolean solveKT() {

int sol[][] = new int[8][8];

for (int x = 0; x < N; x++)

for (int y = 0; y < N; y++)

sol[x][y] = -1;

int xMove[] = { 2, 1, -1, -2, -2, -1, 1, 2 };

int yMove[] = { 1, 2, 2, 1, -1, -2, -2, -1 };

sol[0][0] = 0;

if (!solveKTUtil(0, 0, 1, sol, xMove, yMove)) {

System.out.println("Solution does not exist");

return false;

} else

printSolution(sol);
return true;

static boolean solveKTUtil(int x, int y, int movei,

int sol[][], int xMove[],

int yMove[]) {

int k, next_x, next_y;

if (movei == N * N)

return true;

for (k = 0; k < 8; k++) {

next_x = x + xMove[k];

next_y = y + yMove[k];

if (isSafe(next_x, next_y, sol)) {

sol[next_x][next_y] = movei;

if (solveKTUtil(next_x, next_y, movei + 1,

sol, xMove, yMove))

return true;

else

sol[next_x][next_y] = -1;

return false;

public static void main(String args[]) {


solveKT();

STUDENTS CLUB

import java.util.*;

public class Main

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("\nEnter number of students : ");

int n = sc.nextInt();

int cost[][] = new int[n + 1][n + 1];

for (int i = 1; i <= n; i++) {

System.out.println();

for (int j = 1; j <= n; j++) {

System.out.printf("\nEnter cost for assigning student %d to club %d : ", i, j);

cost[i][j] = sc.nextInt();

int vis_count = 0, ans = -1;

int vis[] = new int[n + 1];

for (int i = 0; i <= n; i++)

vis[i] = -1;

while (vis_count < n) {

int lb = Integer.MAX_VALUE, club = -1, sum;

for (int i = 1; i <= n; i++) {


sum = 0;

if (vis[i] == -1) {

sum += cost[vis_count + 1][i];

for (int j = 1; j <= n; j++) {

if (i == j)

continue;

if (vis[j] != -1) {

sum += cost[vis[j]][j];

} else {

int mini = (int) 1e8;

for (int k = vis_count + 2; k <= n; k++) {

if (mini > cost[k][j])

mini = cost[k][j];

sum += mini;

if (lb > sum) {

lb = sum;

club = i;

vis_count++;

if (vis_count == n)

ans = lb;

vis[club] = vis_count;

System.out.print("\nAssignment of clubs(club number : assigned student) to students --->\n\n");

for (int i = 1; i <= n; i++) {


System.out.print(i + " : " + vis[i] + "\n");

System.out.printf("\nThe minimum cost of assigning %d students to %d clubs is : %d\n", n, n, ans);

SQUARE OF NUMBER

import java.util.*;
public class Square
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
long num = sc.nextLong();
long ans = squareofnum(num);
System.out.println("Square is " + ans);
}
public static long squareofnum(long num){
if(num < 10){
return num * num;
}
long temp = num;
long count = 0;
while(temp > 0){
count ++;
temp /= 10;
}
long half = count / 2;
long a = num / (long)Math.pow(10, half); //left part of number
long b = num % (long)Math.pow(10, half); //right part of number

long ll = squareofnum(a);
long rr = squareofnum(b);
long lr_rl = squareofnum(a + b) - ll - rr;

return (ll * (long)Math.pow(10, 2 * half) + lr_rl *


(long)Math.pow(10, half)+rr);
}
}

You might also like