# Java data structure 54: Course Design of depth first traversal and breadth first traversal of graph data structure

Posted by saidbakr on Thu, 25 Jun 2020 10:49:29 +0200

## 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++) {
}
for (int i = 0; i < m; i++) {
String[] strings = scanner.nextLine().split(" ");
Graph graph = list.get(Integer.parseInt(strings.substring(1)) - 1);
graph = list.get(Integer.parseInt(strings.substring(1)) - 1);
}

//Start depth traversal
visited = new ArrayList<>();
Graph cGraph = list.get(0);
DFS(cGraph);
for (int i = 0; i < visited.size(); i++) {
System.out.print("v" + visited.get(i).gravity + " ");
}

System.out.println();

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);
ArrayList<Graph> cGraphs = new ArrayList<>();
BFS(cGraphs);

for (int i = 0; i < visited.size(); i++) {
System.out.print("v" + visited.get(i).gravity + " ");
}
}

/**
* @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)){
isEmpty = false;
}
//Save as connection point for next level
}
}
}
//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) {
cGraphs = new ArrayList<>();
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))
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;
}
cList.sort(Comparator.comparingInt(o -> (o.gravity)));
Collections.reverse(cList);
//        System.out.println(cList);

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 +
'}';
}
}
}
``````

Topics: Java REST