Merge branch 'master' into master

This commit is contained in:
vinayak
2020-05-05 17:05:09 +05:30
committed by GitHub
3 changed files with 88 additions and 95 deletions

View File

@ -48,6 +48,6 @@ function calcFactorial (num) {
} }
// Run `factorial` Function to find average of a list of numbers. // Run `factorial` Function to find average of a list of numbers.
/* global prompt */
var num = prompt('Enter a number: ') var num = console.log('Enter a number: ')
console.log(calcFactorial(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

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()