单源最短路径(dijkstra算法)php实现 2.更新与i直接相邻顶点的dist值。(dist[j]=min{dist[j],dist[i]+cost[i][j]}) 3.知道U=V,停止。 利用php特有的性质,其代码如下:
做一个医学项目,其中在病例评分时会用到单源最短路径的算法。单源最短路径的dijkstra算法的思路如下:
如果存在一条从i到j的最短路径(Vi.....Vk,Vj),Vk是Vj前面的一顶点。那么(Vi...Vk)也必定是从i到k的最短路径。Dijkstra是以最短路径长度递增,逐次生成最短路径的算法。例如:对于源顶点V0,首先选择其直接相邻的顶点中长度最短的顶点Vi,那么当前已知可得从V0到达Vj顶点的最短距离dist[j]=min{dist[j],dist[i]+cost[i][j]}。假设G=
1.从V-U中选择使dist[i]值最小的顶点i,将i加入到U中;function dijkstra(){ $node_info_arr=array( //结点的邻接表结构 array( 'node_id'=>0, //某个结点的id 'next_node'=>array(4,2,1), 'node_type'=>0, 'cost'=>array(10,30,100) ), array( 'node_id'=>4, //某个结点的id 'next_node'=>array(3), 'node_type'=>1, 'cost'=>array(50) ), array( 'node_id'=>3, //某个结点的id 'next_node'=>array(1), 'node_type'=>1, 'cost'=>array(10) ), array( 'node_id'=>2, //某个结点的id 'next_node'=>array(3,1), 'node_type'=>1, 'cost'=>array(60,60) ), array( 'node_id'=>1, //某个结点的id 'next_node'=>array(), 'node_type'=>2, 'cost'=>array() ) ); $start_node_id=false; //起始结点id $i_cost=array(array()); //两个节点之间的开销 $i_dist=array(); //起始点到各点的最短距离 $b_mark=array(); //是否加入了 foreach($node_info_arr as &$node_info){ if($node_info['node_type']==0){ $start_node_id=$node_info['node_id']; //找到初始节点 } foreach($node_info['next_node'] as $key=>$next_node){ $i_cost[$node_info['node_id']][$next_node]=$node_info['cost'][$key]; } $i_dist[$node_info['node_id']]='INF'; //初始化为无穷大 $b_mark[$node_info['node_id']]=false; //初始化未加入 } if($start_node_id===false){ return '302'; } //计算初始结点到各节点的最短路径 $i_dist[$start_node_id]=0; //初始点到其本身的距离为0 $b_mark[$start_node_id]=true; //初始点加入集合 $current_node_id=$start_node_id; //最近加入的节点id $node_count=count($node_info_arr); for($i=0;$i$val){ if($i_dist[$key]=='INF'||$i_dist[$key]>$i_dist[$current_node_id]+$val){ $i_dist[$key]=$i_dist[$current_node_id]+$val; } } } foreach($i_dist as $key=>$val){ if(!$b_mark[$key]){ if($val!='INF'&&($min=='INF'||$min>$val)){ $min=$val; $candidate_node_id=$key; //候选最近结点id } } } if($min=='INF'){ break; } $current_node_id=$candidate_node_id; $b_mark[$current_node_id]=true; } foreach($i_dist as $key=>$val){ echo $start_node_id.'=>'.$key.':'.$val.'<br>'; }}
其中例子为图:
运行结果为:
0=>0:0
0=>4:10
0=>3:60
0=>2:30
0=>1:70
转自:康瑞的部落 ? 单源最短路径php实现

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
