Skip to content

Instantly share code, notes, and snippets.

@happyduck-git
Created June 5, 2022 12:31
Show Gist options
  • Save happyduck-git/71d40330ed8bf74effa4b2ba54a08148 to your computer and use it in GitHub Desktop.
Save happyduck-git/71d40330ed8bf74effa4b2ba54a08148 to your computer and use it in GitHub Desktop.
public void bFS(int from, int[][] matrix) {
Queue<Integer> queue = new LinkedList<>();
boolean[] visited = new boolean[matrix.length];
queue.offer(from);
visited[from] = true;
while(!queue.isEmpty()) {
from = queue.poll();
for(int i = 0; i < matrix[from].length; i++) {
if(matrix[from][i] == 1 && !visited[i]) {
queue.offer(i);
visited[i] = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment