Fastrail08

Leetcode 802. Eventual Safe Nodes (IMPORTANT Kahn's Algorithm Insight)

Aug 18th, 2025 (edited)
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. /*
  2. QUESTION - Leetcode 802. Eventual Safe Nodes
  3.  
  4. IMPORTANT DISCUSSION GPT - https://chatgpt.com/share/68a34a20-82c8-8012-a7bb-cf4c0ae54605
  5.  
  6. */
  7.  
  8. /*
  9.  
  10. We reverse the original graph. So all the terminal nodes in the original graph (outdegree = 0), now become source nodes in reverse graph(indegree = 0) for applying kahn's algorithm.
  11.  
  12. So in reverse graph, we can say:
  13. All the nodes which will start kahn's algorithm will always start from a terminal node of original graph.
  14. All the paths that are considered in kahn's algorithm will always originate from a terminal node.
  15.  
  16. 3 statements to solve this:
  17. So for some intermediate node X, whose indegree becomes equal to 0, during kahn's algorithm execution, we can say-
  18. 1) jitne bhi tareeke se X tak aaya jaa Sakta tha, vo saare tareeke consider karliye ya JITNE BHI DIFFERENT PATH SE 'X' TAK AAYA JA SAKTA THA, VO SAARE CONSIDER KARLIYE (tabhi indegree 0 ho gyi)
  19.  
  20. 2) Aur saare paths jinse humne algorithm shuru ki thi, vo kisi na kisi terminal node se hi originate hue the. (Kahn Algorithm source nodes in reversed graph are terminal nodes of original graph)
  21.  
  22. 3) IMPORTANT CONCLUSION - JITNE BHI PATHS SE X TAK AAYA JAA SAKTA THA, VO PATH HAMESHA HI KISI NA KISI TERMINAL NODES SE HI NIKLA HOGA (reverse graph mein), but original graph mein the same can be stated as, JITNE BHI TAREEKE SE X SE NIKLA JAA SAKTA THA (every path from X), VO KISI NA KISI TERMINAL NODE PE HI END HOGA (all the paths from X will always end at a terminal node), which is definition of safe nodes in question.
  23.  
  24. */
  25.  
  26.  
  27. class Solution {
  28. public:
  29.  
  30.     void bfs(vector<int> &indegree, vector<int> &order, vector<vector<int>  > &adj){
  31.         queue<int> q;
  32.         for(int i = 0; i < indegree.size(); i++){
  33.             if(indegree[i] == 0){
  34.                 q.push(i);
  35.             }
  36.         }
  37.         while(!q.empty()){
  38.             int front = q.front();
  39.             q.pop();
  40.            
  41.             order.push_back(front);
  42.             for(int i = 0; i < adj[front].size(); i++){
  43.                 int node = adj[front][i];
  44.                 indegree[node]--;
  45.                 if(indegree[node] == 0){
  46.                     q.push(node);
  47.                 }
  48.             }
  49.         }
  50.     }
  51.  
  52.     vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
  53.         vector<int> indegree(graph.size(), 0);
  54.         vector<vector<int> > adj(graph.size(), vector<int>());
  55.         for(int i = 0; i < graph.size(); i++){
  56.             for(int j = 0; j < graph[i].size(); j++){
  57.                 int node = graph[i][j];
  58.                 adj[node].push_back(i);
  59.             }
  60.         }
  61.         for(int i = 0; i < adj.size(); i++){
  62.             for(int j = 0; j < adj[i].size(); j++){
  63.                 int node = adj[i][j];
  64.                 indegree[node]++;
  65.             }
  66.         }
  67.        
  68.         vector<int> order;
  69.         bfs(indegree, order, adj);
  70.         sort(order.begin(), order.end());
  71.         return order;
  72.     }
  73. };
Advertisement
Add Comment
Please, Sign In to add comment