From e5b75ad8bb88e37b31cfc97de388187298383c45 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sat, 1 Jan 2022 17:52:20 +0100 Subject: [PATCH] Add matrix->matrix-graph and matrix->graph->graph. --- graph.rkt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/graph.rkt b/graph.rkt index b6c420f..2feef6b 100644 --- a/graph.rkt +++ b/graph.rkt @@ -32,6 +32,7 @@ weighted-graph? weighted-graph/undirected weighted-graph/directed undirected-graph directed-graph (rename-out [g:matrix-graph? matrix-graph?]) + matrix->matrix-graph matrix-graph->graph bfs bfs/generalized fewest-vertices-path dfs dfs/generalized @@ -120,6 +121,9 @@ (graph (g:directed-graph es ws))) ;; 2.3 Matrix Graphs + (define (matrix->matrix-graph mtx) + (g:matrix->matrix-graph mtx)) + (define matrix-graph->graph graph) ;; 4 Basic Graph Functions ;; 4.1 Breadth-first Search @@ -240,6 +244,7 @@ (define (graphviz g #:output [output #f] #:colors [colors #f]) (g:graphviz (gg g) #:output output #:colors colors))) +(require (only-in math/matrix Matrix)) (require/typed/provide 'graph-wrapper [#:opaque Graph graph?] @@ -282,6 +287,8 @@ [directed-graph (->* ((Listof (List Any Any))) ((Listof Any)) Graph)] ;; 2.3 Matrix Graphs + [matrix->matrix-graph (-> (Matrix Any) Matrix-Graph)] + [matrix-graph->graph (-> Matrix-Graph Graph)] ;; 4 Basic Graph Functions ;; 4.1 Breadth-first Search @@ -370,6 +377,7 @@ ;; functions can be invoked without errors. The tests do not check ;; whether the results make sense. (require typed/rackunit) + (require (only-in math/matrix matrix)) ;; TODO: Submit an update to hash->list in Racket and then remove ;; this function. @@ -431,7 +439,10 @@ "digraph G {\n\tnode0 [label=\"c\"];\n\tnode1 [label=\"a\"];\n\tnode2 [label=\"b\"];\n\tsubgraph U {\n\t\tedge [dir=none];\n\t}\n\tsubgraph D {\n\t\tnode1 -> node2 [label=\"1\"];\n\t\tnode2 -> node0 [label=\"hello\"];\n\t}\n}\n") ;; 2.3 Matrix Graphs - (check-false (matrix-graph? (directed-graph '((a b) (b c)))))) + (check-false (matrix-graph? (directed-graph '((a b) (b c))))) + (define mg (matrix->matrix-graph (matrix [[1 2] [3 #f]]))) + (check-true (matrix-graph? mg)) + (check-true (has-vertex? (matrix-graph->graph mg) 1))) (test-case "4 Basic Graph Functions" ;; 4.1 Breadth-first Search