• Graph类:

    function Graph(v) { this.vertices = v; this.edges = 0; this.adj = [];
    //初始化adj for (var i = 0; i < this.vertices; ++i) { this.adj[i] = []; this.adj[i].push(""); } this.addEdge = addEdge; this.showGraph = showGraph; } //插入边
    //邻接表 function addEdge(v, w) { this.adj[v].push(w); this.adj[w].push(v);
    this.edges++;
    }
    /*邻接矩阵存储......*/


    //输出边 function showGraph() { for (var i = 0; i < this.vertices; ++i) { putstr(i + " -> "); for (var j = 0; j < this.vertices; ++j ) { if (this.adj[i][j] != undefined) { putstr(this.adj[i][j] + ' '); } } print(); } }

    /*
    vertices 节点数, Graph(v)传入的v是节点数
    edges 边数
    adj (邻接表数组,这里使用了法一,一个二维数组,每个节点的临界节点数组组成的数组)
    */

      

    以下测试程序演示了 Graph 类的用法:
    load("Graph.js"); 

    g = new Graph(5); 

    g.addEdge(0,1); 

    g.addEdge(0,2); 

    g.addEdge(1,3); 

    g.addEdge(2,4); 

    g.showGraph();
    程序的输出结果为:
    0 -> 1 2 

    1 -> 0 3 

    2 -> 0 4 

    3 -> 1 

    4 -> 2

    来自:《数据结构与算法JavaScript描述》

  • 相关阅读:
    V8 下的垃圾回收机制
    数据库索引原理
    多线程的实现方法
    网元的概念
    Oracle 数据库实现数据合并:merge
    Linux账号管理
    Linux 进程管理 ps、top、pstree命令
    linux OS与SQL修改时区,系统时间
    数据库的几种模式
    linux上限值网速、限值带宽
  • 原文地址:https://www.cnblogs.com/Longhua-0/p/9573053.html
Copyright © 2020-2023  润新知