This file contains 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
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++) | |
{ |
This file contains 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
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) |
This file contains 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
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()) | |
{ |
This file contains 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
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]]) |
This file contains 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
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()) |
This file contains 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
#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; |
This file contains 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
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) |
This file contains 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
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. |
This file contains 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
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 |