Home php教程 php手册 关键路径:php实现图的邻接表,关键路径,拓朴排序

关键路径:php实现图的邻接表,关键路径,拓朴排序

Jun 21, 2016 am 08:51 AM
function gt private this


//调用
require 'algraph.php';
$a = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
$e = array('ab'=>'3', 'ac'=>'4', 'be'=>'6', 'bd'=>'5', 'cd'=>'8', 'cf'=>'7', 'de'=>'3', 'eg'=>'9', 'eh'=>'4', 'fh'=>'6', 'gj'=>'2', 'hi'=>'5', 'ij'=>'3');
$test = new algraph($a, $e, 1, 1);
print_r($test->criticalpath());
?>
algraph.php
/**
* php实现图的邻接表
*
* @author zhaojiangwei
* @since 2011/11/1 16:00
*/
//顶点类
class vex{
private $data;
private $headlink;//第一条边
private $enterlimit = 0;//顶点的入度
public function vex($data, $headlink = null){
$this->data = $data;
$this->headlink = $headlink;
}
//入度加+n
public function enterlimitadd($n = 1){
$this->enterlimit += $n;
}
public function getenterlimit(){
return $this->enterlimit;
}
public function getdata(){
return $this->data;
}
public function getheadlink(){
return $this->headlink;
}
public function setheadlink(& $link){
$this->headlink = $link;
}
}
//边类
class arc{
private $key;//该边顶点对应在顶点数组的下标
private $weight;//路径长度
private $next;//下一条边
public function arc($key, $weight = null, $next = null){
$this->key= $key;
$this->next = $next;
$this->weight= $weight;
}
public function getweight(){
return $this->weight;
}
public function getkey(){
return $this->key;
}
public function getnext(){
return $this->next;
}
public function setnext($next){
$this->next = $next;
}
}
//邻接表类
class algraph{
private $vexsdata;//外部输入的顶点数据,类似如array('a', 'b');
private $vexs;//顶点数组
private $arcdata;//外部输入的边数据,如array('ab'=>'3'),键为边,值为权值
private $excutedfsresult;//深度优先遍历后的字符串结果
private $haslist; //遍历时储存遍历过的结点下标
private $queue; //广度优先遍历时的存储队列
private $direct; //是否是有向图,0为无向,1为有向
private $weight; //是否带权,0不带,1带
//$direct:是否是有向图,0无向,1有向
//$weight:是否带权,0不带,1带
public function algraph($vexsdata, $arcdata, $direct = 0, $weight = 0){
$this->vexsdata = $vexsdata;
$this->arcdata = $arcdata;
$this->direct = $direct;
$this->weight = $weight;
$this->createheadlist();
$this->createarc();
}
//创建顶点数组
private function createheadlist(){
foreach($this->vexsdata as $value){
$this->vexs[] = new vex($value);
}
}
//创建边表
private function createarc(){
switch($this->weight){
case '0'://不带权
$this->createnoweightarc();
break;
case '1'://带权
$this->createweightarc();
break;
}
}
//创建带权表
private function createweightarc(){
foreach($this->arcdata as $key=>$value){
$edgenode = str_split($key);
$this->createconnect($edgenode[0], $edgenode[1], $value);
if(!$this->direct){//有向图
$this->createconnect($edgenode[1], $edgenode[0], $value);
}
}
}
//创建不带权表
private function createnoweightarc(){
foreach($this->arcdata as $value){
$str = str_split($value);
$this->createconnect($str[0], $str[1]);
if(!$this->direct){
$this->createconnect($str[1], $str[0]);
}
}
}
//依附于边的俩顶点建立关系
//$weight: 权值,默认为无权值
private function createconnect($first, $last, $weight = null){
$lasttemp=& $this->vexs[$this->getvexbyvalue($last)];
$lasttemp->enterlimitadd(1);//入度+1
$firstnode =& $this->vexs[$this->getvexbyvalue($first)];
$lastnode = new arc($this->getvexbyvalue($last), $weight);
$lastnode->setnext($firstnode->getheadlink());
$firstnode->setheadlink(& $lastnode); 本文链接http://www.cxybl.com/html/wlbc/Php/20120607/28508.html



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What does private mean in java What does private mean in java Nov 24, 2022 pm 06:27 PM

In Java, private means "private" and is an access control modifier used to modify classes, properties and methods. Class members modified with private can only be accessed and modified by the methods of the class itself, and cannot be accessed and referenced by any other class (including subclasses of the class); therefore, the private modifier has the highest level of protection.

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

See all articles