当我尝试打印加权有向图(adj 列表)的内容时出现错误输出
Wrong output when I try to print out contents of Weighted Directed Graph (adj list)
此代码背后的想法是使用命令行参数创建图形:AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7
第一个字母是来源,第二个字母是目的地。最后一个数字是边缘权重。我觉得我输入正确,但我的打印方法已关闭。
这是我在 运行 程序时得到的输出:
A --> E weight: 3 --> D weight: 6 --> B weight: 4
B --> C weight: 2
C --> E weight: 3 --> D weight: 6
D --> E weight: 3 --> C weight: 2
E --> B weight: 4
如您所见:
A --> E 应该是 7。
A --> D 应该是 5。
等...
我做错了什么?
package graphs;
class Neighbor{
public int vNum;
public int weight;
public Neighbor next;
//Constructor
Neighbor(int num, int weight, Neighbor nbr){
this.vNum = num;
this.weight = weight;
next = nbr;
}
}
class Vertex{
char v;
Neighbor adj;
//Constructor
Vertex(char vertex, Neighbor neighbors){
this.v = vertex;
this.adj = neighbors;
}
}
public class WDiGraph {
Vertex[] adjList;
//constructor
public WDiGraph(int v, String[] edges){
adjList = new Vertex[v];
//Vertices
for(int i = 0; i < v; i++)
adjList[i] = new Vertex((char)(i + 65), null); //ascii value arithmetic
//Edges
for(int i = 0; i < edges.length; i++){
int src = edges[i].charAt(0) - 65;
int dest = edges[i].charAt(1) - 65;
int weight = edges[i].charAt(2) - 48;
adjList[src].adj = new Neighbor(dest, weight, adjList[src].adj);
}
}
public void print(){
System.out.println();
for(int i = 0; i < adjList.length; i++){
System.out.print(adjList[i].v);
for(Neighbor nbr = adjList[i].adj; nbr != null; nbr = nbr.next)
System.out.print(" --> " + adjList[nbr.vNum].v + " weight: " + adjList[nbr.vNum].adj.weight);
System.out.println("\n");
}
}
public static void main(String[] args) {
WDiGraph wdg = new WDiGraph(5, args); //Instantiates a Weighted DiGraph object with 5 vertices
wdg.print();
}
}
更改行打印粗细如下:
来自
System.out.print(" --> " + adjList[nbr.vNum].v + " weight: " +
adjList[nbr.vNum].adj.weight);
至
System.out.print(" --> " + adjList[nbr.vNum].v + " weight: " + nbr.weight);
此代码背后的想法是使用命令行参数创建图形:AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7
第一个字母是来源,第二个字母是目的地。最后一个数字是边缘权重。我觉得我输入正确,但我的打印方法已关闭。
这是我在 运行 程序时得到的输出:
A --> E weight: 3 --> D weight: 6 --> B weight: 4
B --> C weight: 2
C --> E weight: 3 --> D weight: 6
D --> E weight: 3 --> C weight: 2
E --> B weight: 4
如您所见:
A --> E 应该是 7。
A --> D 应该是 5。
等...
我做错了什么?
package graphs;
class Neighbor{
public int vNum;
public int weight;
public Neighbor next;
//Constructor
Neighbor(int num, int weight, Neighbor nbr){
this.vNum = num;
this.weight = weight;
next = nbr;
}
}
class Vertex{
char v;
Neighbor adj;
//Constructor
Vertex(char vertex, Neighbor neighbors){
this.v = vertex;
this.adj = neighbors;
}
}
public class WDiGraph {
Vertex[] adjList;
//constructor
public WDiGraph(int v, String[] edges){
adjList = new Vertex[v];
//Vertices
for(int i = 0; i < v; i++)
adjList[i] = new Vertex((char)(i + 65), null); //ascii value arithmetic
//Edges
for(int i = 0; i < edges.length; i++){
int src = edges[i].charAt(0) - 65;
int dest = edges[i].charAt(1) - 65;
int weight = edges[i].charAt(2) - 48;
adjList[src].adj = new Neighbor(dest, weight, adjList[src].adj);
}
}
public void print(){
System.out.println();
for(int i = 0; i < adjList.length; i++){
System.out.print(adjList[i].v);
for(Neighbor nbr = adjList[i].adj; nbr != null; nbr = nbr.next)
System.out.print(" --> " + adjList[nbr.vNum].v + " weight: " + adjList[nbr.vNum].adj.weight);
System.out.println("\n");
}
}
public static void main(String[] args) {
WDiGraph wdg = new WDiGraph(5, args); //Instantiates a Weighted DiGraph object with 5 vertices
wdg.print();
}
}
更改行打印粗细如下:
来自
System.out.print(" --> " + adjList[nbr.vNum].v + " weight: " +
adjList[nbr.vNum].adj.weight);
至
System.out.print(" --> " + adjList[nbr.vNum].v + " weight: " + nbr.weight);