javascript实现kruskal算法

<script>

    //图的构建

    function vnode() {

        this.visited = 0;

        this.vertex = 0;

        this.arcs = new Array();

    }

    function G() {

        this.adjlist = new Array();

    }

    //0  1  2  3  4  5

    var a = [[0, 7, 8, 0, 0, 9], //0

    [0, 0, 0, 2, 5, 0], //1

    [0, 0, 0, 5, 0, 6], //2

    [0, 0, 0, 0, 9, 0], //3

    [0, 0, 0, 0, 0, 7], //4

    [0, 0, 0, 0, 0, 0]]; //5

    function creategraph() {

        var g = new G();

        for (i = 0; i < 6; i++) {

            g.adjlist[i] = new vnode();

            g.adjlist[i].vertex = i;

            g.adjlist[i].arcs = (function () {

                var b = new Array();

                for (j = 0; j < 6; j++)

                    if (a[i][j]) { b.push(new Number(j)); b[b.length - 1].weight = a[i][j]; }

                return b;

            })();

        }

        return g;

    }



    var s = new Array();

    var g = creategraph();

    var arcs = new Array();

    for (i = 0; i < 6; i++) {

        for (j = 0; j < g.adjlist[i].arcs.length; j++) {

            var b = {};

            b.a = i;

            b.b = g.adjlist[i].arcs[j];

            b.c = g.adjlist[i].arcs[j].weight;

            arcs.push(b);

        }





    }

    arcs.sort(function (a, b) {

        return a.c - b.c;

    })



    function kruskal() {

        for (i = 0; i < 6; i++) {

            s.push([i]);

            s[i].e = new Array();

        }

        var flag = 0;

        loop: for (i = 0; i < arcs.length; i++) {//判断一条边是否可以被选取

            if (flag) { i = 0; flag = 0; continue; }

            for (j = 0; j < s.length; j++) {//检查选取的边的两个端点是否同时落在以构建的树中

                for (k = 0; k < s[j].length; k++) {

                    if (s[j][k] == arcs[i].a) {//如果一个端点落在一棵树上,继续检查另一个端点是否落在同一棵树上



                        for (l = 0; l < s[j].length; l++) {

                            if (s[j][l] == arcs[i].b) {//如果两端点都落在同一棵树上,则继续取下一条边进行验证

                                continue loop;



                            }

                            if (l == (s[j].length - 1)) {//不落在同一棵树上,则把一个端点所在的树和另一个端点所在的树合并

                                for (m = 0; m < s.length; m++) {

                                    for (n = 0; n < s[m].length; n++) {



                                        if (s[m][n] == arcs[i].b) {//找到另一端点所在的树,进行合并

                                            alert(arcs[i].b + "---" + arcs[i].a);

                                            for (v = 0; v < s[m].length; v++) {

                                                s[j].push(s[m][v]);

                                            }

                                            flag = 1;

                                            s.splice(m, 1);

                                            continue loop;



                                        }





                                    }



                                }





                            }



                        }





                    }





                }





            }



        }

        alert(s.length);

    }

    kruskal();

</script>

 

你可能感兴趣的:(JavaScript)