Complete code for creating call stacks

This commit is contained in:
Folkert Kevelam 2025-03-30 18:59:01 +02:00
parent 5e518b1bdf
commit de4fe423fb

View File

@ -62,22 +62,33 @@
(def label (string/format "(cc %d %d)" amount kind-of-coins)) (def label (string/format "(cc %d %d)" amount kind-of-coins))
(def node_name (string/format "node_%d" counter)) (def node_name (string/format "node_%d" counter))
(def clr (def clr
(cond (= kind-of-coins 0) "lightgray" (cond (<= kind-of-coins 0) "lightgray"
(< amount 0) "lightgray"
(= amount 0) "cyan" (= amount 0) "cyan"
(= kind-of-coins 2) "pink"
(= kind-of-coins 3) "mediumorchid"
(= kind-of-coins 4) "mediumpurple1"
(= kind-of-coins 5) "khaki1"
"lightgreen")) "lightgreen"))
(cond (cond
(= kind-of-coins 2) (dot/add-node (get (get graph :subgraphs) 1) node_name :label label :shape "box" :color clr) (= kind-of-coins 5) (dot/add-node (get (get graph :subgraphs) 4) node_name :label label :shape "box" :fillcolor clr :style "filled" :group "5")
(= kind-of-coins 1) (dot/add-node (get (get graph :subgraphs) 0) node_name :label label :shape "box" :color clr) (= kind-of-coins 4) (dot/add-node (get (get graph :subgraphs) 3) node_name :label label :shape "box" :fillcolor clr :style "filled" :group "4")
(= kind-of-coins 3) (dot/add-node (get (get graph :subgraphs) 2) node_name :label label :shape "box" :fillcolor clr :style "filled" :group "3")
(= kind-of-coins 2) (dot/add-node (get (get graph :subgraphs) 1) node_name :label label :shape "box" :fillcolor clr :style "filled" :group "2")
(= kind-of-coins 1) (dot/add-node (get (get graph :subgraphs) 0) node_name :label label :shape "box" :fillcolor clr :style "filled" :group "1")
(do (do
(dot/add-node graph node_name :label label :shape "box" :color clr))) (dot/add-node graph node_name :label label :shape "box" :fillcolor clr :style "filled")))
(dot/add-relation graph parent node_name) (dot/add-relation graph parent node_name)
node_name) node_name)
(defn count-change2 [amount denom] (defn count-change2 [amount denom]
(def graph (dot/create-graph :digraph "change")) (def graph (dot/create "change" :graph_type :digraph))
(dot/add-subgraph graph (dot/create-subgraph "rank_1" :rankdir "TB")) (dot/add-subgraph graph (dot/create-subgraph "rank_1"))
(dot/add-subgraph graph (dot/create-subgraph "rank_2" :rankdir "LR")) (dot/add-subgraph graph (dot/create-subgraph "rank_2" :rank "same"))
(dot/add-subgraph graph (dot/create-subgraph "rank_3" :rank "same"))
(dot/add-subgraph graph (dot/create-subgraph "rank_4" :rank "same"))
(dot/add-subgraph graph (dot/create-subgraph "rank_5" :rank "same"))
(var counter 0) (var counter 0)
(defn cc [amount kind-of-coins parent] (defn cc [amount kind-of-coins parent]
(def name (add-to graph counter amount kind-of-coins parent)) (def name (add-to graph counter amount kind-of-coins parent))
@ -92,10 +103,10 @@
(cc (- amount (first-denomination kind-of-coins)) kind-of-coins name)))) (cc (- amount (first-denomination kind-of-coins)) kind-of-coins name))))
(def result (cc amount denom "test")) (def result (cc amount denom "test"))
(dot/write-graph graph "exercise_1_14.gv") (dot/write graph "exercise_1_14.gv")
result) result)
(print (count-change2 11 1)) (print (count-change2 11 2))
# Using the graph we can find the following relation for (cc n 1) # Using the graph we can find the following relation for (cc n 1)
# f(0,1) = 1 # f(0,1) = 1