-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraph.php
73 lines (59 loc) · 1.67 KB
/
Graph.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* Graph Data Structure
* Assuming each node is connected to at least one other node.
* Can be direcred/undirecred and/or weighted/unweighted.
* Using adjacency list.
*/
class Graph
{
public $undirecred;
public $weighted;
public $adjacencyList = [];
public $nodeCount = 0;
public $nodes = [];
public $nodeAsKeys = [];
public $visited = [];
// default graph is direcred & unweighted
public function __construct($undirecred = false, $weighted = false)
{
$this->undirecred = $undirecred;
$this->weighted = $weighted;
}
// when we have weighted graph the weight will be 1 for all edges
public function addEdge($from, $to, $weight = 1)
{
$weight = ($this->weighted) ? $weight : 1;
$this->adjacencyList[ $from ][ $to ] = $weight;
if ($this->undirecred)
$this->adjacencyList[ $to ][ $from ] = $weight;
$this->addNode($from);
$this->addNode($to);
}
public function addNode($node)
{
if (! isset($this->nodeAsKeys[ $node ])) {
$this->nodeAsKeys[ $node ] = true;
$this->nodes[] = $node;
$this->nodeCount ++;
$this->visited[$node] = false;
}
}
}
// ----------------
// --- Examples ---
// ----------------
$graph = new Graph(); // direcred & unweighted
$graph->addEdge(1, 2);
$graph->addEdge(2, 3);
$graph->addEdge(2, 4);
$graph->addEdge(3, 4);
$graph->addEdge(4, 1);
var_dump($graph);
$graph = new Graph(true, true); // undirecred & weighted
$graph->addEdge(1, 2, 6);
$graph->addEdge(2, 3, 15);
$graph->addEdge(2, 4, 8);
$graph->addEdge(3, 4, 3);
$graph->addEdge(4, 1, 9);
var_dump($graph);