-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added breadth first search algorithm
- Loading branch information
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
$(document).ready(function() { | ||
var url = window.location.href; | ||
url = new URL(url); | ||
var algo = url.searchParams.get("algo"); | ||
if (algo == "bfs") { | ||
$("#start").click(function() { | ||
currentSelection = ""; | ||
if (currentSource != currentDestination && currentRun == 0) { | ||
//Run bfs | ||
//Find the current Source Node | ||
var node = findNode(currentSource); | ||
var needVisit = new Array(); | ||
|
||
node.edges.forEach(edge => { | ||
if (findNode(edge).valid == true) { | ||
needVisit.push(edge); | ||
} | ||
}); | ||
updateVisit(currentSource); | ||
|
||
var found = false; | ||
var visitOrder = new Array(); | ||
var newNeedVisit = new Array(); | ||
while (!found && needVisit.length > 0) { | ||
var current = needVisit.pop(); | ||
//visualizeDfsNode(current); | ||
visitOrder.push(current); | ||
updateVisit(current); | ||
if (sameNode(current, currentDestination)) { | ||
found = true; | ||
} | ||
if (hasUnvisitedChildNodes(current)) { | ||
findNode(current).edges.forEach(edge => { | ||
if (findNode(edge).valid && findNode(edge).visited == false) { | ||
newNeedVisit.push(edge); | ||
} | ||
}); | ||
} | ||
if (needVisit.length == 0) { | ||
newNeedVisit = [...new Set(newNeedVisit)]; | ||
newNeedVisit.forEach(edge => { | ||
needVisit.push(edge); | ||
}); | ||
newNeedVisit = new Array(); | ||
} | ||
} | ||
currentRun += 1; | ||
visualizeDfsNode(visitOrder, found); | ||
$("#source").attr("draggable", "false"); | ||
$("#destination").attr("draggable", "false"); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
var visualizeDfsNode = async function(nodeList, found) { | ||
for (var i = 0; i < nodeList.length; i++) { | ||
await updateDfsNodeColor(nodeList[i]); | ||
} | ||
if (!found) { | ||
alert("Destination Not Found"); | ||
} | ||
}; | ||
|
||
var updateDfsNodeColor = function(node) { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
//if skyblue or deep skyblue | ||
if ( | ||
findElement(node).css("background-color") == "rgb(135, 206, 235)" || | ||
findElement(node).css("background-color") == "rgb(0, 191, 255)" | ||
) { | ||
findElement(node).css("background-color", "deepskyblue"); | ||
} else { | ||
findElement(node).css("background-color", "skyblue"); | ||
} | ||
resolve("done"); | ||
}, 5); | ||
}); | ||
}; | ||
|
||
var hasUnvisitedChildNodes = function(node) { | ||
var count = 0; | ||
findNode(node).edges.forEach(edge => { | ||
if (findNode(edge).valid && findNode(edge).visited == false) { | ||
count += 1; | ||
} | ||
}); | ||
if (count > 0) { | ||
return true; | ||
} | ||
return false; | ||
}; |