-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 20 convert edge type into a tuple struct (#23) * Changed Edge (tuple type) to Edge (tuple struct) and made related changes * Added Edge changes to sync_digraph, ungraph and sync_ungraph * Changed Path<K, N, E> types iterator to use Edge<K, N, E> instead of (u, v, e) * Corrected kojarasu examples to be stable for assertion tests * Readme updated to reflect changes in pr * Correct incorrect catgory slug in Cargo.toml https://crates.io/category_slugs * 22 add minimum spanning tree to examples (#24) * #22 Add prim's algorithm example * #22 Fix example to work with Edge() tuple struct * #22 Fix exec method in ungraph to work with Edge * Add documentation to prim example, refactor * Improve package description * More descriptive names for examples * Make ungraph methods FnMut, simplify prim's example, add for_each to ungraph * Change map to for_each and remove filter_map from methods * Improve documentation * Corret memory leak. Adjacency lists need to store WeakNode references to avoid circular reference * Improve build.yml to check for leaks on examples with valgrind * Changed branch in build.yml for testing * Remove size test for now * Correct size tests * Restructure node module in ungraph and digraph, chaneg ungraph to use std's BinaryHeap * Copy new implementation to sync versions * Correct documentation * If node that an edge is pointing to has been dropped, panic * Change build.yml to point to master branch Co-authored-by: Satu Koskinen <[email protected]>
- Loading branch information
1 parent
f43f31d
commit ed6d0ec
Showing
63 changed files
with
2,519 additions
and
2,255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Prim's algorithm | ||
// | ||
// Prim's algorithm (also known as Jarník's algorithm) is a greedy algorithm | ||
// that finds a minimum spanning tree for a weighted undirected graph. This | ||
// means it finds a subset of the edges that forms a tree that includes every | ||
// vertex, where the total weight of all the edges in the tree is minimized. | ||
// The algorithm operates by building this tree one vertex at a time, | ||
// from an arbitrary starting vertex, at each step adding the cheapest possible | ||
// connection from the tree to another vertex. | ||
// | ||
// https://en.wikipedia.org/wiki/Prim%27s_algorithm | ||
|
||
use gdsl::ungraph::*; | ||
use gdsl::*; | ||
use std::collections::{BinaryHeap, HashSet}; | ||
use std::cmp::Reverse; | ||
|
||
type N = Node<usize, (), u64>; | ||
type E = Edge<usize, (), u64>; | ||
|
||
// Standard library's BinaryHeap is a max-heap, so we need to reverse the | ||
// ordering of the edge weights to get a min-heap using the Reverse wrapper. | ||
type Heap = BinaryHeap<Reverse<E>>; | ||
|
||
fn prim_minimum_spanning_tree(s: &N) -> Vec<E> { | ||
|
||
// We collect the resulting MST edges in to a vector. | ||
let mut mst: Vec<E> = vec![]; | ||
|
||
// We use a HashSet to keep track of the nodes that are in the MST. | ||
let mut in_mst: HashSet<usize> = HashSet::new(); | ||
|
||
// We use a BinaryHeap to keep track of all edges sorted by weight. | ||
let mut heap = Heap::new(); | ||
|
||
in_mst.insert(*s.key()); | ||
|
||
// Collect all edges reachable from `s` to a Min Heap. | ||
s.bfs().for_each(&mut |edge| { | ||
heap.push(Reverse(edge.clone())); | ||
}).search(); | ||
|
||
// When we pop from the min heap, we know that the edge is the cheapest | ||
// edge to add to the MST, but we need to make sure that the edge | ||
// connecting to a node that is not already in the MST, otherwise we | ||
// we store the edge and continue to the next iteration. When we find | ||
// an edge that connects to a node that is not in the MST, we add the | ||
// stored edges back to the heap. | ||
let mut tmp: Vec<E> = vec![]; | ||
|
||
// While the heap is not empty, search for the next edge | ||
// that connects a node in the tree to a node not in the tree. | ||
while let Some(Reverse(edge)) = heap.pop() { | ||
let Edge(u, v, _) = edge.clone(); | ||
|
||
// If the edge's source node `u` is in the MST... | ||
if in_mst.contains(u.key()) { | ||
|
||
// ...and the edge's destination node `v` is not in the MST, | ||
// then we add the edge to the MST and add all edges | ||
// in `tmp` back to the heap. | ||
if in_mst.contains(v.key()) == false { | ||
in_mst.insert(*v.key()); | ||
mst.push(edge.clone()); | ||
for tmp_edge in &tmp { | ||
heap.push(Reverse(tmp_edge.clone())); | ||
} | ||
} | ||
} else { | ||
|
||
// The edge is the cheapest edge to add to the MST, but | ||
// it's source node `u` nor it's destination node `v` are | ||
// in the MST, so we store the edge and continue to the next | ||
// iteration. | ||
if in_mst.contains(v.key()) == false { | ||
tmp.push(edge); | ||
} | ||
} | ||
|
||
// If neither condition is met, then the edge's destination node | ||
// `v` is already in the MST, so we continue to the next iteration. | ||
} | ||
mst | ||
} | ||
|
||
fn main() { | ||
// Example g1 from Wikipedia | ||
let g1 = ungraph![ | ||
(usize) => [u64] | ||
(0) => [ (1, 1), (3, 4), (4, 3)] | ||
(1) => [ (3, 4), (4, 2)] | ||
(2) => [ (4, 4), (5, 5)] | ||
(3) => [ (4, 4)] | ||
(4) => [ (5, 7)] | ||
(5) => [] | ||
]; | ||
let forest = prim_minimum_spanning_tree(&g1[0]); | ||
let sum = forest.iter().fold(0, |acc, e| acc + e.2); | ||
assert!(sum == 16); | ||
|
||
// Example g2 from Figure 7.1 in https://jeffe.cs.illinois.edu/teaching/algorithms/book/07-mst.pdf | ||
let g2 = ungraph![ | ||
(usize) => [u64] | ||
(0) => [ (1, 8), (2, 5)] | ||
(1) => [ (2, 10), (3, 2), (4, 18)] | ||
(2) => [ (3, 3), (5, 16)] | ||
(3) => [ (4, 12), (5, 30)] | ||
(4) => [ (6, 4)] | ||
(5) => [ (6, 26)] | ||
(6) => [] | ||
]; | ||
let forest = prim_minimum_spanning_tree(&g2[0]); | ||
let sum = forest.iter().fold(0, |acc, e| acc + e.2); | ||
assert!(sum == 42); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.