miku
This commit is contained in:
@@ -0,0 +1,57 @@
|
|||||||
|
import java.util.*;
|
||||||
|
public class Nqueens{
|
||||||
|
static int n;
|
||||||
|
static int[][] board;
|
||||||
|
|
||||||
|
static boolean isSafe(int r, int c){
|
||||||
|
for(int i=0;i<r;i++){
|
||||||
|
if(board[i][c]==1)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(int i=r,j=c;i>=0 && j>=0;i--,j--){
|
||||||
|
if(board[i][j]==1)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(int i=r,j=c;i>=0 && j<n;i--,j++){
|
||||||
|
if(board[i][j]==1)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void solve(int r){
|
||||||
|
if(r==n){
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
for(int j=0;j<n;j++){
|
||||||
|
if(board[i][j]==1){
|
||||||
|
System.out.print(" Q ");
|
||||||
|
}else
|
||||||
|
System.out.print(" . ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
if(isSafe(r,i)){
|
||||||
|
board[r][i] =1;
|
||||||
|
solve(r+1);
|
||||||
|
board[r][i]=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc= new Scanner(System.in);
|
||||||
|
System.out.print("\nEnter n: ");
|
||||||
|
n =sc.nextInt();
|
||||||
|
board = new int[n][n];
|
||||||
|
solve(0);
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
import java.util.*;
|
||||||
|
public class bellmanFord{
|
||||||
|
static class Edge{
|
||||||
|
int src, dest, weigh;
|
||||||
|
Edge(int s, int d, int w){
|
||||||
|
src = s;
|
||||||
|
dest = d;
|
||||||
|
weigh = w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void printPath(int vertex, int[] parent){
|
||||||
|
if(vertex==-1) return;
|
||||||
|
printPath(parent[vertex], parent);
|
||||||
|
System.out.print(vertex+" ");
|
||||||
|
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.print("Enter no. of vertices and edges: ");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
int e = sc.nextInt();
|
||||||
|
int[] parent = new int[n];
|
||||||
|
Edge[] edges = new Edge[e];
|
||||||
|
int[] dist = new int[n];
|
||||||
|
|
||||||
|
System.out.print("Enter the edges(src dest weight): ");
|
||||||
|
for(int i=0;i<e;i++){
|
||||||
|
int s = sc.nextInt();
|
||||||
|
int d = sc.nextInt();
|
||||||
|
int w = sc.nextInt();
|
||||||
|
edges[i] = new Edge(s,d,w);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nEnter source vertex: ");
|
||||||
|
int s = sc.nextInt();
|
||||||
|
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
dist[i] = 999;
|
||||||
|
parent[i] = -1;
|
||||||
|
}
|
||||||
|
dist[s] = 0;
|
||||||
|
|
||||||
|
for(int i=1;i<=n-1;i++){
|
||||||
|
for(int j=0;j<e;j++){
|
||||||
|
int sr = edges[j].src;
|
||||||
|
int d = edges[j].dest;
|
||||||
|
int w = edges[j].weigh;
|
||||||
|
if(dist[sr]!=999 && dist[sr] + w < dist[d]){
|
||||||
|
dist[d] = dist[sr]+w;
|
||||||
|
parent[d] = sr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boolean negcycle = false;
|
||||||
|
|
||||||
|
for(int j=0;j<e;j++){
|
||||||
|
int sr = edges[j].src;
|
||||||
|
int d = edges[j].dest;
|
||||||
|
int w = edges[j].weigh;
|
||||||
|
if(dist[sr]!=999 && dist[sr] + w < dist[d]){
|
||||||
|
dist[d] = dist[sr]+w;
|
||||||
|
negcycle = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(negcycle){
|
||||||
|
System.out.println("\nNegative cycle exists.");
|
||||||
|
|
||||||
|
}else{
|
||||||
|
System.out.println("\nShortest paths from source vertex " + s + ":\n");
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
System.out.println("vertex: "+i);
|
||||||
|
System.out.println("Cost: "+ dist[i]);
|
||||||
|
System.out.println("Path: ");
|
||||||
|
printPath(i, parent);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
import java.util.*;
|
||||||
|
public class dijkstra{
|
||||||
|
static int V;
|
||||||
|
static int minDistance(int[] dist, boolean[] visited){
|
||||||
|
int min = Integer.MAX_VALUE;
|
||||||
|
int minIndex = -1;
|
||||||
|
for(int v=0;v<V;v++){
|
||||||
|
if(!visited[v] && dist[v]<min){
|
||||||
|
min = dist[v];
|
||||||
|
minIndex = v;;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return minIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void printPath(int vertex, int[] parent){
|
||||||
|
if(vertex==-1) return;
|
||||||
|
printPath(parent[vertex], parent);
|
||||||
|
System.out.print(vertex+" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dijkstra(int[][] graph, int source){
|
||||||
|
int[] dist = new int[V];
|
||||||
|
boolean[] visited = new boolean[V];
|
||||||
|
int[] parent = new int[V];
|
||||||
|
|
||||||
|
Arrays.fill(dist, Integer.MAX_VALUE);
|
||||||
|
for(int i=0;i<V;i++)
|
||||||
|
parent[i] = -1;
|
||||||
|
dist[source] = 0;
|
||||||
|
long start = System.nanoTime();
|
||||||
|
for(int count=0;count<V-1;count++){
|
||||||
|
int u = minDistance(dist, visited);
|
||||||
|
visited[u] = true;
|
||||||
|
for(int v=0;v<V;v++){
|
||||||
|
if(!visited[v] && graph[u][v]!=0 && dist[u]!=Integer.MAX_VALUE && dist[u]+graph[u][v]<dist[v]){
|
||||||
|
dist[v] = dist[u]+graph[u][v];
|
||||||
|
parent[v] = u;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
long end = System.nanoTime();
|
||||||
|
|
||||||
|
System.out.println("\nSHortest distances from source "+source+":");
|
||||||
|
for(int i =0;i<V;i++){
|
||||||
|
System.out.println("Vetex: "+i);
|
||||||
|
System.out.println("Cost: "+dist[i]);
|
||||||
|
System.out.println("Path: ");
|
||||||
|
printPath(i, parent);
|
||||||
|
System.out.println("\n");
|
||||||
|
}
|
||||||
|
long executionTime = end - start;
|
||||||
|
|
||||||
|
System.out.println("Execution Time: " + executionTime + " nanoseconds");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("\nENter number of vertices: ");
|
||||||
|
V = sc.nextInt();
|
||||||
|
int[][] graph = new int[V][V];
|
||||||
|
System.out.println("ENter adjacency matrix: ");
|
||||||
|
for(int i=0;i<V;i++){
|
||||||
|
for(int j =0;j<V;j++){
|
||||||
|
graph[i][j] =sc.nextInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int source = 0;
|
||||||
|
dijkstra(graph, source);
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Binary file not shown.
@@ -0,0 +1,38 @@
|
|||||||
|
import java.util.*;
|
||||||
|
public class subsetSum{
|
||||||
|
static boolean found = false;
|
||||||
|
static int[] arr;
|
||||||
|
static int target;
|
||||||
|
static ArrayList<Integer> current= new ArrayList<>();
|
||||||
|
|
||||||
|
static void subset(int index, int sum, ArrayList<Integer> current){
|
||||||
|
if(sum==target){
|
||||||
|
found = true;
|
||||||
|
System.out.println("\nSubset found: "+current);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(arr.length == index || sum>target){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
current.add(arr[index]);
|
||||||
|
subset(index+1, sum+arr[index], current);
|
||||||
|
current.remove(current.size()-1);
|
||||||
|
subset(index+1, sum, current);
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc= new Scanner(System.in);
|
||||||
|
System.out.println("\nEnter size of array: ");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
arr = new int[n];
|
||||||
|
System.out.println("\nENter array elements: ");
|
||||||
|
for(int i=0;i<n;i++)
|
||||||
|
arr[i] = sc.nextInt();
|
||||||
|
System.out.print("Enter target: ");
|
||||||
|
target = sc.nextInt();
|
||||||
|
subset(0,0,new ArrayList<Integer>());
|
||||||
|
if(!found){
|
||||||
|
System.out.println("\nNo subset found");
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user