public class Graph_init {
public static int MAX_GRAPH_SIZE=256; //最大顶点个数;
public static int MAX_WEIGHT=65536; //图中最大权值
public int[][] edge ; //邻阶矩阵
public int graphSize; //定义顶点数量
public Graph_init(){
System.out.println("请输入顶点个数:");
Scanner in=new Scanner(System.in);
graphSize=in.nextInt();
edge=new int[graphSize][graphSize]; //初始化顶点个数
/*
* 存入图的权重数据
* 注意无向图是对称的
*/
for(int i=0;i<graphSize;i++){
edge[i][i]=0;
for(int j=i+1;j<graphSize;j++){
int weight=in.nextInt();
if (weight<0&&weight>6556) {
edge[i][j]=65536;
edge[j][i]=65536;
}else{
edge[i][j]=weight;
edge[j][i]=weight;
}
}
}
}
public void getElem(){ //读取图中相邻节点的权值
for(int i=0;i<edge.length;i++){
for(int j=0;j<edge.length;j++){
System.out.print(edge[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Graph_init init=new Graph_init();
init.getElem();
}
}
图的邻接矩阵存储
最新推荐文章于 2023-11-20 23:00:29 发布