JavaScript模拟图操作

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Graphs</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    canvas{
        border: solid 1px;
    }
    </style>
</head>
<body>
    <h1>
        绘制 图
    </h1>

    <canvas id="ca1" width="300" height="300">
        your browser isn't support the Html5
    </canvas>

    <script>
        // this file aimes to draw a graph directly within a Canvas in HTML5
        // use adjencentMatrix to store the Graph
        const kinds = {
            DG: 0,
            DN: 1,
            UDG: 2,
            UDN: 3
        }
        function GraphM(name){
            // data structure of Graph
            this.name = name;
            this.graphKind = "";
            this.vertexType = "";
            this.edgeType = "";
            this.vexnum = "";
            this.arcnum = "";
            this.location = "";  // 为了画图,多加的字段
            this.adjVex = function(v){
                var index = this.vertexType.indexOf(v);
                var result = [];
                if (index != -1){
                    // scan the adjcent vertex
                    for (let i=0; i<this.vexnum; i++){
                        if (this.edgeType[index][i] !== Infinity){
                            result.push(i);
                        };
                    }
                }else{
                    console.log("not in");
                }
                return result;
            };
        }

        function findIndegree(g){
                // 求所有顶点的入度
                // ------ 邻接矩阵存储 ---------
                result = Array(g.vexnum);
                for(let i=0; i<g.vexnum; i++){
                    var temp = 0;
                    for (let j=0; j<g.vexnum; j++){
                        if (g.edgeType[j][i] !== Infinity){
                            temp++;
                        }
                    }
                    result[i] = temp;
                }
                return result;
        };
        
        function topologicalOrder(g){
            // 求图的拓扑路径
            // ------ 邻接矩阵存储 ---------
            var indegree = findIndegree(g);  // 得到图的入度情况
            var s = []; // 初始化栈以存储入度为0的点
            const result = [];
            for (let i=0; i<g.vexnum; i++){
                if (indegree[i] == 0){
                    s.push(i);
                }
            }
            var count = 0;  // 记录已经输出的点数
            while (s.length != 0){
                var j = s.pop();
                count++;
                console.log(g.vertexType[j]);  // 输出顶点i
                result.push(j);
                var adjVexs = g.adjVex(g.vertexType[j]);
                for (let i=0; i<adjVexs.length; i++){
                    if (!(--indegree[adjVexs[i]])){
                        s.push(adjVexs[i]);
                    }
                }
            }
            if (count < g.vexnum){
                return false;
            }else{
                return result;
            }
        };

        function criticalPath(g){
            // 求图的关键路径
            // ------ 邻接矩阵存储 ---------
            var result = topologicalOrder(g);
            if (!result){
                throw Error("图中存在回路,不能求关键路径");
            }
            var ve = Array(g.vexnum);  // vertex early time
            var vl = Array(g.vexnum);  // vertex late time

            // first to solve ve[] and define ve[0] = 0
            for (let i=0; i<ve.length;i++){
                ve[i] = 0;
            }
            for (let i=0; i<g.vexnum-1; ++i){
                var adjVexs = g.adjVex(g.vertexType[i]);
                for (let j=0; j<adjVexs.length; j++){
                    var k = adjVexs[j];
                    if (ve[i] + g.edgeType[i][adjVexs[j]]>ve[k]){
                        ve[k] = ve[i] + g.edgeType[i][adjVexs[j]];
                    }
                }
            }
            // then initial vl[0 ... n-1] = ve[n-1] and solve the value of each vl
            for (let i=0; i<vl.length;++i){
                vl[i] = ve[ve.length-1];
            }
            while (result.length !== 0){
                var i = result.pop();
                var adjVexs = g.adjVex(g.vertexType[i]);
                for (let j=0; j<adjVexs.length; ++j){
                    var k = adjVexs[j];
                    if (vl[k] - g.edgeType[i][adjVexs[j]] < vl[i]){
                        vl[i] = vl[k] - g.edgeType[i][adjVexs[j]];
                    }
                }
            }

            // 输出关键活动
            for (let i=0; i<g.vexnum; i++){
                var adjVexs = g.adjVex(g.vertexType[i]);
                for (let j of adjVexs){
                    var dur = g.edgeType[i][j];
                    var ee = ve[i];
                    var el = vl[j] - dur;
                    var tag = (ee == el) ? "*": "";
                    console.log(`${i} ${j} ${dur} ${ee} ${el} ${tag}`);
                }
            }

            console.log(`ve[] is ${ve}`);
            console.log(`vl[] is ${vl}`);

        };

        function DFS(g){
            // 深度优先搜索
            // ------ 邻接矩阵存储 ---------
            
        }
        var example = new GraphM("example1");
        example.graphKind = kinds.DN;
        example.vertexType = ["V1", "V2", "V3", "V4", "V5", "V6"];
        example.edgeType = [[Infinity, 5, Infinity, 7, Infinity, Infinity],
        [Infinity,Infinity,4,Infinity,Infinity,Infinity],
        [8,Infinity,Infinity,Infinity,Infinity,9],
        [Infinity,Infinity,5,Infinity,Infinity,8],
        [Infinity,Infinity,Infinity,5,Infinity,Infinity],
        [3,Infinity,Infinity,Infinity,1,Infinity]];
        example.vexnum = 6;
        example.arcnum = 10;
        
        //example for 拓扑排序
        var example2 = new GraphM("example2");
        example2.graphKind = kinds.DN;
        example2.vertexType = ["V1", "V2", "V3", "V4", "V5"];
        example2.edgeType = [[Infinity, 1, Infinity, 1, Infinity],
        [Infinity,Infinity,1,1,Infinity],
        [Infinity,Infinity,Infinity,Infinity,1],
        [Infinity,Infinity,1,Infinity,1],
        [Infinity,Infinity,Infinity,Infinity,Infinity]];
        example2.vexnum = 5;
        example2.arcnum = 7;

        // example for 关键路径
        var example3 = new GraphM("example3");
        example3.graphKind = kinds.DN;
        example3.vertexType = ["V1", "V2", "V3", "V4", "V5", "V6"];
        example3.edgeType = [[Infinity, 3, 2, Infinity, Infinity, Infinity],
        [Infinity,Infinity,Infinity,2,3,Infinity],
        [Infinity,Infinity,Infinity,4,Infinity,3],
        [Infinity,Infinity,Infinity,Infinity,Infinity,2],
        [Infinity,Infinity,Infinity,Infinity,Infinity,1],
        [Infinity,Infinity,Infinity,Infinity,Infinity,Infinity]];
        example3.vexnum = 6;
        example3.arcnum = 8;

        // all data perpare...

        console.log(example);

        // paint 
        var canvas = document.getElementById("ca1");
        var ctx = canvas.getContext("2d"); // 用来获得渲染上下文和它的绘画功能
        // ctx.moveTo(150,150);
        ctx.strokeStyle = "black";
        ctx.beginPath();
        ctx.arc(150,150, 30, 0, Math.PI*2, false);
        ctx.stroke();
        ctx.strokeStyle = "blue";
        ctx.beginPath();
        ctx.arc(150,150, 100, 0, Math.PI*2, false);
        ctx.stroke();
        
        // test 拓扑排序
        // if (topologicalOrder(example2)){
        //     console.log("图中不存在回路。");
        // }else{
        //     console.log("图中存在回路。");
        // }

        // test criticalPath
        criticalPath(example3);
    </script>

