JavaScript数据结构14——图的深度优先遍历
当图用邻接矩阵表示的时候,算法如下
//图的深度优先遍历
//用邻接矩阵存储一个图
//顶点
function Vertex(name) {
this.name =name;
}
//邻接矩阵
//maxvex:顶点数
//arcnum:边数
function arc(maxvex,arcnum){
this.maxvex = maxvex;
this.arcnum = arcnum;
this.data = new Array(maxvex);
for (var i = 0; i < this.data.length; i++) {
this.data[i] = new Array(maxvex);
for (var j = 0; j < this.data[i].length; j++) {
this.data[i][j] = Infinity;
if(i==j){
this.data[i][j] = 0;
}
}
}
}
//图
function Mgraph(maxvex,arcnum,vertexs){
this.arc = new arc(maxvex,arcnum);
this.vertexs = vertexs;
}
//添加边
Mgraph.prototype.addArc = function(start,end,length){
var i = this.vertexs.indexOf(start);
var j = this.vertexs.indexOf(end);
this.arc.data[i][j] = length;
this.arc.data[j][i] = length;
}
//建造一个
var v0 = new Vertex('V0');
var v1 = new Vertex('V1');
var v2 = new Vertex('V2');
var v3 = new Vertex('V3');
var v4 = new Vertex('V4');
var vertexs = [v0,v1,v2,v3,v4];
var mgraph = new Mgraph(5,6,vertexs);
mgraph.addArc(v1,v0,9);
mgraph.addArc(v1,v2,3);
mgraph.addArc(v2,v3,5);
mgraph.addArc(v3,v4,1);
mgraph.addArc(v0,v4,6);
mgraph.addArc(v2,v0,2);
//console.info(mgraph.arc);
//基础算法
Mgraph.prototype.DFS = function(i){
this.vertexs[i].visited = true;
console.info(this.vertexs[i]);
for(var j=0;j<this.arc.maxvex;j++){
var boolean1 = this.arc.data[i][j]>0&&this.arc.data[i][j]<Infinity;
if(boolean1&&this.vertexs[j].visited){
console.info(this.vertexs[j].name+' is visited!')
}
if(boolean1&&!this.vertexs[j].visited){
console.info('to '+this.vertexs[j].name)
this.DFS(j);
}
}
}
Mgraph.prototype.DFSTraverse = function(){
for (var i = 0; i < this.arc.maxvex; i++) {
this.vertexs[i].visited = false;
}
for (var i = 0; i < this.arc.maxvex; i++) {
if(!this.vertexs[i].visited){
this.DFS(i);
}
}
}
mgraph.DFSTraverse();
输出
Vertex { name: 'V0', visited: true }
to V1
Vertex { name: 'V1', visited: true }
V0 is visited!
to V2
Vertex { name: 'V2', visited: true }
V0 is visited!
V1 is visited!
to V3
Vertex { name: 'V3', visited: true }
V2 is visited!
to V4
Vertex { name: 'V4', visited: true }
V0 is visited!
V3 is visited!
V2 is visited!
V4 is visited!
[Finished in 0.1s]
如果用邻接表储存,则代码为
//图的深度优先遍历
//图的邻接表
//顶点
function Vertex(name) {
this.name =name;
}
//边
function EdgeNode(weight,adjVex){
this.weight = weight;
this.adjVex = adjVex;
}
//图
function Graph(vertexs){
this.vertexs = vertexs;
}
var v0 = new Vertex('V0');
var v1 = new Vertex('V1');
var v2 = new Vertex('V2');
var v3 = new Vertex('V3');
var v4 = new Vertex('V4');
var edge1 = new EdgeNode(6,v4);
var edge2 = new EdgeNode(9,v0);
var edge3 = new EdgeNode(2,v0);
var edge4 = new EdgeNode(4,v1);
var edge5 = new EdgeNode(3,v2);
var edge6 = new EdgeNode(5,v3);
v0.firstEdge = edge1;
v1.firstEdge = edge2;
v2.firstEdge = edge3;
v3.firstEdge = edge4;
edge2.next = edge5;
edge3.next = edge6;
var vertexs = [v0,v1,v2,v3,v4];
var g = new Graph(vertexs);
console.info(g.vertexs);
//基础算法
Graph.prototype.DFS = function(vertex){
vertex.visited = true;
console.info(vertex.name);
var p = vertex.firstEdge;
while(p){
if(p.adjVex.visited){
console.info(p.adjVex.name+' is visited!');
}
if(!p.adjVex.visited){
console.info('to '+p.adjVex.name);
this.DFS(p.adjVex);
}
p = p.next;
}
}
Graph.prototype.DFSTraverse = function(){
for (var i = 0; i < this.vertexs.length; i++) {
this.vertexs[i].visited = false;
}
for (var i = 0; i < this.vertexs.length; i++) {
if(!this.vertexs[i].visited){
this.DFS(this.vertexs[i]);
}
}
}
g.DFSTraverse();
打印
[ Vertex {
name: 'V0',
firstEdge: EdgeNode { weight: 6, adjVex: [Object] } },
Vertex {
name: 'V1',
firstEdge: EdgeNode { weight: 9, adjVex: [Object], next: [Object] } },
Vertex {
name: 'V2',
firstEdge: EdgeNode { weight: 2, adjVex: [Object], next: [Object] } },
Vertex {
name: 'V3',
firstEdge: EdgeNode { weight: 4, adjVex: [Object] } },
Vertex { name: 'V4' } ]
V0
to V4
V4
V1
V0 is visited!
to V2
V2
V0 is visited!
to V3
V3
V1 is visited!
[Finished in 0.1s]