graph-algorithm

When is it practical to use Depth-First Search (DFS) vs Breadth-First Search (BFS)?

孤街浪徒 提交于 2019-11-26 22:14:10
问题 I understand the differences between DFS and BFS, but I'm interested to know when it's more practical to use one over the other? Could anyone give any examples of how DFS would trump BFS and vice versa? 回答1: That heavily depends on the structure of the search tree and the number and location of solutions (aka searched-for items). If you know a solution is not far from the root of the tree, a breadth first search (BFS) might be better. If the tree is very deep and solutions are rare, depth

Obtaining connected components in R

China☆狼群 提交于 2019-11-26 21:49:41
问题 I have a matrix with values 0 or 1 and I would like to obtain a list of groups of adjacent 1's. For example, the matrix mat = rbind(c(1,0,0,0,0), c(1,0,0,1,0), c(0,0,1,0,0), c(0,0,0,0,0), c(1,1,1,1,1)) > mat [,1] [,2] [,3] [,4] [,5] [1,] 1 0 0 0 0 [2,] 1 0 0 1 0 [3,] 0 0 1 0 0 [4,] 0 0 0 0 0 [5,] 1 1 1 1 1 should return the following 4 connected components: C1 = {(1,1);(2,1)} C2 = {(2,4)} C3 = {(3,3)} C4 = {(5,1);(5,2);(5,3);(5,4);(5,5)} Does anybody has an idea of how to do it fast in R? My

What are the differences between segment trees, interval trees, binary indexed trees and range trees?

十年热恋 提交于 2019-11-26 21:12:40
What are differences between segment trees, interval trees, binary indexed trees and range trees in terms of: Key idea/definition Applications Performance/order in higher dimensions/space consumption Please do not just give definitions. All these data structures are used for solving different problems: Segment tree stores intervals, and optimized for " which of these intervals contains a given point " queries. Interval tree stores intervals as well, but optimized for " which of these intervals overlap with a given interval " queries. It can also be used for point queries - similar to segment

What is the problem name for Traveling salesman problem(TSP) without considering going back to starting point?

£可爱£侵袭症+ 提交于 2019-11-26 16:05:31
问题 I would like to know what is the problem name for TSP w/o considering the way of going back to starting point and what is the algorithm to solve this. I looked into Shortest path problem but that is not what I am looking for, the problem only find the shortest path from 2 assigned points. But what I am looking for is the problem which we give n points and inputting only 1 starting point. Then, find the shortest path traveling all points exactly once. (end point can be any point.) I also

Negative weights using Dijkstra's Algorithm

左心房为你撑大大i 提交于 2019-11-26 12:40:56
I am trying to understand why Dijkstra's algorithm will not work with negative weights. Reading an example on Shortest Paths , I am trying to figure out the following scenario: 2 A-------B \ / 3 \ / -2 \ / C From the website: Assuming the edges are all directed from left to right, If we start with A, Dijkstra's algorithm will choose the edge (A,x) minimizing d(A,A)+length(edge), namely (A,B). It then sets d(A,B)=2 and chooses another edge (y,C) minimizing d(A,y)+d(y,C); the only choice is (A,C) and it sets d(A,C)=3. But it never finds the shortest path from A to B, via C, with total length 1.

Why is depth-first search claimed to be space efficient?

和自甴很熟 提交于 2019-11-26 12:05:44
问题 In an algorithms course I\'m taking, it\'s said that depth-first search (DFS) is far more space efficient than breadth-first search (BFS). Why is that? Although they are basically doing the same thing, in DFS we\'re stacking the current node\'s successors while in BFS we\'re enqueueing the successors. 回答1: Your confusion is stemming from the fact that you apparently assume that DFS algorithm can be obtained from BFS algorithm by replacing the FIFO queue with a LIFO stack. This is a popular

Calculating the shortest route between two points

折月煮酒 提交于 2019-11-26 09:11:51
问题 I have been working in the past weeks on a multiplayer HTML5 game, using nodejs and websockets . I\'ve been stuck in this problem for a little while. Imagine that I have this tilesheet map implemented with an array ( as shown below ). 1 or brown tiles - there is an obstacle in the way and the player can not pass through it. 0 or green tiles - are free paths where the player is allowed to move. Access any tile on the map by calling: array[x][y] I would like to create the fastest algorithm

Negative weights using Dijkstra's Algorithm

若如初见. 提交于 2019-11-26 03:03:36
问题 I am trying to understand why Dijkstra\'s algorithm will not work with negative weights. Reading an example on Shortest Paths, I am trying to figure out the following scenario: 2 A-------B \\ / 3 \\ / -2 \\ / C From the website: Assuming the edges are all directed from left to right, If we start with A, Dijkstra\'s algorithm will choose the edge (A,x) minimizing d(A,A)+length(edge), namely (A,B). It then sets d(A,B)=2 and chooses another edge (y,C) minimizing d(A,y)+d(y,C); the only choice is

how to rasterize rotated rectangle (in 2d by setpixel)

纵然是瞬间 提交于 2019-11-26 01:09:27
问题 I have got a four 2d vertices A B C D of rotated rectangle, I need to rasterize/draw it (efficiently) in pixelbufer with setpixel(x,y,color) how to do it? i was trying with some code like // convertilg a b c d do up down left right, // calculating some dx_left dx_right on y-- // etc (frustrating on special cases when there are 2 up_y vertices in same line etc) for(;;) { drawhorizontalline(y, xstart, xend, color); if(y==downy) break; y--; xstart+=dxstart; xend+=dxend; if(y==lefty) dxstart =

Finding all cycles in a directed graph

末鹿安然 提交于 2019-11-25 22:48:01
问题 How can I find (iterate over) ALL the cycles in a directed graph from/to a given node? For example, I want something like this: A->B->A A->B->C->A but not: B->C->B 回答1: I found this page in my search and since cycles are not same as strongly connected components, I kept on searching and finally, I found an efficient algorithm which lists all (elementary) cycles of a directed graph. It is from Donald B. Johnson and the paper can be found in the following link: http://www.cs.tufts.edu/comp