depth-first-search

Find number of unique routes to specific node using Depth First Search

。_饼干妹妹 提交于 2019-11-30 22:47:45
I have a directed graph with vertexes 123456. Using a depth first search, if I wanted to be able to find the number of unique routes from 1-4 (for example), how would I go about doing it? Here is my current DFS. private final Map<Character, Node> mNodes; private final List<Edge> mEdges; private List<Node> mVisited = new ArrayList<>(); int weight; int numberOfPaths; public DepthFirstSearch(Graph graph){ mNodes = graph.getNodes(); mEdges = new ArrayList<>(graph.getEdges()); for(Node node : mNodes.values()){ node.setVisited(false); } } public void depthFirstSearch(Node source){ source.setVisited

Finding all possible paths of n length in hexagonal grid

别来无恙 提交于 2019-11-30 17:21:57
问题 Assume that a function takes s (origin hexagon), f (target hexagon) and n (length of the path) values as parameters and outputs list of all possible paths that has n length. To visualize the problem please check the figure below: Let's say our origin ( s ) is the red dotted hex (2, 3) and the target ( f ) is the blue dotted one (5, 2) . We want to reach blue dotted hex in 5 steps ( n = 5 ). Also consider that if a walk reaches a specific hex, it may stay in that hex as well in the next step.

C++ pass by reference

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 15:19:46
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 pointers. In my current code, i use pass by value . Here is the code (probably isn't Cppthonic as it should):

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

大憨熊 提交于 2019-11-30 12:07:42
问题 '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

JavaScript Depth-first search

被刻印的时光 ゝ 提交于 2019-11-30 09:54:49
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 represents a single node in the graph: "use strict"; define([], function () { return function (theValue) {

Dijkstra's algorithm with 'must-pass' nodes

南楼画角 提交于 2019-11-30 07:39:25
问题 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

Topological sort to find the number of paths to t

吃可爱长大的小学妹 提交于 2019-11-30 07:13:11
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_t(u) + 1 else then if color(v) == WHITE then DFS-Visit(v) paths_to_t(u) = paths_to_t(u) + paths_to_t(v

What is breadth-first search useful for?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 06:44:04
问题 Usually when I've had to walk a graph, I've always used depth-first search because of the lower space complexity. I've honestly never seen a situation that calls for a breadth-first search, although my experience is pretty limited. When does it make sense to use a breadth-first search? UPDATE : I suppose my answer here shows a situation where I've used a BFS (because I thought was a DFS). I'm still curious to know though, why it was useful in this case. 回答1: When you want to reach a node by

Is Pre-Order traversal on a binary tree same as Depth First Search?

偶尔善良 提交于 2019-11-30 03:31:46
It seems to me like Pre-order traversal and DFS are same as in both the cases we traverse till the leaf node in a depth wise fashion. Could anyone please correct me if I am wrong? Thanks in advance! Pre-order is one type of DFS. There are three types of depth-first traversal: pre-order, in-order, and post-order. Check out here for more info. It probably depends on the definition and implementation of the depth-first algorithm. The DefaultMutableTreeNode class of the Java Swing's JTree component has the following enumeration methods used for tree traversal: depthFirstEnumeration()

Edge classification in a DFS

為{幸葍}努か 提交于 2019-11-30 03:25:44
According to the book (Intro to Algorithm), in dfs, edges are classified as 4 kinds: Tree Edge, if in edge (u,v), v is first discovered, then (u, v) is a tree edge. Back Edge, if ......, v is discovered already and v is an ancestor, then it's a back edge. Forward Edge, if ......, v is discovered already and v is a descendant of u, forward edge it is. Cross Edge, all edges except for the above three. My question is how can I identify whether v is u's ancestor or descendant when I'm trying to figure out if (u, v) is a back or forward edge? If you really need it, you can check it by maintaining