public void deleteEdge(int vl, int v2) //删除边<v1,v2>if (v1 < 0 II v1 >= vertices.getSize0 Il v2 < 0 II v2 >:vertices.getSizeO)thrownewException("参数vl或v2越界出错!");if (edge[vl, v2] == maxWeight Il v1 == v2)throw newException("该边不存在!");edge[vl,v2]=maxWeight;//置边的权值为无穷大/边的个数减1numOfEdges--;
public void deleteEdge(int v1, int v2) { //删除边<v1,v2> if (v1 < 0 || v1 >= vertices.getSize() || v2 < 0 || v2 >= vertices.getSize()) throw new Exception("参数v1或v2越界出错!"); if (edge[v1, v2] == maxWeight || v1 == v2) throw new Exception("该边不存在!"); edge[v1, v2] = maxWeight; //置边的权值为无穷大 numOfEdges-; //边的个数减1 }
public int getFirstNeighbor(int v){//取结点v的第一个邻接结点。若存在返回该结点的下标序号,否则返回-1if (v < 0 II v >= vertices.getSize0)thrownewException("参数v越界出错!");for (int col = O; col < vertices.getSizeO; col++)if (edge[v, col] ≥> 0 && edge[v, col] < maxWeight)return col;return -1;
public int getFirstNeighbor(int v) { //取结点v的第一个邻接结点。若存在返回该结点的下标序 号,否则返回-1 if (v < 0 || v >= vertices.getSize()) throw new Exception("参数v越界出错!"); for (int col = 0; col < vertices.getSize(); col++) if (edge[v, col] > 0 && edge[v, col] < maxWeight) return col; return -1; }
public int getNextNeighbor(int vl, int v2)//取结点v1的邻接结点v2后的邻接结点/若存在返回该结点的下标序号,否则返回-1if (v1 < 0 II v1 >= vertices.getSize0 II v2 < 0 Il v2 >vertices.getSizeO)thrownewException("参数v1或v2越界出错!");for (int col = v2 + 1; col < vertices.getSizeO; col++)if (edge[vl, col] > 0 && edge[vl, col] < maxWeight)return col;return -1;
public int getNextNeighbor(int v1, int v2) { //取结点v1的邻接结点v2后的邻接结点 //若存在返回该结点的下标序号,否则返回-1 if (v1 < 0 || v1 >= vertices.getSize() || v2 < 0 || v2 >= vertices.getSize()) throw new Exception("参数v1或v2越界出错!"); for (int col = v2 + 1; col < vertices.getSize(); col++) if (edge[v1, col] > 0 && edge[v1, col] < maxWeight) return col; return -1; }
0A1B00A=0V=00(D00E00(a)(b)
B A D C E = E D C B A V = 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 A (b) (a)
可以把创建图所需的数据和方法设计为类RowColWeightclassRowColWeightint row;int col;int weight;public RowColWeight(int r, int c, int w) row =r;col = c;weight =w;1
可以把创建图所需的数据和方法设计为类RowColWeight class RowColWeight{ int row; int col; int weight; public RowColWeight(int r, int c, int w) { row = r; col = c; weight = w; }