使用GraphRead-Class我没有写自己,我要完成一个深度优先搜索算法,以便从文本文件中编辑图形。
这是该班级的样子:
public class GraphRead {
public static Graph<Vert,Edge<Vert>> FileToGraph(String dat, boolean directed, boolean standardIds, boolean weighted) {
FileInputStream fis = null;
Graph<Vert,Edge<Vert>> G = null;
try {
fis = new FileInputStream(dat);
}
catch ( Exception e) {
System.out.println(dat + " couldn't be opened");
System.out.println(e.getMessage());
}
try {
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader (isr);
// read number of vertices
String aRow;
aRow = br.readLine();
int n = new Integer(aRow);
// read number of edges
aRow = br.readLine();
int m = new Integer(aRow);
// construct graph
G = new Graph<Vert,Edge<Vert>>(n, directed);
if (!standardIds) { // vertices have arbitrary ids and get a name
// read vertices (1. substring: id, 2. substring: name)
for (int i = 1; i <= n; i++) {
aRow = br.readLine();
int sepIndex1 = aRow.indexOf(' ');
String vId = aRow.substring(0, sepIndex1);
String vStr = aRow.substring(sepIndex1+ 1, aRow.length());
int id = new Integer(vId);
Vert v = new Vert(id,vStr);
G.addVertex(v);
}
}
else {
// add vertices with standard ids (0 .. n-1)
for (int i = 0; i < n; i++) {
G.addVertex(new Vert(i));
}
}
// read edges with weight
if (weighted) {
for (int i = 1; i <= m; i++) {
aRow = br.readLine();
int sepIndex1 = aRow.indexOf(' ');
int sepIndex2 = aRow.indexOf(' ', sepIndex1+1);
String vStr = aRow.substring(0, sepIndex1);
String uStr = aRow.substring(sepIndex1+ 1, sepIndex2);
String wStr = aRow.substring(sepIndex2+ 1, aRow.length());
int vId = new Integer(vStr);
int uId = new Integer(uStr);
int w = new Integer(wStr);
Vert v = G.getVertex(vId);
Vert u = G.getVertex(uId);
G.addEdge(new Edge<Vert>(v,u,w));
}
}
else { // read edges without weight;
for (int i = 1; i <= m; i++) {
aRow = br.readLine();
int sepIndex1 = aRow.indexOf(' ');
String vStr = aRow.substring(0, sepIndex1);
String uStr = aRow.substring(sepIndex1+ 1, aRow.length());
int vId = new Integer(vStr);
int uId = new Integer(uStr);
Vert v = G.getVertex(vId);
Vert u = G.getVertex(uId);
G.addEdge(new Edge<Vert>(v,u));
}
}
fis.close();
}
catch (Exception e) {
System.out.println("Reading was not successful");
System.out.println(e.getMessage());
}
return G;
}
}
我总是得到“字符串索引超出范围3”的异常。 而且我不知道为什么。
文件具有以下格式:
1.行:顶点数(n)
2.行:边数(米)
顶点的ID和顶点的名称用空格分隔。
随后的m行:边的起点和终点以及边的权重(仅当weighted = true时)用空格分隔。
如果图形是有向的,则参数“ directed”为true。
在有针对性的情况下:“ dat”中的每个边e =(a,b)仅添加到变量a的邻接表中。
在没有方向的情况下:将每个边e =(a,b)添加到变量a的邻接表和变量b的邻接表中。
如果顶点的ID在0到n-1之间且没有名称,则参数“ standardIds”为true。
如果对边缘进行加权,则参数“ weighted”为true。
Graph-txt的内容如下:
9
11
0 1
0 4
1 2
2 5
5 3
3 2
3 0
3 4
6 3
7 6
8 7
9
有什么办法可以解决这个问题吗?谢谢!
答案 0 :(得分:0)
此行发生错误
String uStr = aRow.substring(sepIndex1+ 1, sepIndex2);
原因是因为文件中的一行中最多只能有2个数字,因此sepIndex2
为1时,以下内容会将sepIndex1
设置为-1
int sepIndex2 = aRow.indexOf(' ', sepIndex1+1);
为清楚起见,这就是我在测试时使用的内容。
输入文件的最终版本
9
2
0 1
0 4
1 2
2 5
5 3
3 2
3 0
3 4
6 3
7 6
8 7
在我的代码版本中,我删除了对未知类(例如Graph
等)的任何引用以及一些其他编辑。这样做是因为我只对读取文件的逻辑感兴趣
public static void main(String[] args) {
GraphRead.FileToGraph("~/temp/graf.txt", true, false, false);
}
public static void FileToGraph(String dat, boolean directed, boolean standardIds, boolean weighted) {
FileInputStream fis = null;
try {
fis = new FileInputStream(dat);
} catch (Exception e) {
System.out.println(dat + " couldn't be opened");
System.out.println(e.getMessage());
}
try {
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
// read number of vertices
String aRow;
aRow = br.readLine();
int n = new Integer(aRow);
// read number of edges
aRow = br.readLine();
int m = new Integer(aRow);
if (!standardIds) { // vertices have arbitrary ids and get a name
// read vertices (1. substring: id, 2. substring: name)
System.out.println("!standardIds");
for (int i = 1; i <= n; i++) {
aRow = br.readLine();
int sepIndex1 = aRow.indexOf(' ');
String vId = aRow.substring(0, sepIndex1);
String vStr = aRow.substring(sepIndex1 + 1, aRow.length());
int id = new Integer(vId);
System.out.println(vId + ":" + vStr);
}
}
// read edges with weight
if (weighted) {
System.out.println("weighted");
for (int i = 1; i <= m; i++) {
aRow = br.readLine();
int sepIndex1 = aRow.indexOf(' ');
int sepIndex2 = aRow.indexOf(' ', sepIndex1 + 1);
String vStr = aRow.substring(0, sepIndex1);
String uStr = aRow.substring(sepIndex1 + 1, sepIndex2);
String wStr = aRow.substring(sepIndex2 + 1, aRow.length());
System.out.println(uStr + ":" + wStr);
}
} else { // read edges without weight;
System.out.println("!weighted");
for (int i = 1; i <= m; i++) {
aRow = br.readLine();
int sepIndex1 = aRow.indexOf(' ');
String vStr = aRow.substring(0, sepIndex1);
String uStr = aRow.substring(sepIndex1 + 1, aRow.length());
System.out.println(vStr + ":" + uStr);
}
}
fis.close();
} catch (Exception e) {
System.out.println("Reading was not successful");
System.out.println(e.getMessage());
}
}