-
Notifications
You must be signed in to change notification settings - Fork 18
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
Conversation
…data-structures-and-algorithms into branch-MST-adjmatrix
Orthogonal Range Searching
There was a problem hiding this 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; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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!
refactor: DisjointSet
@junnengsoo can we close this? |
A separate PR for MST was merged to main. |
No description provided.