Skip to content

Instantly share code, notes, and snippets.

View rohithpeddi's full-sized avatar

Rohith Peddi rohithpeddi

View GitHub Profile
@rohithpeddi
rohithpeddi / FastReader.java
Last active December 2, 2016 12:42
Class to read input faster
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
@rohithpeddi
rohithpeddi / EdgeWeightedGraph.java
Last active December 2, 2016 09:45
Undirected Graph Data Stucture with edge weights and ADJACENCY LIST implementation
static class EdgeWeightedGraph {
ArrayList<ArrayList<Edge>> adj;
int noOfVertices;
public EdgeWeightedGraph(int v) {
this.adj = new ArrayList<ArrayList<Edge>>(v + 1);
this.noOfVertices = v+1;
// Vertices start from 1, ignore the list at index 0
@rohithpeddi
rohithpeddi / GraphCycleDetector.java
Last active November 10, 2016 15:55
Detects a Cycle in a graph
static class CycleDetector {
ArrayList<ArrayList<Edge>> adj;
boolean[] marked;
boolean hasCycle = false;
public CycleDetector(ArrayList<ArrayList<Edge>> adj) {
this.adj = adj;
marked = new boolean[adj.size()];
@rohithpeddi
rohithpeddi / DFS.java
Created November 12, 2016 16:24
DFS of EdgeWeightedGraph
static class DFS{
boolean[] marked;
public DFS(EdgeWeightedGraph G, int source){
marked = new boolean[G.noOfVertices];
dfs(G,source);
}
public void dfs(EdgeWeightedGraph G, int v){
marked[v] = true;
@rohithpeddi
rohithpeddi / BFS.java
Last active November 13, 2016 08:02
BFS of EdgeWeightedGraph
static class BFS {
boolean[] marked;
int[] distTo;
public BFS(EdgeWeightedGraph G, int source) {
marked = new boolean[G.noOfVertices];
distTo = new int[G.noOfVertices];
for(int i=0; i<G.noOfVertices; i++ ){
distTo[i] = Integer.MAX_VALUE ;
@rohithpeddi
rohithpeddi / SourceSinkBFS.java
Created November 13, 2016 09:55
Source to sink BFS of EdgeWeightedGraph
static class BFS {
boolean[] marked;
int[] distTo;
int sink;
public BFS(EdgeWeightedGraph G, int source, int destination) {
marked = new boolean[G.noOfVertices];
distTo = new int[G.noOfVertices];
@rohithpeddi
rohithpeddi / ConnectedComponents.java
Created November 15, 2016 05:41
Gives info on connected components vertices of EdgeWeightedGraph
static class ConnectedComponents{
boolean[] marked;
int[] id;
int count;
public ConnectedComponents(EdgeWeightedGraph G){
marked = new boolean[G.noOfVertices];
id = new int[G.noOfVertices];
count = 1;
for(int i=1; i<G.noOfVertices; i++){
@rohithpeddi
rohithpeddi / Dijkstra.java
Last active November 28, 2016 03:50
Dijkstra's Algorithm for EdgeWeightedGraph
static class Dijkstra {
int source;
int v;
int[] distTo;
boolean[] marked;
Edge[] edgeTo;
EdgeWeightedGraph G;
@rohithpeddi
rohithpeddi / Factorial.java
Created November 17, 2016 15:49
Fast factorial calculator - up to 1000!
// USAGE: FactorialPoorMans F = new FactorialPoorMans();
// String n! = F.factorial(n);
static class FactorialPoorMans {
public FactorialPoorMans() {
}
private long N;
public String factorial(int n) {
@rohithpeddi
rohithpeddi / BTtoBST.java
Created November 20, 2016 08:51
Converts a binary tree to binary search tree
class BTtoBST
{
static ArrayList<Integer> inOrder(Node root, ArrayList<Integer> orig){
if(root==null) return orig;
inOrder(root.left, orig);
orig.add(root.data);
inOrder(root.right, orig);
return orig;