Giter VIP home page Giter VIP logo

js-graph-algos's Introduction

#JS Graph Algorithms

This is partial code for learning about graph traversals. The file search.js is setup to use the sample data in slugraph.js, which is based on the adjacency list defined in graph.js.

Run

npm install

to make sure you have a local copy of the NPM Collections package for use in the queue in your Breadth-First Search solution.

For reference, traversal algorithms are given below. To turn these into valid JS, you'll need to add declarations and decide what it means to be visited or unvisited. Depending on your choices, you may not need to do the initializations at the beginning of the algorithm. The only catch is that the vertices in slugraph.js are numbered starting at 1, rather than 0.

For the purposes of playing around you can defined the functions process and processEdge to simply log out a message about visiting a vertex or edge.

For breadth-first search, there is a queue constructor in queue.js.

BFS(G, s) {
  for each vertex v in G {
    state[v] = unvisited;
    parent[v] = null;
  };

  state[s] = visited;
  queue.enqueue(s);

  while (queue not empty) {
    u = queue.dequeue();
    process(u);
    for each vertex v in neighbor(u) {
      processEdge(u,v);
      if (state[v] == unvisited) {
        state[v] == visited;
        parent[v] = u;
        queue.enqueue(v);
      }
    }
  }
}

For depth-first search, you can either replace the queue with a stack (changing enqueue to push, and dequeue to pop), or you can use the following recursive form:

DFS(G,u) {
  state[u] = visited;
  process(u);
  for each v in neighbors(u) {
    processEdge(u,v);
    if (state[v] == unvisited) {
      parent[v] = u;
      DFS(G,v);
    }
  }
}

js-graph-algos's People

Contributors

bjkeller avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

codefellows

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.