graph-algorithm

Minimal addition to strongly connected graph

这一生的挚爱 提交于 2019-12-05 01:22:03
I have a set of nodes and set of directed edges between them. The edges have no weight. How can I found minimal number of edges which has to be added to make the graph strongly connected (ie. there should be a path from every node to all others)? Does this problem have a name? It's a really classical graph problem. Run algorithm like Tarjan-SCC algorithm to find all SCCs. Consider each SCC as a new vertice, link a edge between these new vertices according to the origin graph, we can get a new graph. Obviously, the new graph is a Directed Acyclic Graph(DAG). In the DAG, find all vertices whose

Directed Acyclic Graph with multi-parent nodes

孤街醉人 提交于 2019-12-04 19:24:50
Given: A directed acyclic graph with weighted edges, where a node can have multiple parents. Problem: For each child of root node, find a minimum-cost(sum of weights) path from such a child to some leaf which can be reached. A node can only be present in one such min-cost paths. Example graph: In the above graph, for node 2, all the available paths are: 2 -> 5 2 -> 1 -> 9 -> 6 2 -> 1 -> 10 -> 6 Among which 2 -> 1 -> 10 -> 6 has minimum cost of 3.5 Similarly, for node 4, all the available paths are: 4 -> 11 -> 8 4 -> 7 -> 10 -> 6 Among which 4 -> 7 -> 10 -> 6 has minimum cost of 3.0 The result

Can we apply the Bellman-Ford algorithm to an Undirected Graph?

旧城冷巷雨未停 提交于 2019-12-04 18:48:33
问题 I know that Bellman-Ford Algorithm works for directed graphs. Will it will work for an undirected graph? It seems that with an undirected graph, it will not be able to detect cycles because parallel edges will be considered cycles. Is this true or not? Can the algorithm be applied? 回答1: As a matter of fact any undirected graph is also a directed graph. You just have to specify any edges {u, v} twice (u, v) and (v, u). But don't forget, that this also means any edge with a negative weight will

What algorithms does D3.js use for the force-directed graph?

假如想象 提交于 2019-12-04 18:15:30
问题 I would be interested to know exactly what algorithms D3 uses to achieve the force-directed graph feature in the library. Having read Kobourov's summary of the history of force-directed graphs left me a bit baffled as to what is the exact algorithm or method (combination of algorithms / heuristics) used in the library. D3 API reference says Barnes-Hut algorithm is used to calculate the charges acting on bodies, an O(N*log(N)) operation. Kobourov's article mentions that Quigley-Eades algorithm

Algorithm for finding a spanning tree with weights of 1 and 2 only

断了今生、忘了曾经 提交于 2019-12-04 18:05:19
Given a weighted, connected, simple undirected graph G with weights of only 1 and 2 on each edge, find the MST of G in O(V+E). Any ideas? Sorry for the phrasing of the question, I tried translating it as best as I could. In Prim's algorithm you need a way of storing active edges such that you can access and delete the edge with lowest weight. Normally there are a wide range of weights and some kind of heap data structure is used to store the edges. However, in this case, the weights are either 1 or 2, and so you can simply store the edges in 2 separate lists, one for edges with weight 1, and

To print the boundary of Binary Tree

懵懂的女人 提交于 2019-12-04 17:51:44
问题 I have been asked in an interviews to print the boundary of the Binary Tree. For example. 1 / \ 2 3 / \ / \ 4 5 6 7 / \ \ 8 9 10 Answer will be : 1, 2, 4, 8, 9, 10, 7, 3 I have given the following answer. First Method : I have used a Bool variable to solve the above problem. void printLeftEdges(BinaryTree *p, bool print) { if (!p) return; if (print || (!p->left && !p->right)) cout << p->data << " "; printLeftEdges(p->left, print); printLeftEdges(p->right, false); } void printRightEdges

Why does Dijkstra's Algorithm use a heap (priority queue)?

最后都变了- 提交于 2019-12-04 17:41:57
问题 I have tried using Djikstra's Algorithm on a cyclic weighted graph without using a priority queue (heap) and it worked. Wikipedia states that the original implementation of this algorithm does not use a priority queue and runs in O(V 2 ) time. Now if we just removed the priority queue and used normal queue, the run time is linear, i.e. O(V+E). Can someone explain why we need the priority queue? 回答1: I had the exact same doubt and found a test case where the algorithm without a priority_queue

Iterative Deepening A Star (IDA*) to solve n-puzzle (sliding puzzle) in Java

蓝咒 提交于 2019-12-04 17:08:25
I've implemented a program able to solve the n-puzzle problem with A*. Since the space of the states is too big I cannot precompile it and I have to calculate the possible states at runtime. In this way A* works fine for a 3-puzzle, but for a 4-puzzle can take too long. Using Manhattan distance adjusted with linear conflicts, if the optimal solution requires around 25 moves is still fast, around 35 takes 10 seconds, for 40 takes 180 seconds. I haven't tried more yet. I think that's because I must keep all visited states, since I'm using functions admissible but (I think) not consistent (i

Algorithm to find a random Hamiltonian path in a grid?

放肆的年华 提交于 2019-12-04 12:02:52
问题 I'm looking for an efficient algorithm that is able to find an as random as possible Hamiltonian path in a bidirectional N*M grid. Does anyone know where I can find, or how to go about constructing such an algorithm? I've already found an efficient approach (see image below). The end result here is a Hamiltonian cycle. Removing a random edge will make it a Hamiltonian path. This algorithm is efficient, but does not provide enough randomness. This approach will always have the begin and end

Determine if a given weighted graph has unique MST

空扰寡人 提交于 2019-12-04 10:56:44
问题 I'm looking for an algorithm (or any other way) to determine if a given weighted graph has unique MST (Minimum spanning tree) in O(ElogV)? I don't know anything about the weights (e.g. weight(e1) != weight(e2)), and the algorithm just return True if this graph has only one unique MST or False if not. I started by using Kruskal's algo, and check if find-set(u)==find-set(v) so there is a circle in the MST, but this way does not cover all the scenarios as I thought :( Thanks a lot! Tomer. 回答1: