(java)DFS求连通分量的个数

直接上代码:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[][] g=new int[][]
				{
					{0,1,0,0},
					{1,0,0,0},
					{0,0,0,0},
					{0,0,0,0}
				};
				System.out.println(DFS(g));
	}
	public static int DFS(int[][] grid){
		boolean[] visited=new boolean[grid.length];
		int count=0;
		for(int i=0;i


你可能感兴趣的:((java)DFS求连通分量的个数)