Skip to content

Instantly share code, notes, and snippets.

View keithwhor's full-sized avatar
🍁

Keith Horwood keithwhor

🍁
View GitHub Profile
@keithwhor
keithwhor / graph_jlm_example.js
Last active May 1, 2016 23:31
Joe likes Minecraft example
let joe = {type: 'node', properties: {name: 'joe'}, input: [], output: []};
let likes = {type: 'edge', properties: {name: 'likes'}, input: null, output: null};
let minecraft = {type: 'node', properties: {name: 'minecraft'}, input: [], output: []};
joe.output.push(likes);
likes.input = joe;
likes.output = minecraft;
minecraft.input.push(likes);
@keithwhor
keithwhor / graph_unit.js
Created July 21, 2015 16:55
Basic Unit class for graphs
class Unit {
// Entity is used as node or edge type, for different classifications
// i.e. 'person', 'game', 'road', etc.
constructor(entity, properties) {
this.entity = entity + '';
this.load(properties || {});
@keithwhor
keithwhor / graph_node.js
Created July 21, 2015 17:19
Node class for graphs
class Node extends Unit {
constructor(entity, properties) {
super(entity, properties);
this.edges = [];
this.inputEdges = [];
this.outputEdges = [];
}
@keithwhor
keithwhor / graph_edge.js
Last active May 6, 2016 22:00
Edge class for graphs
class Edge extends Unit {
constructor(entity, properties) {
super(entity, properties);
this.inputNode = null;
this.outputNode = null;
this.duplex = false;
// Create nodes...
let joe = new Node('person');
joe.set('name', 'Joe');
let minecraft = new Node('game');
minecraft.set('name', 'Minecraft');
// Create edge...
let likes = new Edge('likes');
@keithwhor
keithwhor / graph_jlm_example_3.js
Last active May 6, 2016 22:00
Extended minecraft graph example
// Add even more nodes
let mojang = new Node('company', {name: 'Mojang'});
let microsoft = new Node('company', {name: 'Microsoft'});
let jennifer = new Node('person', {name: 'Jennifer'});
new Edge('founded').link(notch, mojang);
new Edge('acquired').link(microsoft, mojang);
new Edge('purchased').link(jennifer, minecraft);
new Edge('prints_money_for').link(minecraft, microsoft);
@keithwhor
keithwhor / graph_map_example.js
Created July 21, 2015 20:08
Map user behavior to graph
let users = getUsers(); // abstract function to get user data (i.e. SQL)
let listings = getListings(); // ... listings
let views = getViews(); // ... etc.
let favorites = getFavorites();
let requests = getRequests();
// quick and dirty O(n) function to get a node by id
function getNodeById(nodes, id) {
return nodes.filter(function(node) {
return node.get('id') === id;
views.forEach(function(view) {
view.setDistance(4);
});
favorites.forEach(function(favorite) {
favorite.setDistance(2);
});
requests.forEach(function(request) {
request.setDistance(1);
@keithwhor
keithwhor / graph_unitgraph_closest_example.js
Last active February 19, 2018 17:37
UnitGraph example, loading graph and finding closest nodes
let ug = require('ug');
let graph = new ug.Graph();
// load graph from flatfile into RAM
graph.load('/path_to_saved_graph.ugd', function() {
// get the closest 100 'listings' nodes, at a minimum depth (distance) of 3
let results = graph.closest(node, {
compare: function(node) { return node.entity === 'listing'; },
@keithwhor
keithwhor / graph_unitgraph_generate_example.js
Last active May 6, 2016 22:00
UnitGraph example, generating graph
let ug = require('ug');
let graph = new ug.Graph();
// fetch data
let users = getUsers(); // abstract function to get user data (i.e. SQL)
let listings = getListings(); // ... listings
let views = getViews(); // ... etc.
let favorites = getFavorites();