简体   繁体   English

java中的BFS和DFS并制作图形

[英]BFS and DFS in java and making the graph

I'm trying to make a graph and then Breadth-First Search or Depth-First Search through that graph.我正在尝试制作一个图表,然后通过该图表进行广度优先搜索或深度优先搜索。 How do you implement the graph in Java?你如何在Java中实现图形?

This is a very abstract question, as a graph can be represented in dozens different ways (different data structures).这是一个非常抽象的问题,因为图可以用几十种不同的方式(不同的数据结构)表示。

If you're not too familiar with graphs I would recommend you to start with:如果您不太熟悉图表,我建议您从以下开始:

  • Map<Integer,ArrayList<Integer>> (adjacency list) for sparse graph (when the number of edges is close to the minimal number of edges), where vertices are your keys in the map, and their neighbor vertices are the values. Map<Integer,ArrayList<Integer>> (邻接表)用于稀疏图(当边数接近最小边数时),其中顶点是地图中的键,它们的邻居顶点是值。
  • int[n][n] (adjacency matrix) for dense graphs (when the number of edges is close to the max number of edges), where n - number of vertices. int[n][n] (邻接矩阵)用于密集图(当边数接近最大边数时),其中 n - 顶点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM