Skip to content

Instantly share code, notes, and snippets.

View arkanath's full-sized avatar

Arkanath Pathak arkanath

View GitHub Profile
@arkanath
arkanath / DFS, recursive with cycle detection
Created September 24, 2014 12:07
Depth First Search, Recursive with Cycle Detection
vector<int> adjList[N];
int visited[N];
bool dfsrecursiveandcycle(int u, int parent)
{
cout << "visited " << u << endl;// :-)
visited[u] = 1;
bool ans = false;
for(int i=0;i<adjList[u].size();i++)
{
@arkanath
arkanath / Dijkstra's Algorithm
Last active August 29, 2015 14:06
Dijkstra Implementation, without priority queue
int shortestdistance[N];
int explored[N];
vector<pair<int, int> > adjList[N]; // stores edges with cost
void dijkstra(int node)// Does not use priority queue, O(M+N2) implementation
{
shortestdistance[node] = 0;
explored[node] = 1;
int discovered = 1;
int lastadded = node;
while(discovered<N)
@arkanath
arkanath / DFS with Stack
Created September 24, 2014 06:08
Depth First Traversal, with Stack
vector<int> adjList[N];
int visited[N];
void dfsstack(int u)
{
stack<int> nstack;//stack used in place of recursion stack
visited[u] = 1;
nstack.push(u);
while(!nstack.empty())
{
@arkanath
arkanath / DFS with Recursion
Created September 24, 2014 06:06
Depth First Traversal, with Recursion
vector<int> adjList[N];
int visited[N];
void dfsrecursive(int u)
{
visited[u] = 1;
for(int i=0;i<adjList[u].size();i++)
{
if(!visited[adjList[u][i]])
@arkanath
arkanath / BFS with Queue
Last active August 29, 2015 14:06
Breadth First Traversal, with Queue
vector<int> adjList[N];
int visited[N];
void bfs(int u)
{
visited[u] = 1;//mapping the visited nodes, by default 0
queue<int> nq;//queue used
nq.push(u);//push initial node
while(!nq.empty())
@arkanath
arkanath / BFS, with layers
Created September 24, 2014 05:33
Breadth First Traversal, without using queue.
#define nodecount 10000
bool discovered[nodecount];
vector<vector<int> > L;//Layers
void BFS(int s, vector<vector<int, int> > & adjList)//BFS for arrays, without queue, non-recursive, by @arkanathpathak
{
fill(discovered, discovered+nodecount, false);
discovered[s] = true;
for(int v=0;v<nodecount;v++)
{
if(v!=s)discovered[v]=false;
@arkanath
arkanath / Minimize Maximum Lateness - Pseudocode
Created February 8, 2014 10:31
Minimize Maximum Lateness - Pseudocode
Problem: Variation of Interval Scheduling, with deadlines and duration given, all must be performed on a single resource, find the ordering with minimum maximum lateness of any interval.
Algorithm:
* Sort Jobs w.r.t the deadlines
* Let them be d1, d2, ... ,dn
* Initially f=s
* For (i=1:n)
@arkanath
arkanath / Interval Scheduling Problem - Pseudocode
Created February 8, 2014 10:28
Interval Scheduling Problem - Pseudocode
Problem: Given set of intervals with starting time and end time, give the set of intervals with maximum cardinality such that no intervals overlap.
Algorithm:
* Initially let R be the set of all requests.
* While R is not empty:
* Choose a request i with earliest finish time
* add to queue A
* delete all incompatible from R.
@arkanath
arkanath / Interval Partitioning Problem - Pseudocode
Last active July 12, 2018 17:58
Interval Partitioning Problem - Pseudocode
Problem: Given intervals with finish and end times, partition the intervals into labels with minimum amount of labels so that no two intervals in any label overlap.
Algorithm:
* Sort Intervals w.r.t. starting time, breaking tries arbitrarily.
* Let them be I1, I2, ... , In
* For j = 1:n
* For each (i<j && Ii,Ij overlap)
* Exclude the label of Ii from considering for Ij
* EndFor