GraphNotes
Directed Graph Cycle Detection Code: (3 methods)
1. Using Color / DFS.
bool cycleCheck(vector<int>adj[],int node,vector<int>&color)
{
bool check = 0;
color[node]=1;
for(auto ch:adj[node])
{
if(color[ch]==0)
{
check |= cycleCheck(adj,ch,color);
}
else if(color[ch]==1)
{
check = 1;
}
}
color[node]=2;
return check;
}
int main()
{
vector<int>color(v,0);
bool check = 0;
for(int i=0;i<v;i++)
{
if(color[i]==0)
{
check |= cycleCheck(adj,i,color);
}
}
//Check 1 then cycle.
//check 0 then not cycle.
}2. Using BFS or Kahn’s algorithm:
Logic : Try to find the topological sort of the graph if it is possible then no cycle else cycle. So, create a indegree array and push all the nodes with indegree 0 in the queue and then pop the node and reduce the indegree of the child nodes and push the child nodes with indegree 0 in the queue and repeat the process until the queue is empty.
3. Using DFS/(visited & dfs_visited arrays ) :
UNDIRECTED Graph Cycle Detection Code: (2 Method)
1. Using DFS & parent array.
2. Using BFS.
3. Count of all cycles without any inner cycle in a given Graph
TOPOSORT CODE :
1. Using simple DFS and Reversing any of its DFS vectors , But you need to be sure that Cycle is not present in the graph AND It also work if you not start from root.
2. BFS TOPOSORT : (Using Indegree) Also Called Kahn's Algorithm
DSU (Disjoint set union) :
What is a Disjoint set data structure?
What it is used for ?
DSU functoin provide :
1. Union : It is used to merge two sets.
2. Find : It is used to find the parent/Representative of the set.
3. MakeSet : It is used to create a set of a single element.
DSU : Partitioning the individuals into different sets according to the groups in which they fall. This method is known as a Disjoint set Union which maintains a collection of Disjoint sets and each set is represented by one of its members call ultimate parent.
Data Structure used for DSU :
Find opeartion :
Union operation (Two Ways) :
Complete CODE :
MST (Minimum Spanning Tree) , Two Algorithms :
Steps :
Complexity Analysis :
Bridge :
Algorithm to find minimum distance of every node from source :(Dijkstra Algorithm, Bellman Ford Algorithm AND Toposort + Relaxation of nodes)
Used to find minimum/maximum distance of every node from source.
TO check Negative weighted cycle use Bellman Ford Algorithm :
Floyd Warshall Algorithm : (Used to find minimum distance of every node from every node)
One Thing To remember if that K which is the intermediate node should be the outermost loop.
Strongly Connected Component / Kosaraju Algorithm , (Directed Graph) :

M-Coloring Problem : (Graph Coloring)
Bipartite Graph : (Same For Two Clique Problem) , For Undirected Graph
2. BFS
Path And Cycle Problems(Undirected Graph) : (Cycle is also called circuit)
- Hamiltonian Path AND Hamiltonian Cycle(NP COMPLETE Problem ) :1. Hamiltonian Path : A path that visits every vertex exactly once.Time Complexity : O(V!) {Back Tracking}2. Hamiltonian Cycle : A cycle that visits every vertex exactly once and returns to the starting point.
Questions :
Articulation Point : (LEFT)
Maximum Bipartite Matching : (LEFT)
Maximum Flow : (LEFT)
Minimum Cut : (LEFT)
KMP Algorithm : (LEFT)
Rabin Karp Algorithm : (LEFT)
Z Algorithm : (LEFT)
Manacher Algorithm : (LEFT)
Aho Corasick Algorithm : (LEFT)
Suffix Array : (LEFT)
TRIE : (Dictonary Code)
MergeSort : (O(logn)(n))
Last updated