graph-algorithm

Singly connected Graph?

无人久伴 提交于 2019-12-08 01:36:19
问题 A singly connected graph is a directed graph which has at most 1 path from u to v ∀ u,v. I have thought of the following solution: Run DFS from any vertex. Now run DFS again but this time starting from the vertices in order of decreasing finish time. Run this DFS only for vertices which are not visited in some previous DFS. If we find a cross edge in the same component or a forward edge, then it is not Singly connected. If all vertices are finished and no such cross of forward edges, then

Shortest path graph algorithm help Boost

三世轮回 提交于 2019-12-08 01:34:59
问题 I have a rectangular grid shaped DAG where the horizontal edges always point right and the vertical edges always point down. The edges have positive costs associated with them. Because of the rectangular format, the nodes are being referred to using zero-based row/column. Here's an example graph: Now, I want to perform a search. The starting vertex will always be in the left column (column with index 0) and in the upper half of the graph. This means I'll pick the start to be either (0,0), (1

Algorithm to find intersections in a polar plane

痴心易碎 提交于 2019-12-07 23:18:53
问题 I have a polar plane relative to a base point (colored green in the diagram below). The points and segments are represented thus: class Node { int theta; double radius; } class Segment { //each segment must have node that is northern relative to other Node northern; Node southern; } I want to figure out if the red line going from the base point to each segment node intersects any other segment. In this example, the red line does intersect. What algorithmic approach should I apply?

Directed graph linear algorithm

本秂侑毒 提交于 2019-12-07 20:36:30
I would like to know the best way to calculate the length of the shortest path between vertex s and every other vertex of the graph in linear time using dynamic programming. The graph is weighted DAG. What you can hope for is an algorithm linear in the number of edges and vertices, i.e. O(|E| + |V|) , which also works correctly in presence of negative weights. This is done by first computing a topological order and then 'exploring' the graph in the order given by this topological order. Some notation: let's call d'(s,v) the shortest distance from s to v and d(u,v) the length/weight of the arc

Kruskal's Algorithm in C++

人走茶凉 提交于 2019-12-07 18:51:56
问题 I am looking for C++ Kruskal implementations to benchmark against my own... If you know a few good ones, please share! 回答1: There's boost::kruskal_minimum_spanning_tree. Prim's algorithm is there too if you want to compare against that. 来源: https://stackoverflow.com/questions/4424512/kruskals-algorithm-in-c

Printing(not detecting) cycle with topological sort

让人想犯罪 __ 提交于 2019-12-07 18:37:46
问题 This is a question in a Data Structures and Algorithm Analysis 3rd edition which was also asked in one of our exams. Write down an algorithm to topologically sort a graph represented by an adjacency list, modified such that the algorithm prints out a cycle, if it is found. First, explain your idea in a few sentences. (Don’t use depth first search, we want just a modification of the basic topological sort.) And the answer is: If no vertex has indegree 0, we can find a cycle by tracing

graph algorithms: reachability from adjacency map

久未见 提交于 2019-12-07 15:51:53
问题 I have a dependency graph that I have represented as a Map<Node, Collection<Node>> (in Java-speak, or f(Node n) -> Collection[Node] as a function; this is a mapping from a given node n to a collection of nodes that depend on n ). The graph is potentially cyclic*. Given a list badlist of nodes, I would like to solve a reachability problem: i.e. generate a Map<Node, Set<Node>> badmap that represents a mapping from each node N in the list badlist to a set of nodes which includes N or other node

How can I find Maximum Common Subgraph of two graphs?

混江龙づ霸主 提交于 2019-12-07 15:30:56
问题 Hi i need a help in finding a graph algorithm im working on the following equation related to distance functions d (g1, g2) = 1- │mcs(g1,g2) │ / │g1│+│g2│-│mcs (g1, g2) │ Where d (g1,g2) : is a distance function based on maximum common sub graph . g1, g2 are two graphs . mcs (g1,g2) : is the maximum common sub graph of two graphs g1,g2 where mcs is the largest graph (by some measure involving the number of nodes and edges )contained in both subject graphs . │g1│ : Cardinality of the common

Algorithm like Bellman-Ford, only for multiple start, single destination?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 10:01:11
问题 Algorithms like the Bellman-Ford algorithm and Dijkstra's algorithm exist to find the shortest path from a single starting vertex on a graph to every other vertex. However, in the program I'm writing, the starting vertex changes a lot more often than the destination vertex does. What algorithm is there that does the reverse--that is, given a single destination vertex, to find the shortest path from every starting vertex? 回答1: Just reverse all the edges, and treated destination as start node.

Closest pair of points

浪子不回头ぞ 提交于 2019-12-07 08:06:13
问题 In http://en.wikipedia.org/wiki/Closest_pair_of_points_problem we can see that it mentions that is at most 6 points that is closest to the point on the other half, which can be represented as the graph below: My question is for point P1 and Point P2, the distance to the red point will exceed sqrt(2)*d, why it is part of the solution? Why it is not at most 4 points that is closest to P rather than at most 6 points? Thanks. 回答1: P1 and P2 are not part of the solution, but they have to be