programming-examples/php/Algo/Base4.php

71 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2019-11-15 12:59:38 +01:00
<?php
namespace Graphp\Algorithms\TravelingSalesmanProblem;
use Fhaculty\Graph\Walk;
use Fhaculty\Graph\Vertex;
use Fhaculty\Graph\Edge\Base as Edge;
use Fhaculty\Graph\Set\Edges;
use Graphp\Algorithms\Base as AlgorithmBase;
abstract class Base extends AlgorithmBase
{
/**
* get resulting graph with the (first) best circle of edges connecting all vertices
*
* @throws Exception on error
* @return Graph
* @uses Tsp::getGraph()
* @uses AlgorithmTsp::getEdges()
* @uses Graph::createGraphCloneEdges()
*/
public function createGraph()
{
return $this->getGraph()->createGraphCloneEdges($this->getEdges());
}
/**
* get graph this algorithm operates on
*
* @return Graph
*/
abstract protected function getGraph();
/**
* get start vertex this algorithm starts on
*
* @return Vertex
*/
abstract protected function getVertexStart();
/**
* get (first) best circle connecting all vertices
*
* @return Walk
* @uses AlgorithmTsp::getEdges()
* @uses AlgorithmTsp::getVertexStart()
* @uses Walk::factoryCycleFromEdges()
*/
public function getCycle()
{
return Walk::factoryCycleFromEdges($this->getEdges(), $this->getVertexStart());
}
public function getWeight()
{
$weight = 0;
foreach ($this->getEdges() as $edge) {
$weight += $edge->getWeight();
}
return $weight;
}
/**
* get array of edges connecting all vertices in a circle
*
* @return Edges
*/
abstract public function getEdges();
}