Data structures and algorithms in Arabic with Omar Morsy @omarmorsy02 Graph Depth-First Search workat.tech/pr... workat.tech/pr... / @omarmorsy02 @omarmorsy02
Пікірлер: 4
@alfykerolous18692 ай бұрын
why would u use a visited arr since it's a tree and ACYCLIC no need for it just check when dfsing :D send ur parent or previous and donot dfs if you u r ur parent
@ahmdelemam2 ай бұрын
Yes, we explained that at the end, just be patient and keep watching 😃
@alfykerolous18692 ай бұрын
@@ahmdelemam mb😁
@HassanAhmed-kx2yu3 ай бұрын
A non-recursive (iterative) implementation of pre-order traversal using a stack : ArrayList dfs(ArrayList[] adjList) { ArrayList traversal = new ArrayList(); Set visited = new HashSet(); Stack stack = new Stack(); stack.push(0); visited.add(0); while (!stack.empty()) { var top = stack.pop(); traversal.add(top); for (int i=adjList[top].size() - 1; i >= 0; i--) { if(!visited.contains(adjList[top].get(i))) { stack.push(adjList[top].get(i)); visited.add(adjList[top].get(i)); } } } return traversal; }