I have the following function that I would like to translate to JS (I am still new to JS, so having some difficulty):
It takes a complete graph of N nodes and enumerates all the unique pair matchings.
/**
*
* @param nodes The nodes still to be added to our edge list.
* @param edges The current edge list. This is mutated, so always return a clone!
*/
public static <N> List<Map<N,N>> enumerateEdges(List<N> nodes,Map<N,N> edges){
if(nodes.isEmpty()) // No more nodes to create edges from, so return our current edge list in a new list.
return Collections.singletonList(new HashMap<>(edges));
N start = nodes.get(0); //The start node of our next pair.
List<Map<N,N>> acc = new LinkedList<>(); //The accumulation of the EdgeLists
for(int i = 1; i<nodes.size(); ++i){
N end = nodes.get(i); //The end node of our pair
edges.put(start,end); //Add this pair to our edge list
List<N> unused = new ArrayList<>(nodes); // The nodes not used in our edge list.
unused.remove(i);
unused.remove(0);
acc.addAll(enumerateEdges(unused,edges));
edges.remove(start); //Remove this pair from our edge list.
}
return acc;
}
Called with:
List<Map<Integer,Integer>> results = enumerateEdges(Arrays.asList(0,1,2,3),new HashMap<>());
My current attempt at this doesn't work. It outputs empty arrays when doing a console.log().
function enumerateEdges(nodes, edges) {
if (nodes.length == 0) return [];
let start = nodes[0];
let acc = [];
for(let i = 1; i < nodes.length; i++) {
let end = nodes[i];
edges = [ {start,end} ];
let unused = nodes.slice(0);
unused.splice(i,1);
unused.splice(0,1);
acc.push.apply(acc, enumerateEdges(unused,edges));
edges.splice(0, 1);
}
return acc;
}
Calling this with:
let nodes = [1,2,3,4];
let edges = [];
enumerateEdges(nodes, edges);
Does anyone have any ideas? Much appreciated.
.splice()is not a replacement for.remove()because it does not modify the array; it returns a new array.acc.push.apply(acc, enumerateEdges(unused,edges))doesn't actually push edges toaccsplicedoes mutate the array likeremovedoes.push.applyworks, but the function result is always an empty array, because that is what you return (by error) in the firstifstatement..slice():) One little "p".