Online Judge Solutions

Sunday, December 7, 2014

LintCode: Topological Sorting

Topological Sorting

Given an directed graph, a topological order of the graph nodes is defined as follow:
  • For each directed edge A-->B in graph, A must before B in the order list.
  • The first node in the order can be any node in the graph with no nodes direct to it.
Find any topological order for the given graph.
Note
You can assume that there is at least one topological order in the graph.
Example
For graph as follow: 
The topological order can be:
[0, 1, 2, 3, 4, 5]
or
[0, 2, 3, 1, 5, 4]
or
....
 
/**
 * Definition for Directed graph.
 * struct DirectedGraphNode {
 *     int label;
 *     vector<DirectedGraphNode *> neighbors;
 *     DirectedGraphNode(int x) : label(x) {};
 * };
 */
class BFS_Solution {

private:
   void findDependency(
       DirectedGraphNode* node, 
       unordered_map<int, DirectedGraphNode *> &nodes,
       unordered_map<int, set<int>> &ancestors,
       unordered_map<int, set<int>> &descedants)
   {
      if (nodes.count(node->label) == 0)
      {
         nodes[node->label] = node;
         for(DirectedGraphNode* nb : node->neighbors)
         {
             descedants[node->label].insert(nb->label);
             ancestors[nb->label].insert(node->label);
             findDependency(nb, nodes, ancestors, descedants);
         } 
      }
   }
   
public:
    /**
     * @param graph: A list of Directed graph node
     * @return: Any topological order for the given graph.
     */
    vector<DirectedGraphNode*> topSort(vector<DirectedGraphNode*> graph) 
    {
        int n = graph.size();
        vector<DirectedGraphNode *> output(n);
        vector<int> tasksReady(n);
        unordered_map<int, DirectedGraphNode *> nodes;
        unordered_map<int, set<int>> ancestors;
        unordered_map<int, set<int>> descedants; 
        
        for(DirectedGraphNode* node :graph)
           findDependency(node, nodes, ancestors, descedants);
        
        int next = 0;
        for(auto kv : nodes) 
            if (ancestors.count(kv.first) == 0)
               tasksReady[next++] = kv.first;
        
        for(int i = 0; i < n; i++) {
            int readyTaskId = tasksReady[i];
            output[i] = nodes[readyTaskId];            
            for(int descedantId: descedants[readyTaskId]) {
               ancestors[descedantId].erase(readyTaskId);
               if(ancestors[descedantId].size() == 0)
                    tasksReady[next++] = descedantId;
            }
        }
        
        return output;
    }
};

=======================================================================
class DFS_Solution {

private:
   void findDependency(
       DirectedGraphNode* node,
       unordered_map<int, DirectedGraphNode *> &nodes,
       unordered_map<int, set<int>> &ancestors)
   {
      if (nodes.count(node->label) == 0)
      {
         nodes[node->label] = node;
         for(DirectedGraphNode* nb : node->neighbors)
         {
             ancestors[nb->label].insert(node->label);
             findDependency(nb, nodes, ancestors);
         }
      }
   }
 
   void helper(
       unordered_map<int, DirectedGraphNode *> &nodes,
       int currentNodeId,
       unordered_map<int, set<int>> &ancestors,
       unordered_map<int, bool> &scheduled,
       vector<DirectedGraphNode *> &output)
    {
        if (scheduled.count(currentNodeId) == 0)
        {
            scheduled[currentNodeId] = true;
            for(int ancestor: ancestors[currentNodeId])
              helper(nodes, ancestor, ancestors, scheduled, output);
            output.push_back(nodes[currentNodeId]);
        }
    }
public:

    // @param graph: A list of Directed graph node
    // @return: Any topological order for the given graph.
    //
    vector<DirectedGraphNode*> topSort(vector<DirectedGraphNode*> graph)
    {
        int n = graph.size();
        vector<DirectedGraphNode *> output;
        unordered_map<int, bool> scheduled;
       
        unordered_map<int, DirectedGraphNode *> nodes;
        unordered_map<int, set<int>> ancestors;
       
        for(DirectedGraphNode* node :graph)
           findDependency(node, nodes, ancestors);
       
        for(auto kv : nodes)
           helper(nodes, kv.first, ancestors, scheduled, output);
       
        return output;
    }
}

No comments:

Post a Comment