Skip to content

Instantly share code, notes, and snippets.

View dabasajay's full-sized avatar
:octocat:
Insaan bano, scope bhi hai aur sukoon bhi!

Ajay Dabas dabasajay

:octocat:
Insaan bano, scope bhi hai aur sukoon bhi!
  • New Delhi, India
  • 04:33 (UTC +05:30)
View GitHub Profile
@dabasajay
dabasajay / dsu.cpp
Created January 26, 2021 16:27
dsa/graphs/dsu | desc: disjoint set union dsu
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int arr[N];
void initialze(int n){
for (int i = 0; i < n; i++) arr[i] = -1;
}
@dabasajay
dabasajay / cycle detection directed.cpp
Last active January 26, 2021 16:39
dsa/graphs/misc | desc: cycle in directed graph
#include <bits/stdc++.h>
using namespace std;
/*
Using DFS to find a cycle in directed graph:
============================================
- DFS can be used to detect a cycle in a directed graph. DFS for a connected graph produces a tree. There is a cycle in a graph only
if there is a back edge present in the graph. A back edge is an edge that is from a node to itself (self-loop) or one of its
ancestor in the tree produced by DFS.
- Simple visited approach will not work here:
@dabasajay
dabasajay / cycle detection undirected.cpp
Created January 26, 2021 16:40
dsa/graphs/misc/ | desc: cycle in undirected graph
#include <bits/stdc++.h>
using namespace std;
/* Using DFS to find a cycle in undirected graph */
int N;
vector<vector<int>> gph;
vector<bool> vis(N, false);
bool dfs(int src, int par, vector<bool>& vis){
@dabasajay
dabasajay / euler walk.cpp
Created January 26, 2021 17:11
dsa/graphs/trees | desc: Euler walk
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> graph;
vector<int> euler_walk, in, out;
void dfs(int u, int &i, vector<bool> &vis){
vis[u] = true;
euler_walk[i++] = u;
@dabasajay
dabasajay / lca.cpp
Created January 26, 2021 17:14
dsa/graphs/trees/ | desc: Least Common Ancestor LCA DP
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5; // Max num of vertices
const int maxHeight = 20 + 1; // ceil(log2(N)) = 20 for N = 5e5+5
int depth[N], parent[N], dp[N][maxHeight];
vector<int> gph[N];
int n; // Num of vertices (1-indexing)
void dfs (int u, int par, int dep) { // Calculate parent and depth
@dabasajay
dabasajay / topological ordering.cpp
Last active January 26, 2021 17:35
dsa/graphs/sorting/ | desc: topological ordering in DAG
#include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<int>> graph;
vector<int> bfs(){
vector<int> indeg(N, 0);
vector<int> ans;
for (auto i : graph)
@dabasajay
dabasajay / kruskal.cpp
Created January 26, 2021 17:46
dsa/graphs/mst | desc: kruskal minimum spanning tree mst
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, pair<int, int>>
const int N = 2e5 + 5;
int arr[N];
int _find(int key);
void _union(int a, int b);
@dabasajay
dabasajay / prim.cpp
Created January 26, 2021 18:35
dsa/graphs/mst/ | desc: prim minimum spanning tree mst
#include <bits/stdc++.h>
using namespace std;
/* Works for Connected and undirected graph */
// https://www.geeksforgeeks.org/prims-algorithm-using-priority_queue-stl/
#define pp pair<int, int>
int N, MX = INT32_MAX;
vector<vector<pair<int,int>>> graph;
@dabasajay
dabasajay / bfs.cpp
Last active February 16, 2021 09:38
dsa/graphs/traversal/ | desc: Breadth First Search
vector<vector<int>> adj; // adjacency list representation
int n; // number of nodes
int src; // source vertex
queue<int> q;
vector<bool> vis(n);
vector<int> dist(n), par(n);
q.push(src);
vis[src] = true;
@dabasajay
dabasajay / trie.cpp
Last active February 14, 2021 17:32
dsa/strings | desc: Trie Data Structure
class Node {
public:
vector<Node*> children;
bool isLeaf;
Node() {
children.assign(26, NULL);
isLeaf = false;
}
};