This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************ | |
BAG - data structure to store vertices | |
*************************************************/ | |
class Bag<Item extends Comparable<Item>> implements Iterable<Item> { | |
private Node first; | |
private int N; | |
/************NODE CLASS************/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Can be used for bigger powers but be careful of long value overflow | |
current[][] - is the initial state of the matrix used | |
result[][] - is the final matrix after exponentiation | |
power - in the argument tells the exponent of the matrix | |
choose mod accordingly ! | |
*/ | |
public class MatrixExponentiation{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EdgeConnectivity{ | |
/*************** DEPTH FIRST SEARCH IMPLEMENTATION ****************/ | |
public void dfs_cycle(Graph G, int v,int toConnect,int notFrom){ | |
if(v == toConnect){ testmarked[v] = true; return;} | |
//StdOut.println("Checking for v:"+ v + " ,toConnect:"+ toConnect+ " ,notFrom:"+ notFrom); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Biconnected{ | |
private int[] low; | |
private int[] pre; | |
private int count; | |
private boolean[] articulation; | |
public Biconnected(Graph G){ | |
low = new int[G.V()]; pre = new int[G.V()]; articulation = new boolean[G.V()]; | |
for(int i=0; i<low.length;i++){ | |
low[i]=-1; pre[i]=-1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import edu.princeton.cs.algs4.StdOut; | |
public class LinearProbingHashST<Key,Value>{ | |
public int N; | |
public int M =16; | |
public Key[] keys; | |
public Value[] vals; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImprovementsOnMethods{ | |
/* | |
INTERPOLATION SEARCH - improvement on binary search | |
searches for closer values than always choosing mid values | |
Time : O(loglogn) - for sorted and uniformly distributed arrays | |
*/ | |
int interpolationSearch(int[] A, int key){ | |
int lo=0,hi=A.length-1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Suffix array takes | |
Space proportional to O(N) | |
Time proportional to O(NlogN) to build | |
*/ | |
public class SuffixArray { | |
private String[] suffixes; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bag<Item extends Comparable<Item>> implements Iterable<Item> { | |
private Node first; | |
private int N; | |
/************NODE CLASS************/ | |
private class Node{ | |
Item item; Node next; | |
Node(Item k){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For theory refer: https://www.hackerearth.com/practice/notes/segment-tree-and-lazy-propagation/ | |
public class SegmentTree { | |
private Node[] heap; | |
private int[] array; | |
private int size; | |
/********************************************* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Interval1D implements Comparable<Interval1D> { | |
public final int min,max; | |
public Interval1D(int min, int max){ | |
if(min<=max){ | |
this.min = min; | |
this.max = max; | |
} else { |