当前位置: 首页 > Web前端 > HTML

图-邻接矩阵

时间:2023-03-28 12:19:17 HTML

/***模拟一个无向图——邻接矩阵*/classUndirectedVertexGraph{constructor(size){this.size=size;this.vertexesList=[];//保存顶点this.edgeMatex=newArray(10).fill(0).map(()=>newArray(10).fill(0));//用来保存邻接矩阵this.vertexes=0;//保存顶点数this.edges=0;//保存边数}edgeSize(){returnthis.vertexes;}//添加一个顶点addVertex(vertex){this.vertexesList.push(vertex);这个顶点++;}//添加一条新边addEdge(from,to,weight=1){consti=this.vertexesList.indexOf(from);constj=this.vertexesList.indexOf(to);this.edgeMatex[i][j]=权重;this.edgeMatex[j][i]=权重;这个。边缘++;}//删除一条边removeEdge(){consti=this.vertexesList.indexOf(from);constj=this.vertexesList.indexOf(to);this.edgeMatex[i][j]=0;this.edgeMatex[j][i]=0;this.edges--;}//删除一个顶点removeVertex(v){constindex=this.vertexesList.indexOf(v);for(leti=0;i