Skip to content

Instantly share code, notes, and snippets.

@ruzzbot
Created October 11, 2012 04:15
Show Gist options
  • Save ruzzbot/3870124 to your computer and use it in GitHub Desktop.
Save ruzzbot/3870124 to your computer and use it in GitHub Desktop.
Adjacency List for Undirected Graph
define([], function(app) {
var Graph = {},
adj = {}; //Map of adjacency lists for each node
//@nodes (int[])
Graph.init = function(nodes){
//your node labels are consecutive integers starting with one.
//to make the indexing easier we will allocate an array of adjacency one element larger than necessary
adj = adj.clone();
for (var i = 0; i < nodes.length; ++i) {
adj.put(i, new LinkedList<Integer>());
}
};
// Add a neighbor(v2) to vector(v1)
Graph.addNeighbor = function (v1, v2) {
};
Graph.getNeighbors = function (v) {
};
// Return the module for AMD compliance.
return Graph;
});
/* Java example */
/*
class Graph {
//Map of adjacency lists for each node
Map<Integer, List<Integer>> adj;
public Graph(int[] nodes) {
//your node labels are consecutive integers starting with one.
//to make the indexing easier we will allocate an array of adjacency one element larger than necessary
adj = new HashMap<Integer, inkedList<Integer>>();
for (int i = 0; i < nodes.length; ++i) {
adj.put(i, new LinkedList<Integer>());
}
}
public addNeighbor(int v1, int v2) {
adj.get(v1).add(v2);
}
public List<Integer> getNeighbors(int v) {
return adj.get(v);
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment