depth-first-search

Incorrect Placement visited node set in Graph traversal DFS

Incorrect Placement visited node set in Graph traversal DFS Question: I am having trouble understanding why placing visited.append((i,j)) in the else case gives me the right output, while keeping it as shown below gives the incorrect output. I tried to debug with a simpler example [[0,1], [1,1]] But I am not able to figure out …

Total answers: 1

Graph DFS – Confused about the return value of recursion

Graph DFS – Confused about the return value of recursion Question: I have a graph where a path from top-left cell – (0,0) to the bottom-right cell – (m-1,n-1) is valid if the difference between consecutive cells are less than or equal to "val". For instance, this example should return true for a val=2 because …

Total answers: 1

Python. DFS graph traversal, correct output?

Python. DFS graph traversal, correct output? Question: I’m currently getting to grips with graph traversal in Python. Given the following graph: Implemented using this dictionary: graph = {‘0’: set([‘1’, ‘2’, ‘3’]), ‘1’: set([‘0′,’2’]), ‘2’: set([‘0′,’1′,’4’]), ‘3’: set([‘0’]), ‘4’: set([‘2’])} Am I correct in thinking a depth first search traversal beginning from node 0 should return …

Total answers: 2

Iterative version of counting connected components

Iterative version of counting connected components Question: I am learning graph theory and am having a hard time counting the number of connected components in a graph. Can someone help me out? I am trying to come up with an iterative version because I am a bit shaky on recursion (still getting the hang of …

Total answers: 1

DFS algorithm implementation in Python with classes

DFS algorithm implementation in Python with classes Question: I am trying to implement DFS that will return a graph with all the nodes containing their predecessor, while having the color attribute to identify a cycle in the graph (there is a cycle iff (u,v) is a back edge iff v is gray and v.discovery < …

Total answers: 1

How to manipulate and return a string from a recursive function?

How to manipulate and return a string from a recursive function? Question: I wrote the recursive code to get the path taken in a DFS traversal, but I want to get the output into a string variable. Recursion is not my strong suit so I’m having a lot of trouble figuring out the what to …

Total answers: 1

leetcode 39. Combination Sum why do I have to return after I get one solution?

leetcode 39. Combination Sum why do I have to return after I get one solution? Question: so the question is like this: Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations …

Total answers: 1

Problem with understanding the code of 968. Binary Tree Cameras

Problem with understanding the code of 968. Binary Tree Cameras Question: I am studying algorithms and trying to solve the LeetCode problem 968. Binary Tree Cameras: You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its …

Total answers: 1

Recursive DFS into Iterative DFS with global state

Recursive DFS into Iterative DFS with global state Question: Here is this graph algorithm that finds a path between two nodes in a DAG. from typing import * def find_path(graph: List[List[int]], start: int, end: int) -> List[int]: path = [] def dfs(node): path.append(node) if node == end: return True for neighbor in graph[node]: if dfs(neighbor): …

Total answers: 2