Skip to content

Instantly share code, notes, and snippets.

View rohithpeddi's full-sized avatar

Rohith Peddi rohithpeddi

View GitHub Profile
@rohithpeddi
rohithpeddi / FordFulkerson.java
Created November 24, 2016 15:57
Ford-Fulkerson algorithm to compute maxflow of a flow graph.
/**
FordFulkerson ff = new FordFulkerson(flowGraph,source,sink);
System.out.println(ff.getMaxFlow());
**/
static class FlowEdge {
int v;
int w;
int flow;
int capacity;
@rohithpeddi
rohithpeddi / MinHeap.java
Last active November 26, 2016 14:46
ArrayList implementation
/*
insert() - for inserting
delete(Key) - deletion of specific key
min() - extracting minimum
delMin() - deleting minimum
*/
class MinHeap<Key extends Comparable<Key>> {
public ArrayList<Key> hp;
@rohithpeddi
rohithpeddi / MergeSort.java
Created November 26, 2016 19:57
LinkedList Implementation - For faster sorting
class Node {
private final int value;
private Node next;
private Node(final int value) {
this.value = value;
}
}
@rohithpeddi
rohithpeddi / Kruskal.java
Created November 29, 2016 03:37
Kruskal MST with weighted quick union find
static class UnionFind {
int[] id;
int[] sz;
int count;
public UnionFind(int N) {
this.count = N - 1;
this.id = new int[N];
this.sz = new int[N];
for (int i = 1; i < N; i++) {
@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++) {
@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 / 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 / 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 / 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 / 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++){