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
0 answers

Depth-First-Search of B-Tree

I have a B-Tree that I can currently iterate over using recursive inorder traversal. However, I need to iterate inorder NOT recursively. I have tried all kinds of things and the closest I got was using depth-first-search. I am successfully printing…
Luka Jozić
  • 162
  • 2
  • 12
0
votes
0 answers

given a not oriented graph G, find an algorithm that verify if is possible to partition the set of nodes into two sets V1 C V and V2 C V (continue)

given a not oriented graph G=(V,E) represented with adjacency list,find an algorithm(that solve the problem in linear time) that verify if is possible to partition the set of nodes into two sets V1 C V and V2 C V with this condition: V1 U V2 = V V1…
0
votes
1 answer

How I get all the paths at two points in a Directed Graph which has circles

I can get all paths by using DFS if the graph like this: The path is D-C-B-A and D-E-A But the progame go into a loop if the grapg like this: I need some examples or pseudocode to deal with grapg which has circles. If anyone needs the source code…
lxf
  • 51
  • 2
0
votes
1 answer

optimizing depth first search python

I have got parent and child rows with levels. So I am using the code below to identify the parents ,child and their level as follows: instanceID parentInstanceID dependencyInstanceID isCycle level 0 44909 103565 …
Maa
  • 75
  • 6
0
votes
1 answer

Longest distance from a random vertex to the end points of the longest path

I have a tree-like graph/structure with the vertices as cities and let's say that the longest path is from 's' and 't'. Now, I want to prove that the longest path from any other city (let's say 'w') with end at either s or t. What I am thinking…
0
votes
0 answers

I think something is missing in my DFS_cycle function. So my code terminates before calling DFS_cycle function

C program to detect cycle in undirected graph there is problem in my DFS_cycle function. void cycle(struct Graph *graph) { int t=0; for(int i=0;iV;i++) { if(graph->array[i].head!=NULL) { …
0
votes
2 answers

Graph problem for checking whether a permutation A of 1 to N number can be converted to a permutation B if we are given good pairs

I am not sure whether this is an appropriate question to post on this platform. The problem statement is present here: https://www.interviewbit.com/problems/permutation-swaps/ Other than that, here's a short summary of this graph-based…
0
votes
1 answer

How is iterative deepening more efficient than just scanning the nodes at a specified depth level.

Isn't it redundant to rescan n-1 levels of nodes for each iteration?
nope
  • 223
  • 4
  • 15
0
votes
0 answers

Depth First Search in 5 * 5 board (python)

I wanna make some code that search all route in 5 * 5 board, that have to satisfy those conditions below. cannot search the position already searched search all positions in the board you can start searching anywhere in the board so I wrote a code…
0
votes
1 answer

Tree and Graph on same set of vertices

Suppose you are given an undirected graph G and a tree T on the same set of vertices. We would like to know whether it is possible to have an adjacency list representation of G (note that there are multiple options here as the adjacency list can…
0
votes
1 answer

Variable not updating in recursive call

I am trying to implement leetcode allpaths problem I have written the below solution, which is giving me the wrong output, I have purposefully, left the console statements for better debugging. Basically the variable path does not gets updated to…
0
votes
1 answer

Why does my DFS sorting algorithm ignore my graph?

I have the code below to perform a DFS sort on a directed graph (Topological sort, no cycles) def dfs_recursive(graph, vertex, path=[]): path += [vertex] for neighbor in graph[vertex]: if neighbor not in path: path =…
William Merritt
  • 429
  • 1
  • 5
  • 12
0
votes
0 answers

How to recursively find paths within a Directed Graph using DFS

I can't seem to figure out how to translate my solutions to work on an Directed graph instead of an Undirected graph. I currently have two different approaches; Iteration and Recursion. Iteration: def dfs_iterative(graph, start_vertex): visited…
William Merritt
  • 429
  • 1
  • 5
  • 12
0
votes
3 answers

Find if Path Exists in Graph using immutable values in Scala

How do I write the below code using immutables instead of var ? Question: There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges,…
Krishhna
  • 21
  • 2
0
votes
3 answers

Why would you use Trie data structure in WordSearch problem given below?

Given an m x n board of characters and a list of strings words, return all words on the board. Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same…
1 2 3
99
100