In the context of pathfinding in PHP, what role does the "vertex" play and how can it be utilized?
In pathfinding in PHP, a "vertex" represents a point or node in a graph or network. It plays a crucial role in determining the connections between different points and calculating the shortest path. To utilize vertices in pathfinding, you can create a graph data structure with vertices representing points of interest and edges representing connections between them. By implementing algorithms like Dijkstra's or A* on this graph, you can find the shortest path between two vertices.
class Vertex {
public $name;
public $neighbors;
public function __construct($name) {
$this->name = $name;
$this->neighbors = [];
}
public function addNeighbor($neighbor, $weight) {
$this->neighbors[$neighbor] = $weight;
}
}
// Example usage
$vertexA = new Vertex('A');
$vertexB = new Vertex('B');
$vertexC = new Vertex('C');
$vertexA->addNeighbor($vertexB, 5);
$vertexA->addNeighbor($vertexC, 10);
// Implement pathfinding algorithms on this graph