depth-first-search

Topological sort using DFS without recursion

喜你入骨 提交于 2019-11-30 02:22:18
I know the common way to do a topological sort is using DFS with recursion. But how would you do it using stack<int> instead of recursion? I need to obtain the reversed post-order but I'm kinda stuck: The graph is a vector<vector<int> > adjacency list The following is the DFS which I want to use for topological sort bool visited[MAX]={0}; stack<int> dfs, postOrder; vector<int> newVec; vector<int>::iterator it; for(int i=0;i<MAX;i++){ if(visited[i]==false){ dfs.push(i); } while(!dfs.empty()){ int node=dfs.top(); dfs.pop(); visited[node]=true; newVec=graph[node]; //vector of neighboors for(it

Finding the number of paths of given length in a undirected unweighted graph

冷暖自知 提交于 2019-11-30 02:07:07
'Length' of a path is the number of edges in the path. Given a source and a destination vertex, I want to find the number of paths form the source vertex to the destination vertex of given length k. We can visit each vertex as many times as we want, so if a path from a to b goes like this: a -> c -> b -> c -> b it is considered valid. This means there can be cycles and we can go through the destination more than once. Two vertices can be connected by more than one edge. So if vertex a an vertex b are connected by two edges, then the paths , a -> b via edge 1 and a -> b via edge 2 are

C++ pass by reference

爱⌒轻易说出口 提交于 2019-11-29 22:20:25
问题 I've recently (4 days) started to learn C++ coming from C / Java background. In order to learn a new language I ussualy start by re-implementing different classical algorithms, as language specific as I can. I've come to this code, its a DFS - Depth First Search in an unoriented graph. Still from what I read it's best to pass parameters by references in C++. Unfortunately I can't quite grasp the concept of reference. Every time I need a reference, I get confused and I think in terms of

Detecting cycles in a graph using DFS: 2 different approaches and what's the difference

隐身守侯 提交于 2019-11-29 18:52:53
Note that a graph is represented as an adjacency list. I've heard of 2 approaches to find a cycle in a graph: Keep an array of boolean values to keep track of whether you visited a node before. If you run out of new nodes to go to (without hitting a node you have already been), then just backtrack and try a different branch. The one from Cormen's CLRS or Skiena: For depth-first search in undirected graphs, there are two types of edges, tree and back. The graph has a cycle if and only if there exists a back edge. Can somebody explain what are the back edges of a graph and what's the diffirence

Unexpected result of my DFS tree (C++)

只愿长相守 提交于 2019-11-29 16:51:19
I have solved this problem!!! I found that if i have to use vector<Node*> children; . But I am not very sure the reason, can someone tell me why? Thanks:) Question: I use test.cpp to generate a tree structure like: The result of (ROOT->children).size() is 2 , since root has two children. The result of ((ROOT->children)[0].children).size() should be 2 , since the first child of root has two children. But the answer is 0 , why? It really confuse for me. test.cpp (This code is runnable in visual studio 2010) #include <iostream> #include <vector> using namespace std; struct Node { int len; vector

JavaScript Depth-first search

▼魔方 西西 提交于 2019-11-29 15:42:55
问题 I am trying to implement DFS in JavaScript but I am having a little problem. Here is my Algorithm class: "use strict"; define([], function () { return function () { var that = this; this.search = function (searchFor, node) { if (searchFor === node.getValue()) { return node; } var i, children = node.getChildren(), child, found; for (i = 0; i < children.length; i += 1) { child = children[i]; found = that.search(searchFor, child); if (found) { return found; } } }; }; }); My Node class which

Depth-first flattened collection of an object hierarchy using LINQ

蓝咒 提交于 2019-11-29 14:31:46
I have an object hierarchy (MasterNode -> ChildNodes) where master and child nodes are of the same type, and there are only two levels (top level and children) like this ('A' is parent of D,E and F, 'B' is parent of G, etc) A--+ | D | E | F | B--+ | G | C--+ H I Suppose I have a MasterNodes as an IEnumerable of the parent objects (A,B,C) and given a parent object X I can get an IEnumerable of its children by X.children I know that I can enumerate all of the leaf (child nodes) with the SelectMany method or by using from parent in Masternodes from child in parent.children select child This will

Kernel module that iterates over all tasks using depth first tree

喜夏-厌秋 提交于 2019-11-29 13:26:31
问题 So I know how to create a kernel and to iterate over the processes linearly by Simply including linux/sched.h and using the following code: struct task_struct *task; for_each_process(task) { printk("Name: %s PID: [%d]\n", task->comm, task->pid); } How can I print these tasks using a depth first search? I want my output to be similar to the one of ps -eLf . The following patch of code has been given for reference: struct task_struct *task; struct list_head *list; list_for_each(list, &init_task

Topological sort to find the number of paths to t

左心房为你撑大大i 提交于 2019-11-29 09:08:02
问题 I have to develop an O(|V|+|E|) algorithm related to topological sort which, in a directed acyclic graph (DAG), determines the number of paths from each vertex of the graph to t (t is a node with out-degree 0). I have developed a modification of DFS as follow: DFS(G,t): for each vertex u ∈ V do color(u) = WHITE paths_to_t(u) = 0 for each vertex u ∈ V do if color(u) == WHITE then DFS-Visit(u,t) DFS-Visit(u,t): color(u) = GREY for each v ∈ neighbors(u) do if v == t then paths_to_t(u) = paths_to

Dijkstra's algorithm with 'must-pass' nodes

老子叫甜甜 提交于 2019-11-29 05:15:49
I am trying to implement Dijkstra's algorithm which can find the shortest path between the start node and the end node. Before reach the end node there are some 'must-pass' intermediate nodes (more than one) for example 2 or 3 must pass nodes which must pass before reach the end node. If i have one must pass node the solution i found is find two different paths from the must pass node to destination and from must pass node to start node. I am out of ideas how i can implement such an algorithm. Any suggestions? Thanks. List<Node> closestPathFromOrigin = null; double maxD = Double.POSITIVE