</body>
</html>

JS操作实现无向网的Prim算法

'use strict';
// MST Prim

const kinds = {
    DG: 0,
    DN: 1,
    UDG: 2,
    UDN: 3
}

function GraphM(name){
    // data structure of Graph
    this.name = name;
    this.graphKind = "";
    this.vertexType = "";
    this.edgeType = "";
    this.vexnum = "";
    this.arcnum = "";
    this.location = "";  // 为了画图,多加的字段
    this.adjVex = function(v){
        var index = this.vertexType.indexOf(v);
        var result = [];
        if (index != -1){
            // scan the adjcent vertex
            for (let i=0; i<this.vexnum; i++){
                if (this.edgeType[index][i] !== Infinity){
                    result.push(i);
                };
            }
        }else{
            console.log("not in");
        }
        return result;
    };
}

// example for MST
var example4 = new GraphM("example4");
example4.graphKind = kinds.UDN;
example4.vertexType = ["V1", "V2", "V3", "V4", "V5", "V6"];
example4.edgeType = [[Infinity, 6, 1, 5, Infinity, Infinity],
[6,Infinity,5,Infinity,3,Infinity],
[1,5,Infinity,5,6,4],
[5,Infinity,5,Infinity,Infinity,2],
[Infinity,3,6,Infinity,Infinity,6],
[Infinity,Infinity,4,2,6,Infinity]];
example4.vexnum = 6;
example4.arcnum = 10;

