graph-algorithm

algorithm to resolve version scope based dependency

若如初见. 提交于 2019-11-29 02:49:59
问题 I have a problem on a dependency algorithm, the dependency is similar to maven dependency, except it's strict version scope based. For example: component A, version 1 depends on: component B, version 1~3; and component C, version 2~3 component D, version 1 depends on: component B, version 2~4; and component C, version 1~2 Now, I want to get dependencies when I want to install component A, version 1 and component D, version 1. Because they are all depend on component B,C so I need a correct

How to keep track of depth in breadth first search?

a 夏天 提交于 2019-11-29 02:25:43
问题 I have a tree as input to the breadth first search and I want to know as the algorithm progresses at which level it is? # Breadth First Search Implementation graph = { 'A':['B','C','D'], 'B':['A'], 'C':['A','E','F'], 'D':['A','G','H'], 'E':['C'], 'F':['C'], 'G':['D'], 'H':['D'] } def breadth_first_search(graph,source): """ This function is the Implementation of the breadth_first_search program """ # Mark each node as not visited mark = {} for item in graph.keys(): mark[item] = 0 queue, output

Eppstein's algorithm and Yen's algorithm for k shortest paths

放肆的年华 提交于 2019-11-29 01:51:19
I'm trying to understand exactly how these algorithms work, but I have been unable to find a simple explanation. I would greatly appreciate it if someone could provide or point me to a description of these algorithms that is easier to understand than the description in the original papers. Thanks. Alma Rahat First of all let me provide you with the links to the papers you were talking about. Eppstein's paper: D. Eppstein, “Finding the k shortest paths,” SIAM J. Comput., vol. 28, no. 2, pp.652–673, Feb. 1999 Yen's paper: J. Y. Yen, “Finding the K Shortest Loopless Paths in a Network,”

Hopcroft–Karp algorithm in Python

人走茶凉 提交于 2019-11-29 01:09:28
问题 I am trying to implement the Hopcroft Karp algorithm in Python using networkx as graph representation. Currently I am as far as this: #Algorithms for bipartite graphs import networkx as nx import collections class HopcroftKarp(object): INFINITY = -1 def __init__(self, G): self.G = G def match(self): self.N1, self.N2 = self.partition() self.pair = {} self.dist = {} self.q = collections.deque() #init for v in self.G: self.pair[v] = None self.dist[v] = HopcroftKarp.INFINITY matching = 0 while

Algorithm for diameter of graph?

巧了我就是萌 提交于 2019-11-28 23:57:11
If you have a graph, and need to find the diameter of it (which is the maximum distance between two nodes), how can you do it in O(log v * (v + e)) complexity. Wikipedia says you can do this using Dijkstra's algorithm with a binary heap . But I don't understand how this works. Can someone explain please? Or show a pseudocode? For a general Graph G=(V,E) there is no O(log V * (V + E)) time complexity algorithm known for computing the diameter. The current best solution is O(V*V*V) , e.g., by computing all shortest Paths with Floyd Warshall's Algorithm. For sparse Graphs, i.e. when E is in o(N*N

Finding Contiguous Areas of Bits in 2D Bit Array

醉酒当歌 提交于 2019-11-28 19:45:42
问题 The Problem I have a bit array which represents a 2-dimensional "map" of "tiles". This image provides a graphical example of the bits in the bit array: I need to determine how many contiguous "areas" of bits exist in the array. In the example above, there are two such contiguous "areas", as illustrated here: Tiles must be located directly N, S, E or W of a tile to be considered "contiguous". Diagonally-touching tiles do not count. What I've Got So Far Because these bit arrays can become

Computing target number from numbers in a set

*爱你&永不变心* 提交于 2019-11-28 18:21:28
I'm working on a homework problem that asks me this: Tiven a finite set of numbers, and a target number, find if the set can be used to calculate the target number using basic math operations (add, sub, mult, div) and using each number in the set exactly once (so I need to exhaust the set). This has to be done with recursion. So, for example, if I have the set {1, 2, 3, 4} and target 10, then I could get to it by using ((3 * 4) - 2)/1 = 10. I'm trying to phrase the algorithm in pseudo-code, but so far haven't gotten too far. I'm thinking graphs are the way to go, but would definitely

Why can't Prim's or Kruskal's algorithms be used on a directed graph?

ε祈祈猫儿з 提交于 2019-11-28 17:57:29
Prim's and Kruskal's algorithms are used to find the minimum spanning tree of a graph that is connected and undirected. Why can't they be used on a graph that is directed? It's a minor miracle that these algorithms work in the first place -- most greedy algorithms just crash and burn on some instances. Assuming that you want to use them to find a minimum spanning arborescence (directed paths from one vertex to all others), then one problematic graph for Kruskal looks like this. 5 --> a / / ^ s 1| |2 \ v / --> b 3 We'll take the a->b arc of cost 1, then get stuck because we really wanted s->b

Graph serialization

∥☆過路亽.° 提交于 2019-11-28 17:25:59
I'm looking for a simple algorithm to 'serialize' a directed graph. In particular I've got a set of files with interdependencies on their execution order, and I want to find the correct order at compile time. I know it must be a fairly common thing to do - compilers do it all the time - but my google-fu has been weak today. What's the 'go-to' algorithm for this? Andrew Peters Topological Sort (From Wikipedia): In graph theory, a topological sort or topological ordering of a directed acyclic graph (DAG) is a linear ordering of its nodes in which each node comes before all nodes to which it has

Construct a minimum spanning tree covering a specific subset of the vertices

痞子三分冷 提交于 2019-11-28 17:12:09
I have an undirected, positive-edge-weight graph (V,E) for which I want a minimum spanning tree covering a subset k of vertices V (the Steiner tree problem). I'm not limiting the size of the spanning tree to k vertices; rather I know exactly which k vertices must be included in the MST. Starting from the entire MST I could pare down edges/nodes until I get the smallest MST that contains all k . I can use Prim's algorithm to get the entire MST, and start deleting edges/nodes while the MST of subset k is not destroyed; alternatively I can use Floyd-Warshall to get all-pairs shortest paths and