Giter Club home page Giter Club logo

js-graph-traversal's Introduction

Graph Traversal - An Intro to Depth and Breadth First Search

We will be creating three modules:

  1. A Graph Generator module that helps us generate a graph data structure.
  2. A Graph is a data structure that contains nodes
  3. Nodes are connected to each other via edges
  4. A Depth-first Search (DFS) module that takes a graph and traverses it depth-first.
  5. A Breadth-first Search (BFS) module that takes a graph and traverses it breadth-first.

Graph data structure example

    A
    ^
  B   C
  ^   |
 D E  F

Is represented in memory as:

{ name: 'A',
  value: 'foo1',
  neighbors: [
    { name: 'B',
      value: 'foo2',
      neighbors: [
        { name: 'D',
          value: 'foo4',
          neighbors: []
        },
        { name: 'E',
          value: 'foo5',
          neighbors: []
        }
      ]
    },
    { name: 'C',
      value: 'foo3',
      neighbors: [
        { name: 'F',
          value: 'foo6',
          neighbors: []
        }
      ]
    }
  ]
}

Graph Methods

The basic operations provided by a graph data structure usually include:

  1. adjacent(G, x, y): tests whether there is an edge from the vertices x to y; neighbors(G, x): lists all vertices such that there is an edge from the vertices x to y;
  2. add_vertex(G, x): adds the vertex x, if it is not there;
  3. remove_vertex(G, x): removes the vertex x, if it is there;

Depth-first Search Methods

The basic operations provided by a Depth-first Search usually include:

  1. DFS(start, searchFor): Starting at the node start traverse the graph depth-first and return the value at the node whos key matches searchFor. If there are no matches, return false.

Breadth-first Search Methods

  1. BFS(start, searchFor): Starting at the node start traverse the graph breadth-first and return an array of the shortest path between start and the node searchFor. If there are no matches, return false.

Stretch Goals

  1. Write a blog post ELI5 the differences between depth and breadth-first Search.
  2. Write Pseudocode for each implementation
  3. Explain the Big O notation for each method
  4. Provide examples of when you would favor one graph traversal algorithm over the other.
  5. Write Tests using Mocha and Chai.
  6. Implement a Queue Module for Breadth-first search.
  7. Implement a Stack Module for Depth-first search.
  8. Write a recursive and non-recursive implementation of BFS and DFS.
  9. Visualize each method in the DOM.

Additional Resources

Graph

Depth First Search

  • Link: Depth First Search
  • Concepts: Graph Node, Graph theory, search and depth first search

Breadth First Search

Graph Traversal Algorithms Implemented in JavaScript

js-graph-traversal's People

Contributors

joekarlsson avatar vincentnewkirk avatar

Stargazers

Kamuela Franco avatar

Watchers

James Cloos 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.