function Prim(g, v){
    // 对图G的v顶点开始使用Prim算法得到最小生成树
    function data(adjVex, lowcost){
        this.adjVex = adjVex;
        this.lowcost = lowcost;
    }
    // 求closedge中权值不为0的最小的序号
    function minimum(closedge){
        var target = 0;
        var low = Infinity;
        for (let i=0; i<closedge.length;++i){
            if (closedge[i].lowcost !== 0 && closedge[i].lowcost < low){
                low = closedge[i].lowcost;
                target = i;
            }
        }
        return target;
    }
    // 初始化closedge
    var closedge = new Array(g.vexnum);
    for (let i=0; i<closedge.length; ++i){
        var temp = new data("", Infinity);
        closedge[i] = temp;
    }
    // 使初始点的closedge[v] = 0;
    closedge[v].lowcost = 0;
    // first init closedge
    for (let i=0; i<g.vexnum; ++i){
        var cost = g.edgeType[v][i];
        if (cost < closedge[i].lowcost){
            closedge[i].lowcost = cost;
            closedge[i].adjVex = g.vertexType[v];
        }
    }
    
    for (let i=1; i<g.vexnum; i++){
        var v = minimum(closedge);  // new vertex in the set
        // print the edge of chosen
        console.log(`(${closedge[v].adjVex}, ${g.vertexType[v]})`);
        // update the lowcost
        closedge[v].lowcost = 0;
        for (let i=0; i<g.vexnum; i++){
            // update
            var cost = g.edgeType[v][i];
            if (cost < closedge[i].lowcost){
                closedge[i].lowcost = cost;
                closedge[i].adjVex = g.vertexType[v];
            }   
        }
    }
}

Prim(example4, 0);

最后输出结果如下:

PS C:\Users\Administrator\Desktop> node .\mst.js
(V1, V3)
(V3, V6)
(V6, V4)
(V3, V2)
(V2, V5)

其中例子中的图如下:


图片发自简书App

JavaScript实现Dijkstra算法

'use strict';

const kinds = {
    DG: 0,
    DN: 1,
    UDG: 2,
    UDN: 3
}

function GraphM(name){
    // data structure of Graph
    this.name = name;
    this.graphKind = "";
    this.vertexType = "";
    this.edgeType = "";
    this.vexnum = "";
    this.arcnum = "";
    this.location = "";  // 为了画图,多加的字段
    this.adjVex = function(v){
        var index = this.vertexType.indexOf(v);
        var result = [];
        if (index != -1){
            // scan the adjcent vertex
            for (let i=0; i<this.vexnum; i++){
                if (this.edgeType[index][i] !== Infinity){
                    result.push(i);
                };
            }
        }else{
            console.log("not in");
        }
        return result;
    };
}

// example for Dijkstra
var example = new GraphM("example");
example.graphKind = kinds.DN;
example.vertexType = ["V0", "V1", "V2", "V3", "V4", "V5"];
example.edgeType = [[Infinity, Infinity, 10, 30, 100, Infinity],
[Infinity,Infinity,5,Infinity,Infinity,Infinity],
[Infinity,Infinity,Infinity,50,Infinity,Infinity],
[Infinity,Infinity,Infinity,Infinity,Infinity,10],
[Infinity,Infinity,Infinity,20,Infinity,60],
[Infinity,Infinity,Infinity,Infinity,Infinity,Infinity]];
example.vexnum = 6;
example.arcnum = 8;

function dijkstra(g, v0){
    // 迪杰斯特拉算法
    var distance = new Array(g.vexnum);
    var final = new Array(g.vexnum);
    var pathMatrix = [];
    for (let i=0; i<g.vexnum; i++){
        pathMatrix.push(new Array(g.vexnum));
    }


    for (let v=0; v<g.vexnum; v++){
        final[v] = false;
        distance[v] = g.edgeType[v0][v];
        for (let w=0; w<g.vexnum; w++){
            try{
                pathMatrix[v][w] = false;
            }catch (e) {
                console.log(`Error: v is ${v} , w is ${w}`);
            }
            
        }
        if (distance[v] < Infinity){
            pathMatrix[v][v0] = true;
            pathMatrix[v][v] = true;
        }
    }
    // console.log(distance);
    // console.log(pathMatrix);
    // main loop
    distance[v0] = 0;
    final[v0] = true;
    for (let i=1; i<g.vexnum; ++i){
        var min = Infinity;
        for (let w=0; w<g.vexnum; w++){
            if (!final[w]){
                if (distance[w] < min){
                    var v = w;
                    min = distance[w];
                }
            }
        }
        final[v] = true;
        for (let w=0; w<g.vexnum; ++w){
            if (!final[w] && (min + g.edgeType[v][w]<distance[w])){
                distance[w] = min + g.edgeType[v][w];
                pathMatrix[w] = pathMatrix[v];
                pathMatrix[w][w] = true;
            }
        }
    }
    console.log(pathMatrix);
}

function floyd(g){

}

dijkstra(example, 0);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,529评论 5 475
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,015评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,409评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,385评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,387评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,466评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,880评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,528评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,727评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,528评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,602评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,302评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,873评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,890评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,132评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,777评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,310评论 2 342

推荐阅读更多精彩内容