giggity
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
import java.util.*;
|
||||||
Binary file not shown.
@@ -0,0 +1,118 @@
|
|||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class galeShapley {
|
||||||
|
|
||||||
|
static int N;
|
||||||
|
|
||||||
|
static int womanCharToIndex(char ch) {
|
||||||
|
return ch - 'W';
|
||||||
|
}
|
||||||
|
|
||||||
|
static int manCharToIndex(char ch) {
|
||||||
|
return ch - 'A';
|
||||||
|
}
|
||||||
|
|
||||||
|
static boolean prefers(int[][] womenPref, int w, int m, int m1) {
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
if (womenPref[w][i] == m)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (womenPref[w][i] == m1)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void galeShapley(int[][] womenPref, int[][] menPref) {
|
||||||
|
|
||||||
|
int[] womenPartner = new int[N];
|
||||||
|
boolean[] menFree = new boolean[N];
|
||||||
|
int[] nextProposal = new int[N];
|
||||||
|
|
||||||
|
Arrays.fill(womenPartner, -1);
|
||||||
|
Arrays.fill(menFree, true);
|
||||||
|
|
||||||
|
int freeCount = N;
|
||||||
|
|
||||||
|
while (freeCount > 0) {
|
||||||
|
|
||||||
|
int m = -1;
|
||||||
|
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
if (menFree[i] && nextProposal[i] < N) {
|
||||||
|
m = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m == -1)
|
||||||
|
break;
|
||||||
|
|
||||||
|
int w = menPref[m][nextProposal[m]];
|
||||||
|
nextProposal[m]++;
|
||||||
|
|
||||||
|
if (womenPartner[w] == -1) {
|
||||||
|
womenPartner[w] = m;
|
||||||
|
menFree[m] = false;
|
||||||
|
freeCount--;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
int m1 = womenPartner[w];
|
||||||
|
|
||||||
|
if (prefers(womenPref, w, m, m1)) {
|
||||||
|
womenPartner[w] = m;
|
||||||
|
menFree[m] = false;
|
||||||
|
menFree[m1] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nStable matching result:");
|
||||||
|
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
System.out.println(
|
||||||
|
"Woman " + (char)('W' + i) +
|
||||||
|
" is matched with Man " + (char)('A' + womenPartner[i])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.print("Enter number of pairs (n): ");
|
||||||
|
N = sc.nextInt();
|
||||||
|
|
||||||
|
int[][] menPref = new int[N][N];
|
||||||
|
int[][] womenPref = new int[N][N];
|
||||||
|
|
||||||
|
System.out.println("\nEnter Men's Preference Lists:");
|
||||||
|
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
|
||||||
|
System.out.println("Preferences of Man " + (char)('A' + i) + ":");
|
||||||
|
|
||||||
|
for (int j = 0; j < N; j++) {
|
||||||
|
char ch = sc.next().charAt(0);
|
||||||
|
menPref[i][j] = womanCharToIndex(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nEnter Women's Preference Lists:");
|
||||||
|
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
|
||||||
|
System.out.println("Preferences of Woman " + (char)('W' + i) + ":");
|
||||||
|
|
||||||
|
for (int j = 0; j < N; j++) {
|
||||||
|
char ch = sc.next().charAt(0);
|
||||||
|
womenPref[i][j] = manCharToIndex(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
galeShapley(womenPref, menPref);
|
||||||
|
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
@@ -0,0 +1,50 @@
|
|||||||
|
import java.util.*;
|
||||||
|
public class horsePool{
|
||||||
|
static HashMap<Character, Integer> shift = new HashMap<>();
|
||||||
|
static void shiftTable(String pattern){
|
||||||
|
for(char ch = 'A'; ch<='Z';ch++)
|
||||||
|
shift.put(ch, pattern.length());
|
||||||
|
shift.put(' ', pattern.length());
|
||||||
|
for(int i =0;i<pattern.length()-1;i++)
|
||||||
|
shift.put(pattern.charAt(i), pattern.length()-1-i);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int search(String text, String pattern){
|
||||||
|
int n = text.length();
|
||||||
|
int m = pattern.length();
|
||||||
|
|
||||||
|
int i = m-1;
|
||||||
|
while(i<n){
|
||||||
|
int k = 0;
|
||||||
|
while(k<m && pattern.charAt(m-1-k)== text.charAt(i-k)){
|
||||||
|
k++;
|
||||||
|
|
||||||
|
}
|
||||||
|
if(k==m){
|
||||||
|
return i-m+1;
|
||||||
|
}
|
||||||
|
i+=shift.getOrDefault(text.charAt(i), m);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.print("\nEnter text: ");
|
||||||
|
String text = sc.nextLine().toUpperCase();
|
||||||
|
|
||||||
|
System.out.print("\nENter pattern: ");
|
||||||
|
String pattern = sc.nextLine().toUpperCase();
|
||||||
|
|
||||||
|
shiftTable(pattern);
|
||||||
|
int pos = search(text, pattern);
|
||||||
|
if(pos!=-1){
|
||||||
|
System.out.println("Pattern found at index "+ pos);
|
||||||
|
|
||||||
|
}else{
|
||||||
|
System.out.println("Pattern not found");
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,81 @@
|
|||||||
|
import java.util.*;
|
||||||
|
public class kruskalMST{
|
||||||
|
static class Edge implements Comparable<Edge>{
|
||||||
|
int u,v,weight;
|
||||||
|
Edge(int u, int v, int weight){
|
||||||
|
this.u=u;
|
||||||
|
this.v = v;
|
||||||
|
this.weight = weight;
|
||||||
|
|
||||||
|
}
|
||||||
|
public int compareTo(Edge other){
|
||||||
|
return this.weight-other.weight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static int[] parent;
|
||||||
|
|
||||||
|
static int find(int x){
|
||||||
|
if(parent[x]!=x)
|
||||||
|
parent[x] = find(parent[x]);
|
||||||
|
return parent[x];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void union(int x, int y){
|
||||||
|
int rootX = find(x);
|
||||||
|
int rootY = find(y);
|
||||||
|
|
||||||
|
parent[rootY]= rootX;
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.print("ENter number of vertices: ");
|
||||||
|
int N = sc.nextInt();
|
||||||
|
|
||||||
|
int[][] graph = new int[N][N];
|
||||||
|
|
||||||
|
System.out.println("\nEnter adjacency matrix(0 if no edge): ");
|
||||||
|
for(int i =0;i<N;i++){
|
||||||
|
for(int j =0;j<N;j++){
|
||||||
|
graph[i][j] = sc.nextInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
List<Edge> edges = new ArrayList<>();
|
||||||
|
for(int i =0;i<N;i++){
|
||||||
|
for(int j =i+1;j<N;j++){
|
||||||
|
if(graph[i][j]!=0)
|
||||||
|
edges.add(new Edge(i,j,graph[i][j]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Collections.sort(edges);
|
||||||
|
|
||||||
|
parent = new int[N];
|
||||||
|
for(int i = 0;i<N;i++){
|
||||||
|
parent[i]= i;
|
||||||
|
|
||||||
|
}
|
||||||
|
int totalCost = 0;
|
||||||
|
int edgeCount = 0;
|
||||||
|
|
||||||
|
System.out.println("\nEdges in MST:");
|
||||||
|
for(Edge e : edges){
|
||||||
|
int rootU = find(e.u);
|
||||||
|
int rootV = find(e.v);
|
||||||
|
if(rootU!=rootV){
|
||||||
|
System.out.println(e.u+" - "+e.v+" : "+e.weight);
|
||||||
|
totalCost+=e.weight;
|
||||||
|
union(rootU,rootV);
|
||||||
|
edgeCount++;
|
||||||
|
|
||||||
|
if(edgeCount == N-1)
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.print("\ntotal minimum cost: "+totalCost);
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
@@ -0,0 +1,53 @@
|
|||||||
|
import java.util.*;
|
||||||
|
public class mergeSort{
|
||||||
|
static void merge(int arr[], int left, int mid, int right, int temp[]){
|
||||||
|
int i = left;
|
||||||
|
int j = mid+1;
|
||||||
|
int k = left;
|
||||||
|
|
||||||
|
while(i<=mid && j<=right){
|
||||||
|
if(arr[i]<=arr[j]){
|
||||||
|
temp[k++] = arr[i++];
|
||||||
|
}else{
|
||||||
|
temp[k++] = arr[j++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while(i<=mid)
|
||||||
|
temp[k++] = arr[i++];
|
||||||
|
while(j<=right)
|
||||||
|
temp[k++] = arr[j++];
|
||||||
|
|
||||||
|
for(int x = left;x<=right;x++){
|
||||||
|
arr[x] = temp[x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void mergeSort(int arr[], int left, int right, int temp[]){
|
||||||
|
if(left<right){
|
||||||
|
int mid = (right+left)/2;
|
||||||
|
mergeSort(arr, left, mid, temp);
|
||||||
|
mergeSort(arr, mid+1, right, temp);
|
||||||
|
merge(arr, left, mid, right, temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.print("Enter number of elements: ");
|
||||||
|
int n= sc.nextInt();
|
||||||
|
|
||||||
|
int arr[] = new int[n];
|
||||||
|
int temp[] = new int[n];
|
||||||
|
|
||||||
|
System.out.println("Enter "+n+" elements: ");
|
||||||
|
for(int i =0;i<n;i++){
|
||||||
|
arr[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
mergeSort(arr,0,n-1,temp);
|
||||||
|
System.out.println("\nSorted array:");
|
||||||
|
for(int i =0;i<n;i++){
|
||||||
|
System.out.print(arr[i]+ " ");
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
@@ -0,0 +1,58 @@
|
|||||||
|
import java.util.*;
|
||||||
|
public class primMST{
|
||||||
|
static int N;
|
||||||
|
static int[][] graph;
|
||||||
|
static void primMST(){
|
||||||
|
int[] key = new int[N];
|
||||||
|
boolean[] mstSet = new boolean[N];
|
||||||
|
int[] parent = new int[N];
|
||||||
|
|
||||||
|
Arrays.fill(key, Integer.MAX_VALUE);
|
||||||
|
key[0] = 0;
|
||||||
|
parent[0]=-1;
|
||||||
|
for(int count =0;count<N-1;count++){
|
||||||
|
int u =-1;
|
||||||
|
int min = Integer.MAX_VALUE;
|
||||||
|
for(int i =0;i<N;i++){
|
||||||
|
if(!mstSet[i] && key[i]<min){
|
||||||
|
min = key[i];
|
||||||
|
u = i;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mstSet[u] = true;
|
||||||
|
for(int v = 0;v<N;v++){
|
||||||
|
if(!mstSet[v] && graph[u][v]!=0 && graph[u][v]<key[v]){
|
||||||
|
key[v] = graph[u][v];
|
||||||
|
parent[v] = u;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
int totalCost = 0;
|
||||||
|
System.out.println("\nEdges in MST:");
|
||||||
|
for(int i =1;i<N;i++){
|
||||||
|
System.out.println(parent[i]+" - "+i+" : "+graph[i][parent[i]]);
|
||||||
|
totalCost+=graph[i][parent[i]];
|
||||||
|
}
|
||||||
|
System.out.println("Total minimum cost: "+totalCost);
|
||||||
|
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.print("ENter number of vertices:");
|
||||||
|
N = sc.nextInt();
|
||||||
|
|
||||||
|
graph= new int[N][N];
|
||||||
|
|
||||||
|
System.out.println("ENter adjacency matrix(0 if no edges): ");
|
||||||
|
for(int i =0;i<N;i++){
|
||||||
|
for(int j=0;j<N;j++){
|
||||||
|
graph[i][j] = sc.nextInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
primMST();
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
@@ -0,0 +1,57 @@
|
|||||||
|
import java.util.*;
|
||||||
|
public class quickSort{
|
||||||
|
static int partition(int[] arr, int low, int high){
|
||||||
|
int pivot = arr[low];
|
||||||
|
int i = low+1;
|
||||||
|
int j = high;
|
||||||
|
while(i<=j){
|
||||||
|
while(i<=high && arr[i]<=pivot)
|
||||||
|
i++;
|
||||||
|
while(arr[j]>pivot)
|
||||||
|
j--;
|
||||||
|
if(i<j){
|
||||||
|
int temp=arr[i];
|
||||||
|
arr[i]=arr[j];
|
||||||
|
arr[j]=temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int temp = arr[j];
|
||||||
|
arr[j]=arr[low];
|
||||||
|
arr[low] = temp;
|
||||||
|
|
||||||
|
return j ;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void quickSort(int[] arr, int low, int high){
|
||||||
|
if(low<high){
|
||||||
|
int pi = partition(arr, low,high);
|
||||||
|
quickSort(arr, low, pi-1);
|
||||||
|
quickSort(arr, pi+1, high);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.print("\nENter number of products: ");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
|
||||||
|
int[] prices = new int[n];
|
||||||
|
System.out.println("ENter product prices: ");
|
||||||
|
for(int i =0;i<n;i++){
|
||||||
|
prices[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
long startTime = System.nanoTime();
|
||||||
|
quickSort(prices, 0 ,n-1);
|
||||||
|
|
||||||
|
long endTime = System.nanoTime();
|
||||||
|
|
||||||
|
System.out.println("\nSOrted prices list: ");
|
||||||
|
for(int i =0;i<n;i++){
|
||||||
|
System.out.print(prices[i]+ " ");
|
||||||
|
|
||||||
|
}
|
||||||
|
long execTime = endTime - startTime;
|
||||||
|
System.out.println("\nExecution time: "+ execTime+" nanoseconds");
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
@@ -0,0 +1,53 @@
|
|||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class topologicalSort{
|
||||||
|
static int N;
|
||||||
|
static int[][] graph;
|
||||||
|
static boolean[] visited;
|
||||||
|
static Stack<Integer> stack = new Stack<>();
|
||||||
|
|
||||||
|
static void dfs(int v){
|
||||||
|
visited[v] = true;
|
||||||
|
for(int i =0;i<N;i++){
|
||||||
|
if(!visited[i] && graph[v][i]==1){
|
||||||
|
dfs(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stack.push(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void topoSort(){
|
||||||
|
for(int i=0;i<N;i++){
|
||||||
|
if(!visited[i]){
|
||||||
|
dfs(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nValid execution order");
|
||||||
|
while(!stack.isEmpty()){
|
||||||
|
System.out.print((char)('A'+ stack.pop())+"->");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.print("\nEnter number of tasks: ");
|
||||||
|
N = sc.nextInt();
|
||||||
|
graph = new int[N][N];
|
||||||
|
visited = new boolean[N];
|
||||||
|
|
||||||
|
System.out.print("ENter number of dependencies: ");
|
||||||
|
int edges = sc.nextInt();
|
||||||
|
|
||||||
|
System.out.println("Enter dependencies (u v means u -> v):");
|
||||||
|
for(int i = 0;i<edges;i++){
|
||||||
|
char u = sc.next().charAt(0);
|
||||||
|
char v = sc.next().charAt(0);
|
||||||
|
|
||||||
|
graph[u-'A'][v-'A'] = 1;
|
||||||
|
}
|
||||||
|
topoSort();
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user