Skip to content
Open
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
31 changes: 31 additions & 0 deletions Graphs/BellmanFordNegativeCycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Author: Mayank
* Bellman-Ford Algorithm implementation in JavaScript
* Detects shortest paths and checks for negative weight cycles in a graph.
*/

// Bellman-Ford with negative cycle detection

function bellmanFordNegativeCycle(graph, vertices, start) {
const dist = new Array(vertices).fill(Infinity)
dist[start] = 0

for (let i = 0; i < vertices - 1; i++) {
for (const [u, v, w] of graph) {
if (dist[u] !== Infinity && dist[u] + w < dist[v]) {
dist[v] = dist[u] + w
}
}
}

for (const [u, v, w] of graph) {
if (dist[u] !== Infinity && dist[u] + w < dist[v]) {
// A shorter path found after V-1 iterations means a negative cycle exists
return { hasNegativeCycle: true, dist }
}
}

return { hasNegativeCycle: false, dist }
}

export { bellmanFordNegativeCycle }
29 changes: 29 additions & 0 deletions Graphs/test/BellmanFordNegativeCycle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Test file for Bellman-Ford Negative Cycle Detection
*/

import { bellmanFordNegativeCycle } from '../BellmanFordNegativeCycle.js'

test('should find shortest distance without negative cycle', () => {
const graph = [
[0, 1, 4],
[0, 2, 5],
[1, 2, -3],
[2, 3, 4]
]
const result = bellmanFordNegativeCycle(graph, 4, 0)

expect(result.hasNegativeCycle).toBe(false)
expect(result.dist[3]).toBe(5) // shortest distance from 0 to 3
})

test('should detect negative cycle', () => {
const graph = [
[0, 1, 1],
[1, 2, -1],
[2, 0, -1]
]
const result = bellmanFordNegativeCycle(graph, 3, 0)

expect(result.hasNegativeCycle).toBe(true)
})
4 changes: 2 additions & 2 deletions Maths/MobiusFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export const mobiusFunction = (number) => {
return primeFactorsArray.length !== new Set(primeFactorsArray).size
? 0
: primeFactorsArray.length % 2 === 0
? 1
: -1
? 1
: -1
}
42 changes: 42 additions & 0 deletions Sliding-Windows/MinimumWindowSubstring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Author: Mayank
* Minimum Window Substring implementation in JavaScript
* Finds the smallest substring of s that contains all characters of t.
*/

function minWindowSubstring(s, t) {
if (t.length > s.length) return ''

const need = {}
for (let char of t) {
need[char] = (need[char] || 0) + 1
}

let left = 0
let count = t.length
let minLen = Infinity
let minStart = 0

for (let right = 0; right < s.length; right++) {
if (need[s[right]] !== undefined) {
if (need[s[right]] > 0) count--
need[s[right]]--
}

while (count === 0) {
if (right - left + 1 < minLen) {
minLen = right - left + 1
minStart = left
}
if (need[s[left]] !== undefined) {
need[s[left]]++
if (need[s[left]] > 0) count++
}
left++
}
}

return minLen === Infinity ? '' : s.substring(minStart, minStart + minLen)
}

export { minWindowSubstring }
17 changes: 17 additions & 0 deletions Sliding-Windows/test/MinimumWindowSubstring.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Test file for Minimum Window Substring
*/

import { minWindowSubstring } from '../MinimumWindowSubstring.js'

test('return smallest window containing all characters', () => {
expect(minWindowSubstring('ADOBECODEBANC', 'ABC')).toBe('BANC')
})

test('return empty string if no window found', () => {
expect(minWindowSubstring('HELLO', 'XYZ')).toBe('')
})

test('return full string if it matches exactly', () => {
expect(minWindowSubstring('ABC', 'ABC')).toBe('ABC')
})
Loading