Skip to content

Instantly share code, notes, and snippets.

@happyduck-git
Last active June 5, 2022 12:21
Show Gist options
  • Save happyduck-git/b03e726a3b715b0e8a29a083f8a5e937 to your computer and use it in GitHub Desktop.
Save happyduck-git/b03e726a3b715b0e8a29a083f8a5e937 to your computer and use it in GitHub Desktop.
public void dFS(int from, int[][] matrix) {
ArrayList<Node> stack = new ArrayList<>();
if(visited[from]) return;
//첫 방문이면 방문 확인!
visited[from] = true;
stack.add(vertices.get(from)); //Node type의 데이터를 add 하는 부분 (Graph class에 정의한 vertices list를 의미한 것인데 때에 따라 변경될 수 있음)
//stack이 빌 때까지 진행!
while(!stack.isEmpty()) {
Node current = stack.remove(stack.size() - 1);
for(int i = 0; i < matrix[current].length; i++) {
if(matrix[current][i] == 1 && !visited[i]) {
stack.add(vertices.get(current));
visited[i] = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment