数据结构与算法JavaScript描述-个人练习(Graph)

#Data Structure - Graph @(Data Structure & Algorithm)

function Vertex(key){
    this.key = key;
}
function Graph(num){
    this.vNum = num;
    this.edgeNum = 0;
    this.adj = [];//adjacent matrix
    this.visited = [];
    this.edgeTo = [];
}
Graph.prototype = {
    constructor: Graph,
    makeAdj: function(){
        for (var i = 0; i < this.vNum; i++){
            this.adj[i] = [];
        }
    },
    clearVisited: function(){
        for (var i = 0; i < this.vNum; i ++ ){
            this.visited[i] = false;
        }
    },
    //第一维的key为顶点的key,val为第二维数组
    //第二维的key为1,2,3,val为顶点的key
    addEdge: function(v,w){
        this.adj[v].push(w);
        this.adj[w].push(v);
        this.edgeNum ++;
    },
    display: function(){
        for (var i = 0; i < this.vNum; i++){
            console.log(i + "---");
            for (var j = 0; j < this.adj[i].length; j++){
                console.log(this.adj[i][j]);
            }
        }
    },
    //遍历
    //deep first search
    //一个顶点只会被访问一次
    dfs: function(v){
        this.visited[v] = true;
        console.log("Visit " + v);
        //for-in 返回adj第二维的key,即1,2,3,而非顶点的key
        for (var i in this.adj[v]){
            //顶点的key为其value
            var w = this.adj[v][i];
            if (!this.visited[w]){
                this.dfs(w);
            }
        }
    },
    bfs: function(s){
        var q = [];
        this.visited[s] = true;
        console.log("Visit "+s);
        q.push(s);
        while (q.length > 0){
            var v = q.shift();
            for (var i in this.adj[v]){
                var w = this.adj[v][i];
                if (!this.visited[w]){
                    this.edgeTo[w] = v;
                    this.visited[w] = true;
                    console.log("Visit " + w);
                    q.push(w);
                }
            }
        }
    },
    //查找最短路径
    //从s到v点
    pathTo: function(s,v){
        var source = s, //定义起点
            path = [];
        if (!this.hasPathTo(v)){
            return false;
        }
        //从v开始,通过edgeTo()来反向倒推,直到循环回到source为止
        for (var i = v; i != source; i = this.edgeTo[i]){
            path.unshift(i);
        }
        path.unshift(s);
        return path;
    },
    hasPathTo: function(v){//求是否可经遍历被访问
        return this.visited[v];
    },
    //拓扑排序
    topSort: function(){
        var r = [],
            visited = [];
        for (var i = 0; i < this.vNum; i++){
            visited[i] = false;
        }
        for (i = 0; i < this.vNum; i++){
            if (visited[i] === false){
                this.topSortHelper(i, visited, r);
            }
        }
        //print
        while(r.length > 0){
            var v = r.pop();
            console.log(v);
        }
    },
    //recursive part
    topSortHelper: function(v, visited, r){
        visited[v] = true;
        for (var j in this.adj[v]){
            var w = this.adj[v][j];
            if (!visited[w]){
                this.topSortHelper(w,visited,r);
            }
        }
        r.push(v);
    }
};


var g = new Graph(5);
g.makeAdj();
g.clearVisited();
g.addEdge(0,1);
g.addEdge(0,2);
g.addEdge(1,3);
g.addEdge(2,4);
g.display();

console.log("dfs");
g.dfs(0);
console.log("bfs");
g.bfs(0);

//使用最短路径搜索之前必须使用同一个source对图进行遍历!!!
g.bfs(3);
console.log(g.pathTo(3,2));

var h = new Graph(6);
h.makeAdj();
h.clearVisited();
h.addEdge(0,1);
h.addEdge(1,2);
h.addEdge(1,3);
h.addEdge(2,4);
h.addEdge(2,5);
h.topSort();

转载于:https://my.oschina.net/xixine/blog/471547

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值