-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_relations_2.bak
272 lines (227 loc) · 8.19 KB
/
lab_relations_2.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#lang eopl
;;-------------------------------------------------------------------------------
;; Name:
;; Pledge:
;;-------------------------------------------------------------------------------
;; This lab is a continuation of last week's lab about relations.
;; All the functions you wrote last week are now available
;; as helper functions at the bottom of this file,
;; along with the same helper functions as before.
;;
;; Once again, the order of edges inside a relation doesn't matter
;; for the correctness of your outputs.
;; There should also be no duplicate edges in the relations you output.
;;
;; For all of the functions you'll implement below,
;; the empty relation '() is valid input.
;;______________________________________________________________________
;; Implement "compose", which accepts relations
;; RO ("R-outer") and RI ("R-inner")
;; and returns RO ∘ RI, or RO composed with RI.
;; RO ∘ RI = { (x z) | ∃y: (x y) ∈ R Λ (y z) ∈ R }
;;
;; Advice for implementing:
;; Consider every edge (a b) in RI.
;; For every edge (c d) in RO,
;; if b == c then add (a d) to RO ∘ RI.
;;
;; Examples:
;; (compose '((2 4) (3 5) (4 6) (5 7)) '((1 2) (2 3) (3 4) (4 5)))
;; -> '((1 4) (2 5) (3 6) (4 7))
;; (compose '((2 4) (5 8) (3 4)) '((2 4) (5 8) (3 4)))
;; -> '()
;; (compose '((1 1) (2 2) (3 3)) '((1 1) (2 2) (3 3)))
;; -> '((1 1) (2 2) (3 3))
;; (compose '((1 1) (2 1) (3 1) (4 1)) '((1 1) (1 2) (1 3)))
;; -> '((1 1))
;; (compose '((1 1) (1 2) (1 3)) '((1 1) (2 1) (3 1) (4 1)))
;; -> '((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3) (4 1) (4 2) (4 3))
;;
;; Type Signature: (compose relation relation) -> relation
(define (compose RO RI)
"Not implemented")
;; Implement "power", which accepts a relation R and an integer k ≥ 0
;; and returns R^k, which is R composed with itself (k - 1) times.
;;
;; R^0 = '()
;; R^1 = R
;; R^2 = R ∘ R
;; R^3 = R ∘ (R ∘ R)
;; ...
;; R^k = R ∘ R^(k-1)
;;
;; Advice:
;; You'll need to keep track of the initial R to repeatedly compose it.
;; To do this, you'll likely need a helper function.
;;
;; Examples:
;; (power '((1 2) (2 3) (3 4) (4 1)) 0)
;; -> '()
;; (power '((1 2) (2 3) (3 4) (4 1)) 1)
;; -> '((1 2) (2 3) (3 4) (4 1))
;; (power '((1 2) (2 3) (3 4) (4 1)) 2)
;; -> '((1 3) (2 4) (3 1) (4 2))
;; (power '((1 2) (2 3) (3 4) (4 1)) 3)
;; -> '((1 4) (2 1) (3 2) (4 3))
;; (power '((1 2) (3 3) (3 4) (4 2) (5 3)) 4)
;; -> '((3 3) (3 4) (3 2) (5 3) (5 4) (5 2))
;;
;; Type Signature: (power relation int) -> relation
(define (power R k)
"Not implemented")
;; Implement "transitive-closure", which accepts a relation R
;; and returns R+, the transitive closure of R.
;; R+ can be computed by "unioning" successive powers of R.
;; If e = |R| = the number of edges in R:
;; R+ = R ⋃ R^2 ⋃ R^3 ⋃ ... ⋃ R^e.
;;
;; Examples:
;; (transitive-closure '((1 2) (2 3) (3 1)))
;; -> '((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3))
;; (transitive-closure '((1 3) (3 5) (2 4) (5 6) (2 3)))
;; -> '((1 3) (3 5) (2 4) (5 6) (2 3) (1 5) (1 6) (3 6) (2 5) (2 6))
;; (transitive-closure '((1 2) (2 1) (3 4) (4 5)))
;; -> '((1 1) (2 2) (3 5) (1 2) (2 1) (3 4) (4 5))
;;
;; Type Signature: (transitive-closure relation) -> relation
(define (transitive-closure R)
"Not implemented")
;; Implement "transitive?", which accepts a relation R
;; and returns whether R is transitive.
;; This is easy to write if you utilize transitive-closure.
;;
;; Examples:
;; (transitive? '((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3)))
;; -> #t
;; (transitive? '((1 2) (2 3) (3 1)))
;; -> #f
;; (transitive? '())
;; -> #t
;; (transitive? '((1 2) (2 1) (1 1) (2 2)))
;; -> #t
;;
;; Type Signature: (transitive? relation) -> boolean
(define (transitive? R)
"Not implemented")
;; Implement "EQ-relation?", which accepts a relation R and positive integer n
;; and returns whether R is an equivalence relation over the domain [1, n].
;; Recall that a relation is an EQ-relation
;; iff it is symmetric, reflexive, and transitive.
;; Examples:
;; (EQ-relation? '((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3)) 3)
;; -> #t
;; (EQ-relation? '((1 1) (1 2) (2 1)) 2)
;; -> #f
;;
;; Type Signature: (EQ-relation? relation int) -> boolean
(define (EQ-relation? R n)
"Not implemented")
;;__________________________________________________________________________
;; Below are helper functions you may utilize for the functions you write!
;; Returns e ∈ L.
;; Type signature: (element? item list) -> boolean
(define (element? e L)
(member e L))
;; Returns L as a set (removes duplicates).
;; Type signature: (make-set list) -> set
(define (make-set L)
(cond [(null? L) '()]
[(member (car L) (cdr L)) (make-set (cdr L))]
[else (cons (car L) (make-set (cdr L)))]))
;; Returns the set of LA unioned with the set of LB.
;; Type signature: (union list list) -> set
(define (union LA LB)
(make-set (append LA LB)))
;; Returns the set of LA intersected with the set of LB.
;; Type signature: (intersection list list) -> set
(define (intersection LA LB)
(make-set (intersection-helper LA LB)))
(define (intersection-helper LA LB)
(cond [(null? LA) '()]
[(element? (car LA) LB)
(cons (car LA) (intersection-helper (cdr LA) LB))]
[else (intersection-helper (cdr LA) LB)]))
;; Returns SA ⊆ SB.
;; Type signature: (subset? set set) -> boolean
(define (subset? SA SB)
(cond [(null? SA) #t]
[(element? (car SA) SB)
(subset? (cdr SA) SB)]
[else #f]))
;; Returns whether SA and SB contain the same elements.
;; Type signature: (set-equal? set set) -> boolean
(define (set-equal? SA SB)
(and (subset? SA SB)
(subset? SB SA)))
;; Returns the difference of LA as a set and LB as a set.
;; Type signature: (set-difference list list) -> set
(define (set-difference LA LB)
(make-set (set-difference-helper LA LB)))
(define (set-difference-helper LA LB)
(cond [(null? LA) '()]
[(element? (car LA) LB)
(set-difference-helper (cdr LA) LB)]
[else (cons (car LA)
(set-difference-helper (cdr LA) LB))]))
;; Returns the symmetric difference of LA as a set and LB as a set.
;; Type signature: (sym-diff list list) -> set
(define (sym-diff LA LB)
(union (set-difference LA LB)
(set-difference LB LA)))
;; Returns the cardinality of L as a set.
;; Type signature: (cardinality list) -> int
(define (cardinality L)
(length (make-set L)))
;; Returns whether sets SA and SB are disjoint.
;; Type signature: (disjoint? set set) -> boolean
(define (disjoint? SA SB)
(null? (intersection SA SB)))
;; Returns SA ⊇ SB.
;; Type signature: (superset? set set) -> boolean
(define (superset? SA SB)
(subset? SB SA))
;; Returns the set of L, with e added to it.
;; Type signature: (insert element list) -> set
(define (insert e L)
(make-set (cons e L)))
;; Returns set S without element e.
;; Type signature: (remove element set) -> set
(define (remove e S)
(set-difference S (list e)))
;; Returns the relation ((1 1) (2 2) ... (n n))
;; Type Signature: (id int) -> relation
(define (id n)
(if (zero? n) '()
(cons (list n n)
(id (- n 1)))))
;; Returns whether R is reflexive over the domain [1, n].
;; Type Signature: (reflexive? relation int) -> boolean
(define (reflexive? R n)
(subset? (id n) R))
;; Returns the reflexive closure of R over the domain [1, n].
;; Type Signature: (reflexive-closure relation int) -> relation
(define (reflexive-closure R n)
(union R (id n)))
;; Returns the inverse of R.
;; Type Signature: (inverse relation) -> relation
(define (inverse R)
(map reverse R))
;; Returns whether R is symmetric.
;; Type Signature: (symmetric? relation int) -> boolean
(define (symmetric? R)
(set-equal? R (inverse R)))
;; Returns the symmetric closure of R.
;; Type Signature: (symmetric-closure relation) -> relation
(define (symmetric-closure R)
(union R (inverse R)))
;; Returns the set { y | (v, y) ∈ R }.
;; Type Signature: (relates-to vertex relation) -> set
(define (relates-to v R)
(make-set (relates-to-helper v R)))
(define (relates-to-helper v R)
(cond
[(null? R) '()]
[(= v (caar R))
(cons (cadar R)
(relates-to-helper v (cdr R)))]
[else (relates-to-helper v (cdr R))]))