遍历深度优先搜索时从邻接列表获取节点-Java
在遍历我的DFS堆栈时,我正在尝试尽快运行此代码。当前输入文件如下: 在这里,我的遍历深度优先搜索时从邻接列表获取节点-Java,java,algorithm,hashmap,depth-first-search,adjacency-list,Java,Algorithm,Hashmap,Depth First Search,Adjacency List,在遍历我的DFS堆栈时,我正在尝试尽快运行此代码。当前输入文件如下: 在这里,我的Maze类将把数字绑定在一起,并为我创建一个图形。创建图形后,myDFS类通过遍历运行,为提交的.txt文件提供一个或所有解决方案。我最近更改了myMaze类,使其运行更高效,但出现了错误,数据正在解析到myDFS以进行输出。我的新Maze课程如下: import java.io.*; import java.util.*; public class Maze { private final Map&l
Maze
类将把数字绑定在一起,并为我创建一个图形。创建图形后,myDFS
类通过遍历运行,为提交的.txt文件提供一个或所有解决方案。我最近更改了myMaze
类,使其运行更高效,但出现了错误,数据正在解析到myDFS
以进行输出。我的新Maze
课程如下:
import java.io.*;
import java.util.*;
public class Maze {
private final Map<Integer, Set<Integer>> adjList = new HashMap<>();
/**
* The main constructor that takes a String for reading maze file.
*
* @param file
*/
public Maze(File file) throws FileNotFoundException {
try (Scanner scan = new Scanner(file)) {
while (scan.hasNextInt()) {
int node1 = scan.nextInt();
int node2 = scan.nextInt();
this.connect(node1, node2);
this.connect(node2, node1);
}
}
}
/**
* Makes a unidirectional connection from node1 to node2.
*/
private void connect(int node1, int node2) {
if (!this.adjList.containsKey(node1)) {
this.adjList.put(node1, new HashSet<Integer>());
}
this.adjList.get(node1).add(node2);
}
/**
* Returns a human-readable description of the adjacency lists.
*/
public String toString() {
StringBuilder s = new StringBuilder();
for (Map.Entry<Integer, Set<Integer>> adj : this.adjList.entrySet()) {
int from = adj.getKey();
Set<Integer> to = adj.getValue();
s.append(from).append(" connected to ").append(to).append('\n');
}
return s.toString();
}
/**
* Returns the set of nodes connected to a particular node.
*
* @param node - the node whose neighbors should be fetched
*/
public Iterable<Integer> getadjList(int node) {
return Collections.unmodifiableSet(adjList.get(node));
}
/**
* Demonstration of file reading.
*/
public static void main(String[] args) throws FileNotFoundException {
System.err.print("Enter File: ");
Scanner scanFile = new Scanner(System.in);
String file = scanFile.nextLine();
Maze m = new Maze(new File(file));
System.out.println(m);
}
}
编辑::DFS
类在下面,如果有帮助:
import java.io.*;
import java.util.*;
public class DFS {
//starting node, the route to the next node, has node been visited
private int startNode;
private int goalNode;
private int[] route;
private boolean[] visited;
// 2 main arguments - Maze File & user input
public DFS(Maze maze, int input) {
int startNode = 0;
int goalNode = 1;
route = new int[maze.adjList.getNode()];
visited = new boolean[maze.adjList.getNode()];
//Takes user's input and runs desired function
if(input == 1){
findOne(maze, startNode, goalNode);
}
else if (input == 2){
findAll(maze, startNode, goalNode);
}
else {
System.out.println("input invalid. No Solution Returned");
}
}
//Put path to goal in the stack
public Stack<Integer> route(int toGoalNode) {
if (!visited[toGoalNode]) {
return null;
}
Stack<Integer> pathStack = new Stack<Integer>();
for (int routeGoalNode = toGoalNode; routeGoalNode != startNode; routeGoalNode = route[routeGoalNode]) {
pathStack.push(routeGoalNode);
}
pathStack.push(startNode);
reverseStack(pathStack);
return pathStack;
}
//Reverse the stack
public void reverseStack(Stack<Integer> stackToBeReverse) {
if (stackToBeReverse.isEmpty()) {
return;
}
int bottom = popBottomStack(stackToBeReverse);
reverseStack(stackToBeReverse);
stackToBeReverse.push(bottom);
}
//Pop the bottom of the stack
private int popBottomStack(Stack<Integer> stackToBeReverse) {
int popTopStack = stackToBeReverse.pop();
if (stackToBeReverse.isEmpty()) {
return popTopStack;
} else {
int bottomStack = popBottomStack(stackToBeReverse);
stackToBeReverse.push(popTopStack);
return bottomStack;
}
}
//performs DFS and unsets visited to give the result of all paths
private void findAll(Maze maze, int node, int goal) {
visited[node] = true;
if(node == goal) {
printPath(goal);
} else {
for (int con : maze.getadjList(node)) {
if (!visited[con]) {
route[con] = node;
findAll(maze, con, goal);
}
}
}
visited[node] = false;
}
//performs DFS and maintains visited marker giving only one path
private void findOne(Maze maze, int node, int goal) {
visited[node] = true;
for (int con : maze.getadjList(node)) {
if (!visited[con]) {
route[con] = node;
findOne(maze, con, goal);
}
}
}
//Traverse the connections to the goal and print the path taken
public void printPath( int toGoal) {
int goalNode = 1;
if (visited[toGoal]) {
System.out.println("Completed Path: ");
for (int t : route(toGoal)) {
if (t == toGoal) {
System.out.print(t);
} else {
System.out.print(t + " -> ");
}
}
System.out.println();
}
}
public static void main(String[] args){
Scanner scanFile = new Scanner(System.in);
int goalNode = 1;
System.out.print("Enter maze file: ");
String file = scanFile.nextLine();
Maze maze = new Maze(new File(file));
Scanner scanInt = new Scanner(System.in);
System.out.print("Enter desired feedback (1 = one soultion, 2 = all): ");
int input = scanInt.nextInt();
maze.toString();
System.out.println(maze);
DFS dfs = new DFS(maze, input);
dfs.printPath(goalNode);
}
}
import java.io.*;
导入java.util.*;
公共类DFS{
//已访问起始节点,即到下一个节点的路由
私有int startNode;
专用目标节点;
专用int[]路线;
参观私人住宅;;
//2个主要参数-迷宫文件和用户输入
公共DFS(迷宫、整数输入){
int startNode=0;
int goalNode=1;
route=newint[maze.adjList.getNode()];
visited=新布尔值[maze.adjList.getNode()];
//接受用户的输入并运行所需的函数
如果(输入=1){
findOne(迷宫、开始节点、目标节点);
}
else if(输入=2){
findAll(迷宫、起始节点、目标节点);
}
否则{
System.out.println(“输入无效,未返回解决方案”);
}
}
//将目标路径放入堆栈中
公共堆栈路由(int-toGoalNode){
如果(!已访问[toGoalNode]){
返回null;
}
堆栈路径堆栈=新堆栈();
对于(int-routeGoalNode=toGoalNode;routeGoalNode!=startNode;routeGoalNode=route[routeGoalNode]){
push(routeGoalNode);
}
pathStack.push(startNode);
反向堆栈(路径堆栈);
返回路径堆栈;
}
//倒立
公共无效反向堆栈(堆栈反向堆栈){
if(stackToBeReverse.isEmpty()){
返回;
}
int bottom=popBottomStack(stackToBeReverse);
反向堆叠(stackToBeReverse);
向上推(底部);
}
//弹出堆栈的底部
私有int popBottomStack(堆栈堆栈到反向){
int-popTopStack=stackToBeReverse.pop();
if(stackToBeReverse.isEmpty()){
返回popTopStack;
}否则{
int bottomStack=popBottomStack(stackToBeReverse);
堆栈反向推送(popTopStack);
返回底部堆栈;
}
}
//执行DFS和取消访问以给出所有路径的结果
私有void findAll(迷宫迷宫、int节点、int目标){
已访问[节点]=真;
如果(节点==目标){
打印路径(目标);
}否则{
for(int con:maze.getadjList(节点)){
如果(!已访问[con]){
路由[con]=节点;
芬德尔(迷宫,骗局,目标);
}
}
}
已访问的[节点]=false;
}
//执行DFS并维护仅提供一条路径的访问标记
私有void findOne(迷宫迷宫、int节点、int目标){
已访问[节点]=真;
for(int con:maze.getadjList(节点)){
如果(!已访问[con]){
路由[con]=节点;
芬顿(迷宫、骗局、目标);
}
}
}
//遍历到目标的连接并打印所采用的路径
公共void打印路径(int-toGoal){
int goalNode=1;
如果(访问[多哥]){
System.out.println(“完成的路径:”);
用于(int t:路线(多哥)){
如果(t==多哥){
系统输出打印(t);
}否则{
系统输出打印(t+“->”);
}
}
System.out.println();
}
}
公共静态void main(字符串[]args){
扫描仪扫描文件=新扫描仪(System.in);
int goalNode=1;
System.out.print(“输入迷宫文件:”);
String file=scanFile.nextLine();
迷宫迷宫=新迷宫(新文件(文件));
Scanner scanInt=新扫描仪(System.in);
System.out.print(“输入所需反馈(1=一个解决方案,2=全部):”;
int input=scanInt.nextInt();
maze.toString();
系统输出打印LN(迷宫);
DFS=新的DFS(迷宫,输入);
打印路径(目标节点);
}
}
由于邻接列表邻接列表
是一个私有成员,您可以在Maze
中编写一个方法,将该列表的元素公开给DFS
public Set<Integer> getNode(int node) {
return Collections.unmodifiableSet(adjList.get(node));
}
非常感谢,我现在就试试看,如果我不能得到一些结果,我怎么称呼它为我的
DFS
类,因为我已经尝试了我以前的尝试,但他们似乎没有这样做。再次感谢
import java.io.*;
import java.util.*;
public class DFS {
//starting node, the route to the next node, has node been visited
private int startNode;
private int goalNode;
private int[] route;
private boolean[] visited;
// 2 main arguments - Maze File & user input
public DFS(Maze maze, int input) {
int startNode = 0;
int goalNode = 1;
route = new int[maze.adjList.getNode()];
visited = new boolean[maze.adjList.getNode()];
//Takes user's input and runs desired function
if(input == 1){
findOne(maze, startNode, goalNode);
}
else if (input == 2){
findAll(maze, startNode, goalNode);
}
else {
System.out.println("input invalid. No Solution Returned");
}
}
//Put path to goal in the stack
public Stack<Integer> route(int toGoalNode) {
if (!visited[toGoalNode]) {
return null;
}
Stack<Integer> pathStack = new Stack<Integer>();
for (int routeGoalNode = toGoalNode; routeGoalNode != startNode; routeGoalNode = route[routeGoalNode]) {
pathStack.push(routeGoalNode);
}
pathStack.push(startNode);
reverseStack(pathStack);
return pathStack;
}
//Reverse the stack
public void reverseStack(Stack<Integer> stackToBeReverse) {
if (stackToBeReverse.isEmpty()) {
return;
}
int bottom = popBottomStack(stackToBeReverse);
reverseStack(stackToBeReverse);
stackToBeReverse.push(bottom);
}
//Pop the bottom of the stack
private int popBottomStack(Stack<Integer> stackToBeReverse) {
int popTopStack = stackToBeReverse.pop();
if (stackToBeReverse.isEmpty()) {
return popTopStack;
} else {
int bottomStack = popBottomStack(stackToBeReverse);
stackToBeReverse.push(popTopStack);
return bottomStack;
}
}
//performs DFS and unsets visited to give the result of all paths
private void findAll(Maze maze, int node, int goal) {
visited[node] = true;
if(node == goal) {
printPath(goal);
} else {
for (int con : maze.getadjList(node)) {
if (!visited[con]) {
route[con] = node;
findAll(maze, con, goal);
}
}
}
visited[node] = false;
}
//performs DFS and maintains visited marker giving only one path
private void findOne(Maze maze, int node, int goal) {
visited[node] = true;
for (int con : maze.getadjList(node)) {
if (!visited[con]) {
route[con] = node;
findOne(maze, con, goal);
}
}
}
//Traverse the connections to the goal and print the path taken
public void printPath( int toGoal) {
int goalNode = 1;
if (visited[toGoal]) {
System.out.println("Completed Path: ");
for (int t : route(toGoal)) {
if (t == toGoal) {
System.out.print(t);
} else {
System.out.print(t + " -> ");
}
}
System.out.println();
}
}
public static void main(String[] args){
Scanner scanFile = new Scanner(System.in);
int goalNode = 1;
System.out.print("Enter maze file: ");
String file = scanFile.nextLine();
Maze maze = new Maze(new File(file));
Scanner scanInt = new Scanner(System.in);
System.out.print("Enter desired feedback (1 = one soultion, 2 = all): ");
int input = scanInt.nextInt();
maze.toString();
System.out.println(maze);
DFS dfs = new DFS(maze, input);
dfs.printPath(goalNode);
}
}
public Set<Integer> getNode(int node) {
return Collections.unmodifiableSet(adjList.get(node));
}
public int getNumberOfNodes() {
return adjList.size();
}