Code Monkey home page Code Monkey logo

four_color.js's Introduction

four_color.js

Four coloring of a graph, for Javascript

Wossis

Given a map represented as an array of arrays, provide color indices for the cells such that no two adjacent cells have the same color.

That is, "color the map." With a continuous 2d map, this is always possible with no more than four colors.

What

Given a blank map

You can represent a map as an "adjacency matrix" (here's an adjacency matrix for US states for javascript,)

const states = ["AK", "AL", "AR", ...];

const state_adjacency = [
  [],
  [25, 42, 10, 09],
  [24, 42, 25, 18, 43, 36],
  ...
];

This says "state 0 is Alaska, which has no neighbors; state 1 is Alabama, which has neighbors 25 (Missouri,) 42 (Tennessee,) 10 (Georgia,) and 9 (Florida;) state 2 is Arkansas..."

If you have such a matrix, feed it to this library:

> gen4col(state_adjacency, true);  // true means "only give me one solution;" otherwise you'll get a bunch
[0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 2, 1, 0, 0, 1, 0, 0, 2, 2, 1, 1, 0, 1, 2, 3, 3, 2, 0, 0, 2, 2, 1, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 2, 3, 0, 1, 3, 0, 3]

This says "Paint Alaska in color 0, Alabama in 0, Arkansas in 0, Arizona in 0, California in 1, Colorado in 1," and so forth. This allows you to produce a different map:

API

There is only one method: gen4col(vertexArray, returnOnlyOne).

gen4col(vertexArray, returnOnlyOne)

vertexArray is an array of arrays, where the master offset is the vertex ID, and the items in the subordinate arrays are the connected vertices. All links should be listed bidirectionally.

returnOnlyOne is a boolean which tells the algorithm whether to return only the first solution it finds, or every solution.

four_color.js's People

Contributors

stonecypher avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 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.