-
-
Notifications
You must be signed in to change notification settings - Fork 410
feat: add Prim's algorithm for Minimum Spanning Tree #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
import { PriorityQueue } from '../data_structures/heap/heap' | ||
/** | ||
* @function prim | ||
* @description Compute a minimum spanning tree(MST) of a fully connected weighted undirected graph. The input graph is in adjacency list form. It is a multidimensional array of edges. graph[i] holds the edges for the i'th node. Each edge is a 2-tuple where the 0'th item is the destination node, and the 1'th item is the edge weight. | ||
* @Complexity_Analysis | ||
* Time complexity: O(Elog(V)) | ||
* Space Complexity: O(V) | ||
* @param {[number, number][][]} graph - The graph in adjacency list form | ||
* @return {Edge[], number} - [The edges of the minimum spanning tree, the sum of the weights of the edges in the tree] | ||
* @see https://en.wikipedia.org/wiki/Prim%27s_algorithm | ||
*/ | ||
export const prim = (graph: [number, number][][]): [Edge[], number] => { | ||
if (graph.length == 0) { | ||
return [[], 0]; | ||
} | ||
let minimum_spanning_tree: Edge[] = []; | ||
let total_weight = 0; | ||
|
||
let priorityQueue = new PriorityQueue((e: Edge) => { return e.b }, graph.length, (a: Edge, b: Edge) => { return a.weight < b.weight }); | ||
let visited = new Set<number>(); | ||
|
||
// Start from the 0'th node. For fully connected graphs, we can start from any node and still produce the MST. | ||
visited.add(0); | ||
add_children(graph, priorityQueue, 0); | ||
|
||
while (!priorityQueue.isEmpty()) { | ||
// We always store the previously visited edge in `edge.a`, and the newly visited node in `edge.b`. | ||
let edge = priorityQueue.extract(); | ||
if (visited.has(edge.b)) { | ||
continue; | ||
} | ||
minimum_spanning_tree.push(edge); | ||
total_weight += edge.weight; | ||
visited.add(edge.b); | ||
add_children(graph, priorityQueue, edge.b); | ||
} | ||
|
||
return [minimum_spanning_tree, total_weight]; | ||
} | ||
|
||
const add_children = (graph: [number, number][][], priorityQueue: PriorityQueue<Edge>, node: number) => { | ||
for (let i = 0; i < graph[node].length; ++i) { | ||
let out_edge = graph[node][i]; | ||
// By increasing the priority, we ensure we only add each vertex to the queue one time, and the queue will be at most size V. | ||
priorityQueue.increasePriority(out_edge[0], new Edge(node, out_edge[0], out_edge[1])); | ||
} | ||
} | ||
|
||
export class Edge { | ||
a: number = 0; | ||
b: number = 0; | ||
weight: number = 0; | ||
constructor(a: number, b: number, weight: number) { | ||
this.a = a; | ||
this.b = b; | ||
this.weight = weight; | ||
} | ||
} | ||
|
This file contains hidden or 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,85 @@ | ||
import { Edge, prim } from "../prim"; | ||
|
||
let edge_equal = (x: Edge, y: Edge): boolean => { | ||
return (x.a == y.a && x.b == y.b) || (x.a == y.b && x.b == y.a) && x.weight == y.weight; | ||
} | ||
|
||
let test_graph = (expected_tree_edges: Edge[], other_edges: Edge[], num_vertices: number, expected_cost: number) => { | ||
// First make sure the graph is undirected | ||
let graph: [number, number][][] = []; | ||
for (let _ = 0; _ < num_vertices; ++_) { | ||
graph.push([]); | ||
} | ||
for (let edge of expected_tree_edges) { | ||
graph[edge.a].push([edge.b, edge.weight]); | ||
graph[edge.b].push([edge.a, edge.weight]); | ||
} | ||
for (let edge of other_edges) { | ||
graph[edge.a].push([edge.b, edge.weight]); | ||
graph[edge.b].push([edge.a, edge.weight]); | ||
} | ||
|
||
let [tree_edges, cost] = prim(graph); | ||
expect(cost).toStrictEqual(expected_cost); | ||
for (let expected_edge of expected_tree_edges) { | ||
expect(tree_edges.find(edge => edge_equal(edge, expected_edge))).toBeTruthy(); | ||
} | ||
for (let unexpected_edge of other_edges) { | ||
expect(tree_edges.find(edge => edge_equal(edge, unexpected_edge))).toBeFalsy(); | ||
} | ||
}; | ||
|
||
|
||
describe("prim", () => { | ||
|
||
it("should return empty tree for empty graph", () => { | ||
expect(prim([])).toStrictEqual([[], 0]); | ||
}); | ||
|
||
it("should return empty tree for single element graph", () => { | ||
expect(prim([])).toStrictEqual([[], 0]); | ||
}); | ||
|
||
it("should return correct value for two element graph", () => { | ||
expect(prim([[[1, 5]], []])).toStrictEqual([[new Edge(0, 1, 5)], 5]); | ||
}); | ||
|
||
it("should return the correct value", () => { | ||
let expected_tree_edges = [ | ||
new Edge(0, 1, 1), | ||
new Edge(1, 3, 2), | ||
new Edge(3, 2, 3), | ||
]; | ||
|
||
let other_edges = [ | ||
new Edge(0, 2, 4), | ||
new Edge(0, 3, 5), | ||
new Edge(1, 2, 6), | ||
]; | ||
|
||
test_graph(expected_tree_edges, other_edges, 4, 6); | ||
}); | ||
|
||
it("should return the correct value", () => { | ||
let expected_tree_edges = [ | ||
new Edge(0, 2, 2), | ||
new Edge(1, 3, 9), | ||
new Edge(2, 6, 74), | ||
new Edge(2, 7, 8), | ||
new Edge(3, 4, 3), | ||
new Edge(4, 9, 9), | ||
new Edge(5, 7, 5), | ||
new Edge(7, 9, 4), | ||
new Edge(8, 9, 2), | ||
] | ||
|
||
let other_edges = [ | ||
new Edge(0, 1, 10), | ||
new Edge(2, 4, 47), | ||
new Edge(4, 5, 42), | ||
]; | ||
|
||
test_graph(expected_tree_edges, other_edges, 10, 116); | ||
}); | ||
|
||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.