From 5a986000cd9b4e16c5b824af1b160947e4e31afc Mon Sep 17 00:00:00 2001 From: Srishti Soni <92056170+shimmer12@users.noreply.github.com> Date: Sun, 12 Oct 2025 02:36:54 +0530 Subject: [PATCH 1/3] Implement Kruskal's Minimum Spanning Tree algorithm --- graph_algorithms/kruskalMST.r | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 graph_algorithms/kruskalMST.r diff --git a/graph_algorithms/kruskalMST.r b/graph_algorithms/kruskalMST.r new file mode 100644 index 00000000..83d79d01 --- /dev/null +++ b/graph_algorithms/kruskalMST.r @@ -0,0 +1,50 @@ +# Kruskal's Minimum Spanning Tree (base R, small) +# edges: data.frame or matrix with columns u, v, w (1-based node ids) +# n: number of vertices +kruskalMST <- function(edges, n) { + edges <- as.data.frame(edges) + names(edges) <- c("u", "v", "w") + edges <- edges[order(edges$w), ] + + parent <- seq_len(n) + rank <- integer(n) + + find <- function(x) { + while (parent[x] != x) { + parent[x] <<- parent[parent[x]] # path compression + x <- parent[x] + } + x + } + unite <- function(a, b) { + ra <- find(a); rb <- find(b) + if (ra == rb) return(FALSE) + if (rank[ra] < rank[rb]) parent[ra] <<- rb + else if (rank[ra] > rank[rb]) parent[rb] <<- ra + else { parent[rb] <<- ra; rank[ra] <<- rank[ra] + 1L } + TRUE + } + + mst <- edges[0, ] + total <- 0 + for (i in seq_len(nrow(edges))) { + if (unite(edges$u[i], edges$v[i])) { + mst <- rbind(mst, edges[i, ]) + total <- total + edges$w[i] + if (nrow(mst) == n - 1) break + } + } + list(total_weight = total, edges = mst) +} + +# --- tiny demo --- +# Graph: 1--(1)--2, 1--(3)--3, 2--(2)--3, 2--(4)--4, 3--(5)--4 +set.seed(1) +E <- data.frame( + u = c(1, 1, 2, 2, 3), + v = c(2, 3, 3, 4, 4), + w = c(1, 3, 2, 4, 5) +) +res <- kruskalMST(E, n = 4) +print(res$total_weight) # expected 1 + 2 + 4 = 7 +print(res$edges) From 86f6b385f0d7380da71421450b5447edc4f714af Mon Sep 17 00:00:00 2001 From: Srishti Soni <92056170+shimmer12@users.noreply.github.com> Date: Sun, 12 Oct 2025 02:41:29 +0530 Subject: [PATCH 2/3] Update graph_algorithms/kruskalMST.r Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- graph_algorithms/kruskalMST.r | 1 - 1 file changed, 1 deletion(-) diff --git a/graph_algorithms/kruskalMST.r b/graph_algorithms/kruskalMST.r index 83d79d01..cc5c9d8e 100644 --- a/graph_algorithms/kruskalMST.r +++ b/graph_algorithms/kruskalMST.r @@ -39,7 +39,6 @@ kruskalMST <- function(edges, n) { # --- tiny demo --- # Graph: 1--(1)--2, 1--(3)--3, 2--(2)--3, 2--(4)--4, 3--(5)--4 -set.seed(1) E <- data.frame( u = c(1, 1, 2, 2, 3), v = c(2, 3, 3, 4, 4), From 5e30bf8266d6b0119c2b65923201e1b8652eedfa Mon Sep 17 00:00:00 2001 From: Srishti Soni <92056170+shimmer12@users.noreply.github.com> Date: Sun, 12 Oct 2025 02:44:30 +0530 Subject: [PATCH 3/3] Update graph_algorithms/kruskalMST.r Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- graph_algorithms/kruskalMST.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graph_algorithms/kruskalMST.r b/graph_algorithms/kruskalMST.r index cc5c9d8e..dbf60fce 100644 --- a/graph_algorithms/kruskalMST.r +++ b/graph_algorithms/kruskalMST.r @@ -25,7 +25,7 @@ kruskalMST <- function(edges, n) { TRUE } - mst <- edges[0, ] + mst <- data.frame(u = integer(0), v = integer(0), w = numeric(0)) total <- 0 for (i in seq_len(nrow(edges))) { if (unite(edges$u[i], edges$v[i])) {