https://github.com/mgrechanik/kruskal
Kruskal's algorithm finds a minimum spanning tree
https://github.com/mgrechanik/kruskal
graph kruskal kruskal-mst kruskals-algorithm minimum-cost-subgraph minimum-spanning-tree mst path-planning search-algorithm
Last synced: 6 months ago
JSON representation
Kruskal's algorithm finds a minimum spanning tree
- Host: GitHub
- URL: https://github.com/mgrechanik/kruskal
- Owner: mgrechanik
- License: other
- Created: 2024-03-13T07:06:30.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-03-17T13:30:15.000Z (over 1 year ago)
- Last Synced: 2025-02-11T15:53:36.568Z (8 months ago)
- Topics: graph, kruskal, kruskal-mst, kruskals-algorithm, minimum-cost-subgraph, minimum-spanning-tree, mst, path-planning, search-algorithm
- Language: PHP
- Homepage:
- Size: 104 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Kruskal's algorithm to find a minimum spanning tree
## Demo
Example of building a minimum spanning tree:
## Installing
#### Installing through composer::
The preferred way to install this library is through composer.
Either run
```
composer require --prefer-dist mgrechanik/kruskal
```or add
```
"mgrechanik/kruskal" : "~1.0.0"
```
to the require section of your `composer.json`.## How to use
Run the next code:
```php
use mgrechanik\kruskal\Kruskal;$matrix = [
[ 0 , 263, 184, 335],
[263, 0 , 287, 157],
[184, 287, 0 , 259],
[335, 157, 259, 0]
];
$kruskal = new Kruskal($matrix);
if ($kruskal->run()) {
// 1)
var_dump($kruskal->getMinimumSpanningTree());
// 2)
var_dump($kruskal->getDistance());
}
```
We will get:1) Spanning tree as an array of edges
```
Array
(
[0] => Array
(
[0] => 0
[1] => 2
)[1] => Array
(
[0] => 2
[1] => 3
)[2] => Array
(
[0] => 1
[1] => 3
))
```2) Distance of all tree
```
600
```This code will find the next path:
