depth-first-search

Edges of the graph

寵の児 提交于 2019-12-24 11:25:09
问题 In order to find the kind of the edges of a graph, at which we applied the Depth-first search algorithm, we could use this: tree edges: x -> y when [d[y],f[y]] ⊂ [d[x],f[x]] forward edges: x -> y when [d[x],f[x]] ⊂ [d[y],f[y]] back edges: x -> y when [d[y],f[y]] ⊂ [d[x],f[x]] Cross edges: x -> y when [d[x],f[x]] ∩ [d[y],f[y]]=∅ Discovery Time : The discovery time d[v] is the number of nodes discovered or finished before first seeing v. Finishing Time : The finishing time f[v] is the number of

C++ Boost graph library: Building a vector of vertices visited in an undirected graph search?

穿精又带淫゛_ 提交于 2019-12-24 02:12:52
问题 From what I can gather of how to use BGL in order to invoke a DFS of a graph from a known root node I need to do something along the lines of class MyVisitor : public boost::default_dfs_visitor { public: void discover_vertex(MyVertex v, const MyGraph& g) const { cerr << v << endl; return; } }; void bfsMethod(Graph g, int rootNodeId) { boost::undirected_dfs(g, vertex(rootNodeId,g), boost::visitor(vis)); } Now I am not sure how I alter this so that std::vector of vertexId's (or pointers) is

Depth first search in a distributed way

泄露秘密 提交于 2019-12-24 01:19:46
问题 I have tried implementing depth first search in c# but I am not exactly sure how to do this the distributed computing way. If you guys can help me out in this i would be really grateful :) You can find my DFS code below public class DFS { static List<string> traversedList = new List<string>(); static List<string> parentList = new List<string>(); public static void Main(string[] args) { int N = 100; int M = N * 4; int P = N * 16; Stack newstack = new Stack(); List<string> global_list=new List

How to store visited states in iterative deepening / depth limited search?

左心房为你撑大大i 提交于 2019-12-23 21:50:34
问题 Update: Search for the first solution. for a normal Depth First Search it is simple, just use a hashset bool DFS (currentState) = { if (myHashSet.Contains(currentState)) { return; } else { myHashSet.Add(currentState); } if (IsSolution(currentState) return true; else { for (var nextState in GetNextStates(currentState)) if (DFS(nextState)) return true; } return false; } However, when it becomes depth limited, i cannot simply do this bool DFS (currentState, maxDepth) = { if (maxDepth = 0) return

couting back edges to get the number of cylces in a directed graph

断了今生、忘了曾经 提交于 2019-12-23 18:13:46
问题 I have been writing code to get all the possible cycles in a directed graph. Here is an implementation that keeps track of back edges and whenever one back edge is found, it return true that one cycle is detected. I extended this to the following: calculate all the possible back edges in a tree, the number of back edges should give the number of cycles. Not sure if this correct. using this, I implemented the following: The count variable below is not useful. Initially I had it to give the

Checking for odd cycles in an undirected graph

三世轮回 提交于 2019-12-23 16:03:52
问题 I'm back with another similar question. I am currently working on a Java program that will check if a graph is 2-colorable, i.e. if it contains no odd cycles (cycles of odd number length). The entire algorithm is supposed to run in O(V+E) time (V being all vertices and E being all edges in the graph). My current algorithm does a Depth First Search, recording all vertices in the path it takes, then looks for a back edge, and then records between which vertices the edge is between. Next it

How to implement dfs using recursion?

断了今生、忘了曾经 提交于 2019-12-23 09:27:18
问题 I'm trying to implement DFS with recursion using the following code, public static void dfs(int i, int[][] mat, boolean [] visited){ visited[i] = true; // Mark node as "visited" System.out.print(i + "\t"); for ( int j = 0; j < visited.length; j++ ){ if ( mat[i][j] ==1 && !visited[j] ){ dfs(j, mat, visited); // Visit node } } } I have a matrix and an array for tracking visited nodes, // adjacency matrix for uni-directional graph int [][] arr = { // 1 2 3 4 5 6 7 8 9 10 { 0, 1, 1, 1, 0, 0, 0, 0

Algorithm for counting connected components of a graph in Python

岁酱吖の 提交于 2019-12-23 04:14:54
问题 I try to write a script that counts connected components of a graph and I can't get the right solution. I have a simple graph with 6 nodes (vertexes), nodes 1 and 2 are connected, and nodes 3 and 4 are connected (6 vertexes; 1-2,3-4,5,6). So the graph contains 4 connected components. I use following script to count connected components, but I get wrong result (2). nodes = [[1, [2], False], [2, [1], False], [3, [4], False], [4, [3], False], [5, [], False], [6, [], False]] # 6 nodes, every node

Algorithm to find and print simple cycle in complexity of O(n) on undirected graph

一曲冷凌霜 提交于 2019-12-22 10:34:27
问题 Given graph G(V,E), un-directed graph. |E| = m, |V| = n The graph's data structure is Adjacency list How to find and print simple cycle (or print that there is no such cycle) in complexity of O(n) ? (If there is cycle the output should be the list of vertex that are part of the cycle.) I know how to find cycle on complexity of O(n) , there is also gudies on the internet. My problem is how to print it. This is what I tried to do: DFS-CheckCycle ( Graph G) { p[] <- null //parent array foreach v

Ruby recursive DFS method

早过忘川 提交于 2019-12-22 09:57:17
问题 I have some troubles with recursive method for depth first search algorithm implementation. Here's the binary tree photo: The method works well with the right side of the tree (55, 89, 144), but it returns nil when it comes to the left side, even though it puts "yes". So, what's wrong with the code? The node is an instance of Node class that has value (integer) and links to the left and right child (other instances of Node class) which is nil if it doesn't have the child from that side. Here