以下代码为我们提供了构建。我看到了深度优先算法的问题。我在我的dfs方法中获得了nullpointerException
vList[0].visited = true;
。在课程Vertex
中,访问过的变量似乎就在那里。我怀疑这是问题所在。如何让深度首次遍历工作。谢谢。下面是代码。
public class Graph
{
private final int MAX_VERT = 5;
public int [][] adjMat; // 2D adjacency matrix representation of graph
public Vertex vList []; //List of vertices
public StackX stack;
public Graph ()
{
vList = new Vertex [MAX_VERT];
adjMat = new int [MAX_VERT][MAX_VERT];
stack = new StackX();
for(int y=0; y<MAX_VERT; y++) // set adjacency
for(int x=0; x<MAX_VERT; x++) // matrix to 0
adjMat[x][y] = 0;
}
public static void main(String[] args)
{
/*
* The matrix below corresponds to the following weighted graph
* that contains one unidirectional edge and remaining edges are
* bi-directional:
*
* 1
* A +-----+ C
* + \ |
* | \ |
* | \2 | 4
* |8 \ |
* | \ |
* | \|
* + +
* B D
* \ ^
* 6\ | 2 (This is a unidirectional edge from E -> D only)
* \ E+--+ (All the rest of the edges are bi-directional)
*
*/
int graph[][] = {
/* A B C D E */
{-1, 8, 1, 2, -1}, /* A */
{ 8, -1, -1, -1, 6}, /* B */
{ 1, -1, -1, 4, -1}, /* C */
{ 2, -1, 4, -1, -1}, /* D */
{-1, 6, -1, 2, -1} /* E */
};
Vertex v[] = new Vertex[5];
}
static void add_neighbour(Vertex v, int graph[])
{
for (int i = 0; i < graph.length; i++) {
String n = "";
switch(i) {
case 0: n = "A"; break;
case 1: n = "B"; break;
case 2: n = "C"; break;
case 3: n = "D"; break;
case 4: n = "E"; break;
}
if (graph[i] != -1) {
v.add_neighbour(new Neighbour(v.get_label(), n, graph[i]));
}
}
return;
}
public void displayVertex(int v)
{
System.out.print(vList[v].label);
}
//Depth First Search
public void dfs(int [][] adjMat) // depth-first search
{ // begin at vertex 0
vList[0].visited = true; // nullpointer here when compiling
displayVertex(0); // display it
stack.push(0); // push it
while( !stack.isEmpty() ) // until stack empty,
{
// get an unvisited vertex adjacent to stack top
int v = getAdjUnvisitedVertex(stack.peek());
if(v == -1) // if no such vertex,
stack.pop();
else // if it exists,
{
vList[v].visited = true; // mark it
displayVertex(v); // display it
stack.push(v); // push it
}
} // end while
// stack is empty, so we're done
for(int j=0; j<MAX_VERT; j++) // reset flags
vList[j].visited = false;
} // end dfs
//returns an unvisited vertex adj to v
public int getAdjUnvisitedVertex(int v)
{
for(int j=0; j<MAX_VERT; j++)
if(adjMat[v][j]==1 && vList[j].visited==false)
return j;
return -1;
} // end getAdjUnvisitedVertex()
}
class Vertex
{
private String label;
private PriorityQueue<Neighbour> neighbours;
private boolean visited;
public Vertex(String l)
{
label = l;
neighbours = null;
visited = false;
}
public void add_neighbour(Neighbour n)
{
if (neighbours == null)
neighbours = new PriorityQueue<Neighbour>();
neighbours.add(n);
}
public Neighbour remove_neighbour()
{
Neighbour n = null;
if (neighbours.size() > 0) {
n = neighbours.peek();
neighbours.remove(n);
}
return n;
}
public String get_label() { return label; }
public String toString()
{
String s = "Vertex: " + label + "\n";
s += " Neighbours: \n";
Iterator<Neighbour> itr = neighbours.iterator();
while (itr.hasNext()) {
Neighbour n = itr.next();
s += n;
}
return s;
}
}
class Neighbour implements Comparable<Neighbour>
{
private String source;
private String destination;
private Integer weight;
public Neighbour(String s, String d, int w)
{
source = s; destination = d; weight = w;
}
public int compareTo(Neighbour o)
{
if (this.weight < o.weight) return -1;
else if (this.weight > o.weight) return 1;
else return 0;
}
public String toString()
{
return " " + destination + ", weight: " + weight + "\n";
}
}
答案 0 :(得分:0)
发布实际代码。这个根本不编译。
vList
的声明在哪里?
adjMat
的声明在哪里?如何访问label
或visited
等私有变量
来自另一个班级。
这是一个非常草率的提问方式。
无论如何,如果您创建vList
之类的vList = new Vertex[5]
(查看v
中main
的声明),请知道这只分配数组,并引用它包含为null。您还必须分配它们,例如vList[0] = new Vertex()
。