graph-algorithm

How to find connected components in Matlab?

有些话、适合烂在心里 提交于 2019-11-27 09:09:14
array A = 2 3 2 5 4 8 5 6 7 8 I'd like to get the result as 'conidx = [2 3 5 6] and [4 7 8]'. One of the values of [2 3] exists in the 2nd row, One of the values of [2 5] exists in the 4th row, so [2 3], [2 5] and [5 6] are connected, finally I can get the connected indices as [2 3 5 6]. Otherwise, one of the values of [4 8] exists in the 5th row, so [4 8] and [7 8] are connected, finally I can get the connected indices as [4 7 8]. [3]<-->[2]<-->[5]<-->[6] and [4]<-->[8]<-->[7] build a graph and use graphconncomp G = sparse( A(:,1), A(:,2), 1, max(A(:)), max(A(:)) ); G = G + G.'; %' make graph

Best algorithm to determine if an undirected graph is a tree

可紊 提交于 2019-11-27 07:02:56
问题 What is the time complexity of the Best algorithm to determine if an undirected graph is a tree?? can we say Big-oh(n) , with n vertices?? 回答1: Yes, it is O(n). With a depth-first search in a directed graph has 3 types of non-tree edges - cross, back and forward. For an undirected case, the only kind of non-tree edge is a back edge. So, you just need to search for back edges. In short, choose a starting vertex. Traverse and keep checking if the edge encountered is a back edge. If you find n-1

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

做~自己de王妃 提交于 2019-11-27 06:38:25
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. 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 misconception - it is simply not true. The classic DFS algorithm cannot be obtained by replacing the BFS queue with

Minimum number of days required to solve a list of questions

有些话、适合烂在心里 提交于 2019-11-27 04:31:57
问题 There are N problems numbered 1..N which you need to complete. You've arranged the problems in increasing difficulty order, and the ith problem has estimated difficulty level i. You have also assigned a rating vi to each problem. Problems with similar vi values are similar in nature. On each day, you will choose a subset of the problems and solve them. You've decided that each subsequent problem solved on the day should be tougher than the previous problem you solved on that day. Also, to not

Python connected components

拥有回忆 提交于 2019-11-27 04:01:05
问题 I'm writing a function get_connected_components for a class Graph : def get_connected_components(self): path=[] for i in self.graph.keys(): q=self.graph[i] while q: print(q) v=q.pop(0) if not v in path: path=path+[v] return path My graph is: {0: [(0, 1), (0, 2), (0, 3)], 1: [], 2: [(2, 1)], 3: [(3, 4), (3, 5)], \ 4: [(4, 3), (4, 5)], 5: [(5, 3), (5, 4), (5, 7)], 6: [(6, 8)], 7: [], \ 8: [(8, 9)], 9: []} where the keys are the nodes and the values are the edge. My function gives me this

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

时光总嘲笑我的痴心妄想 提交于 2019-11-27 02:28:17
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? Hans-Peter Störr 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 first search (DFS) might take an extremely long time, but BFS could be faster. If the tree is

Efficiently find all connected induced subgraphs

可紊 提交于 2019-11-27 02:19:48
问题 Is there an efficient(*) algorithm to find all the connected (induced) subgraphs of a connected undirected vertex-labelled graph? (*) I appreciate that, in the general case, any such algorithm may have O(2^n) complexity because, for a clique (Kn), there are 2^n connected subgraphs. However, the graphs I'm typically dealing with will have far fewer connected subgraphs, so I'm looking for a way to generate them without having to consider all 2^n subgraphs and throw away those that aren't

Optimal way of filling 2 knapsacks?

一笑奈何 提交于 2019-11-27 01:09:12
问题 The dynamic programming algorithm to optimally fill a knapsack works well in the case of one knapsack. But is there an efficient known algorithm that will optimally fill 2 knapsacks (capacities can be unequal)? I have tried the following two approaches and neither of them is correct. First fill the first knapsack using the original DP algorithm to fill one knapsack and then fill the other knapsack. First fill a knapsack of size W1 + W2 and then split the solution into two solutions (where W1

Why does Dijkstra's algorithm use decrease-key?

久未见 提交于 2019-11-26 23:48:15
问题 Dijkstra's algorithm was taught to me was as follows while pqueue is not empty: distance, node = pqueue.delete_min() if node has been visited: continue else: mark node as visited if node == target: break for each neighbor of node: pqueue.insert(distance + distance_to_neighbor, neighbor) But I've been doing some reading regarding the algorithm, and a lot of versions I see use decrease-key as opposed to insert. Why is this, and what are the differences between the two approaches? 回答1: The

Calculating the shortest route between two points

血红的双手。 提交于 2019-11-26 22:58:36
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 possible to find out the shortest route (if there is one) between two points of the map. How would you