Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e2f6af8

Browse files
Removed all redudant functions from Algorithms
1 parent 3ec43b9 commit e2f6af8

File tree

9 files changed

+92
-221
lines changed

9 files changed

+92
-221
lines changed

‎src/Algorithms/Asearch.js

Lines changed: 43 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,81 @@
1-
import {PriorityQueue} from '../DataStructures/PriorityQueue';
2-
exportfunctionaSearch(grid,start,finish){
1+
import {PriorityQueue} from '../DataStructures/PriorityQueue';
2+
import{getAllNodes,getUnvisitedNeighbours}from'./Dijkstra';
33

4-
let op = [];
5-
let cl = [];
4+
export function aSearch(grid, start, finish) {
5+
6+
let open = [];
7+
let close = [];
68
const allnodes = getAllNodes(grid);
79

8-
let open = new PriorityQueue();
9-
start.aEndDis = calDis(finish,start)
10+
let pqueue = new PriorityQueue();
11+
start.aEndDis = calculateDistance(finish,start)
1012

11-
open.enqueue(start,start.aEndDis+start.cost);
12-
op.push(start);
13+
pqueue.enqueue(start,start.aEndDis+start.cost);
14+
open.push(start);
1315

1416
let closed = new PriorityQueue();
1517
let visitedNodesInOrder = []
1618

19+
while (pqueue.size() > 0) {
1720

21+
pqueue.print()
1822

19-
while(open.size()>0){
20-
21-
open.print()
22-
23-
const curObject = open.dequeue();
23+
const curObject = pqueue.dequeue();
2424
const current = curObject.element;
25-
op = removeElement(op,current);
25+
open = removeElement(open,current);
2626

27-
current.isVisited= true
27+
current.isVisited= true
2828

29-
closed.enqueue(current,current.aEndDis+current.cost);
30-
cl.push(current)
29+
closed.enqueue(current,current.aEndDis+current.cost);
30+
close.push(current)
3131
closed.print()
32-
const neigh = getUnvisitedNeighboursBfs(current,grid);
32+
const neigh = getUnvisitedNeighbours(current,grid);
3333

34-
let short=null
35-
let index=0
34+
let short=null
35+
let index=0
3636

37-
for(let i=0;i<neigh.length;i++){
38-
let s=0,d=0
37+
for (let i = 0; i < neigh.length; i++) {
38+
let s = 0,
39+
d = 0
40+
41+
s = calculateDistance(start, neigh[i])
42+
d = calculateDistance(finish, neigh[i])
3943

40-
s= calDis(start,neigh[i])
41-
d= calDis(finish,neigh[i])
42-
4344

4445
neigh[i].distance = s
45-
neigh[i].aDis = neigh[i].distance+d;
46-
neigh[i].aEndDis = d;
46+
neigh[i].aDis = neigh[i].distance+d;
47+
neigh[i].aEndDis = d;
4748

48-
if(neigh[i] in cl){
49+
if(neigh[i] in close){
4950
continue;
5051
}
5152

52-
if(!(neigh[i] in op)){
53-
open.enqueue(neigh[i],neigh[i].aEndDis+neigh[i].cost);
54-
op.push(neigh[i])
53+
if(!(neigh[i] in open)){
54+
pqueue.enqueue(neigh[i],neigh[i].aEndDis+neigh[i].cost);
55+
open.push(neigh[i])
5556
neigh[i].previousNode = current
5657

5758
}
58-
59-
6059
}
6160

61+
current.isVisited = true;
6262

63-
current.isVisited = true;
64-
65-
visitedNodesInOrder.push(short);
66-
67-
if (current === finish) {
68-
69-
return cl;
70-
}
63+
visitedNodesInOrder.push(short);
7164

65+
if (current === finish) {
66+
return close;
67+
}
7268
}
7369

74-
7570
}
7671

77-
function calDis(start,node){
78-
let dis= Math.abs(start.row - node.row) + Math.abs(start.col - node.col)
72+
function calculateDistance(start,node){
73+
let dis= Math.abs(start.row - node.row) + Math.abs(start.col - node.col)
7974
return dis
8075
}
8176

82-
function removeElement(ar,ele){
83-
return ar.filter(function(val){
84-
return val!=ele;
77+
function removeElement(ar,ele){
78+
return ar.filter(function(val){
79+
return val!=ele;
8580
})
86-
}
87-
88-
function updateUnvisitedNeighbors(node, grid) {
89-
/** Updates the distances of unvisited nodes */
90-
const unvisitedNeighbors = getUnvisitedNeighboursBfs(node, grid);
91-
for (const neighbor of unvisitedNeighbors) {
92-
neighbor.previousNode = node;
93-
}
94-
}
95-
96-
function getUnvisitedNeighboursBfs(node, grid) {
97-
/** Returns all univisted neighbours of given node*/
98-
const neighbors = [];
99-
const col = node.col;
100-
const row = node.row;
101-
if (row > 0) neighbors.push(grid[row - 1][col]);
102-
if (row < grid.length - 1) neighbors.push(grid[row + 1][col]);
103-
if (col > 0) neighbors.push(grid[row][col - 1]);
104-
if (col < grid[0].length - 1) neighbors.push(grid[row][col + 1]);
105-
return neighbors.filter(neighbor => !neighbor.isVisited);
106-
}
107-
108-
function getAllNodes(grid) {
109-
/** Returns all nodes in grid*/
110-
const nodes = [];
111-
for (const row of grid) {
112-
for (const node of row) {
113-
node.distance=0;
114-
nodes.push(node);
115-
}
116-
}
117-
return nodes;
118-
}
119-
81+
}

‎src/Algorithms/Dijkstra.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ export function dijkstra (grid, startNode, finishNode) {
3232
}
3333
}
3434

35-
function sortNodesByDistance(unvisitedNodes) {
35+
exportfunction sortNodesByDistance(unvisitedNodes) {
3636
/** Sort's the unvisited nodes by their relative distance from current node*/
3737
unvisitedNodes.sort((nodeA, nodeB) => (nodeA.distance - nodeB.distance)+(nodeA.cost - nodeB.cost));
3838
}
3939

4040

41-
function updateUnvisitedNeighbors(node, grid) {
41+
exportfunction updateUnvisitedNeighbors(node, grid) {
4242
/** Updates the distances of unvisited nodes */
4343
const unvisitedNeighbors = getUnvisitedNeighbours(node, grid);
4444
for (const neighbor of unvisitedNeighbors) {

‎src/Algorithms/bfs.js

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {getAllNodes} from './Dijkstra';
2-
import {getUnvisitedNeighbours} from './Dijkstra';
1+
import { getAllNodes, getUnvisitedNeighbours, updateUnvisitedNeighbors} from './Dijkstra';
32

43
export function bfs (grid,start,finish) {
54

@@ -17,7 +16,7 @@ export function bfs (grid,start,finish) {
1716
while(!!allnodes.length){
1817

1918
const current = queue.shift();
20-
const neigh = getUnvisitedNeighboursBfs(current,grid);
19+
const neigh = getUnvisitedNeighbours(current,grid);
2120
short=[]
2221

2322
updateUnvisitedNeighbors(current,grid);
@@ -35,26 +34,4 @@ export function bfs (grid,start,finish) {
3534
}
3635

3736

38-
}
39-
40-
function updateUnvisitedNeighbors(node, grid) {
41-
/** Updates the distances of unvisited nodes */
42-
const unvisitedNeighbors = getUnvisitedNeighboursBfs(node, grid);
43-
for (const neighbor of unvisitedNeighbors) {
44-
neighbor.previousNode = node;
45-
}
46-
}
47-
48-
function getUnvisitedNeighboursBfs(node, grid) {
49-
/** Returns all univisted neighbours of given node*/
50-
const neighbors = [];
51-
const col = node.col;
52-
const row = node.row;
53-
if (row > 0) neighbors.push(grid[row - 1][col]);
54-
if (row < grid.length - 1) neighbors.push(grid[row + 1][col]);
55-
if (col > 0) neighbors.push(grid[row][col - 1]);
56-
if (col < grid[0].length - 1) neighbors.push(grid[row][col + 1]);
57-
return neighbors.filter(neighbor => !neighbor.isVisited);
58-
}
59-
60-
37+
}

‎src/Algorithms/dfs.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getAllNodes} from './Dijkstra';
1+
import {getAllNodes,getUnvisitedNeighbours,updateUnvisitedNeighbors} from './Dijkstra';
22

33

44
export function dfs(grid, start, finish) {
@@ -29,23 +29,3 @@ export function dfs(grid, start, finish) {
2929
});
3030
}
3131
}
32-
33-
function updateUnvisitedNeighbors(node, grid) {
34-
/** Updates the distances of unvisited nodes */
35-
const unvisitedNeighbors = getUnvisitedNeighbours(node, grid);
36-
for (const neighbor of unvisitedNeighbors) {
37-
neighbor.previousNode = node;
38-
}
39-
}
40-
41-
function getUnvisitedNeighbours(node, grid) {
42-
/** Returns all univisted neighbours of given node*/
43-
const neighbors = [];
44-
const col = node.col;
45-
const row = node.row;
46-
if (row > 0) neighbors.push(grid[row - 1][col]);
47-
if (row < grid.length - 1) neighbors.push(grid[row + 1][col]);
48-
if (col > 0) neighbors.push(grid[row][col - 1]);
49-
if (col < grid[0].length - 1) neighbors.push(grid[row][col + 1]);
50-
return neighbors.filter(neighbor => !neighbor.isVisited);
51-
}

‎src/Algorithms/greedybfs.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getAllNodes} from './Dijkstra';
1+
import {getAllNodes,getUnvisitedNeighbours,sortNodesByDistance} from './Dijkstra';
22

33
export function greedyBFS(grid, start, finish) {
44
/** Greedy best first search algorithm*/
@@ -29,24 +29,7 @@ export function greedyBFS(grid, start, finish) {
2929
}
3030
}
3131

32-
function sortNodesByDistance(pqueue) {
33-
/** Sort's the priority queue nodes by their relative distance from finish node*/
34-
pqueue.sort((a, b) => a[1] - b[1]);
35-
}
36-
3732
function getManhattanDistance(currNode, finishNode) {
3833
/** Returns the Manhattan Distance between two points*/
3934
return Math.abs(currNode.row - finishNode.row) + Math.abs(currNode.col - finishNode.col)+ Math.abs(currNode.cost - finishNode.cost)
40-
}
41-
42-
function getUnvisitedNeighbours(node, grid) {
43-
/** Returns all univisted neighbours of given node*/
44-
const neighbors = [];
45-
const col = node.col;
46-
const row = node.row;
47-
if (row > 0) neighbors.push(grid[row - 1][col]);
48-
if (row < grid.length - 1) neighbors.push(grid[row + 1][col]);
49-
if (col > 0) neighbors.push(grid[row][col - 1]);
50-
if (col < grid[0].length - 1) neighbors.push(grid[row][col + 1]);
51-
return neighbors.filter(neighbor => !neighbor.isVisited);
52-
}
35+
}

‎src/App.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ import GridLayout from './Components/GridLayoutComponent';
66
function App () {
77
return (
88
<div className="App">
9-
{/*
10-
<button onClick={this.setState({
11-
k: this.state.k+1
12-
})} > Clear Grid </button> */}
13-
14-
<GridLayout ></GridLayout>
15-
9+
<GridLayout></GridLayout>
1610
</div>
1711
);
1812

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /