## 54: depth first traversal and breadth first traversal of Graphs

Time limit: 20000ms memory limit: 131072kB

describe

The information of vertex and edge of an undirected graph is given, and the depth first traversal sequence and the breadth first traversal sequence of the undirected graph are output. Starting from a vertex, if there are more than two vertices that can be accessed, we agree to visit the vertex with the largest number first. The figure corresponding to the example input is as follows:

input

The first line of the input has two integers m and n. The graph g has m vertices and N edges.

The second line is m strings separated by spaces, followed by the name of the first vertex in the figure, the name of the second vertex..... The name of the M vertex.

Then there are n lines. Each line is composed of 2 strings, which are the two vertices of one edge in the graph. We agreed that there would be no double sides.

output

The output has 2 lines.

The first row is a sequence of vertices from the first vertex through depth first traversal of graph g.

The second line is a sequence of vertices from the first vertex through breadth first traversal of graph g.

sample input

`8 9 v1 v2 v3 v4 v5 v6 v7 v8 v1 v2 v1 v3 v1 v6 v2 v3 v2 v4 v3 v4 v4 v6 v5 v6 v7 v8`

sample output

`v1 v6 v5 v4 v3 v2 v7 v8 v1 v6 v3 v2 v5 v4 v7 v8`

Tips

Note: starting from a vertex, if there are more than two vertices that can be accessed, we agree to visit the vertex with the largest number first.

### First of all, I declare that I didn't listen to the class, the code was just playing, and the quality of writing was not good, but I could pass the OpenJudge test, regardless of his 3721. Anyway, if I passed it, I'll do it. OK, I don't need to talk much, just go to the code and run it.

`import java.util.*; /** * @author baikunlong * @date 2020/6/23 10:55 */ public class Main { private static ArrayList<Graph> list = new ArrayList<>(); private static ArrayList<Graph> visited; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); scanner.nextLine(); String[] names = scanner.nextLine().split(" "); for (int i = 0; i < names.length; i++) { list.add(new Graph(Integer.parseInt(names[i].substring(1)))); } for (int i = 0; i < m; i++) { String[] strings = scanner.nextLine().split(" "); Graph graph = list.get(Integer.parseInt(strings[0].substring(1)) - 1); graph.list.add(list.get(Integer.parseInt(strings[1].substring(1)) - 1)); graph = list.get(Integer.parseInt(strings[1].substring(1)) - 1); graph.list.add(list.get(Integer.parseInt(strings[0].substring(1)) - 1)); } //Start depth traversal visited = new ArrayList<>(); Graph cGraph = list.get(0); visited.add(cGraph); DFS(cGraph); for (int i = 0; i < visited.size(); i++) { System.out.print("v" + visited.get(i).gravity + " "); } System.out.println(); //Start breadth traversal visited = new ArrayList<>(); //Restore access status for (int i = 0; i < list.size(); i++) { list.get(i).visited = false; list.get(i).preGraph = null; } cGraph = list.get(0); visited.add(cGraph); ArrayList<Graph> cGraphs = new ArrayList<>(); cGraphs.add(cGraph); BFS(cGraphs); for (int i = 0; i < visited.size(); i++) { System.out.print("v" + visited.get(i).gravity + " "); } } /** * breadth-first search * @param cGraphs Collection of connection points for the current point */ private static void BFS(ArrayList<Graph> cGraphs) { // System.out.println("set " + cGraphs); //Is there a point that hasn't been visited yet boolean isEmpty = true; ArrayList<Graph> nextGraphs = new ArrayList<>(); //Traverse each connection point for (int i = 0; i < cGraphs.size(); i++) { Graph cGraph = cGraphs.get(i); ArrayList<Graph> cList = cGraph.list; if (cList.size() != 0) { cList.sort(Comparator.comparingInt(o -> (o.gravity))); Collections.reverse(cList); //All the child connection points of the connection point are accessed, or follow the order from large size for (int k = 0; k < cList.size(); k++) { Graph graph = cList.get(k); graph.preGraph = cGraph; graph.visited = true; if (!visited.contains(graph)){ visited.add(graph); isEmpty = false; } //Save as connection point for next level nextGraphs.add(graph); } } } //If all connection points are accessed if (isEmpty) { //Traverse the rest of the unconnected points for (int i = 0; i < list.size(); i++) { if (!list.get(i).visited) { visited.add(list.get(i)); cGraphs = new ArrayList<>(); cGraphs.add(list.get(i)); BFS(cGraphs); } } }else { //Visit next level BFS(nextGraphs); } } /** * Depth first traversal * @param cGraph Current point */ private static void DFS(Graph cGraph) { // System.out.println("set v" + cGraph.gravity); //Set accessed cGraph.visited = true; //Add the point if the accessed collection does not contain if (!visited.contains(cGraph)) visited.add(cGraph); ArrayList<Graph> cList = cGraph.list; if (cList.size() == 0) { //If the connection point of this point is empty, it means that it has reached the deepest point, then it will return to the previous point DFS(cGraph.preGraph); return; } //Priority access to large points by weight cList.sort(Comparator.comparingInt(o -> (o.gravity))); Collections.reverse(cList); // System.out.println(cList); //Access to each connection point for (int i = 0; i < cList.size(); i++) { if (!cList.get(i).visited) { cList.get(i).preGraph = cGraph; cGraph = cList.get(i); //Recursively, until there is no connection point DFS(cGraph); return; } } //If you don't go back to the starting point, continue to traverse if (cGraph.preGraph != null) { DFS(cGraph.preGraph); } else { //Traverse the remaining unconnected points for (int i = 0; i < list.size(); i++) { if (!list.get(i).visited) { DFS(list.get(i)); } } } } static class Graph { //weight int gravity; //Connection point ArrayList<Graph> list = new ArrayList<>(); //Access or not boolean visited; //Previous point Graph preGraph; public Graph(int gravity) { this.gravity = gravity; } @Override public String toString() { return "Graph{" + "gravity=" + gravity + '}'; } } }`