Heavely Commented || TopoSort || Khan's Algorithm || BFS || 100% Simple✅✅✅
\nclass Solution {\npublic:\n    \n\t// Function to create adjacency List and indegree of the graph\n    void adjListCreator(vector<vector<int>>&conditions,vect
```\nclass Solution {\npublic:\n    \n\t// Function to create adjacency List and indegree of the graph\n    void adjListCreator(vector<vector<int>>&conditions,vector<vector<int>>&adj,vector<int>& indegree){\n        for(auto &it:conditions){\n            int u=it[0];\n            int v=it[1];\n            adj[v].push_back(u);\n            indegree[u]++;\n        }\n    }\n    //Function of Khan\'s Algorithm\n    void kahnAlgo(vector<vector<int>>&adj,vector<int>& indegree,vector<int>&topoSort){\n        queue<int>qu;\n        for(int i=1;i<indegree.size();i++){\n            if(indegree[i]==0)\n                qu.push(i);\n        }\n        while(!qu.empty()){\n            int k=qu.front();\n            topoSort.push_back(k);\n            qu.pop();\n            for(auto &it:adj[k]){\n                indegree[it]--;\n                if(indegree[it]==0)qu.push(it);\n            }\n        }\n        reverse(topoSort.begin(),topoSort.end());\n    }\n    \n    vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n        vector<vector<int>>res(k,vector<int>(k,0));\n    \n        vector<vector<int>>adjRow(k+1);\n        vector<vector<int>>adjCol(k+1);\n        \n        vector<int>indegreeRow(k+1,0);\n        vector<int>indegreeCol(k+1,0);\n\t\t\n        //Create adjacency List for both condition matrices\n\t\t//And calculate the indegree (i.e. number of incoming nodes) for each nodes\n        adjListCreator(rowConditions,adjRow,indegreeRow);\n        adjListCreator(colConditions,adjCol,indegreeCol);\n        \n        vector<int>topoSortRow,topoSortCol; //Store the REVERSE of topoSort (as work order is reverse of Topological sort) ordering of the two adjecency Lists\n\t\t\n\t\t//Apply khans algorithm to calculate the order of work\n        kahnAlgo(adjRow,indegreeRow,topoSortRow);  \n        kahnAlgo(adjCol,indegreeCol,topoSortCol);\n\t\t\n\t\t// If the size of these two arrays are not equal to k (i.e. the number of nodes) it means there is a cycle present in the graph .Hence we return the empty vector \n        if(topoSortRow.size()!=k or topoSortCol.size()!=k)return {};\n        \n\t\t//Now we arrange the ordering of nodes into the resultant array by applying simple logic\n\t\t//                index= 0  1  2\n\t\t// Lets say topoSortRow= 1  3  2\n\t\t//          topoSortCol= 3  2  1\n\t\t// Since the topoSortRow[0]==topoSorCol[2]==1 , Hence  our res[0][2]=1, Similarly we fill other \'res\' values\n        for(int i=0;i<k;i++){\n            for(int j=0;j<k;j++){\n                if(topoSortRow[i]==topoSortCol[j]){\n                    res[i][j]=topoSortRow[i];\n                    break;\n                }\n            }\n        }\n        //Finally return the resultant array\n        return res;\n    }\n};\n```\n**Hope you find it helpful. If yes, kindly upvote!!**
['Breadth-First Search', 'Graph', 'Topological Sort']