This commit is contained in:
itsvinayak
2020-05-05 16:58:57 +05:30
parent cabf733df2
commit fc0aba01ac
5 changed files with 174 additions and 188 deletions

View File

@ -1,71 +1,66 @@
// starting at s // starting at s
function solve(graph, s) { function solve (graph, s) {
var solutions = {}; var solutions = {}
solutions[s] = []; solutions[s] = []
solutions[s].dist = 0; solutions[s].dist = 0
while(true) { while (true) {
var p = null; var p = null
var neighbor = null; var neighbor = null
var dist = Infinity; var dist = Infinity
for (var n in solutions) {
if (!solutions[n]) { continue }
var ndist = solutions[n].dist
var adj = graph[n]
for(var n in solutions) { for (var a in adj) {
if(!solutions[n]) if (solutions[a]) { continue }
continue
var ndist = solutions[n].dist;
var adj = graph[n];
for(var a in adj) { var d = adj[a] + ndist
if (d < dist) {
if(solutions[a]) p = solutions[n]
continue; neighbor = a
dist = d
var d = adj[a] + ndist;
if(d < dist) {
p = solutions[n];
neighbor = a;
dist = d;
} }
} }
} }
//no more solutions // no more solutions
if(dist === Infinity) { if (dist === Infinity) {
break; break
} }
//extend parent's solution path // extend parent's solution path
solutions[neighbor] = p.concat(neighbor); solutions[neighbor] = p.concat(neighbor)
//extend parent's cost // extend parent's cost
solutions[neighbor].dist = dist; solutions[neighbor].dist = dist
} }
return solutions; return solutions
} }
//create graph // create graph
var graph = {}; var graph = {}
var layout = { var layout = {
'R': ['2'], R: ['2'],
'2': ['3','4'], 2: ['3', '4'],
'3': ['4','6','13'], 3: ['4', '6', '13'],
'4': ['5','8'], 4: ['5', '8'],
'5': ['7','11'], 5: ['7', '11'],
'6': ['13','15'], 6: ['13', '15'],
'7': ['10'], 7: ['10'],
'8': ['11','13'], 8: ['11', '13'],
'9': ['14'], 9: ['14'],
'10': [], 10: [],
'11': ['12'], 11: ['12'],
'12': [], 12: [],
'13': ['14'], 13: ['14'],
'14': [], 14: [],
'15': [] 15: []
} }
//convert uni-directional to bi-directional graph // convert uni-directional to bi-directional graph
// var graph = { // var graph = {
// a: {e:1, b:1, g:3}, // a: {e:1, b:1, g:3},
// b: {a:1, c:1}, // b: {a:1, c:1},
@ -77,27 +72,25 @@ var layout = {
// h: {f:1} // h: {f:1}
// }; // };
for(var id in layout) { for (var id in layout) {
if(!graph[id]) if (!graph[id]) { graph[id] = {} }
graph[id] = {}; layout[id].forEach(function (aid) {
layout[id].forEach(function(aid) { graph[id][aid] = 1
graph[id][aid] = 1; if (!graph[aid]) { graph[aid] = {} }
if(!graph[aid]) graph[aid][id] = 1
graph[aid] = {}; })
graph[aid][id] = 1;
});
} }
//choose start node // choose start node
var start = '10'; var start = '10'
//get all solutions // get all solutions
var solutions = solve(graph, start); var solutions = solve(graph, start)
console.log("From '"+start+"' to"); console.log("From '" + start + "' to")
//display solutions // display solutions
for(var s in solutions) { for (var s in solutions) {
if(!solutions[s]) continue; if (!solutions[s]) continue
console.log(" -> " + s + ": [" + solutions[s].join(", ") + "] (dist:" + solutions[s].dist + ")"); console.log(' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')')
} }
// From '10' to // From '10' to

View File

@ -11,7 +11,7 @@
https://en.wikipedia.org/wiki/Absolute_value https://en.wikipedia.org/wiki/Absolute_value
*/ */
function abs_val (num) { function absVal (num) {
// Find absolute value of `num`. // Find absolute value of `num`.
'use strict' 'use strict'
if (num < 0) { if (num < 0) {
@ -22,5 +22,5 @@ function abs_val (num) {
} }
// Run `abs` function to find absolute value of two numbers. // Run `abs` function to find absolute value of two numbers.
console.log('The absolute value of -34 is ' + abs_val(-34)) console.log('The absolute value of -34 is ' + absVal(-34))
console.log('The absolute value of 34 is ' + abs_val(34)) console.log('The absolute value of 34 is ' + absVal(34))

View File

@ -13,7 +13,7 @@
'use strict' 'use strict'
function calc_range (num) { function calcRange (num) {
// Generate a range of numbers from 1 to `num`. // Generate a range of numbers from 1 to `num`.
var i = 1 var i = 1
var range = [] var range = []
@ -24,9 +24,9 @@ function calc_range (num) {
return range return range
} }
function calc_factorial (num) { function calcFactorial (num) {
var factorial var factorial
var range = calc_range(num) var range = calcRange(num)
// Check if the number is negative, positive, null, undefined, or zero // Check if the number is negative, positive, null, undefined, or zero
if (num < 0) { if (num < 0) {
@ -48,5 +48,5 @@ function calc_factorial (num) {
} }
// Run `factorial` Function to find average of a list of numbers. // Run `factorial` Function to find average of a list of numbers.
var num = prompt('Enter a number: ') var num = console.log('Enter a number: ')
alert(calc_factorial(num)) console.log(calcFactorial(num))

View File

@ -3,7 +3,7 @@
license: GPL-3.0 or later license: GPL-3.0 or later
Modified from: Modified from:
https://github.com/TheAlgorithms/Python/blob/master/maths/find_lcm.py https://github.com/TheAlgorithms/Python/blob/master/maths/findLcm.py
More about LCM: More about LCM:
https://en.wikipedia.org/wiki/Least_common_multiple https://en.wikipedia.org/wiki/Least_common_multiple
@ -12,27 +12,27 @@
'use strict' 'use strict'
// Find the LCM of two numbers. // Find the LCM of two numbers.
function find_lcm (num_1, num_2) { function findLcm (num1, num2) {
var max_num var maxNum
var lcm var lcm
// Check to see whether num_1 or num_2 is larger. // Check to see whether num1 or num2 is larger.
if (num_1 > num_2) { if (num1 > num2) {
max_num = num_1 maxNum = num1
} else { } else {
max_num = num_2 maxNum = num2
} }
lcm = max_num lcm = maxNum
while (true) { while (true) {
if ((lcm % num_1 === 0) && (lcm % num_2 === 0)) { if ((lcm % num1 === 0) && (lcm % num2 === 0)) {
break break
} }
lcm += max_num lcm += maxNum
} }
return lcm return lcm
} }
// Run `find_lcm` Function // Run `findLcm` Function
var num_1 = 12 var num1 = 12
var num_2 = 76 var num2 = 76
console.log(find_lcm(num_1, num_2)) console.log(findLcm(num1, num2))

View File

@ -1,87 +1,81 @@
// create a graph class // create a graph class
class Graph { class Graph {
// defining vertex array and // defining vertex array and
// adjacent list // adjacent list
constructor(noOfVertices) constructor (noOfVertices) {
{ this.noOfVertices = noOfVertices
this.noOfVertices = noOfVertices; this.AdjList = new Map()
this.AdjList = new Map(); }
}
// functions to be implemented // functions to be implemented
// addVertex(v) // addVertex(v)
// addEdge(v, w) // addEdge(v, w)
// printGraph() // printGraph()
// bfs(v) // bfs(v)
// dfs(v) // dfs(v)
}
// add vertex to the graph // add vertex to the graph
addVertex(v) addVertex (v) {
{
// initialize the adjacent list with a // initialize the adjacent list with a
// null array // null array
this.AdjList.set(v, []);
}
// add edge to the graph this.AdjList.set(v, [])
addEdge(v, w) }
{
// add edge to the graph
addEdge (v, w) {
// get the list for vertex v and put the // get the list for vertex v and put the
// vertex w denoting edge between v and w // vertex w denoting edge between v and w
this.AdjList.get(v).push(w); this.AdjList.get(v).push(w)
// Since graph is undirected, // Since graph is undirected,
// add an edge from w to v also // add an edge from w to v also
this.AdjList.get(w).push(v); this.AdjList.get(w).push(v)
} }
// Prints the vertex and adjacency list
// Prints the vertex and adjacency list printGraph () {
printGraph()
{
// get all the vertices // get all the vertices
var get_keys = this.AdjList.keys(); const getKeys = this.AdjList.keys()
// iterate over the vertices // iterate over the vertices
for (var i of get_keys) for (const i of getKeys) {
{ // great the corresponding adjacency list
// great the corresponding adjacency list // for the vertex
// for the vertex const getValues = this.AdjList.get(i)
var get_values = this.AdjList.get(i); let conc = ''
var conc = "";
// iterate over the adjacency list // iterate over the adjacency list
// concatenate the values into a string // concatenate the values into a string
for (var j of get_values) for (const j of getValues) {
conc += j + " "; conc += j + ' '
}
// print the vertex and its adjacency list // print the vertex and its adjacency list
console.log(i + " -> " + conc); console.log(i + ' -> ' + conc)
} }
}
} }
// Example // Example
var graph = new Graph(6); const graph = new Graph(6)
var vertices = [ 'A', 'B', 'C', 'D', 'E', 'F' ]; const vertices = ['A', 'B', 'C', 'D', 'E', 'F']
// adding vertices // adding vertices
for (var i = 0; i < vertices.length; i++) { for (let i = 0; i < vertices.length; i++) {
g.addVertex(vertices[i]); graph.addVertex(vertices[i])
} }
// adding edges // adding edges
g.addEdge('A', 'B'); graph.addEdge('A', 'B')
g.addEdge('A', 'D'); graph.addEdge('A', 'D')
g.addEdge('A', 'E'); graph.addEdge('A', 'E')
g.addEdge('B', 'C'); graph.addEdge('B', 'C')
g.addEdge('D', 'E'); graph.addEdge('D', 'E')
g.addEdge('E', 'F'); graph.addEdge('E', 'F')
g.addEdge('E', 'C'); graph.addEdge('E', 'C')
g.addEdge('C', 'F'); graph.addEdge('C', 'F')
// prints all vertex and // prints all vertex and
// its adjacency list // its adjacency list
@ -91,5 +85,4 @@ g.addEdge('C', 'F');
// D -> A E // D -> A E
// E -> A D F C // E -> A D F C
// F -> E C // F -> E C
g.printGraph(); graph.printGraph()