From 62b6d62c5bd8971df2501cc6170fa558b8dc7939 Mon Sep 17 00:00:00 2001 From: ZainabMalik5 <95160741+ZainabMalik5@users.noreply.github.com> Date: Thu, 5 Oct 2023 14:39:53 +0530 Subject: [PATCH 1/5] Add files via upload --- Maths/Divisors.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Maths/Divisors.js diff --git a/Maths/Divisors.js b/Maths/Divisors.js new file mode 100644 index 0000000000..d282898bbc --- /dev/null +++ b/Maths/Divisors.js @@ -0,0 +1,24 @@ +/** + * @function getDivisors + * @description Generate all unique divisors of a number. + * @param {Integer} num + * @return{Vector} divisors + * @example{10-1,2,5,10} + */ +function getDivisors(num) { + const divisors = []; + for (let i = 1; i * i <= num; i++) { + if (num% i === 0) { + if (num / i !== i) { + divisors.push(i); + const div = num/ i; + divisors.push(div); + } else { + divisors.push(i); + } + } + } + divisors.sort((a, b) => a - b); + return divisors; + } + \ No newline at end of file From 2128786e32f99a37f2440988b8ba9909e7cbc586 Mon Sep 17 00:00:00 2001 From: ZainabMalik5 <95160741+ZainabMalik5@users.noreply.github.com> Date: Sun, 8 Oct 2023 14:15:43 +0530 Subject: [PATCH 2/5] Delete Maths/Divisors.js --- Maths/Divisors.js | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 Maths/Divisors.js diff --git a/Maths/Divisors.js b/Maths/Divisors.js deleted file mode 100644 index d282898bbc..0000000000 --- a/Maths/Divisors.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * @function getDivisors - * @description Generate all unique divisors of a number. - * @param {Integer} num - * @return{Vector} divisors - * @example{10-1,2,5,10} - */ -function getDivisors(num) { - const divisors = []; - for (let i = 1; i * i <= num; i++) { - if (num% i === 0) { - if (num / i !== i) { - divisors.push(i); - const div = num/ i; - divisors.push(div); - } else { - divisors.push(i); - } - } - } - divisors.sort((a, b) => a - b); - return divisors; - } - \ No newline at end of file From a9a06ce81090df9257e09a42a957dd012b967cfb Mon Sep 17 00:00:00 2001 From: ZainabMalik5 <95160741+ZainabMalik5@users.noreply.github.com> Date: Sun, 8 Oct 2023 14:18:18 +0530 Subject: [PATCH 3/5] Add files via upload --- Graphs/cycle.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Graphs/cycle.js diff --git a/Graphs/cycle.js b/Graphs/cycle.js new file mode 100644 index 0000000000..6cde9fa4e3 --- /dev/null +++ b/Graphs/cycle.js @@ -0,0 +1,56 @@ +class Graph { + constructor(vertices) { + this.vertices = vertices; + this.adjList = new Array(vertices); + for (let i = 0; i < vertices; i++) { + this.adjList[i] = []; + } + } + + addEdge(v, w) { + this.adjList[v].push(w); + this.adjList[w].push(v);//Creating Adjacency list. + } + + isCyclic() { + const visited = new Array(this.vertices).fill(false); + + for (let i = 0; i < this.vertices; i++) { + if (!visited[i]) { + if (this.isCyclicUtil(i, -1, visited)) {//Checking if graph contains a cycle. + return true; + } + } + } + + return false; + } + + isCyclicUtil(v, parent, visited) { + visited[v] = true; + + for (const neighbor of this.adjList[v]) { + if (!visited[neighbor]) { + if (this.isCyclicUtil(neighbor, v, visited)) { + return true; + } + } else if (neighbor !== parent) { + return true; + } + } + + return false; + } + } + + /*const graph = new Graph(4); + graph.addEdge(0, 1); + graph.addEdge(1, 2); + graph.addEdge(2, 3); + + if (graph.isCyclic()) { + console.log("The graph contains a cycle."); + } else { + console.log("The graph does not contain a cycle."); + } + **/ \ No newline at end of file From 7eb2142385c02078dec0be78b5a137a330cb7fdd Mon Sep 17 00:00:00 2001 From: ZainabMalik5 <95160741+ZainabMalik5@users.noreply.github.com> Date: Sun, 8 Oct 2023 14:29:08 +0530 Subject: [PATCH 4/5] Add files via upload --- Graphs/kahns_algorithm.js | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Graphs/kahns_algorithm.js diff --git a/Graphs/kahns_algorithm.js b/Graphs/kahns_algorithm.js new file mode 100644 index 0000000000..c2caffa05b --- /dev/null +++ b/Graphs/kahns_algorithm.js @@ -0,0 +1,64 @@ +class Graph { + constructor(vertices) { + this.vertices = vertices; + this.adjList = new Array(vertices); + this.inDegree = new Array(vertices).fill(0); + + for (let i = 0; i < vertices; i++) { + this.adjList[i] = []; + } + } + + addEdge(v, w) { + this.adjList[v].push(w); + this.inDegree[w]++;//Creating adjacency list + } + + topologicalSort() { + const result = []; + const queue = []; + + for (let i = 0; i < this.vertices; i++) { + if (this.inDegree[i] === 0) { + queue.push(i); + } + } + + while (queue.length > 0) { + const vertex = queue.shift(); + result.push(vertex);//Topological Sorting + + for (const neighbor of this.adjList[vertex]) { + this.inDegree[neighbor]--; + if (this.inDegree[neighbor] === 0) { + queue.push(neighbor); + } + } + } + + if (result.length !== this.vertices) { + // The graph has a cycle + return null; + } + + return result; + } + } + + + /*const graph = new Graph(6); + graph.addEdge(5, 2); + graph.addEdge(5, 0); + graph.addEdge(4, 0); + graph.addEdge(4, 1); + graph.addEdge(2, 3); + graph.addEdge(3, 1); + + const topologicalOrder = graph.topologicalSort(); + + if (topologicalOrder) { + console.log("Topological Ordering:", topologicalOrder); + } else { + console.log("The graph contains a cycle."); + } + **/ \ No newline at end of file From d6df1d78a4f3cb4578e0e38604d0167be1036b1d Mon Sep 17 00:00:00 2001 From: ZainabMalik5 <95160741+ZainabMalik5@users.noreply.github.com> Date: Sun, 8 Oct 2023 14:30:17 +0530 Subject: [PATCH 5/5] Delete Graphs/kahns_algorithm.js --- Graphs/kahns_algorithm.js | 64 --------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 Graphs/kahns_algorithm.js diff --git a/Graphs/kahns_algorithm.js b/Graphs/kahns_algorithm.js deleted file mode 100644 index c2caffa05b..0000000000 --- a/Graphs/kahns_algorithm.js +++ /dev/null @@ -1,64 +0,0 @@ -class Graph { - constructor(vertices) { - this.vertices = vertices; - this.adjList = new Array(vertices); - this.inDegree = new Array(vertices).fill(0); - - for (let i = 0; i < vertices; i++) { - this.adjList[i] = []; - } - } - - addEdge(v, w) { - this.adjList[v].push(w); - this.inDegree[w]++;//Creating adjacency list - } - - topologicalSort() { - const result = []; - const queue = []; - - for (let i = 0; i < this.vertices; i++) { - if (this.inDegree[i] === 0) { - queue.push(i); - } - } - - while (queue.length > 0) { - const vertex = queue.shift(); - result.push(vertex);//Topological Sorting - - for (const neighbor of this.adjList[vertex]) { - this.inDegree[neighbor]--; - if (this.inDegree[neighbor] === 0) { - queue.push(neighbor); - } - } - } - - if (result.length !== this.vertices) { - // The graph has a cycle - return null; - } - - return result; - } - } - - - /*const graph = new Graph(6); - graph.addEdge(5, 2); - graph.addEdge(5, 0); - graph.addEdge(4, 0); - graph.addEdge(4, 1); - graph.addEdge(2, 3); - graph.addEdge(3, 1); - - const topologicalOrder = graph.topologicalSort(); - - if (topologicalOrder) { - console.log("Topological Ordering:", topologicalOrder); - } else { - console.log("The graph contains a cycle."); - } - **/ \ No newline at end of file