Questions tagged [depth-first-search]
The depth-first-search tag has no usage guidance.
64
questions
0
votes
2
answers
41
views
Trying to implement BFS and I am stuck
I am trying to write down a code which would blindly search for a condition using breadth first search.I have been thinking of it for quite some time and I cant figure out how to continue.
On the one ...
2
votes
2
answers
112
views
A $O(|E||V|)$ algorithm to determine if a graph is singly connected?
In exercise 22.3-13 of CLRS (Intro to Algorithms 3rd edition), the authors provide the following problem:
A directed graph $G = (V, E)$ is singly connected if the existence of a path from $u$ to $v$ ...
0
votes
0
answers
20
views
Meaning of Depth First Search on certain edges?
I understand depth first search on a graph, but I'm having difficulty understanding what it means to do depth first search on only certain edges. If given an undirected graph, with 2 types of edges, ...
0
votes
0
answers
33
views
Are pre-order, in-order and post-order the only traversals for depth-first search?
With a binary tree, the 3 approaches for the tree below are listed.
...
0
votes
0
answers
16
views
least collection of node disjoint paths that cover a given set of vertices
Given a directed graph G (with cycles) with source and target, and a set of interested vertices S.
we want to find a small n collections of vertice-disjoint-paths (path only have common nodes in ...
0
votes
2
answers
197
views
Why compute finish time in topological sort
In depth first search each vertex can be associated with a discovery time and a finish time. I am reading the following implementation of topological sort in terms of depth first search
...
1
vote
1
answer
185
views
Checking if there exists a 'source' vertex
In a directed graph $G=(V,E)$ we denote a vertex $s\in V$ to be a 'source' if there exists in $G$ a path from $s$ to all other vertices $u \in V$.
The problem asks for an efficient algorithm to return ...
0
votes
0
answers
335
views
Assigning directions to edges in an undirected graph in a way which satifies some condition
I got the following question from my academic instructor and since I am new to graph algorithms I would be happy if someone could review my solution.
Given undirected graph $G = (V,E)$, suggest an ...
-1
votes
1
answer
105
views
Find a Global Sink in a graph
There is a class exercise question asking about global sink in a directed graph.
Finding normal sink is not difficult but the conditions of the question have made me stuck on this question.
Let $G=(V,...
2
votes
1
answer
830
views
How to determine the time and memory complexity for solving a sliding-tile puzzle?
I have seen many posts which were related to algorithms for solving an N⨯N puzzle, but I could not figure out the time complexity or memory complexity in these algorithms, especially when we want to ...
0
votes
1
answer
131
views
Minimizing/Maximizing recursion depth for DFS
The idea for this problem comes from GATE CS 2014 Set-3 Q13.
Given a graph, are there any heuristics to figure out a DFS traversal which has minimum/maximum recursion depth?
Consider the graph from ...
0
votes
1
answer
77
views
Recursive DFS Problem
I have been struggling with this contest problem for awhile now which is found at this link: https://people.eecs.berkeley.edu/~hilfingr/programming-contest/pacific-northwest/2009/b.pdf
Short summary ...
1
vote
1
answer
369
views
What algorithm will visit each node in a graph a number of times equal to the number of paths to that node from the root?
First Few Iterations of the Algorithm
We have an algorithm in which a squirrel visits the nodes of a directed graph.
Our graph has two colors of edges: black and white.
Initially, the graph has black ...
2
votes
1
answer
136
views
Runtime complexity of permutation function
I am trying to find the asymptotic run time complexity of the following function which will return a list of all permutations of nums.
...
1
vote
1
answer
180
views
Confuse on proof of theorem 22.9 (White-path theorem) Depth-First search (DFS) on Cormen-Leiserson-Rivest-Stein "Introduction to algorithms" book
I'm reading the DFS section of CLRS-Introduction to Algorithms, and confuse on the $\Leftarrow$ direction of the proof of the white-path theorem of DFS algorithm in this book.
Note that each node u ...
2
votes
3
answers
1k
views
Determines if the minimum spanning tree only uses edges with an integer weight, when E, V are in O(n)
Given a undirected graph $G=(V,E)$ with $|V|=n$ and $|E|=2022n$ and some weight function $w\colon E\to \mathbb{R}$, and $0≤ w(e) ≤n$ for all $e∈E$,
Describe an algorithm that determines if the MST ...
1
vote
1
answer
73
views
Minimum steps needed to partially cover a sequence of points on a grid
Problem statement: You are in a 2D grid where you can move in any of the 4 directions, no obstacles. You start at position (0, 0).
We say that you partially cover a point $(x,y)$ if your position has ...
0
votes
0
answers
63
views
I do not understand a way of proving the correctness of the algorithm to compute the Strongly Connected Components
"Introduction to algorithms" also known as CLRS, proves the correctness of the algorithm to compute the Strongly Connected Components in two ways, one of which is
Here is another way to ...
2
votes
0
answers
85
views
How to mathematically prove that a topological ordering on a cyclic graph will topologically sort its Strongly Connected Components?
Let's have a standard topological ordering algorithm (from CLRS):
...
1
vote
1
answer
431
views
What is the relation between Topological Sort and Strongly Connected Components?
Both the Topological Sorting algorithm and the algorithm to find Strongly Connected Components build a stack whose top is the last visited vertex.
I find difficult to find an explaination because ...
1
vote
1
answer
49
views
Transforming from neighbour representation to grid representation
Assuming I have a list L of lists. Each list in L has four entries [North, Ost, South, West] which represents the neighbours of a point i on a grid. If the entry is 0, the grid neighbour is not ...
0
votes
1
answer
1k
views
modify dfs to find longest path
Let $G = (V, E)$ be a directed acyclic graph. Let every node $v \in V$ have an additional field $v_d$.
For each vertex $v \in V$, we need to store in $v_d$ the length of the longest path in $G$ that ...
1
vote
0
answers
141
views
Visiting vertices on a graph using DFS and BFS
I have this graph that I created and am wondering how DFS and BFS would work on something like this. I made this graph undirected and am going off the premise that if possible, a vertex should be ...
-2
votes
1
answer
7k
views
Print all nodes which are the endpoint of the diameter of a tree
Given a tree with n nodes, Print all nodes which are the endpoint of the diameter.
1<= n <=100000.
E.g.
For Above tree answer would be A,B,F,G
I tried the O(n2) approach (Running Dfs from each ...
0
votes
1
answer
395
views
Connectivity in Directed Graph
Connectivity in undirected graph can be easily identified using Disjoint Union Set (Union Find). Is there any way to check connectivity in a directed graph efficiently other than doing Depth First ...
0
votes
0
answers
145
views
Iterative version of depth-first-search code to detect cycle in a directed graph [duplicate]
I have solved a problem that required me to find if the given directed graph had a cycle or not. The deal is, I used a recursive approach to solve it in the depth-first-search method. I was just ...
2
votes
1
answer
126
views
In every DFS run on $G$, in every step of DFS, the $G_{\pi}$ is a forest
Studying for my finals. So I'm reading the "Introduction to Algorithms (Third Edition)" book. In the DFS section there is the following section:
Depth-first search yields valuable ...
1
vote
1
answer
63
views
For any direct graph $G(V,E)$, there is always an iteration of DFS algorithm on $G$ so the result does not have any cross trees
I suspect that it is not true but I came across with the following statement:
For any direct graph $G(V,E)$, there is always an iteration of DFS algorithm on $G$ so the result does not have any cross ...
0
votes
1
answer
68
views
Trace minimum spanning tree's path
I have a list of connected rooms, I need to visit each room in the list, but I have to record the exact path to traverse the entire graph.
...
0
votes
3
answers
1k
views
Is DFS better than BFS for space complexity when finding path from root to a node in a tree?
For simplicity, and I think without loss of generality, we can consider a binary tree. Suppose that we want to find the path between the root node and some node in the tree (we don't know where it is ...
1
vote
0
answers
27
views
Time to form a complete graph from n vertices given that only k vertices can be used at a time [closed]
I know this problem is related to the greedy algorithm and max edges incomplete graph but can't come up with a solution.
Problem
You are given two numbers n and k:
n >= k
n is the total # of ...
3
votes
1
answer
1k
views
Find palindrome in directed Graph where edges are either blue or red
This is the given task:
Suppose you are given an arbitrary directed graph G in which each edge is
colored either red or blue, along with two special vertices s and t.
Describe an algorithm that either ...
1
vote
1
answer
303
views
Check if two graphs are edge-disjoint
Two undirected graphs $G$ and $H$ on the vertices $1,2,\ldots,n$ are disjoint if the intersection of
their edge sets is empty.
Assume both $G$ and $H$ are represented by adjacency matrices.
...
0
votes
1
answer
145
views
DFS produces the correct Topologically ordered sequence
Prove that DFS produces the correct topologically ordered sequence.
I am having a hard time understanding the question itself. Should I prove the correctness of DFS? Should I use the pseudocode?
2
votes
1
answer
153
views
find the strong component containing the vertex v
I'm trying to solve this question for practising purposes:
"Describe a linear-time algorithm for computing the strong component containing a given vertex v. On the basis of that algorithm, ...
0
votes
1
answer
178
views
Alternatives for finding sources in a DAG
I have a hard time seeing what the alternative approach is in linearizing a directed acyclic graph (DAG). Chapter 3 of Algorithms by Dasgupta et al. states:
Property Every dag has at least one source ...
0
votes
0
answers
27
views
I need good book about algorithms, which explains recursion, backtracking and depth first recursion for beginners [duplicate]
I'm learning programming, Python 3 in particular, and I have a good book "Beginning Python - From Novice to Professional" by Magnus Lie Hetland, but the book doesn't explain algorithms at ...
1
vote
1
answer
44
views
Is the chalk really needed in the "chalk and string labyrinth" analogy for depth-first search?
I came across the chalk-and-string labyrinth analogy for depth-first search in Algorithms by Dasgupta et al. I have seen the code for the depth first search.
Everybody knows that all you need to ...
0
votes
1
answer
317
views
Create Shortest Path tree for every node after Floyd Warshall in O(nm)
Right now I am stuck with the problem, how all shortest path trees can be created in O(n*m) given G = (V,E,c) with negative and positive costs without negative cycles and n =|V| m = |E| after ...
2
votes
1
answer
208
views
pictorial representation of pre and post ordering for edge types
I am reading chapter 3 Decomposition of Graphs of Algorithms by Dasgupta, Papadimitriou & Vazirani and they show a pictorial representation of pre and post numbers with brackets for tree, forward, ...
0
votes
0
answers
59
views
Finding time complexity of a depth first traversal algorithm with a depth limit to get all node traversals starting from the root node
What would be the time complexity of a depth-first traversal algorithm on a graph, that is simply trying to retrieve all nodes being visited from starting node up until a depth limit is reached (i.e. ...
1
vote
0
answers
50
views
What does "yields" mean in the phrase *yields no back edges* in DFS?
What does yields mean in the phrase yields no back edges in the context of DFS?
...
1
vote
1
answer
210
views
Question about the conditions to find articulation points in a graph
I have been reading a book called The Algorithm Design Manual by Steven Skiena and one of the topics discussed there is an algorithm to find all the articulation points in a graph.
In it, we first ...
1
vote
1
answer
155
views
Maximum Independent Set of special Directed Graph
I was given this special type of Directed Graph and was asked to find it's Maximum Independent Set.
Graph Properties :
Graph has $N$ vertices and $N$ edges
There can be no edge from a vertex $v$ to ...
0
votes
1
answer
3k
views
Why is DFS not suited for shortest path problem?
I am sorry for the repetition of the question. I understand that this question has already been answered before by the community, but most answers tend to focus on unweighted graphs. I want to know ...
1
vote
0
answers
543
views
Kruskal naive implementation: how to use DFS to detect cycle in MST plus next edge?
Most discussion of naive (without Union-Find) implementations of Kruskal's algorithm for finding the minimal spanning tree that I see has a handwavy bit: "just use DFS to detect if there is a ...
1
vote
2
answers
1k
views
Does DFS have better constants/complexity than Backtracking on a Graph?
I came to know through some examples that DFS and Backtracking aren't exactly the same ( A misconception I had since a long time). So now my question is, since Backtracking visits nodes backwards step ...
4
votes
1
answer
175
views
First-time and second-time seen edges in DFS on undirected graphs
Assume an undirected graph and a DFS traversal on it. I am interested in the DFS tree which encodes the discoverer/discovered (parent/child) relationships of the traversal. Just to make sure we are on ...
0
votes
0
answers
256
views
Find every path that passes through certain edges
I'm faced with the following problem:
Given
Directed and unweighted graph, where each edge E has two attributes
Goal
Find every path through the 3 (or more) given edges in a specific order
...
1
vote
1
answer
252
views
What's the name of this DFS variation
Classical DFS:
A set of tasks with precedence constraints (saying “u must be done before v”) are given. This problem can be represented by a directed graph. We assume that the graph is acyclic.
A DFS ...