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 LIS{ | |
/* | |
O(NlogN- version) | |
*/ | |
private static int binarySearch(int table[],int a,int len){ | |
int hi=len-1, lo=0,mid=0,result=-1; | |
while(lo <= hi){ |
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 bst<Key extends Comparable<Key>,Value> { | |
private Node root; | |
private class Node{ | |
Node(Key k,Value v, int n){ | |
this.key = k; this.val = v; this.N = n; | |
} | |
Key key; |
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 RedBlackBST<Key extends Comparable<Key>, Value> { | |
private static final boolean RED = true; | |
private static final boolean BLACK = false; | |
private Node root; // root of the BST | |
private int count = 0; | |
// BST helper node data type | |
private class Node { |
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
/** | |
Usage: | |
In hashmap each edge is mapped to number of vertices on one side of that edge. | |
**/ | |
static class MSTEdgeVertexCount { | |
HashMap<Edge,Integer> edgeCount; | |
public MSTEdgeVertexCount(EdgeWeightedGraph G, int source, Edge e) { | |
edgeCount = new HashMap<Edge,Integer>(); |
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
/** | |
Usage: | |
EdgeWeightedGraph MST; | |
Find a leaf in mst to start traversing. | |
int source=0; | |
Edge sourceEdge = null; | |
for(int i=1; i<=v ;i++){ |
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 Methods{ | |
/*O(N)- Two pair search in a sorted array | |
It is much better than heap cration, tree creation. | |
Does work in place and O(1) extra space | |
*/ | |
public int twoPairSum(int[] a. int k){ | |
int i=0, j= 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
//change max edge weight if needed | |
static class FloydWarshall { | |
private int[][] distTo; | |
private Edge[][] edgeTo; | |
public FloydWarshall(EdgeWeightedGraph G){ | |
int V = G.noOfVertices; | |
distTo = new int[V][V]; | |
edgeTo = new Edge[V][V]; | |
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 FloydWarshall{ | |
/*GIVES ALL PAIRS SHORTEST PATH IN O(N^3)*/ | |
public static void main(String[] args){ | |
/*FOR VERSION WHICH CONSIDERS LEAST RECENTLY ADDED EDGE*/ | |
int[][] lru= new int[V+1][V+1]; | |
for(int i=1; i<=V; i++){ | |
for(int j=1; j<=V; j++){ | |
if(i!=j) lru[i][j] = Integer.MAX_VALUE; |
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
static class EdgeWeightedGraph{ | |
Edge[][] adj; | |
int noOfVertices; | |
public EdgeWeightedGraph(int v){ | |
this.adj = new Edge[v+1][v+1]; | |
this.noOfVertices = v+1; | |
} | |
public void addEdge(Edge edge) { |
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
static class UnionFind { | |
int[] id; | |
int[] sz; | |
int count; | |
public UnionFind(int N) { | |
this.count = N; | |
this.id = new int[N]; | |
this.sz = new int[N]; | |
for (int i = 0; i < N; i++) { |