随笔 - 101  文章 - 0  评论 - 0  阅读 - 456

数据结构与算法-图结构

封装

/**
 *
 * 邻接表表示法
 *
 * */

const {queue} = require('../../队列/基于链表的队列/LinkQueue');


//字典
function Dictionary(){
    this.group = {};
    this.size = 0;


    Dictionary.prototype.set = function (key,val){
        if (!(key in this.group)) this.size +=1;
        this.group[key] = val;
    }


    Dictionary.prototype.get = function (key){
            if (key in this.group)  return this.group[key];
            return null;
    }

    Dictionary.prototype.toString = function (){
        let str = '';
        for (let key in this.group){
            str += this.group[key]+' ';
        }
        return str;
    }



}




//图
function Graph(){

    this.vertexes = [];//顶点
    this.edges = new Dictionary();//边

    //添加顶点
    Graph.prototype.addVertex = function (v){
        this.vertexes.push(v);
        this.edges.set(v,[]);
    }

    //添加边
    Graph.prototype.addEdge = function (v1,v2){
        this.edges.get(v1).push(v2);
        this.edges.get(v2).push(v1);
    }


    //图遍历的两种方式:
    //1、广度优先BFS
    //2、深度度优先DFS


    Graph.prototype.BFS = function (fistv,Handle){
        //初始化状态颜色和队列
        let colors = this.initColor();
        let que = new queue();

        //添加第一个节点
        que.enQueue(fistv);

        while (!que.isEmpty()){

            //访问节点
            let v = que.deQueue();

            //修改状态
            colors[v] = 'gray';

            //获取该节点所连接的节点
            let vlist = this.edges.get(v);

            for (let i = 0; i < vlist.length; i++) {
                if (colors[vlist[i]] === 'white'){
                    colors[vlist[i]] = 'gray';
                    que.enQueue(vlist[i]);
                }
            }
            //处理节点
            Handle(v);
            //修改状态
            colors[v] = 'black';
        }



    }
    Graph.prototype.DFS = function (firstv,handle){
        this.DFSVisit(firstv,this.initColor(),handle);
    }
    Graph.prototype.DFSVisit = function (v,colors,handle){
        //访问
        colors[v] = 'gray';
        //处理
        handle(v);

        let vertexs = this.edges.get(v);
        for (let i = 0; i < vertexs.length; i++) {
            let e = vertexs[i];
            if (colors[e] === 'white'){
                colors[e] = 'gray';
                this.DFSVisit(e,colors,handle);
            }
        }
        colors[v] = 'black';
    }



        //初始化顶点状态
    Graph.prototype.initColor = function (){
        let colors = [];
        for (let i = 0; i < this.vertexes.length; i++) {
            colors[this.vertexes[i]] = 'white'
        }
        return colors
    }

    Graph.prototype.toString = function (){
        let str = '';
        for (let i = 0; i < this.vertexes.length; i++) {
            let str2 = '';
            for (let j = 0; j < this.edges.get(this.vertexes[i]).length; j++) {
                str2 += this.edges.get(this.vertexes[i])[j]+" "
            }
            str += this.vertexes[i]+"->"+str2+"\r\n";
        }
        return str;
    }


}

let graph = new Graph();
let str = '';
let str1 = '';
function handle(v){
    str += v+" ";
}

function handle1(v){
    str1 += v+" ";
}

let myVertex = ['A','B','C','D','E','F','G','H','I'];

for (let i = 0; i < myVertex.length; i++) {
    graph.addVertex(myVertex[i]);
}

graph.addEdge('A','B');
graph.addEdge('A','C');
graph.addEdge('A','D');

graph.addEdge('C','D');
graph.addEdge('C','G');


graph.addEdge('D','G');
graph.addEdge('D','H');

graph.addEdge('B','E');
graph.addEdge('B','F');
graph.addEdge('E','I');


console.log(graph.toString());

graph.BFS('A',handle);
graph.DFS('H',handle1);

console.log(str);
console.log(str1);

posted on   千里码!  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示