連通図は最小の木を生成するプリムのアルゴリズムとクルーズカードのアルゴリズムのJavaScriptを解決して実現します.

3273 ワード

最近は「大話データ構造」を見ています.本の中でC言語で説明したのは、JavaScriptでプリムアルゴリズムとクルスカルアルゴリズムを実現します.ネットの最小生成樹を解きます.
  • プリムアルゴリズム
  • //                 
    const _ = require('lodash')
    //       
    const G_0 = [
      [0, 10, Infinity, Infinity, Infinity, 11, Infinity, Infinity, Infinity],
      [10, 0, 18, Infinity, Infinity, Infinity, 16, Infinity, 12],
      [Infinity, 18, 0, 22, Infinity, Infinity, Infinity, Infinity, 8],
      [Infinity, Infinity, 22, 0, 20, Infinity, 24, 16, 21],
      [Infinity, Infinity, Infinity, 20, 0, 26, Infinity, 7, Infinity],
      [11, Infinity, Infinity, Infinity, 26, 0, 17, Infinity, Infinity],
      [Infinity, 16, Infinity, 24, Infinity, 17, 0, 19, Infinity],
      [Infinity, Infinity, Infinity, 16, 7, Infinity, 19, 0, Infinity],
      [Infinity, 12, 8, 21, Infinity, Infinity, Infinity, Infinity, 0],
    ]
    
    function prim(G) {
      'use strict'
      //    :     0       , lowcost  0        
      let lowcost = G[0], len = G.length, adjvex = _.times(len, () => 0)
      for (let i = 1; i < len; i++) {
        let min = Infinity, j = 1, k = 0
        while (j < len) {
          //                      
          if (lowcost[j] != 0 && lowcost[j] < min) {
            min = lowcost[j]
            k = j
          }
          j++
        }
        //           
        console.log(adjvex[k], k)
        //    k         
        lowcost[k] = 0
    
        //    k                               
        for (let m = 1; m < len; m++) {
          if (lowcost[m] != 0 && G[k][m] < lowcost[m]){
    
            lowcost[m] = G[k][m]
            adjvex[m] = k
    
          }
        }
      }
    }
    
    prim(G_0)
    
  • クルーズカードアルゴリズム
  • /**
     * Created by janeluck on 17/7/11.
     */
    //                   
    const _ = require('lodash')
    //       
    const G_0 = [
      [0, 10, Infinity, Infinity, Infinity, 11, Infinity, Infinity, Infinity],
      [10, 0, 18, Infinity, Infinity, Infinity, 16, Infinity, 12],
      [Infinity, 18, 0, 22, Infinity, Infinity, Infinity, Infinity, 8],
      [Infinity, Infinity, 22, 0, 20, Infinity, 24, 16, 21],
      [Infinity, Infinity, Infinity, 20, 0, 26, Infinity, 7, Infinity],
      [11, Infinity, Infinity, Infinity, 26, 0, 17, Infinity, Infinity],
      [Infinity, 16, Infinity, 24, Infinity, 17, 0, 19, Infinity],
      [Infinity, Infinity, Infinity, 16, 7, Infinity, 19, 0, Infinity],
      [Infinity, 12, 8, 21, Infinity, Infinity, Infinity, Infinity, 0],
    ]
    //              
    function generateEdge(G) {
      'use strict'
      let edge = [], len = G.length
      for (let i = 0; i < len; i++) {
        for (let j = i + 1; j < len; j++) {
          //        
          if (G[i][j] !== Infinity) {
            edge.push({
              'begin': i,
              'end': j,
              'weight': G[i][j]
            })
          }
        }
      }
      return _.sortBy(edge, 'weight')
    }
    function kruskal(G) {
      'use strict'
      const edge = generateEdge(G), find = function (parent, f) {
        while (parent[f] > 0) {
          f = parent[f]
        }
        return f
      }
      let parent = _.times(G.length, () => 0)
      for (let i = 0; i < edge.length; i++) {
        const n = find(parent, edge[i]['begin']), m = find(parent, edge[i]['end'])
    
        // n m  ,                 
        if (n !== m) {
          //                  parant 
          parent[n] = m
          //           
          console.log(edge[i])
        }
      }
    }
    kruskal(G_0)