diff --git a/3-Solving-Problems-By-Searching/c_breadthFirstSearch.js b/3-Solving-Problems-By-Searching/c_breadthFirstSearch.js index 2ca3968..15b5e78 100644 --- a/3-Solving-Problems-By-Searching/c_breadthFirstSearch.js +++ b/3-Solving-Problems-By-Searching/c_breadthFirstSearch.js @@ -1,38 +1,48 @@ $(document).ready(function() { var w = 600, h = 350; - var DELAY = 2000; var intervalFunction = null; function init() { - clearInterval(intervalFunction, DELAY); - var graph = new DefaultGraph(); - var graphProblem = new GraphProblem(graph.nodes, graph.edges, 'A', 'A'); - var graphAgent = new GraphAgent(graphProblem); var options = new DefaultOptions(); options.nodes.next.fill = 'hsla(126, 100%, 69%, 1)'; - var graphDrawAgent = new GraphDrawAgent(graphProblem, 'breadthFirstSearchCanvas', options, h, w); - var queueDrawAgent = new QueueDrawAgent('fifoQueueCanvas', h, w, graphProblem, options); - //Update handler is the function that would be executed every DELAY ms. - var updateFunction = function() { - if (graphProblem.frontier.length > 0) { - var nextNode = breadthFirstSearch(graphProblem); - graphAgent.expand(nextNode); - //If frontier is still present, find the next node to be expanded so it - //could be colored differently + + var drawState = function(n) { + var graph = new DefaultGraph(); + var graphProblem = new GraphProblem(graph.nodes, graph.edges, 'A', 'A'); + var graphAgent = new GraphAgent(graphProblem); + + var graphDrawAgent = new GraphDrawAgent(graphProblem, 'breadthFirstSearchCanvas', options, h, w); + var queueDrawAgent = new QueueDrawAgent('fifoQueueCanvas', h, w, graphProblem, options); + + while (n--) { if (graphProblem.frontier.length > 0) { - graphProblem.nextToExpand = breadthFirstSearch(graphProblem); + var nextNode = breadthFirstSearch(graphProblem); + graphAgent.expand(nextNode); + //If frontier is still present, find the next node to be expanded so it + //could be colored differently + if (graphProblem.frontier.length > 0) { + graphProblem.nextToExpand = breadthFirstSearch(graphProblem); + } else { + graphProblem.nextToExpand = null; + } } else { - graphProblem.nextToExpand = null; + break; } - graphDrawAgent.iterate(); - queueDrawAgent.iterate(); - } else { - clearInterval(intervalFunction, DELAY); } + graphDrawAgent.iterate(); + queueDrawAgent.iterate(); } - intervalFunction = setInterval(updateFunction, DELAY); + + let ac = new AnimationController({ + selector: '#bfsAC', + min: 0, + max: 15, + renderer: drawState + }); + ac.renderFirst(); }; + $('#bfsRestartButton').click(init); $('#fifoWaiting').css('background-color', 'hsl(0,50%,75%)'); $('#fifoNextNode').css('background-color', 'hsl(126, 100%, 69%)'); diff --git a/3-Solving-Problems-By-Searching/c_depthFirstSearch.js b/3-Solving-Problems-By-Searching/c_depthFirstSearch.js index 858bf3d..d50dc81 100644 --- a/3-Solving-Problems-By-Searching/c_depthFirstSearch.js +++ b/3-Solving-Problems-By-Searching/c_depthFirstSearch.js @@ -5,32 +5,43 @@ $(document).ready(function() { var intervalFunction = null; function init() { - clearInterval(intervalFunction, DELAY); - var graph = new DefaultGraph(); - var graphProblem = new GraphProblem(graph.nodes, graph.edges, 'A', 'A'); - var graphAgent = new GraphAgent(graphProblem); var options = new DefaultOptions(); options.nodes.next.fill = 'hsla(126, 100%, 69%, 1)'; - var graphDrawAgent = new GraphDrawAgent(graphProblem, 'depthFirstSearchCanvas', options, h, w); - var queueDrawAgent = new QueueDrawAgent('lifoQueueCanvas', h, w, graphProblem, options); - var updateFunction = function() { - if (graphProblem.frontier.length > 0) { - var nextNode = depthFirstSearch(graphProblem); - graphAgent.expand(nextNode); - //If frontier is still present, find the next node to be expanded so it - //could be colored differently + + var drawState = function(n) { + var graph = new DefaultGraph(); + var graphProblem = new GraphProblem(graph.nodes, graph.edges, 'A', 'A'); + var graphAgent = new GraphAgent(graphProblem); + + var graphDrawAgent = new GraphDrawAgent(graphProblem, 'depthFirstSearchCanvas', options, h, w); + var queueDrawAgent = new QueueDrawAgent('lifoQueueCanvas', h, w, graphProblem, options); + + while (n--) { if (graphProblem.frontier.length > 0) { - graphProblem.nextToExpand = depthFirstSearch(graphProblem); + var nextNode = depthFirstSearch(graphProblem); + graphAgent.expand(nextNode); + //If frontier is still present, find the next node to be expanded so it + //could be colored differently + if (graphProblem.frontier.length > 0) { + graphProblem.nextToExpand = depthFirstSearch(graphProblem); + } else { + graphProblem.nextToExpand = null; + } } else { - graphProblem.nextToExpand = null; + break; } - graphDrawAgent.iterate(); - queueDrawAgent.iterate(); - } else { - clearInterval(intervalFunction, DELAY); } + graphDrawAgent.iterate(); + queueDrawAgent.iterate(); } - intervalFunction = setInterval(updateFunction, DELAY); + + let ac = new AnimationController({ + selector: '#dfsAC', + min: 0, + max: 15, + renderer: drawState + }); + ac.renderFirst(); }; $('#dfsRestartButton').click(init); $('#lifoWaiting').css('background-color', 'hsl(0,50%,75%)'); diff --git a/3-Solving-Problems-By-Searching/c_uniformCostSearch.js b/3-Solving-Problems-By-Searching/c_uniformCostSearch.js index 3c3519c..d3585ae 100644 --- a/3-Solving-Problems-By-Searching/c_uniformCostSearch.js +++ b/3-Solving-Problems-By-Searching/c_uniformCostSearch.js @@ -20,45 +20,51 @@ $(document).ready(function() { }).appendTo(exploredQueueCanvas); //Intial value for separation is 0 $('.ucsSeparation').html(0); - var graph = new DefaultGraph(); - //Precompute costs of all nodes from the initial node var costMap = precomputedCosts(); - var graphProblem = new GraphProblem(graph.nodes, graph.edges, 'A', 'A'); - //Change the text of all the nodes to its cost - for (key in graphProblem.nodes) { - graphProblem.nodes[key].text = costMap[graphProblem.nodes[key].id]; - } - var graphAgent = new GraphAgent(graphProblem, 'ucs'); + var options = new DefaultOptions(); options.nodes.next.fill = 'hsla(126, 100%, 69%, 1)'; options.edges.showCost = true; - var graphDrawAgent = new GraphDrawAgent(graphProblem, 'uniformCostSearchCanvas', options, h, w); - drawList(priorityTwo, graphProblem.frontier, graphProblem, options, costMap); - drawList(exploredTwo, graphProblem.explored, graphProblem, options, costMap); - var updateFunction = function() { - if (graphProblem.frontier.length > 0) { - var nextNode = uniformCostSearch(graphProblem); - graphAgent.expand(nextNode); + + var drawState = function(n) { + var graph = new DefaultGraph(); + var graphProblem = new GraphProblem(graph.nodes, graph.edges, 'A', 'A'); + var graphAgent = new GraphAgent(graphProblem); + for (key in graphProblem.nodes) { + graphProblem.nodes[key].text = costMap[graphProblem.nodes[key].id]; + } + + var graphDrawAgent = new GraphDrawAgent(graphProblem, 'uniformCostSearchCanvas', options, h, w); + var maxCost; + while (n--) { if (graphProblem.frontier.length > 0) { - graphProblem.nextToExpand = uniformCostSearch(graphProblem); + var nextNode = uniformCostSearch(graphProblem); + graphAgent.expand(nextNode); + //If frontier is still present, find the next node to be expanded so it + //could be colored differently + if (graphProblem.frontier.length > 0) { + graphProblem.nextToExpand = uniformCostSearch(graphProblem); + maxCost = graphProblem.nodes[graphProblem.nextToExpand].cost; + } else { + graphProblem.nextToExpand = null; + } } else { - graphProblem.nextToExpand = null; - } - graphDrawAgent.iterate(); - drawList(priorityTwo, graphProblem.frontier, graphProblem, options, costMap); - drawList(exploredTwo, graphProblem.explored, graphProblem, options, costMap); - let maxCost = 0; - //Find the max cost which separates the explored from frontier nodes - if (graphProblem.nextToExpand) { - maxCost = graphProblem.nodes[graphProblem.nextToExpand].cost; + break; } - //Draw it in the front end - $('.ucsSeparation').html(maxCost); - } else { - clearInterval(intervalFunction, DELAY); } + graphDrawAgent.iterate(); + drawList(priorityTwo, graphProblem.frontier, graphProblem, options, costMap); + drawList(exploredTwo, graphProblem.explored, graphProblem, options, costMap); + //Draw it in the front end + $('.ucsSeparation').html(maxCost); } - intervalFunction = setInterval(updateFunction, DELAY); + let ac = new AnimationController({ + selector: '#ucsAC', + min: 0, + max: 15, + renderer: drawState + }); + ac.renderFirst(); }; $('#ucsRestartButton').click(init); $('#ucsWaiting').css('background-color', 'hsl(0,50%,75%)'); @@ -81,7 +87,6 @@ function drawList(two, list, problem, options, costMap) { let text = two.makeText(costMap[node.id], x, y); let fillColor = options.nodes[state].fill; circle.fill = fillColor; - } two.update(); } diff --git a/3-Solving-Problems-By-Searching/index.html b/3-Solving-Problems-By-Searching/index.html index c02d2cb..4c948b8 100644 --- a/3-Solving-Problems-By-Searching/index.html +++ b/3-Solving-Problems-By-Searching/index.html @@ -10,6 +10,7 @@ + @@ -111,10 +112,11 @@