Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>最小生成树</title>
    <style>
        circle {
            fill: blueviolet;
        }

        line {
            stroke-width: 1px;
            stroke: brown;
        }
    </style>
</head>

<body>
    <svg id="svg" xmlns="http://www.w3.org/2000/svg"></svg>
    <script>
        var width = document.body.clientWidth;
        var height = window.innerHeight - 50;
        var svg = document.getElementById("svg");
        var dots = 1000;
        var i = 0;
        var allCircle = [];
        const namespace = "http://www.w3.org/2000/svg";

        svg.setAttribute("width", width);
        svg.setAttribute("height", height);

        for (; i < dots; i++) {
            allCircle.push({
                cx: ~~(Math.random() * width),
                cy: ~~(Math.random() * height)
            });
        }

        class Graph {
            constructor(dots) {
                this.dots = dots;
                this.matrix = [];
                this.visited = [];
                this.lines = [];

                this.drawDots();
                this.adjacencyMatrix();

                console.time('time')
                // this.getMinTree(); // 经典
                this.getHeapPrimTree(); // better
                console.timeEnd('time')

                this.drawLines();
            }
            adjacencyMatrix() {
                var dots = this.dots;
                var length = dots.length;

                for (var i = 0; i < length; i++) {
                    this.matrix[i] = [];
                    for (var j = 0; j < length; j++) {
                        this.matrix[i][j] =
                            i !== j ? this.getDistance2(dots[i], dots[j]) : Number.MAX_SAFE_INTEGER;
                    }
                }
            }
            getHeapPrimTree () {
                let i = 0;
                let j;
                let dotsLength = this.dots.length;
                var currentEndPoint = 0

                for (j = 0; j < dotsLength; j += 1) {
                    if (j === 0) {
                        this.lines.push(null)
                    } else {
                        this.lines.push({ // 用一个长度为节点数量的数组保存权重值和起始位置
                            from: this.dots[0], // 一条边的起点
                            to: this.dots[j], // 一条边的终点
                            weight: this.matrix[0][j],
                        })
                    }
                }

                this.visited.push(0) // 从第 0 顶点开始,表示已经访问过了

                do {
                    var minWeight = Number.MAX_SAFE_INTEGER;

                    for (i = 1; i < dotsLength; i++) {
                        if (this.lines[i].weight < minWeight
                            && this.visited.indexOf(i) === -1) {
                            minWeight = this.lines[i].weight
                            currentEndPoint = i // 找到最小权重
                        }
                    }

                    this.visited.push(currentEndPoint) // 表示此节点已经访问

                    for (j = 0; j < dotsLength; j += 1) {
                        if (this.visited.indexOf(j) === -1
                            && this.matrix[currentEndPoint][j] < this.lines[j].weight) {
                            this.lines[j] = {
                                // 查找与 currentEndPoint 相邻的所有顶点,如果小于当前位置的权重则更新
                                from: this.dots[currentEndPoint],
                                to: this.dots[j],
                                weight: this.matrix[currentEndPoint][j]
                            }
                        }
                    }
                    // 直到所有的节点已经访问完
                } while(this.visited.length < dotsLength)
            }
            getMinTree() {
                var dots = this.dots;
                var length = dots.length;
                var min;
                var from;
                var to;

                this.visited.push(0);
                while (this.visited.length < length) {
                    min = Number.MAX_SAFE_INTEGER;

                    for (var i = 0; i < this.visited.length; i++) {
                        from = this.visited[i];

                        for (var j = 0; j < length; j++) {
                            if (this.matrix[from][j] < min && this.visited.indexOf(j) === -1) {
                                min = this.matrix[from][j];
                                to = j;
                                var realFrom = from;
                            }
                        }
                    }

                    // console.log(realFrom, to);

                    this.lines.push({
                        from: dots[realFrom],
                        to: dots[to]
                    });
                    this.visited.push(to);
                }
            }
            getDistance2(p1, p2) {
                return ~~Math.sqrt((p1.cx - p2.cx) ** 2 + (p1.cy - p2.cy) ** 2);
            }
            drawDots() {
                this.dots.forEach(ele => {
                    var dot = document.createElementNS(namespace, "circle");

                    dot.setAttribute("cx", ele.cx);
                    dot.setAttribute("cy", ele.cy);
                    dot.setAttribute("r", 2);

                    svg.appendChild(dot);
                });
            }
            drawLines() {
                var i = 0;
                var draw = () => {
                    var line = document.createElementNS(namespace, "line");
                    var _ = this.lines[i]

                    if (_) {
                        line.setAttribute("x1", _.from.cx);
                        line.setAttribute("y1", _.from.cy);

                        line.setAttribute("x2", _.to.cx);
                        line.setAttribute("y2", _.to.cy);

                        svg.appendChild(line);
                    }

                    i++;
                    if (i < this.lines.length) {
                        requestAnimationFrame(draw);
                    }
                };

                draw();
            }
        }

        var g = new Graph(allCircle);
        console.log(g);
    </script>
</body>

</html>
            
          
!

CSS

            
              
            
          
!

JS

            
              
            
          
!
999px

Console