Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
edc6f08
Merge pull request #22 from Kes104/Fibonacci
IamBisrutPyne Oct 6, 2025
071cfcc
added quicksort with a sample example
Madipadige-ManishKumar Oct 6, 2025
5371704
Update QuickSort.java
Madipadige-ManishKumar Oct 6, 2025
a5e897e
Update QuickSort.java
IamBisrutPyne Oct 6, 2025
365fe87
Merge pull request #24 from Madipadige-ManishKumar/main
IamBisrutPyne Oct 6, 2025
fa289f0
Create KMPAlgorithm.java
coddingjatin Oct 6, 2025
65568d4
Merge pull request #1 from coddingjatin/coddingjatin-KMPAlgorithm
coddingjatin Oct 6, 2025
4479308
Updated KMPAlgorithm.java
coddingjatin Oct 6, 2025
f95a80c
Merge pull request #25 from coddingjatin/main
IamBisrutPyne Oct 6, 2025
2a7c51a
Create TarjanSCC.java
coddingjatin Oct 7, 2025
ca1607e
prim's algo added
chaanakyaaM Oct 7, 2025
5ce1c21
Update and rename Prim.java to PrimsAlgorithm.java
IamBisrutPyne Oct 7, 2025
1a23b2c
Merge pull request #35 from chaanakyaaM/prim
IamBisrutPyne Oct 7, 2025
9889f99
Merge pull request #34 from coddingjatin/main
IamBisrutPyne Oct 7, 2025
ba7d457
feat: Added Shape Program for Hacktoberfest2025
Ashwil-Colaco Oct 7, 2025
8d5f350
Added N-Queens Algorithm Solution using Backtracking in Java
Sreenivasulu-03 Oct 7, 2025
ed1b2e1
Merge pull request #38 from Sreenivasulu-03/feature/NQueens
IamBisrutPyne Oct 7, 2025
64e20ec
Merge pull request #37 from Ashwil-Colaco/feat/oop-polymorphism-inher…
IamBisrutPyne Oct 7, 2025
7d4892f
bellman ford algo added
chaanakyaaM Oct 8, 2025
bb16c06
feat: add Queue implementation using custom singly linked list
Ahad-23 Oct 8, 2025
99658fb
Update BellmanFord.java
IamBisrutPyne Oct 8, 2025
bf2efa7
Merge pull request #45 from chaanakyaaM/bell
IamBisrutPyne Oct 8, 2025
1d20bca
Update Queue.java
IamBisrutPyne Oct 8, 2025
7ab6e1a
Merge pull request #46 from Ahad-23/feature/queue-datastructure
IamBisrutPyne Oct 8, 2025
e3e6463
SudokuSolver.java
Madipadige-ManishKumar Oct 9, 2025
019c6f2
Update SudokuSolver.java
IamBisrutPyne Oct 9, 2025
70e1597
Merge pull request #52 from Madipadige-ManishKumar/sudokusolver
IamBisrutPyne Oct 9, 2025
f319356
Create LinkedList.java
subham500071430 Oct 9, 2025
727c606
added appropriate exception
subham500071430 Oct 9, 2025
be73b42
Create UnionFind.java
KotlinKing Oct 9, 2025
1577ec1
Rock Paper Scissors Game Java Code
VinayKumarBM Oct 10, 2025
84dcf3e
added AVL tree
Madipadige-ManishKumar Oct 10, 2025
b3f174f
Merge pull request #53 from subham500071430/patch-1
IamBisrutPyne Oct 10, 2025
6f8fce1
Merge pull request #54 from subham500071430/patch-2
IamBisrutPyne Oct 10, 2025
7a85cac
Update UnionFind.java
IamBisrutPyne Oct 10, 2025
ad0f3e8
Merge pull request #55 from KotlinKing/patch-1
IamBisrutPyne Oct 10, 2025
7ae6f55
Update RockPaperScissors.java
IamBisrutPyne Oct 10, 2025
979536f
Merge pull request #56 from VinayKumarBM/main
IamBisrutPyne Oct 10, 2025
7cd77d3
Update AVLTree.java
IamBisrutPyne Oct 10, 2025
fe16655
Merge pull request #57 from Madipadige-ManishKumar/main
IamBisrutPyne Oct 10, 2025
9026744
Added GradeCalculator.java: implemented Java console app for score-to…
Oct 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions Algorithms/BellmanFord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Program Title: Bellman-Ford Shortest Path Algorithm
* Author: [chaanakyaaM]
* Date: 2025-10-08
*
* Description: Computes the shortest path from a single source vertex to all other
* vertices in a weighted, directed graph. It handles graphs with negative edge
* weights but is crucial for detecting and reporting negative cycles.
*
* Time Complexity: O(V * E) (Vertices * Edges)
* Space Complexity: O(V + E) (for the distance array and edge list)
*/

