main-dijkstra.scm 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (import
  2. (except (rnrs base)
  3. let-values
  4. map
  5. error
  6. vector-map)
  7. (only (guile)
  8. lambda* λ
  9. simple-format)
  10. ;; standard library
  11. (ice-9 pretty-print)
  12. ;; custom modules
  13. (fileio)
  14. (pipeline)
  15. (debug)
  16. (list-helpers)
  17. ;; let-values
  18. (srfi srfi-11)
  19. ;; hash tables
  20. (srfi srfi-69)
  21. ;; graph algos
  22. (dijkstra)
  23. (graph-model)
  24. (graph-reader)
  25. (graph-utils)
  26. )
  27. (define input-filename "example-graphs/graph-02.txt")
  28. (define-values (nodes distances-table nodes-table)
  29. (read-graph input-filename))
  30. (define get-neighbors
  31. (λ (node)
  32. "A function, that maps from an actual node struct to a
  33. list of node structures."
  34. (map (λ (node-name)
  35. (hash-table-ref nodes-table node-name))
  36. (node-neighbors node))))
  37. (define get-neighbor-distance
  38. (λ (node1 node2)
  39. "A function that returns the distance from NODE1 to NODE2."
  40. (let ([key (cons (node-name node1) (node-name node2))])
  41. (hash-table-ref distances-table key))))
  42. (define node<
  43. (λ (node1 node2)
  44. "A function introduction an order to nodes. Important for
  45. functional sets implementation backed by a tree
  46. implementation."
  47. (or (< (node-y node1)
  48. (node-y node2))
  49. (and (= (node-y node1)
  50. (node-y node2))
  51. (< (node-x node1)
  52. (node-x node2))))))
  53. (let ([start-node (hash-table-ref nodes-table "A")])
  54. (let-values ([(distances° routes°)
  55. (dijkstra-shortest-path start-node
  56. nodes
  57. get-neighbors
  58. get-neighbor-distance
  59. node<
  60. #:distance< <)])
  61. (simple-format #t "shortest distances from A to other nodes:\n")
  62. (pretty-print (hash-table->alist distances°))
  63. (simple-format #t "shortest path from A to T:\n")
  64. (pretty-print (routes->path routes° (hash-table-ref nodes-table "T")))))