43. Graphs-7 : Extra Algos
1. Kosaraju's Algorithm-
only for Directed graphs
QUES- find no. of strongly connected components (SCCs) & print them
a strongly connected component is a component in which for every pair of nodes u,v we can go from u to v as well as from v to u
OBSERVATION-
1. in original graph, we can traverse bidirectionally within a SCC & unidirectionally b/w SCCs
2. when we reverse direction b/w 2 SCCs , then we can traverse only within a SCC not b/w the 2
3. since we don't know where are SCCs so we reverse all the edges instead
2. reverse the edges- simple
3. take st.top() and start dfs and mark nodes vis and store them one by one into a vector
next pop those elements from stack which are vis & start dfs from first non visited you get & REPEAT
---------------------------------------------------
no. of SCCs = no. of dfs nodes in stack for which we did a dfs call
SCCs = stored in 2d vector
-------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------2. Tarjan's Algorithm-⭐
time of curr node = time of last visited node +1
3. Articulation points- ????
nodes on whose removal graph breaks into multiple components
a node is NOT an articulation point if its child node has a way to connect to its previous node
low of a node = min(low of curr node, time of adj node)
articulation pt : time of parent <= low of curr node
---------------------------------------
1. use time & low arrays as u did in tarjan algo , but in low, along with parent this time also exclude visited nodes when updating low of a node
2. in the condition of checking articulation point add an extra condition of parent != -1
----------------------------------------
1. do dfs and mark time & low of all nodes with default values
2. when u reach a node whose all adj nodes are visited, then update this node's low
3. backtrack to its parent node & check parent node for condition of articulation point
4. again backtrack & repeat from step 2
-----------------------------------------------------------------------------------------------------------------------------







Comments
Post a Comment