Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 1192 · Bridges / Articulation Points

Critical Connections in a Network

LeetCode 1192 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Bridge if low[child] > tin[node].

ಕಠಿಣDepth-First SearchGraph TheoryBiconnected ComponentBridge (Graph)
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Critical Connections in a Network (LeetCode 1192) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Bridges / Articulation Points ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

A bridge is an edge whose removal disconnects the graph. In a DFS, edge (node, child) is a bridge if the child's subtree can't reach back to node or above — i.e. its lowest reachable discovery time is greater than node's discovery time (Tarjan's algorithm).

Approach

  1. 1

    DFS assigns discovery time ttt and low-link ltt.

  2. 2

    After visiting a child, check ttt[node] < ltt[child] → bridge.

  3. 3

    Update ltt[node] with the child's/neighbour's low-link.

  4. 4

    Skip the edge back to the parent.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Tarjan's bridgesPythonO(V+E) time
class Solution:
    def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
        #time of insertion. 
        #lowest time/steps of insertion/to reach node 
        #if an adjacennt node can be visited in less no of steps take it's lowest no of steps
        #if adjacent node can be reached in at least(lowest) in m steps, and parent can be reached in n steps, and m>n, then parent and adjacent - can be a bridge
        ltt = [0]*n
        ttt = [0]*n
        visited = set()
        time = 1
        adj= defaultdict(list)
        bridges = []
        for u,v in connections:
            adj[u].append(v)
            adj[v].append(u)
        def dfs(node,parent):
            nonlocal time
            time+=1
            ltt[node]=time
            ttt[node]=time
            visited.add(node)
            for adj_node in adj[node]:
                if adj_node==parent:
                    continue
                if adj_node not in visited:
                    dfs(adj_node,node)
                    #come back - are you my bridge?
                    if ttt[node]<ltt[adj_node]:
                        bridges.append([node,adj_node])
                #already visited check the low of the adj node
                ltt[node]=min(ltt[node],ltt[adj_node])
        dfs(0,-1)
        return bridges


ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Critical Connections in a Network — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Critical Connections in a Network ಹಿಂದಿನ intuition ಏನು?

A bridge is an edge whose removal disconnects the graph. In a DFS, edge (node, child) is a bridge if the child's subtree can't reach back to node or above — i.e. its lowest reachable discovery time is greater than node's discovery time (Tarjan's algorithm).

Critical Connections in a Network (LeetCode 1192) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. DFS assigns discovery time ttt and low-link ltt. 2. After visiting a child, check ttt[node] < ltt[child] → bridge. 3. Update ltt[node] with the child's/neighbour's low-link. 4. Skip the edge back to the parent.

Critical Connections in a Network ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(V + E). Space: O(V + E). (Accepted approach: Tarjan's bridges.)

Critical Connections in a Network ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Skip only the parent edge, not every visited neighbour.