Q:

Calculating Paths from Network Node Set

I have an array of set of node connections defined by:
var arr = ['1-2','1-6','2-6','2-3','1-4','1-5','6-7','4-7','7-8'];

1-2 means node 1 connects to node 2 and so on.  There are multiple connections as you can see. 
If correctly manipulated, you can notice various unique paths such as 1-2-6-7-8, 1-5 etc.  I would like to calculate paths as:
path = ['1-2-6-7-8','1-6-7-8','1-2-3','1-4-7-8','1-5']

I have done by checking each set by another set but I think my code is too long and does not perform well.  What is the best way to get path array. ( path ends at leaf node)
Thanks. 

A:

How about this solution, and I think it only works with DAG: directed acyclic graphs
function toGraph(arr){
    d = {}
    for (var e=0; e<arr.length; e++){
        var edge = arr[e].split('-')
        if(!d[edge[0]]){d[edge[0]]=[]}
        if(!d[edge[1]]){d[edge[1]]=[]}
        d[edge[0]].push(edge[1])
    }
    return d
}    

function DFS(G,node,path,paths,visited){
    visited[node] = true
    path.push(node)
    if (G[node].length==0 && path.length>1) paths.push('['+path.join('-')+']')

    for (var s=0; s<G[node].length; s++){
        successor = G[node][s]
        if(!visited[successor]){
            DFS(G,successor,path,paths,visited)
        }
    }
    visited[node] = false
    path.pop()
}

function go(){
    var arr = ['1-2','1-6','2-6','2-3','1-4','1-5','6-7','4-7','7-8']   
    var paths = []
    DFS(toGraph(arr),'1',[],paths,{})
    return paths.toString()
}

and when you call go(), you get the following output
[1-2-6-7-8], [1-2-3], [1-6-7-8], [1-4-7-8], [1-5]

