topological-sort

Python: sorting a dependency list

与世无争的帅哥 提交于 2019-11-30 08:39:37
I'm trying to work out if my problem is solvable using the builtin sorted() function or if I need to do myself - old school using cmp would have been relatively easy. My data-set looks like: x = [ ('business', Set('fleet','address')) ('device', Set('business','model','status','pack')) ('txn', Set('device','business','operator')) .... The sort rule should be basically for all value of N & Y where Y > N, x[N][0] not in x[Y][1] Although I'm using Python 2.6 where the cmp argument is still available I'm trying to make this Python 3 safe. So, can this be done using some lambda magic and the key

Topological sort to find the number of paths to t

吃可爱长大的小学妹 提交于 2019-11-30 07:13:11
I have to develop an O(|V|+|E|) algorithm related to topological sort which, in a directed acyclic graph (DAG), determines the number of paths from each vertex of the graph to t (t is a node with out-degree 0). I have developed a modification of DFS as follow: DFS(G,t): for each vertex u ∈ V do color(u) = WHITE paths_to_t(u) = 0 for each vertex u ∈ V do if color(u) == WHITE then DFS-Visit(u,t) DFS-Visit(u,t): color(u) = GREY for each v ∈ neighbors(u) do if v == t then paths_to_t(u) = paths_to_t(u) + 1 else then if color(v) == WHITE then DFS-Visit(v) paths_to_t(u) = paths_to_t(u) + paths_to_t(v

Topological sort of cyclic graph with minimum number of violated edges

流过昼夜 提交于 2019-11-30 07:03:06
I am looking for a way to perform a topological sorting on a given directed unweighted graph, that contains cycles. The result should not only contain the ordering of vertices, but also the set of edges, that are violated by the given ordering. This set of edges shall be minimal. As my input graph is potentially large, I cannot use an exponential time algorithm. If it's impossible to compute an optimal solution in polynomial time, what heuristic would be reasonable for the given problem? David Eisenstat Eades, Lin, and Smyth proposed A fast and effective heuristic for the feedback arc set

Topological sort in OCaml

久未见 提交于 2019-11-30 03:42:49
I'm trying to write topological sorting in ocaml, but I'm a beginner (in OCaml & graphs algorithms) and I can't do this by myself. It's easier for me to think about topological sorting in, for example, C++ (and there is a lot examples of topological sorting in C++ on the Internet), but I want to learn something new. Moreover, I've found some examples of topological sorting written in OCaml, but I don't understand them, to be frankly. Let's say I have a list (int * int list) list , for example: myList = [(1, [2]); (5, [6; 7]); (3, [2]); (6, [3; 7]); (8, [7]); (4, [3; 1])];; and that means, that

Topological sort using DFS without recursion

喜你入骨 提交于 2019-11-30 02:22:18
I know the common way to do a topological sort is using DFS with recursion. But how would you do it using stack<int> instead of recursion? I need to obtain the reversed post-order but I'm kinda stuck: The graph is a vector<vector<int> > adjacency list The following is the DFS which I want to use for topological sort bool visited[MAX]={0}; stack<int> dfs, postOrder; vector<int> newVec; vector<int>::iterator it; for(int i=0;i<MAX;i++){ if(visited[i]==false){ dfs.push(i); } while(!dfs.empty()){ int node=dfs.top(); dfs.pop(); visited[node]=true; newVec=graph[node]; //vector of neighboors for(it

Topological order using bfs

风流意气都作罢 提交于 2019-11-29 14:57:13
The following question was found in Sedgewick and Wayne book about algorithms in java: 4.2.19 Topological sort and BFS. Explain why the following algorithm does not necessarily produce a topological order: Run BFS, and label the vertices by increasing distance to their respective source. I was trying to prove it finding a counter example. But, everytime I try, I get a topological order. I mean, I don't understand why this not work: If the source of a vertex comes before it, why don't we have a topological order? I think to prove it, we need to find a vertex that its source comes before, but I

Topological sort to find the number of paths to t

左心房为你撑大大i 提交于 2019-11-29 09:08:02
问题 I have to develop an O(|V|+|E|) algorithm related to topological sort which, in a directed acyclic graph (DAG), determines the number of paths from each vertex of the graph to t (t is a node with out-degree 0). I have developed a modification of DFS as follow: DFS(G,t): for each vertex u ∈ V do color(u) = WHITE paths_to_t(u) = 0 for each vertex u ∈ V do if color(u) == WHITE then DFS-Visit(u,t) DFS-Visit(u,t): color(u) = GREY for each v ∈ neighbors(u) do if v == t then paths_to_t(u) = paths_to

Topological sort of cyclic graph with minimum number of violated edges

本秂侑毒 提交于 2019-11-29 07:11:00
问题 I am looking for a way to perform a topological sorting on a given directed unweighted graph, that contains cycles. The result should not only contain the ordering of vertices, but also the set of edges, that are violated by the given ordering. This set of edges shall be minimal. As my input graph is potentially large, I cannot use an exponential time algorithm. If it's impossible to compute an optimal solution in polynomial time, what heuristic would be reasonable for the given problem? 回答1:

Topological sort using DFS without recursion

左心房为你撑大大i 提交于 2019-11-28 23:16:52
问题 I know the common way to do a topological sort is using DFS with recursion. But how would you do it using stack<int> instead of recursion? I need to obtain the reversed post-order but I'm kinda stuck: The graph is a vector<vector<int> > adjacency list The following is the DFS which I want to use for topological sort bool visited[MAX]={0}; stack<int> dfs, postOrder; vector<int> newVec; vector<int>::iterator it; for(int i=0;i<MAX;i++){ if(visited[i]==false){ dfs.push(i); } while(!dfs.empty()){

deceptively simple implementation of topological sorting in python

天涯浪子 提交于 2019-11-28 23:15:23
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 "minimal"? I've watched this video and the idea is quite clever so I was wondering if it'd be possible to