mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
Remove live code & console.log, leave examples as comments (Geometry, Graphs, Maths).
This commit is contained in:
@@ -45,12 +45,12 @@ class GraphUnweightedUndirectedAdjacencyList {
|
||||
}
|
||||
}
|
||||
|
||||
function main () {
|
||||
const graph = new GraphUnweightedUndirectedAdjacencyList()
|
||||
graph.addEdge(1, 2) // Component 1
|
||||
graph.addEdge(3, 4) // Component 2
|
||||
graph.addEdge(3, 5) // Component 2
|
||||
console.log(graph.connectedComponents())
|
||||
}
|
||||
export { GraphUnweightedUndirectedAdjacencyList }
|
||||
|
||||
main()
|
||||
// Example
|
||||
|
||||
// const graph = new GraphUnweightedUndirectedAdjacencyList()
|
||||
// graph.addEdge(1, 2) // Component 1
|
||||
// graph.addEdge(3, 4) // Component 2
|
||||
// graph.addEdge(3, 5) // Component 2
|
||||
// const components = graph.connectedComponents()
|
||||
|
||||
@@ -8,4 +8,5 @@ function density (numberOfNodes, numberOfEdges, isDirected = false) {
|
||||
return (multi * numberOfEdges) / (numberOfNodes * (numberOfNodes - 1))
|
||||
}
|
||||
|
||||
console.log(density(10, 2))
|
||||
export { density }
|
||||
|
||||
|
||||
@@ -38,14 +38,14 @@ class GraphUnweightedUndirected {
|
||||
}
|
||||
}
|
||||
|
||||
function main () {
|
||||
const graph = new GraphUnweightedUndirected()
|
||||
graph.addEdge(1, 2)
|
||||
graph.addEdge(2, 3)
|
||||
graph.addEdge(2, 4)
|
||||
graph.addEdge(3, 5)
|
||||
console.log(graph.DFSIterative(5, 1))
|
||||
console.log(graph.DFSIterative(5, 100))
|
||||
}
|
||||
export { GraphUnweightedUndirected }
|
||||
|
||||
main()
|
||||
// Example
|
||||
|
||||
// const graph = new GraphUnweightedUndirected()
|
||||
// graph.addEdge(1, 2)
|
||||
// graph.addEdge(2, 3)
|
||||
// graph.addEdge(2, 4)
|
||||
// graph.addEdge(3, 5)
|
||||
// graph.DFSIterative(5, 1)
|
||||
// graph.DFSIterative(5, 100)
|
||||
|
||||
@@ -33,14 +33,12 @@ class GraphUnweightedUndirected {
|
||||
}
|
||||
}
|
||||
|
||||
function main () {
|
||||
const graph = new GraphUnweightedUndirected()
|
||||
graph.addEdge(1, 2)
|
||||
graph.addEdge(2, 3)
|
||||
graph.addEdge(2, 4)
|
||||
graph.addEdge(3, 5)
|
||||
console.log(graph.DFSRecursive(5, 1))
|
||||
console.log(graph.DFSRecursive(5, 100))
|
||||
}
|
||||
export { GraphUnweightedUndirected }
|
||||
|
||||
main()
|
||||
// const graph = new GraphUnweightedUndirected()
|
||||
// graph.addEdge(1, 2)
|
||||
// graph.addEdge(2, 3)
|
||||
// graph.addEdge(2, 4)
|
||||
// graph.addEdge(3, 5)
|
||||
// graph.DFSRecursive(5, 1)
|
||||
// graph.DFSRecursive(5, 100)
|
||||
|
||||
@@ -47,30 +47,30 @@ function djikstra (graph, V, src) {
|
||||
return dist
|
||||
}
|
||||
|
||||
const V = 9
|
||||
const E = [
|
||||
[0, 1, 4],
|
||||
[0, 7, 8],
|
||||
[1, 7, 11],
|
||||
[1, 2, 8],
|
||||
[7, 8, 7],
|
||||
[6, 7, 1],
|
||||
[2, 8, 2],
|
||||
[6, 8, 6],
|
||||
[5, 6, 2],
|
||||
[2, 5, 4],
|
||||
[2, 3, 7],
|
||||
[3, 5, 14],
|
||||
[3, 4, 9],
|
||||
[4, 5, 10]
|
||||
]
|
||||
export { createGraph, djikstra }
|
||||
|
||||
const graph = createGraph(V, E)
|
||||
const distances = djikstra(graph, V, 0)
|
||||
// const V = 9
|
||||
// const E = [
|
||||
// [0, 1, 4],
|
||||
// [0, 7, 8],
|
||||
// [1, 7, 11],
|
||||
// [1, 2, 8],
|
||||
// [7, 8, 7],
|
||||
// [6, 7, 1],
|
||||
// [2, 8, 2],
|
||||
// [6, 8, 6],
|
||||
// [5, 6, 2],
|
||||
// [2, 5, 4],
|
||||
// [2, 3, 7],
|
||||
// [3, 5, 14],
|
||||
// [3, 4, 9],
|
||||
// [4, 5, 10]
|
||||
// ]
|
||||
|
||||
// const graph = createGraph(V, E)
|
||||
// const distances = djikstra(graph, V, 0)
|
||||
|
||||
/**
|
||||
* The first value in the array determines the minimum distance and the
|
||||
* second value represents the parent node from which the minimum distance has been calculated
|
||||
*/
|
||||
|
||||
console.log(distances)
|
||||
|
||||
@@ -39,28 +39,31 @@ function solve (graph, s) {
|
||||
|
||||
return solutions
|
||||
}
|
||||
// create graph
|
||||
const graph = {}
|
||||
|
||||
const layout = {
|
||||
R: ['2'],
|
||||
2: ['3', '4'],
|
||||
3: ['4', '6', '13'],
|
||||
4: ['5', '8'],
|
||||
5: ['7', '11'],
|
||||
6: ['13', '15'],
|
||||
7: ['10'],
|
||||
8: ['11', '13'],
|
||||
9: ['14'],
|
||||
10: [],
|
||||
11: ['12'],
|
||||
12: [],
|
||||
13: ['14'],
|
||||
14: [],
|
||||
15: []
|
||||
}
|
||||
export { solve }
|
||||
|
||||
// convert uni-directional to bi-directional graph
|
||||
// // create graph
|
||||
// const graph = {}
|
||||
|
||||
// const layout = {
|
||||
// R: ['2'],
|
||||
// 2: ['3', '4'],
|
||||
// 3: ['4', '6', '13'],
|
||||
// 4: ['5', '8'],
|
||||
// 5: ['7', '11'],
|
||||
// 6: ['13', '15'],
|
||||
// 7: ['10'],
|
||||
// 8: ['11', '13'],
|
||||
// 9: ['14'],
|
||||
// 10: [],
|
||||
// 11: ['12'],
|
||||
// 12: [],
|
||||
// 13: ['14'],
|
||||
// 14: [],
|
||||
// 15: []
|
||||
// }
|
||||
|
||||
// // convert uni-directional to bi-directional graph
|
||||
// let graph = {
|
||||
// a: {e:1, b:1, g:3},
|
||||
// b: {a:1, c:1},
|
||||
@@ -72,26 +75,22 @@ const layout = {
|
||||
// h: {f:1}
|
||||
// };
|
||||
|
||||
for (const id in layout) {
|
||||
if (!graph[id]) { graph[id] = {} }
|
||||
layout[id].forEach(function (aid) {
|
||||
graph[id][aid] = 1
|
||||
if (!graph[aid]) { graph[aid] = {} }
|
||||
graph[aid][id] = 1
|
||||
})
|
||||
}
|
||||
// for (const id in layout) {
|
||||
// if (!graph[id]) { graph[id] = {} }
|
||||
// layout[id].forEach(function (aid) {
|
||||
// graph[id][aid] = 1
|
||||
// if (!graph[aid]) { graph[aid] = {} }
|
||||
// graph[aid][id] = 1
|
||||
// })
|
||||
// }
|
||||
|
||||
// choose start node
|
||||
const start = '10'
|
||||
// get all solutions
|
||||
const solutions = solve(graph, start)
|
||||
// // choose start node
|
||||
// const start = '10'
|
||||
// // get all solutions
|
||||
// const solutions = solve(graph, start)
|
||||
|
||||
console.log("From '" + start + "' to")
|
||||
// display solutions
|
||||
for (const s in solutions) {
|
||||
if (!solutions[s]) continue
|
||||
console.log(' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')')
|
||||
}
|
||||
// // for s in solutions..
|
||||
// ' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')'
|
||||
|
||||
// From '10' to
|
||||
// -> 2: [7, 5, 4, 2] (dist:4)
|
||||
|
||||
@@ -23,26 +23,26 @@ const FloydWarshall = (dist) => {
|
||||
return dist
|
||||
}
|
||||
|
||||
const main = () => {
|
||||
// For the following graph (edge weights are shown in brackets)
|
||||
// 4 1 dist[1][2] = dist[2][1] = 1
|
||||
// \ (2)/ \ dist[1][3] = dist[3][1] = 2
|
||||
// \ / \(1) dist[1][4] = dist[4][1] = Infinity
|
||||
// (1)\ / \ dist[3][4] = dist[4][3] = 1
|
||||
// 3 2 dist[2][4] = dist[4][2] = Infinity
|
||||
// dist[2][3] = dist[3][2] = Infinity
|
||||
// Output should be:
|
||||
// [ [0, 1, 2, 3],
|
||||
// [1, 0, 3, 4],
|
||||
// [2, 3, 0, 1],
|
||||
// [3, 4, 1, 0] ]
|
||||
console.log(FloydWarshall(
|
||||
[[0, 1, 2, Infinity],
|
||||
[1, 0, Infinity, Infinity],
|
||||
[2, Infinity, 0, 1],
|
||||
[Infinity, Infinity, 1, 0]
|
||||
]
|
||||
))
|
||||
}
|
||||
export { FloydWarshall }
|
||||
|
||||
// For the following graph (edge weights are shown in brackets)
|
||||
// 4 1 dist[1][2] = dist[2][1] = 1
|
||||
// \ (2)/ \ dist[1][3] = dist[3][1] = 2
|
||||
// \ / \(1) dist[1][4] = dist[4][1] = Infinity
|
||||
// (1)\ / \ dist[3][4] = dist[4][3] = 1
|
||||
// 3 2 dist[2][4] = dist[4][2] = Infinity
|
||||
// dist[2][3] = dist[3][2] = Infinity
|
||||
// Output should be:
|
||||
// [ [0, 1, 2, 3],
|
||||
// [1, 0, 3, 4],
|
||||
// [2, 3, 0, 1],
|
||||
// [3, 4, 1, 0] ]
|
||||
|
||||
// FloydWarshall(
|
||||
// [[0, 1, 2, Infinity],
|
||||
// [1, 0, Infinity, Infinity],
|
||||
// [2, Infinity, 0, 1],
|
||||
// [Infinity, Infinity, 1, 0]
|
||||
// ]
|
||||
// )
|
||||
|
||||
main()
|
||||
|
||||
@@ -101,15 +101,12 @@ class GraphWeightedUndirectedAdjacencyList {
|
||||
}
|
||||
}
|
||||
|
||||
function main () {
|
||||
const graph = new GraphWeightedUndirectedAdjacencyList()
|
||||
graph.addEdge(1, 2, 1)
|
||||
graph.addEdge(2, 3, 2)
|
||||
graph.addEdge(3, 4, 1)
|
||||
graph.addEdge(3, 5, 100) // Removed in MST
|
||||
graph.addEdge(4, 5, 5)
|
||||
console.log(graph)
|
||||
console.log(graph.KruskalMST())
|
||||
}
|
||||
export { GraphWeightedUndirectedAdjacencyList }
|
||||
|
||||
main()
|
||||
// const graph = new GraphWeightedUndirectedAdjacencyList()
|
||||
// graph.addEdge(1, 2, 1)
|
||||
// graph.addEdge(2, 3, 2)
|
||||
// graph.addEdge(3, 4, 1)
|
||||
// graph.addEdge(3, 5, 100) // Removed in MST
|
||||
// graph.addEdge(4, 5, 5)
|
||||
// graph.KruskalMST()
|
||||
|
||||
@@ -30,11 +30,11 @@ class Graph {
|
||||
}
|
||||
}
|
||||
|
||||
(() => {
|
||||
const graph = new Graph()
|
||||
graph.addEdge(1, 2)
|
||||
graph.addEdge(2, 3)
|
||||
graph.addEdge(3, 5)
|
||||
graph.addEdge(1, 5)
|
||||
console.log(graph.nodeNeighbors(1))
|
||||
})()
|
||||
export { Graph }
|
||||
|
||||
// const graph = new Graph()
|
||||
// graph.addEdge(1, 2)
|
||||
// graph.addEdge(2, 3)
|
||||
// graph.addEdge(3, 5)
|
||||
// graph.addEdge(1, 5)
|
||||
// graph.nodeNeighbors(1)
|
||||
|
||||
@@ -46,12 +46,6 @@ Pseudocode:
|
||||
|
||||
Return the count
|
||||
*/
|
||||
const grid = [
|
||||
['1', '1', '0', '0', '0'],
|
||||
['1', '1', '0', '0', '0'],
|
||||
['0', '0', '1', '0', '0'],
|
||||
['0', '0', '0', '1', '1']
|
||||
]
|
||||
|
||||
const islands = (matrixGrid) => {
|
||||
const matrix = matrixGrid
|
||||
@@ -83,4 +77,12 @@ const islands = (matrixGrid) => {
|
||||
}
|
||||
return counter
|
||||
}
|
||||
console.log(islands(grid))
|
||||
|
||||
export { islands }
|
||||
|
||||
// islands(
|
||||
// ['1', '1', '0', '0', '0'],
|
||||
// ['1', '1', '0', '0', '0'],
|
||||
// ['0', '0', '1', '0', '0'],
|
||||
// ['0', '0', '0', '1', '1']
|
||||
// )
|
||||
|
||||
@@ -197,14 +197,12 @@ class GraphWeightedUndirectedAdjacencyList {
|
||||
}
|
||||
}
|
||||
|
||||
function main () {
|
||||
const graph = new GraphWeightedUndirectedAdjacencyList()
|
||||
graph.addEdge(1, 2, 1)
|
||||
graph.addEdge(2, 3, 2)
|
||||
graph.addEdge(3, 4, 1)
|
||||
graph.addEdge(3, 5, 100) // Removed in MST
|
||||
graph.addEdge(4, 5, 5)
|
||||
console.log(graph.PrimMST(1))
|
||||
}
|
||||
export { GraphWeightedUndirectedAdjacencyList }
|
||||
|
||||
main()
|
||||
// const graph = new GraphWeightedUndirectedAdjacencyList()
|
||||
// graph.addEdge(1, 2, 1)
|
||||
// graph.addEdge(2, 3, 2)
|
||||
// graph.addEdge(3, 4, 1)
|
||||
// graph.addEdge(3, 5, 100) // Removed in MST
|
||||
// graph.addEdge(4, 5, 5)
|
||||
// graph.PrimMST(1)
|
||||
|
||||
Reference in New Issue
Block a user