LintCode-Topological Sorting
2014-12-27 00:01
417 查看
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
....
Challenge
Can you do it in both BFS and DFS?
Analysis:
A basica method is recording the pre nodes of every node, then find out a node without pre node in each iteration and delete this node from unvisited set.
DFS: use a recursive method, randomly pick up an unmakred node, before adding it into result list, recursively visite all its neighbors and add its neighbors into list first. In this way, we guarantee that all the nodes belong to some node's post nodes will be added to the result list first.
Solution 1:
Solution 2 (DFS):
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
....
Challenge
Can you do it in both BFS and DFS?
Analysis:
A basica method is recording the pre nodes of every node, then find out a node without pre node in each iteration and delete this node from unvisited set.
DFS: use a recursive method, randomly pick up an unmakred node, before adding it into result list, recursively visite all its neighbors and add its neighbors into list first. In this way, we guarantee that all the nodes belong to some node's post nodes will be added to the result list first.
Solution 1:
/** * 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) { ArrayList<DirectedGraphNode> res = new ArrayList<DirectedGraphNode>(); if (graph.size()==0) return res; //Construct hash map. Map<DirectedGraphNode, Set<DirectedGraphNode>> map = new HashMap<DirectedGraphNode, Set<DirectedGraphNode>>(); for (DirectedGraphNode node: graph){ Set<DirectedGraphNode> set = new HashSet<DirectedGraphNode>(); map.put(node,set); } for (DirectedGraphNode node : graph) for (DirectedGraphNode temp: node.neighbors) map.get(temp).add(node); //Construct topological order sequence. int len = graph.size(); while (graph.size()>0) { int index = 0; while (index<graph.size()){ DirectedGraphNode node = graph.get(index); if (map.get(node).size()==0){ graph.remove(node); res.add(node); for (DirectedGraphNode temp: graph) if (map.get(temp).contains(node)) map.get(temp).remove(node); } else index++; } } return res; } }
Solution 2 (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) { ArrayList<DirectedGraphNode> res = new ArrayList<DirectedGraphNode>(); if (graph.size()==0) return res; Map<DirectedGraphNode,Integer> status = new HashMap<DirectedGraphNode,Integer>(); for (DirectedGraphNode node: graph) status.put(node,0); while (hasUnsorted(status,graph)){ DirectedGraphNode node = null; for (DirectedGraphNode temp : graph) if (status.get(temp)==0) node = temp; search(status, graph, res, node); } return res; } public boolean hasUnsorted(Map<DirectedGraphNode,Integer> status, ArrayList<DirectedGraphNode> graph){ for (DirectedGraphNode node : graph) if (status.get(node)==0) return true; return false; } public void search(Map<DirectedGraphNode,Integer> status, ArrayList<DirectedGraphNode> graph, ArrayList<DirectedGraphNode> res, DirectedGraphNode node){ if (status.get(node)==1) System.out.println("It is not a DAG"); if (status.get(node)==2) return; status.put(node,1); for (DirectedGraphNode next : node.neighbors) search(status,graph,res,next); status.put(node,2); res.add(0,node); } }
相关文章推荐
- Lintcode 376. 二叉树的路径和
- lintcode,二叉树的序列化和反序列化
- lintcode&九章算法——100. 删除有序数组里的重复元素 ? 待解决
- Lintcode - Expression Evaluation
- lintcode:Route Between Two Nodes in Graph
- LintCode:接雨水
- lintcode-108-分割回文串 II
- Lintcode:字符串查找
- Lintcode C++代码
- lintcode 中等题:search in rotated sorted arrayII 搜索旋转排序数组II
- Lintcode: Minimum Subarray
- lintcode segment-tree-query 线段树的查询
- lintcode--报数
- LintCode:K组翻转链表
- lintcode backpack 背包问题
- LintCode Binary Tree Longest Consecution Sequence
- lintcode longest-common-subsequence 最长公共子序列 证明
- lintcode climbing-stairs 爬楼梯 递归 循环
- LintCode SortedArrayToBST 把排序数组转换为高度最小的二叉搜索树
- LintCode 字符串置换 JAVA