graph-algorithm

Dijkstras Algorithm doesn't appear to work, my understanding must be flawed

 ̄綄美尐妖づ 提交于 2019-12-07 04:49:38
问题 Here's my interpretation of how Dijkstra's algorithm as described by wikipedia would deal with the below graph. First it marks the shortest distance to all neighbouring nodes, so A gets 1 and C gets 7. Then it picks the neighbour with the current shortest path. This is A. The origin is marked as visited and will not be considered again. The shortest (and only) path from the origin to B through A is now 12. A is marked as visited. The shortest path from the origin to the Destination through B

Using BFS or DFS to determine the connectivity in a non connected graph?

半腔热情 提交于 2019-12-07 03:42:03
问题 How can i design an algorithm using BFS or DFS algorithms in order to determine the connected components of a non connected graph , the algorithm must be able to denote the set of vertices of each connected component . This is my aproach: 1) Initialize all vertices as not visited. 2) Do a DFS traversal of graph starting from any arbitrary vertex v. If DFS traversal doesn’t visit all vertices, then return false. 3) Reverse all arcs (or find transpose or reverse of graph) 4) Mark all vertices

Graph Has Two / Three Different Minimal Spanning Trees ?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 01:54:03
问题 I'm trying to find an efficient method of detecting whether a given graph G has two different minimal spanning trees. I'm also trying to find a method to check whether it has 3 different minimal spanning trees. The naive solution that I've though about is running Kruskal's algorithm once and finding the total weight of the minimal spanning tree. Later , removing an edge from the graph and running Kruskal's algorithm again and checking if the weight of the new tree is the weight of the

Algorithm to clone a graph

为君一笑 提交于 2019-12-07 01:00:37
问题 Algorithm to clone a tree is quite easy, we can do pre-order traversal for that. Is there an efficient algorithm to clone a graph? I tried a similar approach, and concluded we need to maintain a hash-map of nodes already added in the new graph, else there will be duplication of nodes, since one node can have many parents. 回答1: It suffices to do a depth first search and copy each node as it's visited. As you say, you need some way of mapping nodes in the original graph to corresponding copies

How to iterate through a list of objects and assign children?

冷暖自知 提交于 2019-12-06 15:58:42
I have a Location POJO that stores Location objects parsed in from a JSON file, which I want to map to a graph. Each node's location in the graph corresponds to it's id field, where id="1" is the start node and id="10" is the goal node. To solve this I adapted a Node class to include methods such as setWeight() , addChildLocation() etc , But I'm not sure how to create the graph from my list of locations. I know how to create the graph by hard coding the location's and calling addChildren, by doing the following, but not sure how to create it from a list of already available Location objects:

Shortest path graph algorithm help Boost

时间秒杀一切 提交于 2019-12-06 14:27:44
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,0), (2,0), (3,0) or (4,0). The goal vertex is always in the right column (column with index 6) and

Kruskal's Algorithm in C++

我只是一个虾纸丫 提交于 2019-12-06 09:33:14
I am looking for C++ Kruskal implementations to benchmark against my own... If you know a few good ones, please share! 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

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

我怕爱的太早我们不能终老 提交于 2019-12-06 09:32:11
问题 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

Printing(not detecting) cycle with topological sort

非 Y 不嫁゛ 提交于 2019-12-06 06:39:26
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 backwards through vertices with positive indegree; since every vertex on the trace back has a positive indegree

Algorithm to find intersections in a polar plane

China☆狼群 提交于 2019-12-06 05:55:07
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? Computational complexity is less important than simplicity of implementation. If you are striving for simplicity