当前位置: 首页 > 编程日记 > 正文

优先级队列(小顶堆)的dijkstra算法

php实现迪杰斯特拉算法,并由小顶堆优化

  1 <?php
  2 
  3 class DEdge
  4 {
  5     public $nextIndex, $length;
  6 
  7     public function __construct($nextIndex, $length)
  8     {
  9         $this->nextIndex = $nextIndex;
 10         $this->length = $length;
 11     }
 12 }
 13 
 14 class DNode
 15 {
 16     public $index, $distance, $edges = [];
 17 
 18     public function __construct($index, $distance)
 19     {
 20         $this->index = $index;
 21         $this->distance = $distance;
 22     }
 23 
 24     public function addEdge(DEdge $edge)
 25     {
 26         $this->edges[] = $edge;
 27     }
 28 }
 29 
 30 class Dijkstra
 31 {
 32     protected $origin;
 33     protected $graph = [], $dGraph = [], $heap = [], $visited = [], $heapVisited = [];
 34 
 35     public function __construct(array $graph, $origin)
 36     {
 37         $this->graph = $graph;
 38         $this->origin = $origin;
 39         $this->visited[$origin] = true;
 40         $this->initializeGraph();
 41         $this->initializeHeap();
 42         $this->calculateDistance();
 43     }
 44 
 45     public function printDistance()
 46     {
 47         foreach ($this->dGraph as $dNodes) {
 48             var_dump([$dNodes->index, $dNodes->distance]);
 49         }
 50     }
 51 
 52     protected function initializeGraph()
 53     {
 54         foreach ($this->graph as $index => $edges) {
 55             $dNode = new DNode($index, $edges[$this->origin]);
 56             foreach ($edges as $toIndex => $edge) {
 57                 $dNode->addEdge(new DEdge($toIndex, $edge));
 58             }
 59             $this->dGraph[$dNode->index] = $dNode;
 60         }
 61     }
 62 
 63     protected function initializeHeap()
 64     {
 65         foreach ($this->dGraph as $index => $node) {
 66             if ($index != $this->origin && $node->distance != INF) {
 67                 $this->addToHeap($node);
 68             }
 69         }
 70     }
 71 
 72     protected function calculateDistance()
 73     {
 74         while (($nearestNode = $this->heapPop()) != null) {
 75             foreach ($nearestNode->edges as $edge) {
 76                 if ($this->dGraph[$edge->nextIndex]->distance >
 77                     $nearestNode->distance + $edge->length) {
 78                     $this->dGraph[$edge->nextIndex]->distance =
 79                         $nearestNode->distance + $edge->length;
 80                     if (!isset($this->heapVisited[$edge->nextIndex])) {
 81                         $this->addToHeap($this->dGraph[$edge->nextIndex]);
 82                     } else {
 83                         $this->keepHeap($this->heapVisited[$edge->nextIndex]);
 84                     }
 85                 }
 86             }
 87         }
 88     }
 89 
 90     protected function heapPop()
 91     {
 92         $heapCount = count($this->heap);
 93         if ($heapCount > 0) {
 94             $this->swap(0, $heapCount - 1);
 95         }
 96         $pop = array_pop($this->heap);
 97         $this->keepHeap(0, false);
 98         return $pop;
 99     }
100 
101     protected function keepHeap($startAt, $up = true)
102     {
103         if ($up) {
104             while ($startAt > 0) {
105                 $parentIndex = intval(($startAt - 1) / 2);
106                 if ($this->heap[$parentIndex]->distance > $this->heap[$startAt]->distance) {
107                     $this->swap($parentIndex, $startAt);
108                     $startAt = $parentIndex;
109                     $this->heapVisited[$this->heap[$startAt]->index] = $startAt;
110                 } else {
111                     break;
112                 }
113             }
114         } else {
115             $lastIndex = count($this->heap) - 1;
116             while ($startAt < $lastIndex) {
117                 $lIndex = 2 * $startAt + 1;
118                 $rIndex = $lIndex + 1;
119                 if (isset($this->heap[$rIndex])) {
120                     $minIndex = $this->heap[$lIndex]->distance <
121                     $this->heap[$rIndex]->distance ? $lIndex : $rIndex;
122                 } else if (isset($this->heap[$lIndex])) {
123                     $minIndex = $lIndex;
124                 } else {
125                     break;
126                 }
127                 if ($this->heap[$startAt]->distance > $this->heap[$minIndex]->distance) {
128                     $this->swap($minIndex, $startAt);
129                     $startAt = $minIndex;
130                     $this->heapVisited[$this->heap[$startAt]->index] = $startAt;
131                 } else {
132                     break;
133                 }
134             }
135         }
136     }
137 
138     protected function addToHeap(DNode $dNode)
139     {
140         $this->heap[] = $dNode;
141         $this->keepHeap(count($this->heap) - 1);
142     }
143 
144     protected function swap($index1, $index2)
145     {
146         list($this->heap[$index1], $this->heap[$index2]) =
147             [$this->heap[$index2], $this->heap[$index1]];
148     }
149 }
150 
151 $graph = [
152     [0, 4, INF, 2, INF],
153     [4, 0, 4, 1, INF],
154     [INF, 4, 0, 1, 3,],
155     [2, 1, 1, 0, 7],
156     [INF, INF, 3, 7, 0],
157 ];
158 
159 $start = 0;
160 
161 $dijkstra = new Dijkstra($graph, $start);
162 
163 $dijkstra->printDistance();

