Skip to content

Instantly share code, notes, and snippets.

@happyduck-git
Created June 3, 2022 14:25
Show Gist options
  • Save happyduck-git/11dc6c797f60ddf14ca967092d962393 to your computer and use it in GitHub Desktop.
Save happyduck-git/11dc6c797f60ddf14ca967092d962393 to your computer and use it in GitHub Desktop.
//matrix 형태의 graph에서 dfs 탐색하는 방법
public void depthFirstSearch(int from) {
boolean[] visited = new boolean[matrix.length];
dFSHelper(from, visited);
}
public void dFSHelper(int from, boolean[] visited) {
//방문 이력 체크
if(visited[from]) { return; }
else visited[from] = true; //첫방문이므로 방문했다고 도장 꽝
//첫 방문인 것 확인 후에 edge 연결된 vertices 확인
for(int i = 0; i < matrix[from].length; i++) {
if(visited[from][i] == 1) {
dFSHelper(i, visited);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment