Skip to content

Instantly share code, notes, and snippets.

@kmicinski
Created February 28, 2023 17:46
Show Gist options
  • Save kmicinski/b76338e148d6270e6f7cccdd2f20c4d9 to your computer and use it in GitHub Desktop.
Save kmicinski/b76338e148d6270e6f7cccdd2f20c4d9 to your computer and use it in GitHub Desktop.
;; CIS352 -- 2/28/23
#lang racket
(define x
(hash "A" (set "B" "C")
"B" (set "C" "A")
"C" (set "B")))
(define (count-edges graph)
(foldl
(λ (key acc)
(+ acc (length (set->list (hash-ref graph key)))))
0
(hash-keys graph)))
(define (count-symmetric-edges graph)
(foldl
;; x is each node (root), acc is the total # of
;; symmetric edges
(λ (x acc)
(foldl
;; y is a node within the set of values pointed to
;; by x in the graph, add 1 to acc if (x,y) is a
;; symmetric link, add 0 to acc otherwise
(λ (y acc)
;; have edges (x,y)
;; generalize to transitive closure
;; find all edges (y,z)
;; fold over the zs and add (x,z) to graph
;; (add another foldl)
(if (set-member? (hash-ref graph y) x)
(+ 1 acc)
acc))
acc
(set->list (hash-ref graph x))))
0
(hash-keys graph)))
(define (h g)
(if (equal? (one-step-transitive-add g) g)
g
(h (one-step-transitive-add g))))
#;(define (for-each-key keys)
(match keys
['() 0]
[`(,fst ,rest-keys ...)
(define number-of-nodes-to-which-key-points
(length (set->list (hash-ref graph fst))))
(+ number-of-nodes-to-which-key-points
(for-each-key rest-keys))]))
#;(for-each-key (hash-keys graph))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment