Questions tagged [depth-first-search]

71 questions
7
votes
0 answers

When would Kosaraju's algorithm be a better choice than Tarjan's for strongly connected components?

I know both have runtime complexity $\mathcal{O} (V+E)$, but Tarjan's algorithm does a single DFS pass, whereas Kosaraju's does two DFS passes. Both need extra space (e.g. a dynamic set, often a stack, for the former to keep track of low links, and…
6
votes
2 answers

Algorithm design: find any path in an undirected acyclic graph which has a total sum of the nodes as a specific value

The question was asked in an interview, and I'm not sure if this is the most optimized answer, but here goes- You have an undirected acyclic graph, where each node has a non-negative value x and the edges have 0 value. You need to find a path with…
goelakash
  • 161
  • 4
6
votes
2 answers

Longest path length in an undirected tree, can we prove this algorithm is correct (which it is)?

Hello I solved this leetcode https://leetcode.com/problems/tree-diameter/ question reserved for people who pay the subscription. The question: Given an undirected tree (tree is not disjoint), return its diameter: the number of edges in a longest…
4
votes
1 answer

Determine if a spanning forest is the result of a depth-first search

As per this question, not every depth-first forest can necessarily be produced by a depth-first search (assuming arbitrary choice of node/edges where there are multiple candidates). Given a directed graph $G$, and a spanning forest of $G$, how can…
base12
  • 143
  • 3
4
votes
2 answers

How to represent BFS and DFS between adjacency matrix and list?

I'm trying to figure out how to best represent BFS (Breadth First Search) and DFS (Depth First Search) on a graph, specifically between being represented as an adjacency matrix and an adjacency list. How do I determine which is a better…
4
votes
1 answer

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 the same page, define a discovered vertex x as…
mgus
  • 171
  • 3
3
votes
1 answer

Does the presence or absence of a second "discovered" check in iterative DFS matter?

Until April 2nd, the pseudocode on Wikipedia for iterative depth-first search was the following: procedure DFS_iterative(G, v) is let S be a stack S.push(v) while S is not empty do v = S.pop() if v is not labeled as…
3
votes
1 answer

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 computes a walk from s to t such that the…
user136953
3
votes
0 answers

Backtracking vs Branch-and-Bound

I'm getting a bit confused about the three terms and their differences: Depth-First-Search (DFS), Backtracking, and Branch-and-Bound. What confuses me: Stack Overflow: Difference between 'backtracking' and 'branch and bound', Abhishek Dey:…
3
votes
2 answers

Articulation points (or cut vertices), but only subset of vertices need to be connected

I know we can find all articulation points efficiently in a graph using DFS. But what if not all nodes need to be connected, but instead we have set of node pairs that need to communicate (there is a path between them). How to efficiently find all…
3
votes
0 answers

Comparing classical tree-search algorithms (BFS,DFS,A*,IDS) - when to use one or the other?

I have a question about classical tree-search algorithms as I will have an exam soon and this is the type of questions they might be asking. Although I know how to compare the complexities, optimality, and completeness, I would like to go a bit…
2
votes
1 answer

DFS (Depth-first search) vs BFS (Breadth-first search) Space Optimizations

Problem I am currently digging deep into some optimizations on the classical iterative approaches to both DFS and BFS algorithms. The material I'm currently using at my University presents both iterative approaches as follows. Definitions: G(V,E):…
2
votes
2 answers

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$ implies that $G$ contains at most one simple path…
2
votes
1 answer

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 apply DFS and BFS for solving our puzzle. For…
2
votes
1 answer

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. def permute(nums): res = [] dfs(nums, [], res) return res def dfs(nums, curr,…
1
2 3 4 5