mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
update (#1)
* Graph Theory * Delete Graphs * Graph * Dijkstra Smallest Path * DijkstraSmallestPath after fixing some errors. * Topological Sort directed graphs * correcting name of file * updating DIRECTORY.md * doublylinkedlist * add-doublylinkedlist * add-doublylinkedlist * change in Directory.md * updating DIRECTORY.md Co-authored-by: Nur69 <60115902+Nur69@users.noreply.github.com> Co-authored-by: Stepfen Shawn <m18824909883@163.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: hmizz <hamza.chabchoub@medtech.tn>
This commit is contained in:
@ -22,6 +22,7 @@
|
||||
* Heap
|
||||
* [MinPriorityQueue](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Heap/MinPriorityQueue.js)
|
||||
* Linked List
|
||||
* [DoublyLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Linked%20List/DoublyLinkedList.js)
|
||||
* [singlylinklist](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Linked%20List/singlylinklist.js)
|
||||
* Queue
|
||||
* [Queue](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Queue/Queue.js)
|
||||
@ -43,8 +44,10 @@
|
||||
## maths
|
||||
* [abs](https://github.com/TheAlgorithms/Javascript/blob/master/maths/abs.js)
|
||||
* [average mean](https://github.com/TheAlgorithms/Javascript/blob/master/maths/average_mean.js)
|
||||
* [DijkstraSmallestPath](https://github.com/TheAlgorithms/Javascript/blob/master/maths/DijkstraSmallestPath.js)
|
||||
* [factorial](https://github.com/TheAlgorithms/Javascript/blob/master/maths/factorial.js)
|
||||
* [find lcm](https://github.com/TheAlgorithms/Javascript/blob/master/maths/find_lcm.js)
|
||||
* [graph](https://github.com/TheAlgorithms/Javascript/blob/master/maths/graph.js)
|
||||
|
||||
## Search
|
||||
* [binarySearch](https://github.com/TheAlgorithms/Javascript/blob/master/Search/binarySearch.js)
|
||||
@ -68,4 +71,5 @@
|
||||
* [radixSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/radixSort.js)
|
||||
* [selectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/selectionSort.js)
|
||||
* [shellSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/shellSort.js)
|
||||
* [TopologicalSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TopologicalSort.js)
|
||||
* [wiggleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/wiggleSort.js)
|
||||
|
197
Data Structures/Linked List/DoublyLinkedList.js
Normal file
197
Data Structures/Linked List/DoublyLinkedList.js
Normal file
@ -0,0 +1,197 @@
|
||||
//Hamza chabchoub contribution for a university project
|
||||
function doubleLinkedList() {
|
||||
let Node = function(element) {
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
this.prev = null;
|
||||
}
|
||||
|
||||
let length = 0;
|
||||
let head = null;
|
||||
let tail = null;
|
||||
|
||||
//Add new element
|
||||
this.append = function(element) {
|
||||
let node = new Node(element);
|
||||
|
||||
if(!head){
|
||||
head = node;
|
||||
tail = node;
|
||||
}else{
|
||||
node.prev = tail;
|
||||
tail.next = node;
|
||||
tail = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
}
|
||||
|
||||
|
||||
//Add element
|
||||
this.insert = function(position, element) {
|
||||
|
||||
//Check of out-of-bound values
|
||||
if(position >= 0 && position <= length){
|
||||
let node = new Node(element),
|
||||
current = head,
|
||||
previous,
|
||||
index = 0;
|
||||
|
||||
if(position === 0){
|
||||
if(!head){
|
||||
head = node;
|
||||
tail = node;
|
||||
}else{
|
||||
node.next = current;
|
||||
current.prev = node;
|
||||
head = node;
|
||||
}
|
||||
}else if(position === length){
|
||||
current = tail;
|
||||
current.next = node;
|
||||
node.prev = current;
|
||||
tail = node;
|
||||
}else{
|
||||
while(index++ < position){
|
||||
previous = current;
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
node.next = current;
|
||||
previous.next = node;
|
||||
|
||||
//New
|
||||
current.prev = node;
|
||||
node.prev = previous;
|
||||
}
|
||||
|
||||
length++;
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Remove element at any position
|
||||
this.removeAt = function(position){
|
||||
//look for out-of-bounds value
|
||||
if(position > -1 && position < length){
|
||||
let current = head, previous, index = 0;
|
||||
|
||||
//Removing first item
|
||||
if(position === 0){
|
||||
head = current.next;
|
||||
|
||||
//if there is only one item, update tail //NEW
|
||||
if(length === 1){
|
||||
tail = null;
|
||||
}else{
|
||||
head.prev = null;
|
||||
}
|
||||
}else if(position === length - 1){
|
||||
current = tail;
|
||||
tail = current.prev;
|
||||
tail.next = null;
|
||||
}else{
|
||||
while(index++ < position){
|
||||
previous = current;
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
//link previous with current's next - skip it
|
||||
previous.next = current.next;
|
||||
current.next.prev = previous;
|
||||
}
|
||||
|
||||
length--;
|
||||
return current.element;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//Get the indexOf item
|
||||
this.indexOf = function(elm){
|
||||
let current = head,
|
||||
index = -1;
|
||||
|
||||
//If element found then return its position
|
||||
while(current){
|
||||
if(elm === current.element){
|
||||
return ++index;
|
||||
}
|
||||
|
||||
index++;
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
//Else return -1
|
||||
return -1;
|
||||
};
|
||||
|
||||
//Find the item in the list
|
||||
this.isPresent = (elm) => {
|
||||
return this.indexOf(elm) !== -1;
|
||||
};
|
||||
|
||||
//Delete an item from the list
|
||||
this.delete = (elm) => {
|
||||
return this.removeAt(this.indexOf(elm));
|
||||
};
|
||||
|
||||
//Delete first item from the list
|
||||
this.deleteHead = function(){
|
||||
this.removeAt(0);
|
||||
}
|
||||
|
||||
//Delete last item from the list
|
||||
this.deleteTail = function(){
|
||||
this.removeAt(length-1);
|
||||
}
|
||||
|
||||
//Print item of the string
|
||||
this.toString = function(){
|
||||
let current = head,
|
||||
string = '';
|
||||
|
||||
while(current){
|
||||
string += current.element + (current.next ? '\n' : '');
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
return string;
|
||||
};
|
||||
|
||||
//Convert list to array
|
||||
this.toArray = function(){
|
||||
let arr = [],
|
||||
current = head;
|
||||
|
||||
while(current){
|
||||
arr.push(current.element);
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
return arr;
|
||||
};
|
||||
|
||||
//Check if list is empty
|
||||
this.isEmpty = function(){
|
||||
return length === 0;
|
||||
};
|
||||
|
||||
//Get the size of the list
|
||||
this.size = function(){
|
||||
return length;
|
||||
}
|
||||
|
||||
//Get the head
|
||||
this.getHead = function() {
|
||||
return head;
|
||||
}
|
||||
|
||||
//Get the tail
|
||||
this.getTail = function() {
|
||||
return tail;
|
||||
}
|
||||
}
|
59
Sorts/TopologicalSort.js
Normal file
59
Sorts/TopologicalSort.js
Normal file
@ -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());
|
118
maths/DijkstraSmallestPath.js
Normal file
118
maths/DijkstraSmallestPath.js
Normal file
@ -0,0 +1,118 @@
|
||||
// starting at s
|
||||
function solve(graph, s) {
|
||||
var solutions = {};
|
||||
solutions[s] = [];
|
||||
solutions[s].dist = 0;
|
||||
|
||||
while(true) {
|
||||
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(var a in adj) {
|
||||
|
||||
if(solutions[a])
|
||||
continue;
|
||||
|
||||
var d = adj[a] + ndist;
|
||||
if(d < dist) {
|
||||
|
||||
p = solutions[n];
|
||||
neighbor = a;
|
||||
dist = d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//no more solutions
|
||||
if(dist === Infinity) {
|
||||
break;
|
||||
}
|
||||
|
||||
//extend parent's solution path
|
||||
solutions[neighbor] = p.concat(neighbor);
|
||||
//extend parent's cost
|
||||
solutions[neighbor].dist = dist;
|
||||
}
|
||||
|
||||
return solutions;
|
||||
}
|
||||
//create graph
|
||||
var 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
|
||||
// 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 '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)
|
94
maths/graph.js
Normal file
94
maths/graph.js
Normal file
@ -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();
|
Reference in New Issue
Block a user