43. Graphs-7 : Extra Algos⭐

# Note-

1. SCC:
- only for directed graphs
- strongly connected component ( component in which for every pair of nodes u,v we can go from u to v as well as from v to u )
In a graph having SCCs, we can traverse bidirectionally within a SCC & unidirectionally b/w SCCs. But, when we reverse the graph, then we can traverse only within a SCC not b/w adjacent ones
 
----------
2. Kosaraju and Tarjan Algo are used to find SCCs
3. Applications include finding deadlocks, clusters, echo chambers, dependency cycle, etc.
------------------------------
4. Bridges:
- only for undirected graphs
- they are edges which if removed disconnects the graph.
5. Articulation points: They are node which if removed (along with all its edges) disconnects the graph.
6. Applications include network resilience analysis, thus finding critical node and edge.

7. If discovery time(node B) < disc(A) => B is ancestor of node A
8. disc[curr node] = disc[last visited node] +1
9. disc: discovery time (time when we reached first to this node)
10. low of a node: lowest discovery time amongst all possible reachable nodes from current node, hence we update low of a node

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

1. Kosaraju's Algorithm-

Approach- time: O(V+E)

1. store node in a stack as per topo sort 
2. create a new reversed graph 
3. do dfs on new graph , as per stack nodes
- stores SCCS in a 2d vector, which stores each SCC during final dfs
- no. of SCCs = no. of dfs nodes in stack for which we did a dfs call
-------------------------

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

2. Tarjan's Algorithm-⭐

Used to find:
- bridges (or, non cyclic edges) in a graph
- articulation points
- SCCs
------------------------
# Approach- Modified DFS using discovery time concept

- we need 2 additional arrays in DFS: discovery time[], lowest discovery time[]


- bridge exists between two components only if, discovery time of u < all the discovery times on the right side as shown in image above.
- bridge condition: disc[parent] < low[child] ⭐

when current node finds a visited non parent neighbourlow of current node is updated: 
    low[node] = min( low[node], disc[visited non parent neighbour] )

- when we
backtrack to a parent node, then we update low of parent node:
   low[parent node] = min( low[parent node] , low[child node] )
--------------------------------------------------------------
# Algo
1. for each node set disc time and low to default values as u go deeper using DFS
2. do dfs till u reach a node from which u can't move further (node has a visited adj non parent node)
3. update low of this node & backtrack to parent node
4. check if this edge is a bridge, by comparing disc of parent with low of child node
5. repeat from step 3
----------------------------------------------------------------------------------------------------------------------------

3. Articulation point- (cut vertex)


same as Tarjan Algo with 3 changes-
1. add root node condition
2. count children
3. change < to <= in the AP check condition

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

Comments

Popular posts from this blog

30.) DP on subsequences

36. Graphs-2 (traversal problems)

35. Graphs-1 (basics)