From 61c53dcbeaa8161c45ca841cc21ac4f4752bf298 Mon Sep 17 00:00:00 2001 From: Nur69 <60115902+Nur69@users.noreply.github.com> Date: Fri, 17 Apr 2020 22:06:02 +0200 Subject: [PATCH 1/7] Graph Theory --- Graphs | 1 + 1 file changed, 1 insertion(+) create mode 100644 Graphs diff --git a/Graphs b/Graphs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/Graphs @@ -0,0 +1 @@ + From 0713ebf2c8d1d26eed00f44de7e66d0572fa2802 Mon Sep 17 00:00:00 2001 From: Nur69 <60115902+Nur69@users.noreply.github.com> Date: Fri, 17 Apr 2020 22:08:30 +0200 Subject: [PATCH 2/7] Delete Graphs --- Graphs | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Graphs diff --git a/Graphs b/Graphs deleted file mode 100644 index 8b1378917..000000000 --- a/Graphs +++ /dev/null @@ -1 +0,0 @@ - From c98361b13677c6817c49cf616eefa9b4b1825fc7 Mon Sep 17 00:00:00 2001 From: Nur69 <60115902+Nur69@users.noreply.github.com> Date: Fri, 17 Apr 2020 22:10:40 +0200 Subject: [PATCH 3/7] Graph --- maths/graph.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 maths/graph.js diff --git a/maths/graph.js b/maths/graph.js new file mode 100644 index 000000000..45f3908aa --- /dev/null +++ b/maths/graph.js @@ -0,0 +1,94 @@ +// create a graph class +class Graph { + // defining vertex array and + // adjacent list + constructor(noOfVertices) + { + this.noOfVertices = noOfVertices; + this.AdjList = new Map(); + } + + // functions to be implemented + + // addVertex(v) + // addEdge(v, w) + // printGraph() + + // bfs(v) + // dfs(v) +} + +// add vertex to the graph +addVertex(v) +{ + // initialize the adjacent list with a + // null array + this.AdjList.set(v, []); +} + +// add edge to the graph +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); + + // Since graph is undirected, + // add an edge from w to v also + this.AdjList.get(w).push(v); +} + + +// Prints the vertex and adjacency list +printGraph() +{ + // get all the vertices + var get_keys = this.AdjList.keys(); + + // iterate over the vertices + for (var i of get_keys) +{ + // great the corresponding adjacency list + // for the vertex + var get_values = this.AdjList.get(i); + var conc = ""; + + // iterate over the adjacency list + // concatenate the values into a string + for (var j of get_values) + conc += j + " "; + + // print the vertex and its adjacency list + console.log(i + " -> " + conc); + } +} + + +// Example +var graph = new Graph(6); +var vertices = [ 'A', 'B', 'C', 'D', 'E', 'F' ]; + +// adding vertices +for (var i = 0; i < vertices.length; i++) { + g.addVertex(vertices[i]); +} + +// adding edges +g.addEdge('A', 'B'); +g.addEdge('A', 'D'); +g.addEdge('A', 'E'); +g.addEdge('B', 'C'); +g.addEdge('D', 'E'); +g.addEdge('E', 'F'); +g.addEdge('E', 'C'); +g.addEdge('C', 'F'); + +// prints all vertex and +// its adjacency list +// A -> B D E +// B -> A C +// C -> B E F +// D -> A E +// E -> A D F C +// F -> E C +g.printGraph(); From cdab9f9a3c63fdd130e8c246ca0c3278a16f5a9c Mon Sep 17 00:00:00 2001 From: Nur69 <60115902+Nur69@users.noreply.github.com> Date: Fri, 17 Apr 2020 22:24:08 +0200 Subject: [PATCH 4/7] Dijkstra Smallest Path --- maths/DijkstraSmallestPath.js | 102 ++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 maths/DijkstraSmallestPath.js diff --git a/maths/DijkstraSmallestPath.js b/maths/DijkstraSmallestPath.js new file mode 100644 index 000000000..9afc6c1c6 --- /dev/null +++ b/maths/DijkstraSmallestPath.js @@ -0,0 +1,102 @@ +function solve(graph, s) { + var solution = {}; + solutions[s] = []; + solutions[s].dist = 0; + + while(true) { + var parent = null; + var nearest = null; + var dist = Infinity; + + + for(var n in solutions) { + if(!solutions[n]) + continue + var ndist = solutions[n].dist; + var adj = graph[n]; + //for each of its adjacent nodes... + for(var a in adj) { + //without a solution already... + if(solutions[a]) + continue; + //choose nearest node with lowest *total* cost + var d = adj[a] + ndist; + if(d < dist) { + //reference parent + parent = solutions[n]; + nearest = a; + dist = d; + } + } + } + + + if(dist === Infinity) { + break; + } + + solutions[nearest] = parent.concat(nearest); + + solutions[nearest].dist = dist; + } + + return solutions; +} + +var graph = {}; + +\\create graph + +var 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 +// needs to look like: where: { a: { b: cost of a->b } +// var graph = { +// a: {e:1, b:1, g:3}, +// b: {a:1, c:1}, +// c: {b:1, d:1}, +// d: {c:1, e:1}, +// e: {d:1, a:1}, +// f: {g:1, h:1}, +// g: {a:3, f:1}, +// h: {f:1} +// }; + +for(var 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 +var start = '10'; +//get all solutions +var solutions = solve(graph, start); + +console.log("From '"+start+"' to"); +//display solutions +for(var s in solutions) { + if(!solutions[s]) continue; + console.log(" -> " + s + ": [" + solutions[s].join(", ") + "] (dist:" + solutions[s].dist + ")"); +} From 1b3b125723547b567becbfa0fbdd0deaefb7bb7d Mon Sep 17 00:00:00 2001 From: Nur69 <60115902+Nur69@users.noreply.github.com> Date: Sun, 19 Apr 2020 17:01:26 +0200 Subject: [PATCH 5/7] DijkstraSmallestPath after fixing some errors. --- maths/DijkstraSmallestPath.js | 54 +++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/maths/DijkstraSmallestPath.js b/maths/DijkstraSmallestPath.js index 9afc6c1c6..059b59461 100644 --- a/maths/DijkstraSmallestPath.js +++ b/maths/DijkstraSmallestPath.js @@ -1,52 +1,52 @@ +// starting at s function solve(graph, s) { - var solution = {}; + var solutions = {}; solutions[s] = []; solutions[s].dist = 0; while(true) { - var parent = null; - var nearest = null; + var p = null; + var neighbor = null; var dist = Infinity; - + for(var n in solutions) { if(!solutions[n]) continue var ndist = solutions[n].dist; var adj = graph[n]; - //for each of its adjacent nodes... + for(var a in adj) { - //without a solution already... + if(solutions[a]) continue; - //choose nearest node with lowest *total* cost + var d = adj[a] + ndist; if(d < dist) { - //reference parent - parent = solutions[n]; - nearest = a; + + p = solutions[n]; + neighbor = a; dist = d; } } } - + //no more solutions if(dist === Infinity) { break; } - - solutions[nearest] = parent.concat(nearest); - - solutions[nearest].dist = dist; + + //extend parent's solution path + solutions[neighbor] = p.concat(neighbor); + //extend parent's cost + solutions[neighbor].dist = dist; } return solutions; } - +//create graph var graph = {}; -\\create graph - var layout = { 'R': ['2'], '2': ['3','4'], @@ -66,7 +66,6 @@ var layout = { } //convert uni-directional to bi-directional graph -// needs to look like: where: { a: { b: cost of a->b } // var graph = { // a: {e:1, b:1, g:3}, // b: {a:1, c:1}, @@ -100,3 +99,20 @@ for(var s in solutions) { if(!solutions[s]) continue; console.log(" -> " + s + ": [" + solutions[s].join(", ") + "] (dist:" + solutions[s].dist + ")"); } + +// From '10' to +// -> 2: [7, 5, 4, 2] (dist:4) +// -> 3: [7, 5, 4, 3] (dist:4) +// -> 4: [7, 5, 4] (dist:3) +// -> 5: [7, 5] (dist:2) +// -> 6: [7, 5, 4, 3, 6] (dist:5) +// -> 7: [7] (dist:1) +// -> 8: [7, 5, 4, 8] (dist:4) +// -> 9: [7, 5, 4, 3, 13, 14, 9] (dist:7) +// -> 10: [] (dist:0) +// -> 11: [7, 5, 11] (dist:3) +// -> 12: [7, 5, 11, 12] (dist:4) +// -> 13: [7, 5, 4, 3, 13] (dist:5) +// -> 14: [7, 5, 4, 3, 13, 14] (dist:6) +// -> 15: [7, 5, 4, 3, 6, 15] (dist:6) +// -> R: [7, 5, 4, 2, R] (dist:5) From 9fe41bb219af87601512edf756489977c1e29c86 Mon Sep 17 00:00:00 2001 From: Nur69 <60115902+Nur69@users.noreply.github.com> Date: Sun, 19 Apr 2020 17:10:25 +0200 Subject: [PATCH 6/7] Topological Sort directed graphs --- Sorts/TopologicalSort,js | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Sorts/TopologicalSort,js diff --git a/Sorts/TopologicalSort,js b/Sorts/TopologicalSort,js new file mode 100644 index 000000000..e76bfd666 --- /dev/null +++ b/Sorts/TopologicalSort,js @@ -0,0 +1,59 @@ +function TopologicalSorter() { + var graph = {}, + isVisitedNode, + finishTimeCount, + finishingTimeList, + nextNode; + + this.addOrder = function (nodeA, nodeB) { + nodeA = String(nodeA); + nodeB = String(nodeB); + graph[nodeA] = graph[nodeA] || []; + graph[nodeA].push(nodeB); + } + + this.sortAndGetOrderedItems = function () { + isVisitedNode = Object.create(null); + finishTimeCount = 0; + finishingTimeList = []; + + for (var node in graph) { + if (graph.hasOwnProperty(node) && !isVisitedNode[node]) { + dfsTraverse(node); + } + } + + finishingTimeList.sort(function (item1, item2) { + return item1.finishTime > item2.finishTime ? -1 : 1; + }); + + return finishingTimeList.map(function (value) { return value.node }) + } + + function dfsTraverse(node) { + isVisitedNode[node] = true; + if (graph[node]) { + for (var i = 0; i < graph[node].length; i++) { + nextNode = graph[node][i]; + if (isVisitedNode[nextNode]) continue; + dfsTraverse(nextNode); + } + } + + finishingTimeList.push({ + node: node, + finishTime: ++finishTimeCount + }); + } +} + + +/* TEST */ +var topoSorter = new TopologicalSorter(); +topoSorter.addOrder(5, 2); +topoSorter.addOrder(5, 0); +topoSorter.addOrder(4, 0); +topoSorter.addOrder(4, 1); +topoSorter.addOrder(2, 3); +topoSorter.addOrder(3, 1); +console.log(topoSorter.sortAndGetOrderedItems()); From 0b411b93c036ac159384bbe49c23af8ef3cf144d Mon Sep 17 00:00:00 2001 From: Nur69 <60115902+Nur69@users.noreply.github.com> Date: Tue, 21 Apr 2020 10:18:32 +0200 Subject: [PATCH 7/7] correcting name of file --- Sorts/{TopologicalSort,js => TopologicalSort.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Sorts/{TopologicalSort,js => TopologicalSort.js} (100%) diff --git a/Sorts/TopologicalSort,js b/Sorts/TopologicalSort.js similarity index 100% rename from Sorts/TopologicalSort,js rename to Sorts/TopologicalSort.js