Git Product home page Git Product logo

bfs-lab's Introduction

BFS Lab!

Objectives

  • Translate the breadth first search procedure into code.

Review Breadth First Search Procedure

In this section we will translate our breadth first search algorithm into code. We will work towards a function called bfs that returns a list of vertices in the order they are first visited. We'll provide some guidance along the way.

So let's take another look at our graph.

In breadth first search, we explore the first vertex, and visit the adjacent vertices adding each one to a queue in turn. Then we remove the first vertex added to the queue and explore it.

Let's go back to our representation of our graph and see if we can make more progress translating this into code.

Another Shot at the code

let edges = [
	['14th&6th', '23rd&6th'],
	['23rd&6th', '34th&6th'],
	['34th&6th', '28th&Bwy'],
	['28th&Bwy', '23rd&Bwy'],
	['23rd&Bwy', '14th&Lex'],
	['14th&Lex', '23rd&Lex']
]

let vertices = [
  {name: '34th&6th', distance: null, predecessor: null},
  {name: '23rd&6th', distance: null, predecessor: null},
  {name: '14th&6th', distance: null, predecessor: null},
  {name: '28th&Bwy', distance: null, predecessor: null},
  {name: '23rd&Bwy', distance: null, predecessor: null},
  {name: '14th&Lex', distance: null, predecessor: null},
  {name: '23rd&Lex', distance: null, predecessor: null},
]

What's a good summary of our procedure? Add a vertex to the queue. Then we remove the first vertex added and visit the adjacent vertices. As each is visited, add each vertex to the queue. Then continue the process exploring each vertex explored in turn.

Let's translate this summary into pseudocode.

Note: Pseudocode is code that may not actually work, but reflects our thought process.

Give the pseudocode a shot, it shouldn't take that long.

You may get to something like the following:

	rootNode = vertices[0]
	queue = []
	addVertexToQueue(rootNode)
		// queue = [rootNode]
	while(!queue.length == 0) {
		let firstNode = queue.shift()
	adjacentVertices = findAdjacent(firstNode)
		for vertex in adjacentVertices {
			markDistanceAndPredecessor(vertex)
			addToQueue(vertex)
		}
	}

So we can start to see some methods forming. So now we write the following methods:

findAdjacent

markDistanceAndPredecessor

addToQueue

You can write these methods, and then complete our breadth first search algorithm which returns a list of vertices in the order they are visited. Use the tests in the lab to guide you through.

bfs-lab's People

Contributors

7kingdavid7 avatar alpha-convert avatar annjohn avatar bhollan avatar jakebrady5 avatar jasonpaulso avatar jeffkatzy avatar jeffpwang avatar jonbf avatar maxwellbenton avatar pletcher avatar smulligan85 avatar sylwiavargas avatar tejbans avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bfs-lab's Issues

Solution does not accept array in different order

Solution to test #1 "should return an array of adjacent nodes" accepts
[{name: '23rd&6th', distance: null, predecessor: null}, {name: '28th&Bwy', distance: null, predecessor: null}]
but not
[{name: '28th&Bwy', distance: null, predecessor: null}, {name: '23rd&6th', distance: null, predecessor: null}]

The order of the array received should not matter.

Here is my whole findAdjacent function that works:
`const findAdjacent = (node, verticies, edges) => {
let adjacent = []

edges.forEach(pair => {
if (pair[0] === node) {
adjacent.push(pair[1])
}
if (pair[1] === node) {
adjacent.push(pair[0])
}
})
let nodes = []

while (adjacent.length > 0) {
const name = adjacent.pop()

verticies.forEach(vert => {
  if (name === vert.name && vert.distance === null) {
    nodes.push(vert)
  }
})

}

return nodes.reverse()
}`

and my failing one:
`const findAdjacent = (node, verticies, edges) => {
let adjacent = []

edges.forEach(pair => {
if (pair[0] === node) {
adjacent.push(pair[1])
}
if (pair[1] === node) {
adjacent.push(pair[0])
}
})

let nodes = []

while (adjacent.length > 0) {
const name = adjacent.pop()

verticies.forEach(vert => {
  if (name === vert.name && vert.distance === null) {
    nodes.push(vert)
  }
})

}

return nodes
}`

Please note the only difference being .reverse() on the last line.

test has irrelevant comments

lines 41-43 have comments as follow:
// but these nodes are not in the node list? // 23rd&Broadway // 33rd&Lex

23rd&Bwy actually does exist in the vertices array, on line 22.
33rd&Lex does not exist but is not relevant to the previous question.

These comments just add confusion and don't add any clarifying or relevant information.

predecessors

I found the 'predecessor' properties to be confusing. There wasn't really any explanation of what they are, and they don't appear to have any purpose in the code.

The predecessor seems to be a pointer back the to 'parent' node, and at some iteration of the curriculum it probably made sense, but currently it isn't needed.

I'd be happy to rewrite the lab to remove it if that would be helpful.

findAdjacent function misnamed

the describe line of the #findAdjacent function names it 'findAdjacentNodes' but then the expect line just calls it 'findAdjacent,' so the test fails if you name your function #findAdjacentNodes instead of #findAdjacent. not a huge thing, but causes some initial confusion

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.