Questions tagged [depth-first-search]

Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

Formally, DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hasn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a stack for exploration.

Source.

2567 questions
4
votes
2 answers

C# algorithm search for all paths between two vertices

Recently given a task to implement a directed graph with weights in C#. I am 3/4 of the way there but I have to use test data and return answers based on that data. I have the graph working and I am able to add the costs between 2 nodes as well as…
Brian
  • 59
  • 2
  • 8
4
votes
1 answer

DFS in an undirected graph - can it have cross edges?

Is it possible in an undirected graph for an edge that is leasing to an already visited node to lead towards a vertice that is not an ascendant of the current node? To be more clear, I want to implement a depth-first search on an undirected graph.…
lbicsi
  • 63
  • 1
  • 9
4
votes
3 answers

How do I add finishing times for iterative depth-first search?

I'm trying to create depth-first algorithm that assigns finishing times (the time when a vertex can no longer be expanded) which are used for things like Kosaraju's algorithm. I was able to create a recursive version of DFS fairly easily, but I'm…
Eyeofpie
  • 330
  • 2
  • 13
4
votes
2 answers

Breadth first search is not returning the shortest path

I am trying to use the breadth first search algorithm using Java. Considering the 10x10 grid I am trying to find the last cell 9x9 (The grid starts from 0,0 ). By the time when it reaches the 9x9 it has traversed all the cells in the grid. I heard…
iappmaker
  • 2,945
  • 9
  • 35
  • 76
4
votes
2 answers

Fast method of tile fitting/arranging

For example, let's say we have a bounded 2D grid which we want to cover with square tiles of equal size. We have unlimited number of tiles that fall into a defined number of types. Each type of tile specifies the letters printed on that tile.…
Mansour
  • 1,787
  • 2
  • 20
  • 33
4
votes
3 answers

Dijkstra's algorithm with 'must-pass' nodes

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…
user3778610
4
votes
8 answers

Best and easiest algorithm to search for a vertex on a Graph?

After implementing most of the common and needed functions for my Graph implementation, I realized that a couple of functions (remove vertex, search vertex and get vertex) don't have the "best" implementation. I'm using adjacency lists with linked…
rfgamaral
  • 16,546
  • 57
  • 163
  • 275
4
votes
4 answers

What's time complexity of this algorithm for finding all Path Sum?

Path Sum Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: sum = 11. 5 / \ 4 8 / / \ 2 -2 1 The answer is : [ [5, 4, 2], [5, 8, -2] ] Personally I…
Zhaonan
  • 939
  • 1
  • 11
  • 20
4
votes
3 answers

Simple Java 2d array maze sample

I am working or understanding how to create a simple java 2d maze that should look like this: int [][] maze = { {1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,1,0,1,0,1,0,0,0,0,0,1}, {1,0,1,0,0,0,1,0,1,1,1,0,1}, {1,0,0,0,1,1,1,0,0,0,0,0,1}, …
PuchuKing33
  • 381
  • 3
  • 7
  • 19
4
votes
1 answer

DFS on directed graph & Kosaraju's algorithm

I'm having trouble to understand Kosaraju's algorithm for finding the strongly connected components of a directed graph. Here's what I have on my notebook (I'm a student :D): Start from an arbitrary vertex (label it with #1) and perform a DFS. When…
4
votes
1 answer

R / igraph : getting vertex neighbors list within a depth-first-search callback causes a R to close. Any suggestion?

I'm trying to write a script in R to identify the elements (edges and vertices) belonging to the cycles of the graph. I'm using a callback function within the graph.dfs (igraph R package). I don't want to modify the graph, just want to build a new…
jpl
  • 43
  • 3
4
votes
1 answer

Algorithm to find a hierarchy tree in a directed acyclic graph?

I have a graph with properties as shown below: Adjacency Matrix: Graph Representation: Which algorithm can be applied to find a hierarchy tree of the above graph? In my words, a hierarchy tree is one in which nodes are connected and directed…
4
votes
2 answers

Checking for odd cycles in an undirected graph

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…
devers
  • 163
  • 1
  • 11
4
votes
1 answer

Number of crucial nodes in an Undirected graph

I have a graph G containing N nodes and E edges.Each edge is undirectional.The goal is to find the no. of crucial nodes. A node which makes the graph disconnected when it is removed is called a crucial node. The goal is to find the no. of such nodes…
Sr1n4th
  • 251
  • 2
  • 8
4
votes
3 answers

Finding the Longest Path in a Binary Tree

I want to find the longest path in a Binary Tree. I plan to add them to a list, that way I can tell my enemy character to take the long path on easy mode. private static ArrayList depthFirstSearch(BinaryNode node) { if(node != null) …
Mike John
  • 818
  • 4
  • 11
  • 29