Lintcode 127. 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.

 Notice

You can assume that there is at least one topological order in the graph.

Example

For graph as follow:

picture

The topological order can be:

[0, 1, 2, 3, 4, 5]
[0, 2, 3, 1, 5, 4]
...
题目思路:

这题就是topological sorting的算法之一:我使用dfs。

实现的是链接的算法--topological sorting

class Solution {
public:
    /*
     * @param graph: A list of Directed graph node
     * @return: Any topological order for the given graph.
     */
    vector<DirectedGraphNode*> topSort(vector<DirectedGraphNode*>& graph) {
        // write your code here
        vector<DirectedGraphNode*> res;
        set<int> visited;
    
        if(graph.size()==0)
            return graph;
        
        for(int i=0;i<graph.size();i++){
            if(visited.find(graph[i]->label) == visited.end())
                topSort(res, visited, graph[i]);
        }
        
        reverse(res.begin(), res.end());
        return res;
    }
    
private:
    void topSort(vector<DirectedGraphNode*>& res,
            set<int>& visited, DirectedGraphNode* node){
                
        if(visited.find(node->label) != visited.end())
            return;
           
        visited.insert(node->label);
        for(int i=0; i < node->neighbors.size(); i++){
            topSort(res, visited, node->neighbors[i]);
        }
        res.push_back(node);
    }

};

这种方法需要保证的是,第0个节点一定要是入度为零的节点,否则会出错。

第二种方法采用的是使用map统计所有节点的入度节点,然后使用queue保存入度为零的节点,最后使用bfs。

class Solution {
public:
    /**
     * @param graph: A list of Directed graph node
     * @return: Any topological order for the given graph.
     */
    vector<DirectedGraphNode*> topSort(vector<DirectedGraphNode*> graph) {
        // write your code here
        int size = graph.size(), i = 0;
        if(size <= 0) {
            return vector<DirectedGraphNode*>();
        }

        vector<DirectedGraphNode*> result;
        queue<DirectedGraphNode*> noPreNode;
        map<DirectedGraphNode*, int> nodeIndegree;

        getIndegree(graph, nodeIndegree);

        for(i=0; i<size; i++) {
            if(nodeIndegree[graph[i]] == 0) {
                noPreNode.push(graph[i]);
            }
        }

        while(!noPreNode.empty()) {
            result.push_back(noPreNode.front());
            for(i=0; i<noPreNode.front()->neighbors.size(); i++) {
                if(--nodeIndegree[noPreNode.front()->neighbors[i]] == 0) {
                    noPreNode.push(noPreNode.front()->neighbors[i]);
                }
            }
            noPreNode.pop();
        }
        return result;
    }

    void getIndegree(vector<DirectedGraphNode*> &graph, map<DirectedGraphNode*, int> &nodeIndegree) {
        int size = graph.size();
        for(int i=0; i<size; i++) {
            int size2 = graph[i]->neighbors.size();
            for(int j=0; j<size2; j++) {
                nodeIndegree[graph[i]->neighbors[j]]++;
            }
        }
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值