haskell - Find a distance in graphs between nodes -
i got list in hand such:
[(1,2),(1,4),(2,4),(3,9),(4,7),(7,9)]
i have implement function takes: list of existing relations, pair of new realiton,a distance n.
function should work in way: takes parameters, calculates distance between nodes given in new relation, if distance <= distance n, function returns list including new relationship.
for ex:
list = [(1,2),(1,4),(2,4),(3,9),(4,7),(7,9)] new_relation = [(1,3)] distance_n = 4
it return [(1,2),(1,3),(1,4),(2,4),(3,9),(4,7),(7,9)]
if distance 3 return original list
[(1,2),(1,4),(2,4),(3,9),(4,7),(7,9)]
how can this? have problems graphs. note: should implemented in haskell.
both the containers package , the graphs package have adjacency list representations similar yours.
a general method of computing shortest paths contains functional implementation of djikstra's algorithm finding graph distances, works on adjacency matrix. either change of representation or alter algorithm work on adjacency lists.
once have function distance :: graph -> vertex -> vertex -> distance, , function addedge :: edge -> graph -> graph, golden. addedge should relatively easy write independent of representation, in general adding edge means have throw-away previous, cached distance calculations.
Comments
Post a Comment