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
/** | |
FordFulkerson ff = new FordFulkerson(flowGraph,source,sink); | |
System.out.println(ff.getMaxFlow()); | |
**/ | |
static class FlowEdge { | |
int v; | |
int w; | |
int flow; | |
int capacity; |
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
/* | |
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; |
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 Node { | |
private final int value; | |
private Node next; | |
private Node(final int value) { | |
this.value = 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 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++) { |
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++) { |
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
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
//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 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
/** | |
Usage: | |
EdgeWeightedGraph MST; | |
Find a leaf in mst to start traversing. | |
int source=0; | |
Edge sourceEdge = null; | |
for(int i=1; i<=v ;i++){ |