转载于:https://www.cnblogs.com/SHQHDMR/p/11184192.html

相关文章:

室内设计木地板材质合集包 Arroway – Design Craft Vol.4

室内设计木地板材质合集包 Arroway – Design Craft Vol.4 室内设计木地板材质合集包 Arroway – Design Craft Vol.4 阿洛维——设计工艺第四卷 大小&#xff1a;20G 信息: 云桥网络 平台获取素材&#xff01; 36种单板纹理 纹理包括漫反射、法线、凹凸、反射率、环境遮挡…

linux下有关phy的命令,linux – 如何为Debian安装b43-lpphy-installer?

b43-lpphy-installer是Ubuntu的包的名称,而不是Debian的包.你可以在jessie(Debian 8)中使用命令安装它&#xff1a;sudo apt-get install firmware-b43-installer通过内核版本,您似乎正在使用Debian 8.要了解有关debian软件包的详细信息,您可以按名称或文件搜索软件包&#xff…

Idea SpringBoot 基于 Docker容器环境进行远程调试

远程服务环境要求 对启动的jar服务命令进行修改&#xff0c;改成远程调试模式启动 eg: java -jar -agentlib:jdwptransportdt_socket,servery,suspendn,address18761 app.jar此命令特别之处是 关注监听端口&#xff1a;address18761&#xff0c;这端口号随性定义。 -agentl…

[转载]python optionparser1

原文地址&#xff1a;python optionparser1作者&#xff1a;afu7Python 有两个内建的模块用于处理命令行参数&#xff1a; 一个是 getopt&#xff0c;《Deep in python》一书中也有提到&#xff0c;只能简单处理 命令行参数&#xff1b; 另一个是 optparse&#xff0c;它功能强…

回溯法实现正则匹配判断

*&#xff1a;匹配任意个字符 &#xff1f;&#xff1a;匹配至多1个字符 <?phpclass MNode {public $strIndex;public $patIndex;public $leftMatch null; //精确匹配public $midMatch null; //模式匹配public $rightMatch null; //不能匹配public function __con…

Blender中的Python脚本介绍学习教程

Blender中的Python脚本介绍学习教程 MP4 |视频:h264&#xff0c;1280720 |音频:AAC&#xff0c;48000 Hz 语言&#xff1a;英语中英文字幕&#xff08;根据原英文字幕机译更准确&#xff09;|大小解压后:1.63 GB |时长:2h 39m 你会学到什么 云桥网络 平台获取教程&#xf…

linux的veth导致网络不通,linux的veth对网桥通信实验

本实验脚本如下:#!/bin/bash#网桥名称bridgebr0#网桥接入端ipip1192.168.10.1ip2192.168.10.2#veth名称tap1tap1tap2tap2#创建网络命名空间ip netns add ns1ip netns add ns2#创建并启用网桥br0,且关闭stpip link add $bridge type bridgeip link set $bridge type bridge stp_…

图片和文件上传的两款插件

订定转载于:https://www.cnblogs.com/mc67/p/4818276.html

