37. Graphs-3 (cycle undir)

# Cycle detection in undirected graphs-

- if during traversal, we find a visited non-parent node as a neighbour of a node,
then cycle is present

- in dfs, take: int par, as another argument
- in bfs, modify queue to store parent for each node
------------------------
1. using BFS    

------------------------------------------------
2. using DFS   

-------------------------------------------------------------------------------------------------------------
1. Bipartite graph check

Ques- colour a graph with 2 colours such that no 2 adjacent nodes have same colour
Info- a graph is bipartite if it is linear or it has even length cycle
-------------------------
Algo- if you encounter a visited non parent node which has same colour as the current node, then graph is not bipartite
-----------------------------
1. using BFS
- instead of visited array we will take colour array
- also here we do not need parent node info, as colour array has enough info
--------------------------------------------------------
2. using DFS
---------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

30.) DP on subsequences

36. Graphs-2 (traversal problems)

35. Graphs-1 (basics)