最小生成树

  1. use std::vec::Vec;
  2. #[derive(Debug)]
  3. pub struct Edge {
  4. source: i64,
  5. destination: i64,
  6. cost: i64,
  7. }
  8. impl PartialEq for Edge {
  9. fn eq(&self, other: &Self) -> bool {
  10. self.source == other.source
  11. && self.destination == other.destination
  12. && self.cost == other.cost
  13. }
  14. }
  15. impl Eq for Edge {}
  16. impl Edge {
  17. fn new(source: i64, destination: i64, cost: i64) -> Self {
  18. Self {
  19. source,
  20. destination,
  21. cost,
  22. }
  23. }
  24. }
  25. fn make_sets(number_of_vertices: i64) -> Vec<i64> {
  26. let mut parent: Vec<i64> = Vec::with_capacity(number_of_vertices as usize);
  27. for i in 0..number_of_vertices {
  28. parent.push(i);
  29. }
  30. parent
  31. }
  32. fn find(parent: &mut Vec<i64>, x: i64) -> i64 {
  33. let idx: usize = x as usize;
  34. if parent[idx] != x {
  35. parent[idx] = find(parent, parent[idx]);
  36. }
  37. parent[idx]
  38. }
  39. fn merge(parent: &mut Vec<i64>, x: i64, y: i64) {
  40. let idx_x: usize = find(parent, x) as usize;
  41. let parent_y: i64 = find(parent, y);
  42. parent[idx_x] = parent_y;
  43. }
  44. fn is_same_set(parent: &mut Vec<i64>, x: i64, y: i64) -> bool {
  45. find(parent, x) == find(parent, y)
  46. }
  47. pub fn kruskal(mut edges: Vec<Edge>, number_of_vertices: i64) -> (i64, Vec<Edge>) {
  48. let mut parent: Vec<i64> = make_sets(number_of_vertices);
  49. edges.sort_unstable_by(|a, b| a.cost.cmp(&b.cost));
  50. let mut total_cost: i64 = 0;
  51. let mut final_edges: Vec<Edge> = Vec::new();
  52. let mut merge_count: i64 = 0;
  53. for edge in edges.iter() {
  54. if merge_count >= number_of_vertices - 1 {
  55. break;
  56. }
  57. let source: i64 = edge.source;
  58. let destination: i64 = edge.destination;
  59. if !is_same_set(&mut parent, source, destination) {
  60. merge(&mut parent, source, destination);
  61. merge_count += 1;
  62. let cost: i64 = edge.cost;
  63. total_cost += cost;
  64. let final_edge: Edge = Edge::new(source, destination, cost);
  65. final_edges.push(final_edge);
  66. }
  67. }
  68. (total_cost, final_edges)
  69. }
  70. #[cfg(test)]
  71. mod tests {
  72. use super::*;
  73. #[test]
  74. fn test_seven_vertices_eleven_edges() {
  75. let mut edges: Vec<Edge> = Vec::new();
  76. edges.push(Edge::new(0, 1, 7));
  77. edges.push(Edge::new(0, 3, 5));
  78. edges.push(Edge::new(1, 2, 8));
  79. edges.push(Edge::new(1, 3, 9));
  80. edges.push(Edge::new(1, 4, 7));
  81. edges.push(Edge::new(2, 4, 5));
  82. edges.push(Edge::new(3, 4, 15));
  83. edges.push(Edge::new(3, 5, 6));
  84. edges.push(Edge::new(4, 5, 8));
  85. edges.push(Edge::new(4, 6, 9));
  86. edges.push(Edge::new(5, 6, 11));
  87. let number_of_vertices: i64 = 7;
  88. let expected_total_cost = 39;
  89. let mut expected_used_edges: Vec<Edge> = Vec::new();
  90. expected_used_edges.push(Edge::new(0, 3, 5));
  91. expected_used_edges.push(Edge::new(2, 4, 5));
  92. expected_used_edges.push(Edge::new(3, 5, 6));
  93. expected_used_edges.push(Edge::new(0, 1, 7));
  94. expected_used_edges.push(Edge::new(1, 4, 7));
  95. expected_used_edges.push(Edge::new(4, 6, 9));
  96. let (actual_total_cost, actual_final_edges) = kruskal(edges, number_of_vertices);
  97. assert_eq!(actual_total_cost, expected_total_cost);
  98. assert_eq!(actual_final_edges, expected_used_edges);
  99. }
  100. #[test]
  101. fn test_ten_vertices_twenty_edges() {
  102. let mut edges: Vec<Edge> = Vec::new();
  103. edges.push(Edge::new(0, 1, 3));
  104. edges.push(Edge::new(0, 3, 6));
  105. edges.push(Edge::new(0, 4, 9));
  106. edges.push(Edge::new(1, 2, 2));
  107. edges.push(Edge::new(1, 3, 4));
  108. edges.push(Edge::new(1, 4, 9));
  109. edges.push(Edge::new(2, 3, 2));
  110. edges.push(Edge::new(2, 5, 8));
  111. edges.push(Edge::new(2, 6, 9));
  112. edges.push(Edge::new(3, 6, 9));
  113. edges.push(Edge::new(4, 5, 8));
  114. edges.push(Edge::new(4, 9, 18));
  115. edges.push(Edge::new(5, 6, 7));
  116. edges.push(Edge::new(5, 8, 9));
  117. edges.push(Edge::new(5, 9, 10));
  118. edges.push(Edge::new(6, 7, 4));
  119. edges.push(Edge::new(6, 8, 5));
  120. edges.push(Edge::new(7, 8, 1));
  121. edges.push(Edge::new(7, 9, 4));
  122. edges.push(Edge::new(8, 9, 3));
  123. let number_of_vertices: i64 = 10;
  124. let expected_total_cost = 38;
  125. let mut expected_used_edges = Vec::new();
  126. expected_used_edges.push(Edge::new(7, 8, 1));
  127. expected_used_edges.push(Edge::new(1, 2, 2));
  128. expected_used_edges.push(Edge::new(2, 3, 2));
  129. expected_used_edges.push(Edge::new(0, 1, 3));
  130. expected_used_edges.push(Edge::new(8, 9, 3));
  131. expected_used_edges.push(Edge::new(6, 7, 4));
  132. expected_used_edges.push(Edge::new(5, 6, 7));
  133. expected_used_edges.push(Edge::new(2, 5, 8));
  134. expected_used_edges.push(Edge::new(4, 5, 8));
  135. let (actual_total_cost, actual_final_edges) = kruskal(edges, number_of_vertices);
  136. assert_eq!(actual_total_cost, expected_total_cost);
  137. assert_eq!(actual_final_edges, expected_used_edges);
  138. }
  139. }