Created
October 11, 2012 04:15
-
-
Save ruzzbot/3870124 to your computer and use it in GitHub Desktop.
Adjacency List for Undirected Graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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