这是关于图中深度优先搜索的代码。谁知道为什么在这段代码中使用了 bufferedReader 类?为什么不使用 nextInt 函数呢?它的特权是什么?是为了加快处理速度吗?谢谢 :)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Graph
{
int g[][];
int v,e;
int visited[];
void createGraph()throws IOException
{
int a,b;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n Enter Number Of Vertices = ");
v=Integer.parseInt(br.readLine());
System.out.print("\n Enter Number Of Edges = ");
e=Integer.parseInt(br.readLine());
g=new int[v+1][v+1];
for(int i=1;i<=e;i++)
{
System.out.print("\n Enter Edge Infomation ");
System.out.print("\n From =");
a=Integer.parseInt(br.readLine());
System.out.print("\n To =");
b=Integer.parseInt(br.readLine());
g[a][b]=g[b][a]=1;
}
}
void callDFS()
{
visited = new int[v+1];
dfs(1);
}
void dfs(int k)
{
System.out.print(k + "\t");
visited[k]=1;
for(int i=1;i<=v;i++)
{
if(g[k][i] !=0 && visited[i]!=1)
dfs(i);
}
}
}
class DFS
{
public static void main(String args[])throws IOException
{
Graph g = new Graph();
g.createGraph();
g.callDFS();
}
}