import java.util.Arrays;

public class BellmanFord {

// Structure to represent a weighted edge
static class Edge {
int source, destination, weight;

Edge(int source, int destination, int weight) {
this.source = source;
this.destination = destination;
this.weight = weight;
}
}

// Function that implements the Bellman-Ford algorithm
public static void bellmanFord(Edge[] edges, int V, int E, int source) {
// Initialize distances from source to all vertices as INFINITE
int[] distance = new int[V];
Arrays.fill(distance, Integer.MAX_VALUE);
distance[source] = 0;

// Relax all edges |V| - 1 times
for (int i = 1; i < V; i++) {
for (int j = 0; j < E; j++) {
int u = edges[j].source;
int v = edges[j].destination;
int weight = edges[j].weight;

if (distance[u] != Integer.MAX_VALUE && distance[u] + weight < distance[v]) {
distance[v] = distance[u] + weight;
}
}
}

// Check for negative-weight cycles
for (int i = 0; i < E; i++) {
int u = edges[i].source;
int v = edges[i].destination;
int weight = edges[i].weight;

if (distance[u] != Integer.MAX_VALUE && distance[u] + weight < distance[v]) {
System.out.println("Graph contains negative weight cycle");
return;
}
}

// Print the distances
printSolution(distance);
}

// Utility function to print the distance array
public static void printSolution(int[] distance) {
System.out.println("Vertex Distance from Source");
for (int i = 0; i < distance.length; i++) {
if (distance[i] == Integer.MAX_VALUE) {
System.out.println(i + " INF");
} else {
System.out.println(i + " " + distance[i]);
}
}
}

public static void main(String[] args) {
// Number of vertices and edges
int V = 5; // Number of vertices
int E = 8; // Number of edges

// List of edges (source, destination, weight)
Edge[] edges = new Edge[E];
edges[0] = new Edge(0, 1, -1);
edges[1] = new Edge(0, 2, 4);
edges[2] = new Edge(1, 2, 3);
edges[3] = new Edge(1, 3, 2);
edges[4] = new Edge(1, 4, 2);
edges[5] = new Edge(3, 2, 5);
edges[6] = new Edge(3, 1, 1);
edges[7] = new Edge(4, 3, -3);

// Source vertex
int source = 0;

// Run Bellman-Ford algorithm
bellmanFord(edges, V, E, source);
}
}
95 changes: 95 additions & 0 deletions Algorithms/KMPAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// KMP Algorithm in Java
// ------------------------------------------------------
// Author: Jatin Vishwakarma
// Description: Implementation of the Knuth–Morris–Pratt (KMP)
// Pattern Searching Algorithm in Java.
// This algorithm efficiently finds all occurrences of a pattern
// in a given text with O(n + m) time complexity.

import java.util.Scanner;

