-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.php
45 lines (28 loc) · 966 Bytes
/
algorithms.php
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
<?php
/*
Common graph algorithms and mathematical functions implemented using dbgraph-php
*/
require 'DBGraph/DBGraph.php';
use DBGraph\Graph;
$graph = new Graph();
$graph->setStorageBackend("session");
// verify Handshake Theorem: sum of degrees of vertices is twice the number of edges
$a1 = $graph->addVertex(["name" => "V1"]);
$a2 = $graph->addVertex(["name" => "V2"]);
$a3 = $graph->addVertex(["name" => "V3"]);
$a4 = $graph->addVertex(["name" => "V4"]);
$a5 = $graph->addVertex(["name" => "V5"]);
$graph->addEdge($a1,$a2);
$graph->addEdge($a2,$a3);
$graph->addEdge($a3,$a3);
$graph->addEdge($a3,$a4);
$graph->addEdge($a4,$a5);
$graph->addEdge($a5,$a1);
echo $graph->printEdges();
$vertex_degreesum = 0;
foreach ($graph->vertices as $vertex) {
$vertex_degreesum += intval($graph->vertexDegree($vertex));
}
echo "Handshake Theorem verification:";
echo "\nVertex Degree Sum:".$vertex_degreesum;
echo "\nEdge Count:".count($graph->edges)."\n";