-
Notifications
You must be signed in to change notification settings - Fork 222
Adds Bi-Directional Search Diagram #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,126 @@ | ||
| //Function to clone a object | ||
| function cloneObject(obj) { | ||
| return JSON.parse(JSON.stringify(obj)); | ||
| } | ||
| //Function to calculate euclidean distance | ||
| function distance(point1, point2) { | ||
| return Math.sqrt(Math.pow(point1[0] - point2[0], 2) + Math.pow(point1[1] - point2[1], 2)); | ||
| } | ||
|
|
||
| class Node { | ||
| constructor(id, x, y, adjacent) { | ||
| this.id = id; | ||
| this.x = x; | ||
| this.y = y; | ||
| this.adjacent = (adjacent != undefined) ? adjacent : []; | ||
| } | ||
| } | ||
| //Generates a random planar graph | ||
| function randomPlanarGraph(height, width, totalNodes) { | ||
| let nodes = [], | ||
| edges = []; | ||
| let grid = gitteredGrid(height, width, totalNodes); | ||
| let rowSize = grid.length; | ||
| let columnSize = grid[0].length; | ||
| //Extract the nodes from the grid | ||
| for (let i = 0; i < rowSize; i++) { | ||
| for (let j = 0; j < columnSize; j++) { | ||
| nodes.push(new Node(i * columnSize + j, grid[i][j][0], grid[i][j][1])); | ||
| } | ||
| } | ||
| //Randomly generate edges between nodes. | ||
| let minDistance = 1.6 * Math.sqrt(height * width / totalNodes) | ||
| for (let i = 0; i < nodes.length; i++) { | ||
| for (let j = i + 1; j < nodes.length; j++) { | ||
| if (distance([nodes[i].x, nodes[i].y], [nodes[j].x, nodes[j].y]) < minDistance) { | ||
| nodes[i].adjacent.push(j); | ||
| nodes[j].adjacent.push(i); | ||
| edges.push([i, j]); | ||
| } | ||
| } | ||
| } | ||
| return [nodes, edges]; | ||
| } | ||
|
|
||
| class Graph { | ||
| constructor(height, width, totalNodes) { | ||
| this.totalNodes = 20; | ||
| this.nodes = []; | ||
| this.height = height; | ||
| this.width = width; | ||
| this.totalNodes = totalNodes; | ||
| [this.nodes, this.edges] = randomPlanarGraph(this.height, this.width, this.totalNodes); | ||
| } | ||
|
|
||
| getAdjacent(id) { | ||
| return this.nodes[id].adjacent; | ||
| } | ||
| } | ||
|
|
||
| class BreadthFirstSearch { | ||
| constructor(graph, initial) { | ||
| this.graph = graph; | ||
| this.initial = initial; | ||
| this.frontier = [this.initial]; | ||
| //State is true if the node is unexplored and false if explored or in frontier | ||
| this.state = new Array(this.graph.nodes.length).fill(true); | ||
| this.frontierIndex = 0; | ||
| } | ||
| //Expands a node from the frontier and returns that node | ||
| step() { | ||
| if (this.frontier.length <= this.frontierIndex) { | ||
| return undefined; | ||
| } else { | ||
| let nextNode = this.frontier[this.frontierIndex]; | ||
| //Remove nextNode from frontier | ||
| this.frontierIndex++; | ||
| //Add to explored | ||
| this.state[nextNode] = false; | ||
| //Get adjacent nodes which are unexplored | ||
| let adjacentNodes = this.graph.getAdjacent(nextNode).filter(x => this.state[x]); | ||
| //Mark each adjacent node | ||
| adjacentNodes.forEach(x => this.state[x] = false); | ||
| //Push to frontier | ||
| adjacentNodes.forEach(x => this.frontier.push(x)); | ||
| return nextNode; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class BidirectionalProblem { | ||
| constructor(graph) { | ||
| this.graph = graph; | ||
| this.initial = 0; | ||
| //Force the initial node to be around the middle of the canvas. | ||
| for (let i = 0; i < this.graph.nodes.length; i++) { | ||
| if (distance([this.graph.width / 2, this.graph.height / 2], [this.graph.nodes[i].x, this.graph.nodes[i].y]) < 50) { | ||
| this.initial = i; | ||
| break; | ||
| } | ||
| } | ||
| //Final node is chosen randomly | ||
| this.final = Math.floor(Math.random() * this.graph.nodes.length); | ||
| this.sourceBFS = new BreadthFirstSearch(this.graph, this.initial); | ||
| this.destBFS = new BreadthFirstSearch(this.graph, this.final); | ||
| } | ||
|
|
||
| iterate() { | ||
| let obj = { | ||
| done: false | ||
| } | ||
| //Iterate Source side BFS | ||
| let nextNode = this.sourceBFS.step(); | ||
| obj.source = nextNode; | ||
| if (!this.destBFS.state[nextNode]) { | ||
| obj.done = true; | ||
| } | ||
|
|
||
| //Iterate Destination side BFS | ||
| nextNode = this.destBFS.step(); | ||
| obj.dest = nextNode; | ||
| if (!this.sourceBFS.state[nextNode]) { | ||
| obj.done = true; | ||
| } | ||
| return obj; | ||
| } | ||
| } | ||
This file contains hidden or 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,132 @@ | ||
| class BidirectionalDiagram { | ||
| constructor(selector, h, w) { | ||
| this.selector = selector; | ||
| this.h = h; | ||
| this.w = w; | ||
| this.selector.select('canvas').remove(); | ||
| this.root = this.selector | ||
| .append('canvas') | ||
| .attr('height', this.h) | ||
| .attr('width', this.w); | ||
| this.context = this.root.node().getContext("2d"); | ||
| this.context.clearRect(0, 0, this.w, this.h); | ||
| this.delay = 4; | ||
| } | ||
|
|
||
| init(problem, textElement) { | ||
| this.problem = problem; | ||
| this.nodes = this.problem.graph.nodes; | ||
| this.edges = this.problem.graph.edges; | ||
| this.initial = this.problem.initial; | ||
| this.final = this.problem.final; | ||
| this.textElement = textElement; | ||
|
|
||
| this.initialColor = 'hsl(0, 20%, 80%)'; | ||
| this.edgeColor = 'hsl(0, 2%, 80%)'; | ||
| this.sourceBFSColor = 'hsl(209, 100%, 50%)'; | ||
| this.destBFSColor = 'hsl(209, 30%, 50%)'; | ||
| this.sourceColor = 'hsl(209, 100%, 20%)'; | ||
| this.destColor = 'hsl(209, 100%, 20%)'; | ||
| this.nodeSize = 3.5; | ||
| this.textColorScale = d3.scaleLinear().domain([0, this.nodes.length / 2]) | ||
| .interpolate(d3.interpolateRgb) | ||
| .range([d3.hsl('hsla(102, 100%, 50%, 1)'), d3.hsl('hsla(0, 100%, 50%, 1)')]); | ||
|
|
||
| //Draw all nodes | ||
| for (let i = 0; i < this.nodes.length; i++) { | ||
| this.colorNode(i, this.initialColor); | ||
| } | ||
| //Draw all edges | ||
| for (let i = 0; i < this.edges.length; i++) { | ||
| let d = this.edges[i]; | ||
| this.context.beginPath(); | ||
| this.context.lineWidth = 1; | ||
| this.context.strokeStyle = this.edgeColor; | ||
| this.context.moveTo(this.nodes[d[0]].x, this.nodes[d[0]].y); | ||
| this.context.lineTo(this.nodes[d[1]].x, this.nodes[d[1]].y); | ||
| this.context.stroke(); | ||
| this.context.closePath(); | ||
| } | ||
|
|
||
| //Initial Node | ||
| this.context.fillStyle = this.sourceColor; | ||
| this.context.beginPath(); | ||
| this.context.arc(this.nodes[this.initial].x, this.nodes[this.initial].y, 1.2 * this.nodeSize, 0, 2 * Math.PI, true); | ||
| this.context.fill(); | ||
| this.context.closePath(); | ||
| this.steps++; | ||
| this.textElement.text(this.steps); | ||
| //Final Node | ||
| this.context.fillStyle = this.destColor; | ||
| this.context.beginPath(); | ||
| this.context.arc(this.nodes[this.final].x, this.nodes[this.final].y, 1.2 * this.nodeSize, 0, 2 * Math.PI, true); | ||
| this.context.fill(); | ||
| this.context.closePath(); | ||
| this.steps++; | ||
| this.textElement.text(this.steps); | ||
| this.textElement.style('color', this.textColorScale(this.steps)); | ||
| this.steps = 0; | ||
| this.bfs(); | ||
| } | ||
|
|
||
| colorNode(node, color) { | ||
| //If the given node is not an initial node or final node | ||
| if (node != this.initial && node != this.final) { | ||
| this.context.fillStyle = color; | ||
| this.context.beginPath(); | ||
| this.context.arc(this.nodes[node].x, this.nodes[node].y, this.nodeSize, 0, 2 * Math.PI, true); | ||
| this.context.fill(); | ||
| this.context.closePath(); | ||
| this.steps++; | ||
| //Update steps in the page | ||
| this.textElement.style('color', this.textColorScale(this.steps)); | ||
| this.textElement.text(this.steps); | ||
| } | ||
| } | ||
|
|
||
| bfs() { | ||
| this.intervalFunction = setInterval(() => { | ||
| let next = this.problem.iterate(); | ||
|
|
||
| if (next.source) { | ||
| this.colorNode(next.source, this.sourceBFSColor) | ||
| } | ||
| if (next.dest) { | ||
| this.colorNode(next.dest, this.destBFSColor) | ||
| } | ||
| if (next.done) { | ||
| clearInterval(this.intervalFunction) | ||
| } | ||
| }, this.delay); | ||
| } | ||
| } | ||
|
|
||
| class BFSDiagram extends BidirectionalDiagram { | ||
| constructor(selector, h, w) { | ||
| super(selector, h, w); | ||
| } | ||
|
|
||
| bfs() { | ||
| this.bfsAgent = new BreadthFirstSearch(this.problem.graph, this.initial); | ||
| this.intervalFunction = setInterval(() => { | ||
| let node = this.bfsAgent.step(); | ||
| this.colorNode(node, this.sourceBFSColor); | ||
| if (node == this.final) { | ||
| clearInterval(this.intervalFunction) | ||
| } | ||
| }, this.delay); | ||
| } | ||
| } | ||
|
|
||
| $(document).ready(function() { | ||
| function init() { | ||
| let bidirectionalDiagram = new BidirectionalDiagram(d3.select('#backtracking').select('#biCanvas'), 500, 550); | ||
| let bfsDiagram = new BFSDiagram(d3.select('#backtracking').select('#bfsCanvas'), 500, 550) | ||
| let graph = new Graph(500, 530, 1500); | ||
| let problem = new BidirectionalProblem(graph); | ||
| bidirectionalDiagram.init(problem, d3.select('#backtracking').select('#biStepCount')); | ||
| bfsDiagram.init(problem, d3.select('#backtracking').select('#bfsStepCount')); | ||
| } | ||
| init(); | ||
| $('#backtracking .restart-button').click(init); | ||
| }); |
This file contains hidden or 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,17 @@ | ||
| function gitteredGrid(height, width, totalNodes) { | ||
| let cellSize = Math.sqrt(height * width / totalNodes); | ||
| let columnSize = Math.floor(width / cellSize); | ||
| let rowSize = Math.floor(height / cellSize); | ||
| let grid = []; | ||
|
|
||
| for (let i = 0; i < rowSize; i++) { | ||
| grid.push(new Array(columnSize)); | ||
| } | ||
|
|
||
| for (let i = 0; i < rowSize; i++) { | ||
| for (let j = 0; j < columnSize; j++) { | ||
| grid[i][j] = [cellSize * j + cellSize / 6 + Math.random() * cellSize * 2 / 3, cellSize * i + cellSize / 6 + Math.random() * cellSize * 2 / 3]; | ||
| } | ||
| } | ||
| return grid; | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've already written breadth first search for chapter 3; is it possible to reuse that, or make the other version compatible with this one?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The earlier Breadth First Search was simply a 3 line code that takes the problem object and returns the first element from its frontier. Here, the Breadth First Search class performs what the
class GraphAgentperformed earlier. But in this particular case, I am using generators. Generators prove to be especially useful here since there are 2 bfs running simultaneously and then there is one diagram that extracts nodes from the envelopeiterate()function from Bi-directional Problem.Additionally, the implementation of the graphs is also different. For example, the
getAdjacentfunction earlier simply scanned the edges list to get the nodes adjacent to a node; but in this case, that will be too costly since there are thousands of nodes. So here, we keep a list of adjacent nodes in the graph node itself. Similarly, there are more fundamental differences between the implementations. Due to all this reasons, I thought trying to reuse the code would get in my way of making the diagram.What are your thoughts on this part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok, that makes sense