Skip to content

Instantly share code, notes, and snippets.

@wohhie
Created July 16, 2021 01:40
Show Gist options
  • Save wohhie/7c2698d34167c33e4b24dea3961e97bd to your computer and use it in GitHub Desktop.
Save wohhie/7c2698d34167c33e4b24dea3961e97bd to your computer and use it in GitHub Desktop.
public void traverse(int[][] G, int start){
int[] visited = new int[G.length];
Queue<Integer> queue = new LinkedList<>();
System.out.print(start + " ");
// visited first 1 node
visited[start] = 1;
// insert into the queue
queue.add(start);
while (!queue.isEmpty()){
int u = queue.remove();
for (int v = 1; v < G.length; v++){
if(G[u][v] == 1 && visited[v] == 0){
System.out.print(v + " ");
visited[v] = 1;
queue.add(v);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment