lintcode-medium-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.

Example

For graph as follow:

The topological order can be:

[0, 1, 2, 3, 4, 5]
[0, 2, 3, 1, 5, 4]
...

Challenge

Can you do it in both BFS and DFS?

/**
 * Definition for Directed graph.
 * class DirectedGraphNode {
 *     int label;
 *     ArrayList<DirectedGraphNode> neighbors;
 *     DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); }
 * };
 */
public class Solution {
    /**
     * @param graph: A list of Directed graph node
     * @return: Any topological order for the given graph.
     */
    public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
        // write your code here

        if(graph == null || graph.size() == 0)
            return graph;

        ArrayList<DirectedGraphNode> res = new ArrayList<DirectedGraphNode>();
        HashMap<DirectedGraphNode, Integer> map = new HashMap<DirectedGraphNode, Integer>();

        for(DirectedGraphNode node: graph)
            map.put(node, 0);

        for(DirectedGraphNode node: graph){
            for(DirectedGraphNode n: node.neighbors){
                    map.put(n, map.get(n) + 1);
            }
        }

        DirectedGraphNode start = null;
        Queue<DirectedGraphNode> queue = new LinkedList<DirectedGraphNode>();

        for(DirectedGraphNode temp: graph){
            if(map.get(temp) == 0){
                start = temp;
                queue.offer(start);
                res.add(start);
            }
        }

        while(!queue.isEmpty()){
            DirectedGraphNode temp = queue.poll();

            for(DirectedGraphNode n: temp.neighbors){
                map.put(n, map.get(n) - 1);

                if(map.get(n) == 0){
                    queue.offer(n);
                    res.add(n);
                }
            }
        }

        return res;
    }
}
时间: 2024-09-30 11:21:24

lintcode-medium-Topological Sorting的相关文章

Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 922 Accepted Submission(s): 269 Problem Description A topological sort or topological ordering of a directed graph i

hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5195 Description A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for ev

BC DZY Loves Topological Sorting

DZY Loves Topological Sorting Accepts: 112 Submissions: 586 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 一张有向图的拓扑序列是图中点的一个排列,满足对于图中的每条有向边(u→v) 从 u 到 v,都满足u在排列中出现在v之前. 现在,DZY有一张有向无环图(DAG).你要在最多删去k条边之后,求出字典序最大

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

DZY Loves Topological Sorting (BC #35 hdu 5195 topsort+优先队列)

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 264    Accepted Submission(s): 63 Problem Description A topological sort or topological ordering of a directed gr

拓扑排序(Topological Sorting)

一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个条件: 每个顶点出现且只出现一次. 若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面. 有向无环图(DAG)才有拓扑排序,非DAG图没有拓扑排序一说. 例如,下面这个图: 它是一个 DAG 图,那么如何写出它的拓扑排序呢?这里说一种比较常用的方法: 从 DAG

hdu.5195.DZY Loves Topological Sorting(topo排序 &amp;&amp; 贪心)

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 866    Accepted Submission(s): 250 Problem Description A topological sort or topological ordering of a directed g

URAL(timus) 1280 Topological Sorting(模拟)

Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world championship in programming and decided to study N subjects (for convenience we will number these subjects from 1 to N). Michael has worked out a study plan

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

HDU 5195 - DZY Loves Topological Sorting

题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会把当前还没入队的di≤k的最大的i找出来,把它的di条入边都删掉,然后加入拓扑序列. 删除的一定是小连大的边,因为大连小的边在拓扑序列生成的时候就去掉了 1 #include <iostream> 2 #include <cstdio> 3 #include <queue>