-
-
Save wohhie/7c2698d34167c33e4b24dea3961e97bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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