Skip to content
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

feat: Branch mst adjmatrix #80

Closed
wants to merge 57 commits into from

Conversation

junnengsoo
Copy link
Collaborator

No description provided.

@junnengsoo junnengsoo added the enhancement New feature or request label Mar 26, 2024
Copy link
Owner

@4ndrelim 4ndrelim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not done yet, ill update on Friday after i tweak Disjoint Set implementation to better handle duplicates and to be more portable!

@@ -0,0 +1,36 @@
package algorithms.minimumSpanningTree.kruskal;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our other files typically place helper class in the same file. For consistency sake, we could shift this to Kruskal.java too. But if there are any concerns do bring it up!

* Constructor to initialize Disjoint Set with a known list of listOfNodes.
* @param listOfNodes
*/
public DisjointSet(Node[] listOfNodes) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, but we should align our implementation of disjoint set. There already is an existing one which u can either import or copy-paste (maybe this is preferable? to be explicit).

Ok but i did notice a concern as i was looking through. Both urs and my implementation does not handle duplicates well. I will tweak the official DisjointSet implementation of our repo to identify by memory address instead.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok on 2nd thought. this is quite a non-concern. DisjointSet isn't at all suited for duplicates

}
}

return mstMatrix;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant rmb if this was what we discussed. But we do this just as a representation to capture the edges right? Do you know of any online helper functions to visualise the mst lol

* adjacency list may be used instead
* A Node class is implemented to encapsulate the current minimum weight to reach the node.
*/
public class Kruskal {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall logic is neat and clear! thanks!

@@ -0,0 +1,38 @@
package algorithms.minimumSpanningTree.kruskal;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup same for this class also. Maybe can put at the bottom as helper classes

*/
public class Prim {
public static int[][] getPrimsMST(Node[] nodes, int[][] adjacencyMatrix) {
PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> a.getCurrMinWeight() - b.getCurrMinWeight());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a small helper comment to remind its a min-heap

PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> a.getCurrMinWeight() - b.getCurrMinWeight());
int[][] mstMatrix = new int[nodes.length][nodes.length]; // MST adjacency matrix

int[] parent = new int[nodes.length]; // To track the parent node of each node in the MST
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice. Map<> works too but i think array is even simpler.

@@ -0,0 +1,76 @@
package algorithms.minimumSpanningTree.prim;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe the underlying assumption is that the graph is connected right? i think both ur implementation should work fine for unconnected graphs too (not a thorough analysis.. jsut a quick run-through in my head) - that is, your return matrix should capture separate MSTs, if exist. Do double check and lmk if im wrong!

@4ndrelim
Copy link
Owner

4ndrelim commented Apr 9, 2024

@junnengsoo can we close this?

@junnengsoo
Copy link
Collaborator Author

A separate PR for MST was merged to main.

@junnengsoo junnengsoo closed this Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MST
3 participants