Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Gauravbio wants to merge 1 commit into TheAlgorithms:master from Gauravbio:dsu
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Data-Structures/Disjoint-Set/UnionByRank.js
View file Open in desktop
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
61 changes: 61 additions & 0 deletions Data-Structures/Disjoint-Set/UnionBySize.js
View file Open in desktop
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
21 changes: 21 additions & 0 deletions Data-Structures/Disjoint-Set/test/UnionByRank.test.js
View file Open in desktop
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()
})
})
29 changes: 29 additions & 0 deletions Data-Structures/Disjoint-Set/test/UnionBySize.test.js
View file Open in desktop
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)
})
})
10 changes: 8 additions & 2 deletions Maths/test/EuclideanDistance.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import { EuclideanDistance } from '../EuclideanDistance.js'

describe('EuclideanDistance', () => {
it('should calculate the distance correctly for 2D vectors', () => {
expect(EuclideanDistance([0, 0], [2, 2])).toBeCloseTo(2.8284271247461903, 10)
expect(EuclideanDistance([0, 0], [2, 2])).toBeCloseTo(
2.8284271247461903,
10
)
})

it('should calculate the distance correctly for 3D vectors', () => {
expect(EuclideanDistance([0, 0, 0], [2, 2, 2])).toBeCloseTo(3.4641016151377544, 10)
expect(EuclideanDistance([0, 0, 0], [2, 2, 2])).toBeCloseTo(
3.4641016151377544,
10
)
})

it('should calculate the distance correctly for 4D vectors', () => {
Expand Down

AltStyle によって変換されたページ (->オリジナル) /