2022-2028年中国装配式装修行业市场研究及前瞻分析报告

【报告类型】产业研究 【报告价格】4500起 【出版时间】即时更新&#xff08;交付时间约3个工作日&#xff09; 【发布机构】智研瞻产业研究院 【报告格式】PDF版 本报告介绍了中国装配式装修行业市场行业相关概述、中国装配式装修行业市场行业运行环境、分析了中国装配式…

使用 acl 库编写发送邮件的客户端程序

2019独角兽企业重金招聘Python工程师标准>>> 邮件做为最早和最广的互联应网用之一&#xff0c;已经与人们的生活息息相关。我们虽然经常使用 Outlook Express/Outlook/Foxmail 等邮件客户端发送邮件&#xff0c;但并不关心发送过程的细节。如果您是一名程序员&#…

Unreal Engine+Houdini创造程序性游戏场景视频教程

Unreal EngineHoudini创造程序性游戏场景视频教程 大小解压后&#xff1a;27.4G 持续时间14小时30分 包括项目文件 1920X1080 高清视频 程序游戏环境——虚幻引擎和Houdini 信息: 云桥网络 平台获取教程 导言: 欢迎来到虚幻引擎4和Houdini的程序游戏环境课程&#xff0…

清理内存clear

清理内存clear&#xff1a; package com.android.cleanprocesstool;import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import a…

linux mv 环境变量,linux环境变量,cp,mv命令,more,less,cat,tail,head,的使用...

linux环境变量&#xff0c;cp&#xff0c;mv命令&#xff0c;more&#xff0c;less&#xff0c;cat&#xff0c;tail&#xff0c;head&#xff0c;的使用[email protected] ~]# cp /usr/bin/ls /tmp/[[email protected] ~]# PATH$PATH:/tmp/ path的使用/usr/local/sbin…

进入Docker容器命令

进入Docker容器命令 docker执行命令: docker exec -it [容器ID或者容器名称] /bin/bash 如果出现下述问题&#xff1a; OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec: \"/bin/bash\": stat /b…

dmalloc 原文 翻译整理

http://blog.csdn.net/cardinal_508/article/details/5553387 L13 从快速入门开始&#xff08;Quickstart&#xff09; 这个库是一个文件中所有简化用法中最常见的&#xff1a;FTP下载它&#xff0c;编译它&#xff08;-03&#xff09;&#xff0c;并连接到其他程序。 全部编译…

视频分辨率无损放大软件 Topaz Video Enhance AI 2.3.0

视频分辨率无损放大软件 Topaz Video Enhance AI 2.3.0 Topaz Video Enhance AI是一款非常好用的视频分辨率放大软件&#xff0c;用户可以通过这款软件将视频的分辨率进行自定义调节&#xff0c;最高能够将其放大至8K分辨率&#xff0c;并提供真实的细节和动作一致性&#xff…

linux 6.8 dns,CentOS6.8下安装DNS服务器

CentOS6.8下安装DNS服务器1、安装DNS服务器组件安装bind# yum install bind bind-libs bind-utils bind-chroot2、修改主配置文件/etc/named.conf需要修改的如下(带红色标注)&#xff1a;# vi /etc/named.confoptions {listen-on port 53{ any; };//listen-on-v6 port 53 { ::1…

delphi 10 seattle 中 解决IOS 9 限制使用HTTP 服务问题

IOS 9 于17号早上正式开始推送&#xff0c;早上起来立马安装&#xff0c;这次升级包只有1G&#xff0c; 安装空间也大大降低&#xff08;想起IOS 8 升级时&#xff0c;几乎把手机里面的东西删光了&#xff0c;满眼都是泪&#xff09;。 虽然安装后&#xff0c;网上几乎是铺天盖…

2022-2028年中国装备制造产业深度分析及发展规划咨询建议报告(全卷)

【报告类型】产业研究 【报告价格】4500起 【出版时间】即时更新&#xff08;交付时间约3个工作日&#xff09; 【发布机构】智研瞻产业研究院 【报告格式】PDF版 本报告介绍了中国装备制造行业市场行业相关概述、中国装备制造行业市场行业运行环境、分析了中国装备制造行…

安装flex4 plug-in插件的时候遇到老是在起始处安装不起

