graph-algorithm

Finding All Connected Components of an Undirected Graph

。_饼干妹妹 提交于 2019-12-18 12:36:35
问题 I have a list of objects (undirected edges) like below: pairs = [ pair:["a2", "a5"], pair:["a3", "a6"], pair:["a4", "a5"], pair:["a7", "a9"] ]; I need to find all components (connected nodes) in separate groups. So from given pairs I need to get: groups = [ group1: ["a2", "a5", "a4"], group2: ["a3", "a6"], group3: ["a7", "a9"] ]; I actually read some answers on here and googled this and that's how I learned this is called "finding connected components in a graph" but, could't find any sample

How to find if a graph is bipartite?

◇◆丶佛笑我妖孽 提交于 2019-12-18 11:26:30
问题 I have been trying to understand the bipartite graph. To my understanding it is a graph G which can be divided into two subgraphs U and V.So that intersection of U and V is a null set and union is graph G. I am trying to find if a graph is bipartite or not using BFS. Still it is not clear to me that how can we find this using BFS. Let us say we have graph defined as below. a:e,f b:e c:e,f,h d:g,h e:a,b,c f:a,c,g g:f,d h:c,d What i need here is step by step explanation of how this graph is a

Algorithm for finding a Hamilton Path in a DAG

本秂侑毒 提交于 2019-12-18 10:34:25
问题 I am referring to Skienna's Book on Algorithms. The problem of testing whether a graph G contains a Hamiltonian path is NP-hard , where a Hamiltonian path P is a path that visits each vertex exactly once. There does not have to be an edge in G from the ending vertex to the starting vertex of P , unlike in the Hamiltonian cycle problem. Given a directed acyclic graph G ( DAG ), give an O(n + m) time algorithm to test whether or not it contains a Hamiltonian path. My approach, I am planning to

Shortest path with a twist

爷,独闯天下 提交于 2019-12-18 06:57:10
问题 I have n vertices and m undirected weighted edges between them (weights are representing minutes). Each vertex contains a number of minutes required to drink a coffee on that vertex. I want to determine the shortest amount of time (minutes) neccessary to get from vertex v to vertex w but with the additional constraint that I have to drink my coffee on exactly one of the vertices on my way from v to w ). Example : (number in the vertex is the amount of minutes required to drink a coffee, the

Relationship between BFS and topological sort

心不动则不痛 提交于 2019-12-18 05:06:23
问题 Topological sort can be done using both a DFS(having edges reversed) and also using a queue . A BFS can also be done using a queue . Is there any relationship between the way elements are stored and retrieved while using queue for a BFS to that when used a queue for topological sorting . Clarification will be helpful . Thanks. 回答1: No, there is not necessarily any relationship. I assume you are referring to the algorithm by Kahn from wikipedia/Topological_sorting#Algorithms, which wikipedia

What's the good of using 3 states for a vertex in DFS?

匆匆过客 提交于 2019-12-18 04:54:07
问题 In the explanation of depth-first search (DFS) in Algorithms in a Nutshell (2nd edition) , the author used 3 states for a vertex, say white (not visited), gray (has unvisited neighbors), black (visited). Two states ( white and black ) are enough for a traverse. Why add the gray state? What's it used for? 回答1: This is a variation of the DFS algorithm shown in Introduction to Algorithms by Coerman at al. When you use 3 colors instead of only 2, it gives you more information. Fist, it allows you

How to find the shortest simple path in a Tree in a linear time?

醉酒当歌 提交于 2019-12-17 18:56:35
问题 Here is a problem from Algorithms book by Vazirani The input to this problem is a tree T with integer weights on the edges. The weights may be negative, zero, or positive. Give a linear time algorithm to find the shortest simple path in T. The length of a path is the sum of the weights of the edges in the path. A path is simple if no vertex is repeated. Note that the endpoints of the path are unconstrained. HINT: This is very similar to the problem of finding the largest independent set in a

Explanation of Algorithm for finding articulation points or cut vertices of a graph

送分小仙女□ 提交于 2019-12-17 15:29:02
问题 I have searched the net and could not find any explanation of a DFS algorithm for finding all articulation vertices of a graph. There is not even a wiki page. From reading around, I got to know the basic facts from here. PDF There is a variable at each node which is actually looking at back edges and finding the closest and upmost node towards the root node. After processing all edges it would be found. But I do not understand how to find this down & up variable at each node during the

Algorithm to simplify a weighted directed graph of debts

爱⌒轻易说出口 提交于 2019-12-17 10:44:08
问题 I've been using a little python script I wrote to manage debt amongst my roommates. It works, but there are some missing features, one of which is simplifying unnecessarily complicated debt structures. For example, if the following weighted directed graph represents some people and the arrows represent debts between them (Alice owes Bob $20 and Charlie $5, Bob owes Charlie $10, etc.): It is clear that this graph should be simplified to the following graph: There's no sense in $10 making its

How to find connected components in Matlab?

删除回忆录丶 提交于 2019-12-17 09:52:46
问题 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] 回答1: build a