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


----------------------------------------------------------------
ALGO-
1. sort all edges according to finishing time
2. reverse the graph
3. do dfs
-------------------------------------------------------------
1. do dfs from starting node, when u reach an end insert it into a stack, and backtrack & insert similarly for other branches as well

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-⭐

QUES- Bridges in graph or, finding non cyclic edges in a graph

we will use time array, lowestTime array & visited array

update the low of a node using the visited adjacent nodes ka low, except parent node
------------------------
to check if when an edge b/w 2 nodes is removed that if a new component is built or not-
see if,
time of parent node  > low of a node ==> no bridge
time of parent node < low of a node  ==> bridge found
----------------------
time of curr node = time of last visited node +1 
low of parent node = min low of (parent node, child/ adj node)
----------------------
do dfs till u reach a node from which u can't move further-->for a node---->set time & low to default values---->check if it has a visited adj node which ain't parent node--->update low of curr node--->backtrack to the parent node-->check if you can break the edge--->backtrack to parent node
----------------------------

----------------------------------------------------------------------------------------------------------------------------

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

Popular posts from this blog

18. greedy algorithm

19. strings (LB)

17. BINARY SEARCH on 2d arrrays