7.0

Week 11 Set b

home work!

Programming Language ISL+

Due Date Thursday 11/15 at 9:00pm (Week 11)

Purpose To design functions over graphs.

Finger Exercises

Exercise 1 From HTDP, 471 472

Graded Exercises

For this homework, we will use the following data definition for a graph:
; A Graph is a (make-graph [List-of Symbol] [Symbol -> [List-of Symbol]])
(define-struct graph [nodes neighbors])
; and represents the nodes and edges in a graph
; All of the symbols in nodes are assumed to be unique, as are the symbols in
; any list returned by neighbors, and all of the symbols returned by neighbors
; are assumed to be in nodes.

Graph Warmup

Exercise 2 Design the function neighbor-of? that takes a graph and two symbols and determines if the second symbol is a neighbor of the first one. Assume both symbols are in the graph.

Exercise 3 Design the function both-neighbors that takes two symbols and a graph and returns the list of both of their neighbors, and be sure not to include any duplicates in the output. Assume both symbols are in the graph. Hint: A function you’ve previously designed will likely be of help here.

Graph Equality: Exact

Exercise 4 Design graph=?, which determines if two graphs are equal. Keep in mind the order of nodes as well as the order of edges are unimportant, but the actual names of the nodes being the same as well as the same nodes having the same neighbors are.

Graph Manipulation

For this portion of the homework, you should use the following testing pattern when testing some function f that outputs a graph. Check the docs for check-satisfied for more info.

; graph=?/curried : Graph -> [Graph -> Boolean]
; Curried graph=?
(define graph=?/curried (λ (g1) (λ (g2) (graph=? g1 g2))))
 
; f : Graph ... -> Graph
; Do something to g
(define (f g ...) ...)
 
(check-satisfied (f some-input-graph ...)
                 (graph=?/curried
                  some-expected-graph))

Also, as you create and manipulate graphs, always be sure to uphold the assumptions we made about graphs in our data definition.

Exercise 5 Design the function collapse, which takes in three nodes and a graph. The first two nodes are assumed to be in the graph, and the third is assumed not to be.

The function collapses the first two nodes into one new node, which is named by the third node. All nodes in the graph that were pointing to either of the first two given nodes should now point to this new one, and the new node should point to all nodes either of the first two nodes were pointing to. Hint: A function you’ve previously designed will likely be of help here.

Exercise 6 Design reverse-edges which takes a graph and reverses the edges. Hint: A function you’ve previously designed will likely be of help here.

Exercise 7 Design the function rename, which takes a graph and a list of symbols that is the same length of the nodes in the graph and renames the nodes in the graph to the names in the list of symbols. The nth node in the graph’s nodes should be renamed as the nth node in the given list. Assume the given list has no duplicates.

Exercise 8 Design the function swap, which swaps a graph’s nodes with its edges.

More formally, if the n1th node points to the n2th node in the graph, the new graph should have a node of the form 'n1->n2. If nodes 'n1->n2 and 'n3->n4 exist in this new graph, then 'n1->n2 points to 'n3->n4 if and only if n2 equals n3. We use this index-based naming convention as a way to avoid any possible difficulty with potentially problematic initial node names, such as 'a->b.

For example, here is a graph and its swapped verison:

(make-graph '(a b c)
            (λ (n) (cond [(symbol=? n 'a) '(b c)]
                         [(symbol=? n 'b) '(b)]
                         [(symbol=? n 'c) '(a)])))
(make-graph '(0->1 0->2 1->1 2->0)
            (λ (n) (cond [(symbol=? n '0->1) '(1->1)]
                         [(symbol=? n '0->2) '(2->0)]
                         [(symbol=? n '1->1) '(1->1)]
                         [(symbol=? n '2->0) '(0->1 0->2)])))

You will find the following function helpful. Note that this is the only place in the homework any function from racket/string is allowed to be used.
(require racket/string)
; node-name->numbers : Symbol -> (list Nat Nat)
; Convert a symbol of the form 'n1->n2 to (list n1 n2)
(define (node-name->numbers s)
  (map string->number (string-split (symbol->string s) "->")))
(check-expect (node-name->numbers '0->3) '(0 3))

Hint: A function you’ve previously designed will likely be of help here.

Graph Walks

Exercise 9 Design close?, which takes a graph, two nodes, and a natural number, and determines if the second node is within that many steps of the first node. Assume both nodes are in the graph.

Exercise 10 Design find-all-paths, which finds all possible paths between two given nodes in a graph. Assume both nodes are in the graph. Some examples are illustrated below:
(define G1
 (make-graph '(A B C D E F G)
             (λ (n)
               (cond [(symbol=? n 'A) '(B E)]
                     [(symbol=? n 'B) '(E F)]
                     [(symbol=? n 'C) '(D)]
                     [(symbol=? n 'D) '()]
                     [(symbol=? n 'E) '(C F A)]
                     [(symbol=? n 'F) '(D G)]
                     [(symbol=? n 'G) '()]))))
(find-all-paths G1 'C 'C) -> '((C)) ; src = dest
(find-all-paths G1 'C 'G) -> '() ; no paths from 'C to 'G
(find-all-paths G1 'A 'B) -> '((A B))
(find-all-paths G1 'E 'G) -> '((E F G)  (E A B F G))
(find-all-paths G1 'B 'G) -> '((B E F G)  (B F G))
(find-all-paths G1 'A 'G) -> '((A B E F G) (A B F G) (A E F G))

Graph Properties

Use check-satisfied to test the following functions, on graphs which both do and don’t have the properties they are checking. You will have to do some slightly clever manipulation to test on graphs that don’t have the property in question, but it shouldn’t require defining a new, named function.

Exercise 11 Design the function connected? which determines if every node in a graph can reach every other node in a graph. Hint: A function you’ve previously designed will likely be of help here.

Exercise 12 Design undirected? which determines if each edge in the graph has a matching edge going the opposite direction. Hint: A function you’ve previously designed will likely be of help here.

Extra Credit - Graph Equality: Structural

The following exercise is extra credit. Not completing it will have no negative impact on your grade.

Exercise 13 Design graph-shape=?, which determines if two graphs have the same shapes. Here, we do not care about the names of the nodes being the same. Hint: A function you’ve previously designed will likely be of help here.