数据结构中 图的建立及输出
发布网友
发布时间:2022-05-11 01:26
我来回答
共2个回答
热心网友
时间:2023-11-24 02:41
package graph;
public class Graphm implements Graph{
private int[][]matrix;
private int numEdge;
public int[]Mark;
public Graphm(int n){
Mark=new int[n];
matrix=new int[n][n];
numEdge=0;
}
public int n()
public int e()
public Edge first (int v){
for(int i=0;i<Mark.length;i++)
if(matrix[v][i]!=0)
return new Edgem(v,i);
return null;
}
public Edge next(Edge w){
if(w==null)return null;
for(int i=w.v2()+1;i<Mark.length;i++ )
if(matrix[w.v1()][i]!=0)
return new Edgem(w.v1(),i);
return null;
}
public boolean isEdge(Edge w){
if(w==null)return false;
else return matrix[w.v1()][w.v2()]!=0;
}
public boolean isEdge(int i,int j){
return matrix[i][j]!=0;}
public int v1(Edge w)
public int v2(Edge w)
public void setEdge(int i,int j,int wt){
// Assert.notFalse(wt!=0,"Cannot set weight to 0");
matrix[i][j]=wt;
numEdge++;
}
public void setEdge(Edge w,int weight){
if(w!=null)setEdge(w.v1(),w.v2(),weight);
}
public void delEdge(Edge w){
if(w!=null)
if(matrix[w.v1()][w.v2()]!=0){
matrix[w.v1()][w.v2()]=0;
numEdge--;
}
}
public void delEdge(int i,int j){
if(matrix[i][j]!=0){
matrix[i][j]=0;
numEdge--;
}
}
public int weight(int i,int j){
if(matrix[i][j]==0)return Integer.MAX_VALUE;
else return matrix[i][j];
}
public int weight(Edge w){
// Assert.notNull(w,"Can't take weight of null edge");
if(matrix[w.v1()][w.v2()]==0)return Integer.MAX_VALUE;
else return matrix[w.v1()][w.v2()];
}
public void setMark(int v,int val)
public int getMark(int v){return Mark[v];
}
}
package graph;
public class Graphm implements Graph{
private int[][]matrix;
private int numEdge;
public int[]Mark;
public Graphm(int n){
Mark=new int[n];
matrix=new int[n][n];
numEdge=0;
}
public int n()
public int e()
public Edge first (int v){
for(int i=0;i<Mark.length;i++)
if(matrix[v][i]!=0)
return new Edgem(v,i);
return null;
}
public Edge next(Edge w){
if(w==null)return null;
for(int i=w.v2()+1;i<Mark.length;i++ )
if(matrix[w.v1()][i]!=0)
return new Edgem(w.v1(),i);
return null;
}
public boolean isEdge(Edge w){
if(w==null)return false;
else return matrix[w.v1()][w.v2()]!=0;
}
public boolean isEdge(int i,int j){
return matrix[i][j]!=0;}
public int v1(Edge w)
public int v2(Edge w)
public void setEdge(int i,int j,int wt){
// Assert.notFalse(wt!=0,"Cannot set weight to 0");
matrix[i][j]=wt;
numEdge++;
}
public void setEdge(Edge w,int weight){
if(w!=null)setEdge(w.v1(),w.v2(),weight);
}
public void delEdge(Edge w){
if(w!=null)
if(matrix[w.v1()][w.v2()]!=0){
matrix[w.v1()][w.v2()]=0;
numEdge--;
}
}
public void delEdge(int i,int j){
if(matrix[i][j]!=0){
matrix[i][j]=0;
numEdge--;
}
}
public int weight(int i,int j){
if(matrix[i][j]==0)return Integer.MAX_VALUE;
else return matrix[i][j];
}
public int weight(Edge w){
// Assert.notNull(w,"Can't take weight of null edge");
if(matrix[w.v1()][w.v2()]==0)return Integer.MAX_VALUE;
else return matrix[w.v1()][w.v2()];
}
public void setMark(int v,int val)
public int getMark(int v){return Mark[v];
}
}
package graph;
public class Graphm implements Graph{
private int[][]matrix;
private int numEdge;
public int[]Mark;
public Graphm(int n){
Mark=new int[n];
matrix=new int[n][n];
numEdge=0;
}
public int n()
public int e()
public Edge first (int v){
for(int i=0;i<Mark.length;i++)
if(matrix[v][i]!=0)
return new Edgem(v,i);
return null;
}
public Edge next(Edge w){
if(w==null)return null;
for(int i=w.v2()+1;i<Mark.length;i++ )
if(matrix[w.v1()][i]!=0)
return new Edgem(w.v1(),i);
return null;
}
public boolean isEdge(Edge w){
if(w==null)return false;
else return matrix[w.v1()][w.v2()]!=0;
}
public boolean isEdge(int i,int j){
return matrix[i][j]!=0;}
public int v1(Edge w)
public int v2(Edge w)
public void setEdge(int i,int j,int wt){
// Assert.notFalse(wt!=0,"Cannot set weight to 0");
matrix[i][j]=wt;
numEdge++;
}
public void setEdge(Edge w,int weight){
if(w!=null)setEdge(w.v1(),w.v2(),weight);
}
public void delEdge(Edge w){
if(w!=null)
if(matrix[w.v1()][w.v2()]!=0){
matrix[w.v1()][w.v2()]=0;
numEdge--;
}
}
public void delEdge(int i,int j){
if(matrix[i][j]!=0){
matrix[i][j]=0;
numEdge--;
}
}
public int weight(int i,int j){
if(matrix[i][j]==0)return Integer.MAX_VALUE;
else return matrix[i][j];
}
public int weight(Edge w){
// Assert.notNull(w,"Can't take weight of null edge");
if(matrix[w.v1()][w.v2()]==0)return Integer.MAX_VALUE;
else return matrix[w.v1()][w.v2()];
}
public void setMark(int v,int val)
public int getMark(int v){return Mark[v];
}
}
package graph;
public class Graphm implements Graph{
private int[][]matrix;
private int numEdge;
public int[]Mark;
public Graphm(int n){
Mark=new int[n];
matrix=new int[n][n];
numEdge=0;
}
public int n()
public int e()
public Edge first (int v){
for(int i=0;i<Mark.length;i++)
if(matrix[v][i]!=0)
return new Edgem(v,i);
return null;
}
public Edge next(Edge w){
if(w==null)return null;
for(int i=w.v2()+1;i<Mark.length;i++ )
if(matrix[w.v1()][i]!=0)
return new Edgem(w.v1(),i);
return null;
}
public boolean isEdge(Edge w){
if(w==null)return false;
else return matrix[w.v1()][w.v2()]!=0;
}
public boolean isEdge(int i,int j){
return matrix[i][j]!=0;}
public int v1(Edge w)
public int v2(Edge w)
public void setEdge(int i,int j,int wt){
// Assert.notFalse(wt!=0,"Cannot set weight to 0");
matrix[i][j]=wt;
numEdge++;
}
public void setEdge(Edge w,int weight){
if(w!=null)setEdge(w.v1(),w.v2(),weight);
}
public void delEdge(Edge w){
if(w!=null)
if(matrix[w.v1()][w.v2()]!=0){
matrix[w.v1()][w.v2()]=0;
numEdge--;
}
}
public void delEdge(int i,int j){
if(matrix[i][j]!=0){
matrix[i][j]=0;
numEdge--;
}
}
public int weight(int i,int j){
if(matrix[i][j]==0)return Integer.MAX_VALUE;
else return matrix[i][j];
}
public int weight(Edge w){
// Assert.notNull(w,"Can't take weight of null edge");
if(matrix[w.v1()][w.v2()]==0)return Integer.MAX_VALUE;
else return matrix[w.v1()][w.v2()];
}
public void setMark(int v,int val)
public int getMark(int v){return Mark[v];
}
}
package graph;
public class Graphm implements Graph{
private int[][]matrix;
private int numEdge;
public int[]Mark;
public Graphm(int n){
Mark=new int[n];
matrix=new int[n][n];
numEdge=0;
}
public int n()
public int e()
public Edge first (int v){
for(int i=0;i<Mark.length;i++)
if(matrix[v][i]!=0)
return new Edgem(v,i);
return null;
}
public Edge next(Edge w){
if(w==null)return null;
for(int i=w.v2()+1;i<Mark.length;i++ )
if(matrix[w.v1()][i]!=0)
return new Edgem(w.v1(),i);
return null;
}
public boolean isEdge(Edge w){
if(w==null)return false;
else return matrix[w.v1()][w.v2()]!=0;
}
public boolean isEdge(int i,int j){
return matrix[i][j]!=0;}
public int v1(Edge w)
public int v2(Edge w)
public void setEdge(int i,int j,int wt){
// Assert.notFalse(wt!=0,"Cannot set weight to 0");
matrix[i][j]=wt;
numEdge++;
}
public void setEdge(Edge w,int weight){
if(w!=null)setEdge(w.v1(),w.v2(),weight);
}
public void delEdge(Edge w){
if(w!=null)
if(matrix[w.v1()][w.v2()]!=0){
matrix[w.v1()][w.v2()]=0;
numEdge--;
}
}
public void delEdge(int i,int j){
if(matrix[i][j]!=0){
matrix[i][j]=0;
numEdge--;
}
}
public int weight(int i,int j){
if(matrix[i][j]==0)return Integer.MAX_VALUE;
else return matrix[i][j];
}
public int weight(Edge w){
// Assert.notNull(w,"Can't take weight of null edge");
if(matrix[w.v1()][w.v2()]==0)return Integer.MAX_VALUE;
else return matrix[w.v1()][w.v2()];
}
public void setMark(int v,int val)
public int getMark(int v){return Mark[v];
}
}
public class Graphm implements Graph{
private int[][]matrix;
private int numEdge;
public int[]Mark;
public Graphm(int n){
Mark=new int[n];
matrix=new int[n][n];
numEdge=0;
}
public int n()
public int e()
public Edge first (int v){
for(int i=0;i<Mark.length;i++)
if(matrix[v][i]!=0)
return new Edgem(v,i);
return null;
}
public Edge next(Edge w){
if(w==null)return null;
for(int i=w.v2()+1;i<Mark.length;i++ )
if(matrix[w.v1()][i]!=0)
return new Edgem(w.v1(),i);
return null;
}
public boolean isEdge(Edge w){
if(w==null)return false;
else return matrix[w.v1()][w.v2()]!=0;
}
public boolean isEdge(int i,int j){
return matrix[i][j]!=0;}
public int v1(Edge w)
public int v2(Edge w)
public void setEdge(int i,int j,int wt){
// Assert.notFalse(wt!=0,"Cannot set weight to 0");
matrix[i][j]=wt;
numEdge++;
}
public void setEdge(Edge w,int weight){
if(w!=null)setEdge(w.v1(),w.v2(),weight);
}
public void delEdge(Edge w){
if(w!=null)
if(matrix[w.v1()][w.v2()]!=0){
matrix[w.v1()][w.v2()]=0;
numEdge--;
}
}
public void delEdge(int i,int j){
if(matrix[i][j]!=0){
matrix[i][j]=0;
numEdge--;
}
}
public int weight(int i,int j){
if(matrix[i][j]==0)return Integer.MAX_VALUE;
else return matrix[i][j];
}
public int weight(Edge w){
// Assert.notNull(w,"Can't take weight of null edge");
if(matrix[w.v1()][w.v2()]==0)return Integer.MAX_VALUE;
else return matrix[w.v1()][w.v2()];
}
public void setMark(int v,int val)
public int getMark(int v){return Mark[v];
}
热心网友
时间:2023-11-24 02:41
十字链表
简单一点就用邻接矩阵好了