commit 796dec1af723c352ef9eb9c5faf837e0ec58f980 Author: Wander_Lust Date: Mon May 18 20:03:32 2026 +0530 alr diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aad2d55 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.class +mergeSort.java +kruskal.java diff --git a/floydWarshall.java b/floydWarshall.java new file mode 100644 index 0000000..017d7d3 --- /dev/null +++ b/floydWarshall.java @@ -0,0 +1 @@ +import java.util.*; diff --git a/galeShapley.class b/galeShapley.class new file mode 100644 index 0000000..db0b8db Binary files /dev/null and b/galeShapley.class differ diff --git a/galeShapley.java b/galeShapley.java new file mode 100644 index 0000000..ddf6854 --- /dev/null +++ b/galeShapley.java @@ -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(); + } +} diff --git a/horsePool.class b/horsePool.class new file mode 100644 index 0000000..111de49 Binary files /dev/null and b/horsePool.class differ diff --git a/horsePool.java b/horsePool.java new file mode 100644 index 0000000..3981d8b --- /dev/null +++ b/horsePool.java @@ -0,0 +1,50 @@ +import java.util.*; +public class horsePool{ + static HashMap 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{ + 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 edges = new ArrayList<>(); + for(int i =0;ipivot) + j--; + if(i stack = new Stack<>(); + + static void dfs(int v){ + visited[v] = true; + for(int i =0;i"); + } + } + 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