我是java新手。有谁能请“用下面的算法在java中实现bfs”吗?
algorithm.jpg
要实现的代码:
import java.util.Scanner;
public class BFS{
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int[][] graph = takeInputGraph(sc);
System.out.println("Give input of the source node");
int s = sc.nextInt();
bfs(graph,s);
}
public static int[][] takeInputGraph(Scanner sc){
System.out.println("Input the number of nodes in the graph");
int node = sc.nextInt();
System.out.println("Input the number of edges in the graph");
int edge = sc.nextInt();
int[][] mat = new int[node][node];
for(int c=0; c<edge; c++){
System.out.println("Enter the first node of the "+(c+1)+"th edge");
int node1 = sc.nextInt();
System.out.println("Enter the second node of the "+(c+1)+"th edge");
int node2 = sc.nextInt();
mat[node1][node2] = 1;
mat[node2][node1] = 1;
}
return mat;
}
public static void bfs(int[][] g, int s){
}
}
1条答案
按热度按时间wf82jlnq1#