Skip to content

Instantly share code, notes, and snippets.

View rohithpeddi's full-sized avatar

Rohith Peddi rohithpeddi

View GitHub Profile
@rohithpeddi
rohithpeddi / LIS.java
Created December 15, 2016 15:36
Longest Increasing Subsequence two implementations - [O(N^2)-dp version] & [O(NlogN)-binarysearch & dp version]
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){
@rohithpeddi
rohithpeddi / bst.java
Created December 12, 2016 10:54
Binary Search Tree with duplicates allowed
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;
@rohithpeddi
rohithpeddi / RedBlackBST.java
Created December 12, 2016 08:47
Self Balanced RedBlack Tree modifiable according to the need or use as it is :P
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 {
@rohithpeddi
rohithpeddi / MSTEdgeVertexCount.java
Created December 4, 2016 10:54
MSTEdgeVertexCount.java with hashmap implementation
/**
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>();
@rohithpeddi
rohithpeddi / MSTEdgeVertexCount.java
Last active December 4, 2016 10:49
Finds the number of vertices on either side of an edge in a tree graph.
/**
Usage:
EdgeWeightedGraph MST;
Find a leaf in mst to start traversing.
int source=0;
Edge sourceEdge = null;
for(int i=1; i<=v ;i++){
@rohithpeddi
rohithpeddi / Methods.java
Last active December 19, 2016 09:43
Twopair sum[Fast], Prime Sieve, Binary Search
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;
@rohithpeddi
rohithpeddi / FloydWarshall.java
Last active December 2, 2016 14:18
FloydWarshall with adjacency matrix graph implementation
//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];
@rohithpeddi
rohithpeddi / FloydWarshall.java
Created December 2, 2016 12:15
Main loop written for two most common versions
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;
@rohithpeddi
rohithpeddi / AdjMatrixEdgeWeightedGraph.java
Created December 2, 2016 09:44
EdgeWeightedGraph with ADJACENCY MATRIX implementation
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) {
@rohithpeddi
rohithpeddi / UnionFind.java
Last active December 2, 2016 12:36
Weighted Quick Union Find - Dynamic connectivity with path compression
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++) {