-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efd3128
commit b503ed2
Showing
8 changed files
with
173 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod cycles; | ||
pub mod minimum_spanning_tree; |
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 @@ | ||
pub mod kruskal_naive; |
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,114 @@ | ||
use crate::{ | ||
algorithms::cycles::is_acyclic::is_acyclic, | ||
graph::{ | ||
graph::{Edge, Graph, Path}, | ||
undirected_graph::UndirectedGraph, | ||
}, | ||
}; | ||
|
||
pub fn run(g: &dyn Graph) -> Path { | ||
kruskal_naive(g) | ||
} | ||
|
||
fn kruskal_naive(g: &dyn Graph) -> Path { | ||
let mut tmp = UndirectedGraph::new(); | ||
let mut edges: Vec<&Edge> = g.get_edges().iter().collect(); | ||
edges.sort_by_key(|e| e.2); | ||
|
||
let mut mst = vec![]; | ||
for e in edges { | ||
tmp.add_edge(e.0, e.1, e.2); | ||
|
||
if is_acyclic(&tmp) { | ||
mst.push(e.clone()); | ||
} else { | ||
tmp.delete_edge(&e.0, &e.1); | ||
} | ||
} | ||
|
||
return mst; | ||
} | ||
|
||
mod tests { | ||
use crate::{ | ||
algorithms::minimum_spanning_tree::kruskal_naive::kruskal_naive, | ||
graph::{graph::Graph, undirected_graph::UndirectedGraph}, | ||
}; | ||
|
||
#[test] | ||
fn kruskal_naive_simple_graph_success() { | ||
let mut g = UndirectedGraph::new(); | ||
|
||
g.add_edge(1, 2, 4993); | ||
g.add_edge(2, 3, 1392); | ||
g.add_edge(3, 4, 8856); | ||
g.add_edge(4, 5, -433); | ||
g.add_edge(5, 6, 6590); | ||
g.add_edge(6, 7, -7462); | ||
g.add_edge(7, 8, 6658); | ||
g.add_edge(8, 9, -976); | ||
g.add_edge(9, 10, 9698); | ||
|
||
let expected = vec![ | ||
(6, 7, -7462), | ||
(8, 9, -976), | ||
(4, 5, -433), | ||
(2, 3, 1392), | ||
(1, 2, 4993), | ||
(5, 6, 6590), | ||
(7, 8, 6658), | ||
(3, 4, 8856), | ||
(9, 10, 9698), | ||
]; | ||
let current = kruskal_naive(&g); | ||
assert_eq!(expected, current); | ||
} | ||
|
||
#[test] | ||
fn kruskal_naive_graph_with_cycle_success() { | ||
let mut g = UndirectedGraph::new(); | ||
|
||
g.add_edge(1, 2, 4993); | ||
g.add_edge(1, 5, 2432); | ||
g.add_edge(2, 3, 1392); | ||
g.add_edge(2, 4, 4687); | ||
g.add_edge(2, 6, -34); | ||
g.add_edge(3, 4, 8856); | ||
g.add_edge(3, 7, 844); | ||
g.add_edge(3, 8, -433); | ||
g.add_edge(5, 9, -432); | ||
g.add_edge(6, 9, -7462); | ||
g.add_edge(6, 7, 442); | ||
g.add_edge(8, 10, -976); | ||
|
||
let expected = vec![ | ||
(6, 9, -7462), | ||
(8, 10, -976), | ||
(3, 8, -433), | ||
(5, 9, -432), | ||
(2, 6, -34), | ||
(6, 7, 442), | ||
(3, 7, 844), | ||
(1, 5, 2432), | ||
(2, 4, 4687), | ||
]; | ||
let current = kruskal_naive(&g); | ||
assert_eq!(expected, current); | ||
} | ||
|
||
#[test] | ||
fn kruskal_naive_full_connected_graph_success() { | ||
let mut g = UndirectedGraph::new(); | ||
|
||
g.add_edge(1, 2, -544); | ||
g.add_edge(1, 3, 455); | ||
g.add_edge(1, 4, -12); | ||
g.add_edge(2, 3, 84); | ||
g.add_edge(2, 4, 27); | ||
g.add_edge(3, 4, -7); | ||
|
||
let expected = vec![(1, 2, -544), (1, 4, -12), (3, 4, -7)]; | ||
let current = kruskal_naive(&g); | ||
assert_eq!(expected, current); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub mod directed_graph; | ||
pub mod graph; | ||
pub mod undirected_graph; |
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