madhu242

Topological Sort

Oct 31st, 2022
2,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | Source Code | 0 0
  1. // Topological Sort
  2. using namespace std;
  3.  
  4. class TopologicalSort {
  5.  public:
  6.   static vector<int> sort(int vertices, const vector<vector<int>>& edges) {
  7.     vector<int> sortedOrder;
  8.     unordered_map<int, int> inDegree;
  9.     unordered_map<int, vector<int>> graph;
  10.     queue<int> sources;
  11.  
  12.     for (int i = 0; i < vertices; i++) {
  13.       inDegree[i] = 0;
  14.       graph[i] = vector<int>();
  15.     }
  16.  
  17.     for (int i = 0; i < edges.size(); i++) {
  18.       int parent = edges[i][0];
  19.       int child = edges[i][1];
  20.  
  21.       inDegree[child]++;
  22.       graph[parent].push_back(child);
  23.     }
  24.  
  25.     for (int i = 0; i < vertices; i++) {
  26.       if (inDegree[i] == 0) {
  27.         sources.push(i);
  28.       }
  29.     }
  30.  
  31.     cout << "sources [0] = " << sources.front();
  32.  
  33.     while (!sources.empty()) {
  34.       int s = sources.front()
  35.       sources.pop();
  36.       sortedOrder.push_back(s);
  37.       for (auto e : graph[s]) {
  38.         inDegree[e]--;
  39.         if (inDegree[e] == 0) {
  40.           sources.push(e);
  41.         }
  42.       }
  43.     }
  44.  
  45.     if (sortedOrder.size() != vertices) {
  46.       return vector<int>();
  47.     }
  48.  
  49.     return sortedOrder;
  50.   }
  51. };
  52.  
  53.  
  54. // Find out if tasks could be scheduled
  55.  
  56. There are ‘N’ tasks, labeled from ‘0’ to ‘N-1’. Each task can have some prerequisite tasks which need to be completed before it can be scheduled. Given the number of tasks and a list of prerequisite pairs, find out if it is possible to schedule all the tasks.
  57.  
  58. class TaskScheduling {
  59.  public:
  60.   static bool isSchedulingPossible(int vertices, const vector<vector<int>>& edges) {
  61.       unordered_map<int, int> outDegree;
  62.       unordered_map<int, vector<int>> graphInDegree;
  63.       queue<int> sinks;
  64.       vector<int> sortedOrder;
  65.  
  66.       for (int i = 0; i < vertices; i++) {
  67.         outDegree[i] = 0;
  68.         graphInDegree[i] = vector<int>();
  69.       }
  70.  
  71.       for (int i = 0; i < edges.size(); i++) {
  72.         int parent = edges[i][0];
  73.         int child = edges[i][1];
  74.         outDegree[parent]++;
  75.         graphInDegree[child].push_back(parent);
  76.       }
  77.  
  78.       for (int i = 0; i < vertices; i++) {
  79.         if (outDegree[i] == 0) {
  80.           sinks.push(i);
  81.         }
  82.       }
  83.  
  84.       while (!sinks.empty()) {
  85.         int s = sinks.front();
  86.         sinks.pop();
  87.         sortedOrder.push_back(s);
  88.         for (auto e : graphInDegree[s]) {
  89.           outDegree[e]--;
  90.           if (outDegree[e] == 0) {
  91.             sinks.push(e);
  92.           }
  93.         }
  94.       }
  95.    
  96.       return sortedOrder.size() == vertices;
  97.   }
  98. };
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment