topological-sort

Topological sorting using std::sort

和自甴很熟 提交于 2019-11-28 05:12:00
问题 Note: While writing this question, I think I already found the answer. Feel free to ammend or append it with a better version. I thought it might be nice to document my problem. edit I was wrong, my aswer was not correct. Considering a list of integer pairs: I'd like to topologically sort them based on a partial ordering. This is similar to Is partial-order, in contrast to total-order, enough to build a heap? , but I'd like to use std::sort instead of std::priority_queue. To do so I wrote

deceptively simple implementation of topological sorting in python

陌路散爱 提交于 2019-11-27 01:50:34
问题 Extracted from here we got a minimal iterative dfs routine, i call it minimal because you can hardly simplify the code further: def iterative_dfs(graph, start, path=[]): q = [start] while q: v = q.pop(0) if v not in path: path = path + [v] q = graph[v] + q return path graph = { 'a': ['b', 'c'], 'b': ['d'], 'c': ['d'], 'd': ['e'], 'e': [] } print(iterative_dfs(graph, 'a')) Here's my question, how could you transform this routine into a topological sort method where the routine also becomes

How to sort depended objects by dependency

核能气质少年 提交于 2019-11-26 23:44:00
I have a collection: List<VPair<Item, List<Item>> dependencyHierarchy; The first item in pair is some object (item) and the second one is a collection of the same type objects that the first one depends on. I want to get a List<Item> in order of dependency, so there are not items that depend on the first element and so on (no cycled dependency!). Input: Item4 depends on Item3 and Item5 Item3 depends on Item1 Item1 does not depend on any one Item2 depends on Item4 Item5 does not depend on any one Result: Item1 Item5 Item3 Item4 Item2 Thank you. SOLUTION: Topological Sorting (thanks to Loïc

How to sort depended objects by dependency

穿精又带淫゛_ 提交于 2019-11-26 08:46:47
问题 I have a collection: List<VPair<Item, List<Item>> dependencyHierarchy; The first item in pair is some object (item) and the second one is a collection of the same type objects that the first one depends on. I want to get a List<Item> in order of dependency, so there are not items that depend on the first element and so on (no cycled dependency!). Input: Item4 depends on Item3 and Item5 Item3 depends on Item1 Item1 does not depend on any one Item2 depends on Item4 Item5 does not depend on any