Git Product home page Git Product logo

bfs-lab-dc-web-051319'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-dc-web-051319's People

Contributors

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

Watchers

James Cloos avatar Kevin McAlear avatar  avatar Mohawk Greene avatar  avatar Belinda Black avatar Bernard Mordan avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar  avatar

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.