我正在做一个宽度优先的搜索程序,它将读入邻接列表类型文本文件,然后相应地对其执行BFS。我无法在文本文件中读取,然后将其添加为节点的数组列表。将txt文件转换为邻接列表,然后制作图
我该如何得到它来正确读取每一行并将它的连接关联起来?
我已经测试了我的程序,在我的main中手动添加节点,然后制作一个图形并在其上执行BFS。
这里是我的节点类:
import java.util.*;
public class Node {
public String data; // data element
public boolean visited=false; // flag to track the already visited node
public List<Node> adjacentNodes = new LinkedList<Node>(); // adjacency list
// public List adjacentNodes = new LinkedList(); // adjacency list
public Node rootNode;
public Node(String data){
this.data = data;
}
public void addAdjacentNode(final Node node){
adjacentNodes.add(node);
node.adjacentNodes.add(this);
// adjacentNodes.add(rootNode);
// node.adjacentNodes.add(this)
}
}
这里是我的Graph类:(当我试图在我的文本文件读取距离我的主)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.*;
/*- enqueue the start node to a Queue
- make the start node as visited
- while queue is not empty
- dequeue the node lets say u
- print or whatever you want to
- for every adjacent node v of u
- if v is not already visited
- mark v as visited
- enqueue v to the Queue*/
public class Graph {
public List nodes = new ArrayList();
public void breadthFirstTraversal(Node rootNode){
Queue<Node> q = new LinkedList<Node>();
// Queue q = new LinkedList();
q.add(rootNode);
System.out.print(rootNode.data + " ");
// printNode(rootNode);
rootNode.visited=true;
while(!q.isEmpty()){
Node n = (Node)q.poll();
System.out.print(n.data + " ");
for(Node adj : n.adjacentNodes){
if(!adj.visited){
adj.visited=true;
q.add(adj);
}
}
clearNodes();
}
}
private void clearNodes() {
// TODO Auto-generated method stub
nodes = null; //clear nodes and set to null
}
/* private void printNode(Node node) {
// TODO Auto-generated method stub
System.out.print(node);
}*/
public static void main(String[] args) throws FileNotFoundException {
Graph g = new Graph();
Scanner scan = new Scanner(new File("Connections.txt")); // scanner to read file
String line = scan.nextLine(); // read first line
int nbLine = Integer.parseInt(line); // get number of lines
ArrayList<int[]> al = new ArrayList<int[]>();
for(int i = 0; i < nbLine; i++) { // read each line
line = scan.nextLine();
String[] token = line.split(" "); // split each number into different String
int[] points = new int[token.length - 1]; // prepare array of int[] - 1
// int[] point = new int[];
int[] point = new int[token.length];
for(int j = 0; j < token.length; j++){ // skip first one
points[j-1] = Integer.parseInt(token[j]); // store as int
al.add(points); // save in ArrayList
}
/* Scanner s = new Scanner(new File("C:/Users/cantuj3/Documents/Ass 2/Connections.txt"));
ArrayList<Node> list = new ArrayList<Node>();
while (s.hasNext()){
g.nodes.add(s.next());
//list.add(g);
}
s.close();*/
/* Node frankfurt = new Node("frankfurt");
Node mannheim = new Node("mannheim");
Node wurzburg = new Node("wurzburg");
Node stuttgard = new Node("stuttgard");
Node kassel = new Node("kassel");
Node karlsruhe = new Node("karlsruhe");
Node erfurt = new Node("erfurt");
Node numberg = new Node("numberg");
Node augsburg = new Node("augsburg");
Node munchen = new Node("munchen");
Graph g = new Graph();
g.nodes.add(frankfurt);
g.nodes.add(mannheim);
g.nodes.add(wurzburg);
g.nodes.add(stuttgard);
g.nodes.add(kassel);
g.nodes.add(karlsruhe);
g.nodes.add(erfurt);
g.nodes.add(numberg);
g.nodes.add(augsburg);
g.nodes.add(munchen);
frankfurt.addAdjacentNode(mannheim);
frankfurt.addAdjacentNode(wurzburg);
frankfurt.addAdjacentNode(kassel);
mannheim.addAdjacentNode(karlsruhe);
karlsruhe.addAdjacentNode(augsburg);
augsburg.addAdjacentNode(munchen);
munchen.addAdjacentNode(kassel);
munchen.addAdjacentNode(numberg);
wurzburg.addAdjacentNode(erfurt);
wurzburg.addAdjacentNode(numberg);
numberg.addAdjacentNode(stuttgard);
g.breadthFirstTraversal(frankfurt);*/
}
}
这里是我的输入文件:
01 02
02 01 03
03 02 04 05
04 03
05 03 06
06 05
这里的代码块我在写我的主要通过自身:
Scanner scan = new Scanner(new File("Connections.txt")); // scanner to read file
String line = scan.nextLine(); // read first line
int nbLine = Integer.parseInt(line); // get number of lines
ArrayList<int[]> al = new ArrayList<int[]>();
for(int i = 0; i < nbLine; i++) { // read each line
line = scan.nextLine();
String[] token = line.split(" "); // split each number into different String
int[] points = new int[token.length - 1]; // prepare array of int[] - 1
// int[] point = new int[];
int[] point;
for(int j = 0; j < token.length; j++){ // skip first one
points[j-1] = Integer.parseInt(token[j]); // store as int
al.add(points); // save in ArrayList
}
上午我在正确的轨道?
输入文件是什么样的?你到目前为止尝试过什么,以及你正在运行什么具体问题?实际行为与预期行为有什么不同? –
@JasonC我更新了我的代码,并添加了输入文件,以及我写出来的一段代码,虽然如上所述,但有一行出现问题 – AOE
更新!我摆脱了那个错误 – AOE