弗洛伊德(Floyd)算法——javascript的实现

//html

弗洛伊德(Floyd)算法——javascript的实现_第1张图片

//html
//javascript
    var floyd = function(){
      console.time("floyd");
      for(k=1;k<=9;k++){
        for(i=1;i<=9;i++){
          for(j=1;j<=9;j++){
            var value1 = document.getElementById(i+""+j).innerHTML;
            var value2 = document.getElementById(i+""+k).innerHTML;
            var value3 = document.getElementById(k+""+j).innerHTML;

            //如果是正无穷,则值转化为可计算的Infinity,否则转化为普通数字
            value1 = isNaN(parseInt(value1))?Infinity:parseInt(value1);
            value2 = isNaN(parseInt(value2))?Infinity:parseInt(value2);
            value3 = isNaN(parseInt(value3))?Infinity:parseInt(value3);
            
            //算法核心
            if(value1 > value2 + value3){
              value1 = value2 + value3;
              //更新矩阵
              document.getElementById(i+""+j).innerHTML = value1+"";
            }
          }
        }
      }
      console.timeEnd("floyd");
      var floyd_button = document.getElementById("floyd_button");
      floyd_button.innerHTML = "请按F12进入console查看算法运行时间!"
    }

你可能感兴趣的:(javascript,html,算法)