Posts

Showing posts with the label Algorithms

Producer - Consumer Problem Java

Producer - Consumer Problem implementation: IntegerBuffer.java public class IntegerBuffer { private int index; private int[] buffer; private final int SIZE = 2; public IntegerBuffer() { this.buffer = new int[SIZE]; } public synchronized void add(int number) { while (index == buffer.length - 1) { try { wait(); System.out.println("IntegerBuffer.add() waiting"); } catch (InterruptedException e) { } } buffer[index++] = number; notifyAll(); } public synchronized int remove() { while (index == 0) { try { wait(); System.out.println("IntegerBuffer.remove() waiting"); } catch (InterruptedException e) { } } in...

Implemention of LRU Cache

Source: import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedDeque; /** * Class LRU Cache * @author Shashwat * @param <Key> * @param <Value> */ public class LRUCache<Key, Value> { private final int MAX_SIZE; private ConcurrentHashMap<Key, Value> map; private ConcurrentLinkedDeque<Key> queue; public LRUCache(final int maxSize) { this.MAX_SIZE = maxSize; this.map = new ConcurrentHashMap<Key, Value>(this.MAX_SIZE); this.queue = new ConcurrentLinkedDeque<Key>(); } public Value get(Key key) { return this.map.get(key); } public void put(final Key key, final Value value) { if (this.map.containsKey(key)) { this.queue.remove(key); } while (this.queue.size() >= this.MAX_SIZE) { Key o...

Print two dimensional array in spiral order

Source code: package com.in2soft.test; public class PrintSpiralTest { /** * @since Dec 26, 2015 * @author Shashwat.Anand */ public static void main(String[] args) { int[][] twoDArry = { { 2, 4, 6, 8 }, { 5, 9, 12, 16 }, { 2, 11, 5, 9 }, { 3, 2, 1, 8 } }; printInSpiralOrder(twoDArry, twoDArry[0].length, twoDArry.length); } private static void printInSpiralOrder(int[][] twoDArry, int m, int n) { int t = 0, b = m - 1, l = 0, r = n - 1; int dir = 0; // 0 = -> ; 0 = bottom; 0 = <-; 0 = top while (t <= b && l <= r) { if (dir == 0) { for (int i = l; i <= r; i++) { System.out.println(twoDArry[t][i]); } t++; dir = 1; } else if (dir == 1) { for (int i = t; i <= b; i++) { System.out.println(twoDArry[i][r]); } r--; dir = 2; } else if (dir == 2) { for (int i = r; i >= l; i--) { System.out.println(twoDArry...

Find two strings are anagram in Java ?

Source Code : import java.util.Arrays; /** * Anagram.java * @author Shashwat Anand */ public class Anagram { public static void main(String[] args) { System.out.println(testAnagram("Shashwat", "Shawatsh")); System.out.println(testAnagram("Shashwat", "Shawatwh")); System.out.println(testAnagramWithInbuiltMethods("Shashwat", "Shawatsh")); System.out.println(testAnagramWithInbuiltMethods("Shashwat", "Shawatwh")); } private static boolean testAnagramWithInbuiltMethods(String str1, String str2) { char[] charArray1 = str1.toCharArray(); char[] charArray2 = str2.toCharArray(); Arrays.sort(charArray1); Arrays.sort(charArray2); return Arrays.equals(charArray1, charArray2); } private static boolean testAnagram(String str1, String str2) { ...

Alpha-Beta Pruning Algorithm

Alpha–Beta Pruning is a search algorithm that seeks to decrease the number of nodes that are evaluated by the minimax algorithm in its search tree alphabeta(origin, depth, -∞, +∞, TRUE) Pseudocode alphabeta(node, depth, α, β, maximizingPlayer) if depth = 0 or node is a terminal node return the heuristic value of node if maximizingPlayer for each child of node α := max(α, alphabeta(child, depth - 1, α, β, FALSE)) if β ≤ α break (* β cut-off *) return α else for each child of node β := min(β, alphabeta(child, depth - 1, α, β, TRUE)) if β ≤ α break (* α cut-off *) return β