public class KMPAlgorithm {

// Function to preprocess the pattern and build the LPS (Longest Prefix Suffix) array
private static void computeLPSArray(String pattern, int[] lps) {
int length = 0; // length of the previous longest prefix suffix
int i = 1;
lps[0] = 0; // LPS of the first character is always 0

// Loop to calculate lps[i] for i = 1 to pattern.length - 1
while (i < pattern.length()) {
if (pattern.charAt(i) == pattern.charAt(length)) {
length++;
lps[i] = length;
i++;
} else {
if (length != 0) {
// This is tricky — consider the example "AAACAAAA"
length = lps[length - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}

// KMP Search Function
public static void KMPSearch(String pattern, String text) {
int M = pattern.length();
int N = text.length();

// Create LPS array
int[] lps = new int[M];
computeLPSArray(pattern, lps);

int i = 0; // index for text[]
int j = 0; // index for pattern[]

boolean found = false;

while (i < N) {
if (pattern.charAt(j) == text.charAt(i)) {
i++;
j++;
}

if (j == M) {
System.out.println("✅ Pattern found at index: " + (i - j));
j = lps[j - 1];
found = true;
}

// Mismatch after j matches
else if (i < N && pattern.charAt(j) != text.charAt(i)) {
// Do not match lps[0..lps[j-1]] characters, they will match anyway
if (j != 0)
j = lps[j - 1];
else
i++;
}
}

if (!found)
System.out.println("❌ Pattern not found in the given text.");
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("=====================================");
System.out.println(" KMP STRING MATCHING ALGORITHM");
System.out.println("=====================================");
System.out.print("Enter the text: ");
String text = sc.nextLine();

System.out.print("Enter the pattern to search: ");
String pattern = sc.nextLine();

System.out.println("\n🔍 Searching for pattern...");
KMPSearch(pattern, text);

System.out.println("\n--- Program Completed Successfully ---");
sc.close();
}
}
116 changes: 116 additions & 0 deletions Algorithms/NQueens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import java.util.Scanner;

/**
* Program Title: N-Queens Solver
* Author: [Sreenivasulu-03]
* Date: 2025-10-07
*
* Description: This program solves the N-Queens problem using backtracking.
* It places N queens on an N×N chessboard so that no two queens attack each other.
* The program prints all possible solutions and demonstrates recursion, backtracking,
* and array manipulation in Java.
*
* Time Complexity: O(N!)
* Space Complexity: O(N^2) for the board representation
*/
public class NQueens {

/**
* Solves the N-Queens problem and prints all solutions.
* @param n The size of the chessboard and the number of queens.
*/
public static void solveNQueens(int n) {
char[][] board = new char[n][n];

// Initialize board with empty cells
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = '.';
}
}

backtrack(board, 0);
}

/**
* Recursively tries to place queens row by row using backtracking.
* @param board The current state of the chessboard.
* @param row The current row to place a queen.
*/
private static void backtrack(char[][] board, int row) {
int n = board.length;

if (row == n) {
printBoard(board);
return;
}

for (int col = 0; col < n; col++) {
if (isSafe(board, row, col)) {
board[row][col] = 'Q';
backtrack(board, row + 1);
board[row][col] = '.'; // Backtrack
}
}
}

/**
* Checks if placing a queen at (row, col) is safe.
* @param board The current board state.
* @param row Row index.
* @param col Column index.
* @return true if safe, false otherwise.
*/
private static boolean isSafe(char[][] board, int row, int col) {
int n = board.length;

// Check column
for (int i = 0; i < row; i++) {
if (board[i][col] == 'Q') return false;
}

// Check upper-left diagonal
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == 'Q') return false;
}

// Check upper-right diagonal
for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
if (board[i][j] == 'Q') return false;
}

return true;
}

/**
* Prints the current board configuration.
* @param board The chessboard to print.
*/
private static void printBoard(char[][] board) {
System.out.println("Solution:");
for (char[] row : board) {
for (char c : row) {
System.out.print(c + " ");
}
System.out.println();
}
System.out.println();
}

public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Enter the number of queens (N): ");

while (!sc.hasNextInt()) {
System.out.print("Invalid input! Enter a numeric value: ");
sc.next();
}
int n = sc.nextInt();

solveNQueens(n);

} catch (Exception e) {
System.err.println("An unexpected error occurred: " + e.getMessage());
}
}
}
75 changes: 75 additions & 0 deletions Algorithms/PrimsAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Program Title: Prim's Algorithm for Minimum Spanning Tree (MST)
* Author: [@chaanakyaaM]
* Date: [2025-10-07]
*
* Description: Finds the Minimum Spanning Tree (MST) of a connected, undirected
* weighted graph using a Priority Queue (Min-Heap) to efficiently select the
* next cheapest edge.
* Time Complexity: O(E log V) (where E is edges, V is vertices)
* Space Complexity: O(V + E)
*/

import java.util.*;

class Edge {
int to;
int weight;

Edge(int to, int weight) {
this.to = to;
this.weight = weight;
}
}

public class PrimsAlgorithm {
public static int prim(int n, List<List<Edge>> adj) {
boolean[] visited = new boolean[n];
PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight));
pq.offer(new Edge(0, 0)); // Start from node 0
int mstWeight = 0;

while (!pq.isEmpty()) {
Edge current = pq.poll();

if (visited[current.to]) continue;

visited[current.to] = true;
mstWeight += current.weight;

for (Edge neighbor : adj.get(current.to)) {
if (!visited[neighbor.to]) {
pq.offer(neighbor);
}
}
}

return mstWeight;
}

public static void main(String[] args) {
int n = 5; // number of nodes
List<List<Edge>> adj = new ArrayList<>();

for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}

// Adding undirected edges
addEdge(adj, 0, 1, 2);
addEdge(adj, 0, 3, 6);
addEdge(adj, 1, 2, 3);
addEdge(adj, 1, 3, 8);
addEdge(adj, 1, 4, 5);
addEdge(adj, 2, 4, 7);
addEdge(adj, 3, 4, 9);

int result = prim(n, adj);
System.out.println("Total weight of MST: " + result);
}

static void addEdge(List<List<Edge>> adj, int u, int v, int w) {
adj.get(u).add(new Edge(v, w));
adj.get(v).add(new Edge(u, w)); // Since the graph is undirected
}
}
Loading