function Graph(){
this.vertices = [];//图的顶点
this.edgeList = new Map();//采用邻接表保存边,使用的是JavaScript的Map数据结构
this.verticesTrue = new Map();//对顶点进行访问
this.result = [];
}
Graph.prototype = {
//添加顶点
addVertex: function(value){
this.edgeList.set(value, []);
this.verticesTrue.set(value,true);
return this.vertices.push(value);
},
//添加边,两个方向都添加
addEdge: function(value1, value2){
if(!this.edgeList.get(value1))return false;
if(!this.edgeList.get(value2))return false;
this.edgeList.get(value1).push(value2);
this.edgeList.get(value2).push(value1);
return true;
},
//打印邻接表到控制台
print: function(){
var str = "";
for(let i = this.vertices.length-1;i>=0;i--){
str += this.vertices[i]+" : ";
str += this.edgeList.get(this.vertices[i]).join(",");
无向图的查找连通分量
最新推荐文章于 2024-05-21 14:15:14 发布