-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.php
More file actions
68 lines (53 loc) · 1.96 KB
/
bfs.php
File metadata and controls
68 lines (53 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php declare(strict_types=1);
use App\BreadthFirstSearch\Graph;
use App\BreadthFirstSearch\BreadthFirstSearch;
use App\BreadthFirstSearch\GraphFactory;
require_once __DIR__ . '/vendor/autoload.php';
ini_set('display_errors', '1');
error_reporting(E_ALL);
$bfsObject = new BreadthFirstSearch(new GraphFactory());
$graph = $bfsObject->getGraph();
if (isset($_POST['start'], $_POST['end'])) {
bfs($graph, $_POST['start'], $_POST['end']);
}
function bfs(Graph $graph, string $start, string $end): void
{
$result = $graph->search($start, $end);
if (null !== $result->getEndNode()) {
echo 'It found ' . $result->getEndNode()->getValue() . '-';
}
$path = ' Search path: ';
$iMax = count($result->getPath());
foreach ($result->getPath() as $i => $n) {
$path .= $n->getValue();
if ($i < $iMax - 1) {
$path .= ' --> ';
}
}
echo $path . '<br />';
}
?>
<h2>Breadth first search:</h2>
<form id="s" method="post">
<select name="start">
<?php if (isset($_POST['start'])): ?>
<option value="<?php echo $_POST['start']; ?>"><?php echo $_POST['start']; ?></option>
<?php else: ?>
<option value="">Select start value</option>
<?php endif; ?>
<?php foreach ($graph->getNodes() as $node): ?>
<option value="<?php echo $node->getValue(); ?>"><?php echo $node->getValue(); ?></option>
<?php endforeach; ?>
</select>
<select name="end">
<?php if (isset($_POST['end'])): ?>
<option value="<?php echo $_POST['end']; ?>"><?php echo $_POST['end']; ?></option>
<?php else: ?>
<option value="">Select end value</option>
<?php endif; ?>
<?php foreach ($graph->getNodes() as $node): ?>
<option value="<?php echo $node->getValue(); ?>"><?php echo $node->getValue(); ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="Submit" value="Search">
</form>