算法分析课程作业(仅供参考)
基于栈的实现方式
源代码:
import java.util.Stack;
/**
* @description: Depth first search algorithm
* @author: Qing Zhang
* @time: 09
*/
public class DFS {
/**
* @Description: Depth first search algorithm based on stack.
* @Param: [paraGraph : Graph that need to be searched, paraNode : Node currently being traversed]
* @return: java.lang.String
*/
public static String depthFirstSearch(int[][] paraGraph, int paraNode) {
Stack<Integer> tempStack = new Stack<>();
boolean[] visited = new boolean[paraGraph.length];
boolean flag = false;
visited[paraNode] = true;
tempStack.push(paraNode);
String resString = "" + paraNode;
int tempNode;
while (!tempStack.isEmpty()) {
tempNode = tempStack.peek();
for (int i = 0; i < paraGraph.length; i++) {
if (!visited[i] && paraGraph[tempNode][i] == 1) {
visited[i] = true;
tempStack.push(i);
resString += i;
flag = true;
break;
}
}
if (!flag) {
tempStack.pop();
}
flag = false;
}
return resString;
}
/**
* @Description: Used to test depth-first traversal.
* @Param: []
* @return: void
*/
public static void unitTestForDFS() {
int[][] testGraph = new int[][]{
{0, 1, 0, 1, 0, 0, 1},
{1, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 1, 0},
{1, 0, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0}
};
// Depth-first traversal starting at each node.
for (int i = 0; i < testGraph.length; i++) {
String str = DFS.depthFirstSearch(testGraph, i);
System.out.println("Use the " + (i + 1) + "-th node as the starting position");
System.out.println(str);
}
}
public static void main(String[] args) {
DFS.unitTestForDFS();
}
}
构造数据:
运行结果:
C:\Users\Administrator\.jdks\temurin-11.0.12-1\bin\java.exe "-javaagent:E:\IntelliJ IDEA 2020.1\lib\idea_rt.jar=55244:E:\IntelliJ IDEA 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\JavaProject\JustForTest\out\production\JustForTest DFS
Use the 1-th node as the starting position
0143256
Use the 2-th node as the starting position
1032564
Use the 3-th node as the starting position
2301465
Use the 4-th node as the starting position
3014625
Use the 5-th node as the starting position
4103256
Use the 6-th node as the starting position
5230146
Use the 7-th node as the starting position
6014325
Process finished with exit code 0
基于递归的实现方式
源代码:
/**
* @description: Depth first search algorithm based on recursion
* @author: Qing Zhang
* @time: 09
*/
public class DFS_Recursion {
// Judge whether it has been traversed.
private static boolean[] visited;
/**
* @Description: Depth first search algorithm based on recursion.
* @Param: [paraGraph : Graph that need to be searched, paraNode : Node currently being traversed]
* @return: java.lang.String
*/
public static String depthFirstSearchByRecursion(int[][] paraGraph, int paraNode) {
visited[paraNode] = true;
String resString = "" + paraNode;
for (int i = 0; i < paraGraph.length; i++) {
if (!visited[i] && paraGraph[paraNode][i] == 1) {
resString += depthFirstSearchByRecursion(paraGraph, i);
}
}
return resString;
}
/**
* @Description: Used to test depth-first traversal based on recursion.
* @Param: []
* @return: void
*/
public static void unitTestForDFSByRecursion() {
int[][] testGraph = new int[][]{
{0, 1, 0, 1, 0, 0, 1},
{1, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 1, 0},
{1, 0, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0}
};
// Depth-first traversal starting at each node.
for (int i = 0; i < testGraph.length; i++) {
visited = new boolean[testGraph.length];
String str = DFS_Recursion.depthFirstSearchByRecursion(testGraph, i);
System.out.println("Use the " + (i + 1) + "-th node as the starting position");
System.out.println(str);
}
}
public static void main(String[] args) {
DFS_Recursion.unitTestForDFSByRecursion();
}
}
构造数据:
运行结果:
C:\Users\Administrator\.jdks\temurin-11.0.12-1\bin\java.exe "-javaagent:E:\IntelliJ IDEA 2020.1\lib\idea_rt.jar=60437:E:\IntelliJ IDEA 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\JavaProject\JustForTest\out\production\JustForTest DFS_Recursion
Use the 1-th node as the starting position
0143256
Use the 2-th node as the starting position
1032564
Use the 3-th node as the starting position
2301465
Use the 4-th node as the starting position
3014625
Use the 5-th node as the starting position
4103256
Use the 6-th node as the starting position
5230146
Use the 7-th node as the starting position
6014325
Process finished with exit code 0