mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@ -1,24 +1,24 @@
|
||||
class Graph {
|
||||
constructor () {
|
||||
constructor() {
|
||||
this.adjacencyMap = {}
|
||||
}
|
||||
|
||||
addVertex (vertex) {
|
||||
addVertex(vertex) {
|
||||
this.adjacencyMap[vertex] = []
|
||||
}
|
||||
|
||||
containsVertex (vertex) {
|
||||
return typeof (this.adjacencyMap[vertex]) !== 'undefined'
|
||||
containsVertex(vertex) {
|
||||
return typeof this.adjacencyMap[vertex] !== 'undefined'
|
||||
}
|
||||
|
||||
addEdge (vertex1, vertex2) {
|
||||
addEdge(vertex1, vertex2) {
|
||||
if (this.containsVertex(vertex1) && this.containsVertex(vertex2)) {
|
||||
this.adjacencyMap[vertex1].push(vertex2)
|
||||
this.adjacencyMap[vertex2].push(vertex1)
|
||||
}
|
||||
}
|
||||
|
||||
printGraph (output = value => console.log(value)) {
|
||||
printGraph(output = (value) => console.log(value)) {
|
||||
const keys = Object.keys(this.adjacencyMap)
|
||||
for (const i of keys) {
|
||||
const values = this.adjacencyMap[i]
|
||||
@ -34,13 +34,14 @@ class Graph {
|
||||
* Prints the Breadth first traversal of the graph from source.
|
||||
* @param {number} source The source vertex to start BFS.
|
||||
*/
|
||||
bfs (source, output = value => console.log(value)) {
|
||||
bfs(source, output = (value) => console.log(value)) {
|
||||
const queue = [[source, 0]] // level of source is 0
|
||||
const visited = new Set()
|
||||
|
||||
while (queue.length) {
|
||||
const [node, level] = queue.shift() // remove the front of the queue
|
||||
if (visited.has(node)) { // visited
|
||||
if (visited.has(node)) {
|
||||
// visited
|
||||
continue
|
||||
}
|
||||
|
||||
@ -56,8 +57,9 @@ class Graph {
|
||||
* Prints the Depth first traversal of the graph from source.
|
||||
* @param {number} source The source vertex to start DFS.
|
||||
*/
|
||||
dfs (source, visited = new Set(), output = value => console.log(value)) {
|
||||
if (visited.has(source)) { // visited
|
||||
dfs(source, visited = new Set(), output = (value) => console.log(value)) {
|
||||
if (visited.has(source)) {
|
||||
// visited
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
class Graph {
|
||||
// defining vertex array and
|
||||
// adjacent list
|
||||
constructor (noOfVertices) {
|
||||
constructor(noOfVertices) {
|
||||
this.noOfVertices = noOfVertices
|
||||
this.AdjList = new Map()
|
||||
}
|
||||
@ -17,7 +17,7 @@ class Graph {
|
||||
// dfs(v)
|
||||
|
||||
// add vertex to the graph
|
||||
addVertex (v) {
|
||||
addVertex(v) {
|
||||
// initialize the adjacent list with a
|
||||
// null array
|
||||
|
||||
@ -25,7 +25,7 @@ class Graph {
|
||||
}
|
||||
|
||||
// add edge to the graph
|
||||
addEdge (v, w) {
|
||||
addEdge(v, w) {
|
||||
// get the list for vertex v and put the
|
||||
// vertex w denoting edge between v and w
|
||||
this.AdjList.get(v).push(w)
|
||||
@ -36,7 +36,7 @@ class Graph {
|
||||
}
|
||||
|
||||
// Prints the vertex and adjacency list
|
||||
printGraph (output = value => console.log(value)) {
|
||||
printGraph(output = (value) => console.log(value)) {
|
||||
// get all the vertices
|
||||
const getKeys = this.AdjList.keys()
|
||||
|
||||
|
@ -1,18 +1,18 @@
|
||||
class Graph {
|
||||
constructor () {
|
||||
constructor() {
|
||||
this.adjacencyObject = {}
|
||||
}
|
||||
|
||||
addVertex (vertex) {
|
||||
addVertex(vertex) {
|
||||
if (!this.adjacencyObject[vertex]) this.adjacencyObject[vertex] = []
|
||||
}
|
||||
|
||||
addEdge (vertex1, vertex2) {
|
||||
addEdge(vertex1, vertex2) {
|
||||
this.adjacencyObject[vertex1].push(vertex2)
|
||||
this.adjacencyObject[vertex2].push(vertex1)
|
||||
}
|
||||
|
||||
removeEdge (vertex1, vertex2) {
|
||||
removeEdge(vertex1, vertex2) {
|
||||
this.adjacencyObject[vertex1] = this.adjacencyObject[vertex1].filter(
|
||||
(v) => v !== vertex2
|
||||
)
|
||||
@ -21,7 +21,7 @@ class Graph {
|
||||
)
|
||||
}
|
||||
|
||||
removeVertex (vertex) {
|
||||
removeVertex(vertex) {
|
||||
while (this.adjacencyObject[vertex].length) {
|
||||
const adjacentVertex = this.adjacencyObject[vertex].pop()
|
||||
this.removeEdge(vertex, adjacentVertex)
|
||||
@ -31,14 +31,14 @@ class Graph {
|
||||
/**
|
||||
* Return DFS (Depth First Search) List Using Recursive Method
|
||||
*/
|
||||
DFS (start) {
|
||||
DFS(start) {
|
||||
if (!start) return null
|
||||
|
||||
const result = []
|
||||
const visited = {}
|
||||
const adjacencyObject = this.adjacencyObject
|
||||
|
||||
function dfs (vertex) {
|
||||
function dfs(vertex) {
|
||||
if (!vertex) return null
|
||||
visited[vertex] = true
|
||||
result.push(vertex)
|
||||
@ -56,7 +56,7 @@ class Graph {
|
||||
/**
|
||||
* Return DFS(Depth First Search) List Using Iteration
|
||||
*/
|
||||
DFSIterative (start) {
|
||||
DFSIterative(start) {
|
||||
if (!start) return null
|
||||
|
||||
const stack = [start]
|
||||
@ -80,7 +80,7 @@ class Graph {
|
||||
return result
|
||||
}
|
||||
|
||||
BFS (start) {
|
||||
BFS(start) {
|
||||
if (!start) return null
|
||||
|
||||
const queue = [start]
|
||||
|
@ -18,7 +18,7 @@ describe('Test Graph2', () => {
|
||||
graph.addEdge('C', 'F')
|
||||
|
||||
it('Check adjacency lists', () => {
|
||||
const mockFn = jest.fn()
|
||||
const mockFn = vi.fn()
|
||||
graph.printGraph(mockFn)
|
||||
|
||||
// Expect one call per vertex
|
||||
|
Reference in New Issue
Block a user