2019独角兽企业重金招聘Python工程师标准>>> 安装flex4 plug-in插件的时候遇到老是在起始处安装不起 解决方案&#xff1a;1、有可能你安装的目录有中文字符 2、你放安装文件的目录有中文字符 3、关闭360安全卫士和防火墙 转载于:https://my.oschina.net/u/1159401/…

Python教学课程分享10-异常处理结构

10.1 异常概念与常见表现形式 异常是一个事件&#xff0c;这个事件会在程序执行过程中发生&#xff0c;影响程序的正常进行。一般情况下&#xff0c;在Python无法正常进行程序时就会发生异常。异常是Python的对象&#xff0c;它表示一个错误&#xff0c;在Python脚本在发生异常…

24组8K真实路面材质贴图素材 VizPeople – Pavement Textures V1

24组8K真实路面材质贴图素材 VizPeople – Pavement Textures V1 24组8K真实路面材质贴图素材 VizPeople – Pavement Textures V1 大小解压后&#xff1a;5.98G 我们的第一个纹理收藏&#xff01;24个漂亮的无缝着色器&#xff0c;专为图形设计师和建筑可视化设计。现代和经…

linux查找项目中的问题,教你如何快速定位项目中慢查询[项目管理]

1. 使用对象&#xff1a; 项目经理或者项目管理者2. 数据库&#xff1a; mysql3. 快速定位慢查询&#xff1a;启动mysql时&#xff0c;启动慢查询日志&#xff1a;3.1 Window系统&#xff1a;第一种&#xff1a;bin\mysqlId.exe --safe-mode --slow-query-log (可在my.ini中配…

TCP的三次握手和四次分手

参考链接&#xff1a; http://blog.csdn.net/whuslei/article/details/6667471转载于:https://www.cnblogs.com/HuoAA/p/4826380.html

IDEA的Docker插件实战(Dockerfile篇)

IDEA的Docker插件实战(Dockerfile篇) IntelliJ IDEA的Docker插件能帮助我们将当前工程制作成Docker镜像、运行在指定的远程机器上&#xff0c;是学习和开发阶段的好帮手&#xff0c;本文一起来实战此插件的基本用法&#xff1b; 关于系列文章 本文是《IDEA的Docker插件实战》系…

技术变成客户才值钱

什么事与钱关联都显得有些俗&#xff0c;但没有钱又觉得这个世界这样的苦逼。作为一个技术人员&#xff0c;绝大多数人都在“苦逼”的生活中仰望“土豪”的生活&#xff0c;而唯一能够让我们达到这一目标的唯一途径就是将技术变成客户。技术不值钱似乎成了一个不争的实事&#…

浏览器会缓存js文件

项目中修改了一个js文件&#xff0c;然后重新发布到测试环境服务器&#xff0c;发现没有生效&#xff0c;页面依然报参数校验失败&#xff0c;经排查&#xff0c;发现浏览器中使用的还是旧的js文件&#xff0c;Chrome浏览器对js文件有缓存&#xff0c;只需要Ctrl shift del清…

视频色彩校正简介 Introduction to Video Color Correction

视频色彩校正简介 Introduction to Video Color Correction 视频色彩校正简介 Introduction to Video Color Correction MP4 |视频:h264&#xff0c;1280x720 |音频:AAC&#xff0c;44.1 KHz&#xff0c;2 Ch 云桥网络 平台获取教程&#xff01; 技能水平:初级中级|语言&…

linux下移动c盘文件位置,问个问题我在unbuntu下为何找不到windows c盘文件

问个问题我在unbuntu下为何找不到windows c盘文件发布时间:2008-08-08 08:07:13来源:红联作者:fzmhlxk这是不是和重ghost安装过xp有关啊是不是引导文件的问题我查看了 应到文件title Ubuntu 7.10, kernel 2.6.22-15-genericroot (hd0,6)kernel /boot/vmlinuz-2.6.22-15-generic…

一个妹子图应用客户端源码

源码GankMeizhi&#xff0c;也是一个干妹纸应用的客户端&#xff0c;目前该应用还没有上传到应用商店中&#xff0c;大家可以自行修改一下吧&#xff0c;没错。又是一个妹子图app&#xff0c;依然采集自干货集中营。 源码下载&#xff1a; http://code.662p.com/view/11060.ht…