-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
fixes: 1487 #1488
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
Closed
Closed
fixes: 1487 #1488
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Data Structure - DISJOINT SET | ||
// Use-case - Used to find two nodes are in same component or not | ||
|
||
// Time complexity | ||
// constructor - O(n) | ||
// find_parent - O(1) | ||
// union_by_rank- O(1) | ||
|
||
// Space Complexity: O(n) | ||
|
||
class DSU { | ||
parent = [] | ||
rank = [] | ||
|
||
constructor(number_of_node) { | ||
this.parent = [...this.parent, ...Array(number_of_node).fill(0)] | ||
this.rank = [...this.rank, ...Array(number_of_node).fill(0)] | ||
|
||
// initially all nodes are parent of itself | ||
// initially there are (number_of _node) components | ||
for (let i = 0; i < number_of_node; i++) { | ||
this.parent[i] = i | ||
} | ||
} | ||
|
||
find_parent(node) { | ||
// if current node is parent of itself | ||
if (node == this.parent[node]) return node | ||
|
||
// Path Compression for finding the ultimate parent efficiently | ||
return (this.parent[node] = this.find_parent(this.parent[node])) | ||
} | ||
|
||
union_by_rank(node_1, node_2) { | ||
let node_1_parent = this.find_parent(node_1) | ||
let node_2_parent = this.find_parent(node_2) | ||
|
||
// if two nodes are already in same component | ||
if (node_1_parent === node_2_parent) return | ||
// if node_1_parent has more rank than node_2_parent | ||
else if (this.rank[node_1_parent] > this.rank[node_2_parent]) { | ||
// merging component 2 into component 1 | ||
this.parent[node_2_parent] = node_1_parent | ||
} | ||
|
||
// if node_2_parent has more rank than node_1_parent | ||
else if (this.rank[node_2_parent] > this.rank[node_1_parent]) { | ||
// merging component 1 into component 2 | ||
this.parent[node_1_parent] = node_2_parent | ||
} | ||
|
||
// if node_1_parent and node_2_parent has equal rank | ||
else { | ||
// we can merge any one component to other component because both have same rank | ||
// we are merging component 1 into component 2 | ||
this.parent[node_1_parent] = node_2_parent | ||
this.rank[node_2_parent]++ | ||
} | ||
} | ||
} | ||
|
||
export default DSU |
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,61 @@ | ||
// Data Structure - DISJOINT SET | ||
// Use-case - Used to find two nodes are in same component or not | ||
|
||
// Time complexity | ||
// constructor - O(n) | ||
// find_parent - O(1) | ||
// union_by_size- O(1) | ||
|
||
// Space Complexity: O(n) | ||
|
||
class DSU { | ||
parent = [] | ||
size = [] | ||
|
||
constructor(number_of_node) { | ||
this.parent = [...this.parent, ...Array(number_of_node).fill(0)] | ||
// initially all components have single node itself | ||
this.size = [...this.size, ...Array(number_of_node).fill(1)] | ||
|
||
// initially all nodes are parent of itself | ||
// initially there are (number_of _node) components | ||
for (let i = 0; i < number_of_node; i++) { | ||
this.parent[i] = i | ||
} | ||
} | ||
|
||
find_parent(node) { | ||
// if current node is parent of itself | ||
if (node == this.parent[node]) return node | ||
|
||
// Path Compression for finding the ultimate parent efficiently | ||
return (this.parent[node] = this.find_parent(this.parent[node])) | ||
} | ||
|
||
union_by_size(node_1, node_2) { | ||
let node_1_parent = this.find_parent(node_1) | ||
let node_2_parent = this.find_parent(node_2) | ||
|
||
// if two nodes are already in same component | ||
if (node_1_parent === node_2_parent) return | ||
// if node_1_parent has more size than node_2_parent | ||
else if (this.size[node_1_parent] > this.size[node_2_parent]) { | ||
// merging component 2 into component 1 | ||
this.parent[node_2_parent] = node_1_parent | ||
this.size[node_1_parent] += this.size[node_2_parent] | ||
} | ||
|
||
// if node_2_parent has more or equal size than node_1_parent | ||
else { | ||
this.parent[node_1_parent] = node_2_parent | ||
this.size[node_2_parent] += this.size[node_1_parent] | ||
} | ||
} | ||
|
||
find_size(node) { | ||
let parent = this.find_parent(node) | ||
return this.size[parent] | ||
} | ||
} | ||
|
||
export default DSU |
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,21 @@ | ||
import { expect, test } from 'vitest' | ||
import DSU from '../UnionByRank.js' | ||
import { describe } from 'node:test' | ||
|
||
describe('union_by_rank', () => { | ||
const dsu = new DSU(5) | ||
|
||
dsu.union_by_rank(0, 2) | ||
dsu.union_by_rank(4, 2) | ||
dsu.union_by_rank(3, 1) | ||
|
||
test('find parent of a particular node', () => { | ||
expect(dsu.find_parent(0)).toBe(2) | ||
expect(dsu.find_parent(4)).toBe(2) | ||
}) | ||
|
||
test('finding if two nodes are in same component or not', () => { | ||
expect(dsu.find_parent(4) == dsu.find_parent(0)).toBeTruthy() | ||
expect(dsu.find_parent(3) == dsu.find_parent(2)).toBeFalsy() | ||
}) | ||
}) |
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,29 @@ | ||
import { describe } from 'node:test' | ||
import { expect, test } from 'vitest' | ||
import DSU from '../UnionBySize' | ||
|
||
describe('union_by_size', () => { | ||
const dsu = new DSU(5) | ||
|
||
dsu.union_by_size(0, 2) | ||
dsu.union_by_size(4, 2) | ||
dsu.union_by_size(3, 1) | ||
|
||
test('find parent of a particular node', () => { | ||
expect(dsu.find_parent(0)).toBe(2) | ||
expect(dsu.find_parent(4)).toBe(2) | ||
}) | ||
|
||
test('finding if two nodes are in same component or not', () => { | ||
expect(dsu.find_parent(4) == dsu.find_parent(0)).toBeTruthy() | ||
expect(dsu.find_parent(3) == dsu.find_parent(2)).toBeFalsy() | ||
}) | ||
|
||
test('finding size of a component', () => { | ||
expect(dsu.find_size(3)).toBe(2) | ||
|
||
// all nodes unnited in single component | ||
dsu.union_by_size(3, 0) | ||
expect(dsu.find_size(3)).toBe(5) | ||
}) | ||
}) |
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
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.