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
0
votes
1 answer

Maximum distance of and uncolored vertex from any of the colored vertices | Optimal Solution

Problem There are n locations ( vertices ) in a city, which are connected through streets of length 1 ( edge length = 1 ). Some of the locations have petrol stations ( colored vertex ) while others don't. People at locations with no petrol stations,…
0
votes
0 answers

recursively generating string from tree in python

I am using the networkx library, and I need to convert a tree T into a string that represents the tree structure. I am trying to do it recursively, but I cannot get it right. Nodes of the tree have attributes like "children", which is the number of…
marco trevi
  • 219
  • 4
  • 12
0
votes
0 answers

BFS and DFS visited list

I've been trying to understand BFS and DFS better, and I was wondering if I could get some help on this: I want to return the list of visited nodes for both of them. Since it would be redundant to post snippets for each and ask the same question,…
0
votes
1 answer

Recursive DFS function that looks if a position in a binary matrix has a 1 and all other elements in its row and column have 0

I'm working on LeetCode 1582. Special Positions in a Binary Matrix. How could I make a function that uses recursion and DFS to return the count of how many special positions are in a m x n binary matrix. A position (i, j) is called special if…
croatoan
  • 31
  • 4
0
votes
0 answers

How do I represent the directed graph given below and traverse BFS and DFS algorithm?

How do I represent the directed graph given below and traverse the following graph using the Breadth-first search (BFS) and depth-first search (DFS) algorithm? enter image description here
0
votes
1 answer

Determining Time Complexity For an Algorithm

Hi I am having trouble determing the time complexity for this algorithm. What the algorithm does is it finds the maximum conversion from one currency to another. It uses DFS and Backtracking and it is clear that the space complexity is O(depth) but…
0
votes
1 answer

Space/Time Complexity of DFS/Backtracking Question

I'm writing a function which calculates the max path from one node to another in an adjacency list graph after going through the graph in DFS/backtracking manner. The path will not have any cycles, but can have the same nodes within a different…
0
votes
2 answers

How is leetcode 79 false for `"bbbaabbbbbab"`?

LeetCode Problem description here. Given an m x n grid of characters board and a string word, return true if word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or…
user14955967
0
votes
1 answer

Translating java DFS algorithm code to Dart

I've been doing some research to find a suitable algorithm for suggesting friends. I came across DFS, but I've never implemented it in Dart before. Could someone please help me t translate it into Dart? Below is the java code: public class…
callMe_whatever
  • 71
  • 1
  • 2
  • 12
0
votes
1 answer

Depth-First-Search, Backtracking when constraint failed

So, I do understand depth-first-search is not appropriate for this problem and something like UCS or Astar would be a lot better, just attempting to see if it is possible with a DFS approach. I need to find a path within a cost budget, my approach…
0
votes
0 answers

Correct approach to resolve a list of "execute after" tasks

I'm trying to rank/resolve a set of tasks with dependencies among them. For example, I have below objects representing tasks class Task: def __init__(self, name): self.name = name self.execute_after = [] if __name__ ==…
0
votes
2 answers

How to implement the startsWith function of a trie in python

I have the following implementation of a trie in python. I want to implement the startsWith function that will return a list of all words that start with the prefix. Currently it return tue or false. Im not sure how to get a list of words in my…
0
votes
0 answers

Detect Cycle in a Undirected graph: Wrong output

I am trying to solve the question, detect cycle in a undirected graph. I have understood the logic and implemented it but my code is not giving the correct output. It is printing true in every case. I am not able to find what mistake i am…
0
votes
1 answer

Depth First Search in Prolog

subtrees([]). subtrees([(Cost,T)|Rest]) :− number(Cost), istree(T), subtrees(Rest). istree(tree(_,Children)) :− subtrees(Children). dfs(GoalValue,tree(GoalValue,_),GoalValue,0). dfs(GoalValue,tree(Value,[(Cost,T)|Rest]),Path,FinalCost)…
Pam Cesar
  • 73
  • 1
  • 6
0
votes
2 answers

Unsure if this is breadth first or depth first search?

I'm reviewing bfs and dfs concepts and recently wrote this search method for a Trie tree. I believe it is bfs because we're searching each level starting from the root if the next value exists. I'm not sure what implementing dfs would look like in a…