8.10
3.6 Functional Graphs
(require cpsc411/graph-lib) | package: cpsc411-lib |
This library defines a functional graph library, where graphs are represented as
lists of proper association lists for simple serialization.
A predicates that returns #t if v is a graph.
A graph is an proper association list from a vertex to a list of vertexes. A directed edge exists between a vertex V1 to V2 if V2 is in the list associated with V1.
Examples:
> (graph? '()) #t
> (graph? '((x ()))) #t
> (graph? '((x (y)))) #t
> (graph? '((x y))) #f
> (graph? '((x (y)) (y (x)))) #t
Returns a new graph, initialized to include each element of vs as
a vertex in the graph.
Examples:
procedure
(add-vertex g v) → graph?
g : graph? v : any/c
Returns a new graph equivalent to g with v added a vertex.
Example:
> (add-vertex (new-graph) 'a) '((a ()))
procedure
(remove-vertex g v) → graph?
g : graph? v : any/c
Returns a new graph equivalent to g twith the vertex v removed from the graph.
Examples:
> (add-vertex (new-graph) 'a) '((a ()))
> (remove-vertex (add-vertex (new-graph) 'a) 'a) '()
> (add-directed-edge (new-graph '(a b)) 'a 'b) '((b ()) (a (b)))
> (remove-vertex (add-directed-edge (new-graph '(a b)) 'a 'b) 'b) '((a ()))
procedure
(add-directed-edge g v u) → graph?
g : graph? v : any/c u : any/c
Returns a new graph equivalent to g with a directed edge from
v to u.
Assumes both v and u are already vertexes in the graph.
Example:
> (add-directed-edge (new-graph '(a b)) 'a 'b) '((b ()) (a (b)))
Returns a new graph equivalent to g with a directed edge from
v to u, and a directed edge from u to v.
Assumes both v and u are already vertexes in the graph.
Example:
Returns a new graph equivalent to g with a directed edge from
v to every element of us.
Assumes all vertexes are already in g.
Example:
> (add-directed-edges (new-graph '(a b c d)) 'a '(b c)) '((d ()) (c ()) (b ()) (a (c b)))
Returns a new graph equivalent to g with a directed edge from
v to every element of us, and from every element of
us to v.
Assumes all vertexes are already in g.
Example:
procedure
(get-neighbors g v) → graph?
g : graph? v : any/c
Returns a list of each vertex with an edge coming from v in the graph
g.
Returns the empty set if v isn’t in the graph.
Examples:
> (add-edges (new-graph '(a b c d)) 'a '(b c)) '((d ()) (c (a)) (b (a)) (a (c b)))
> (get-neighbors (add-edges (new-graph '(a b c d)) 'a '(b c)) 'a) '(c b)