Uploaded by xetoki2344

assignment 1 solutions

advertisement
SOEN331: Introduction to Formal Methods
for Software Engineering
Assignment 1: Algebraic Specifications
C. Constantinides and S. Symeonidis
January 23, 2015
1
Introduction and background
For this assignment you must work in pairs. A Graph Abstract Data Type (ADT), suitable
for undirected graphs where a generic element of type Element is stored at each edge, has
the following Application Programming Interface (API):
1. newgraph: Create and return an empty undirected graph.
2. vertices: Return the set of all the vertices of the graph.
3. edges: Return the set of all the edges of the graph.
4. countAllVertices: Return the number of vertices currently present in the graph.
5. countAllEdges: Return the number of edges currently present in the graph.
6. getEdge(v, w): Return the edge between vertices v and w; An error occurs if there is
no such edge.
7. incidentEdges(v): Return the set of the edges incident on vertex v.
8. opposite(v, e): Return the endvertex of edge e distinct from vertex v; an error
occurs if e is not incident on v.
9. endVertices(e): Return the set of the end vertices of edge e.
10. areAdjacent(v, w): Test whether vertices v and w are adjacent.
11. insertVertex(v): Insert a new vertex and return an updated graph.
12. removeVertex(v): Remove vertex v and all its incident edges and return an updated
graph.
13. insertEdge(v, w, x): Create and insert a new undirected edge with end vertices v
and w and storing element x and return an updated graph.
1
14. removeEdge(v, w): Remove edge (v, w) and return an updated graph.
15. getEdgeElem(e): Return the element associated with edge e.
16. replaceEdgeElem(e, x): Replace the element stored at edge e with x and return an
updated graph.
Notes: An undirected edge is modeled as a set {{v1 , v2 }, x} of its end vertices v1 and v2
with an element x. You may assume the availability of types N, Boolean, Edge (imports
type V ertex and it is defined over some generic Element type) and Set.
Axioms: Your axioms should correspond to the following principles:
1. Operation newgraph produces a graph with an empty collection of vertices.
2. Operation newgraph produces a graph with an empty collection of edges.
3. The number of vertices in a newly created graph is zero.
4. The number of edges in a newly created graph is zero.
5. The addition of two distinct vertices in a newly created graph produces a collection of
vertices with cardinality 2.
6. The addition of two identical vertices in a newly created graph produces a collection
of vertices with cardinality 1.
7. Operations insertVertex and removeVertex are mutually inverse.
8. Operations insertEdge and removeEdge are mutually inverse.
9. Two vertices are adjacent if they are directly connected.
10. For the graph (v)--x--(w)--y--(u), operation incidentEdges for w should return
the set {{{v, w}, x}, {{w, u}, y}}.
11. For a graph that contains (v)--x--(w)--y--(u), operation opposite for vertex v and
the edge that connects it to w should return the vertex w.
12. For the graph (v)--x--(w), operation endVertices on this edge should return the set
{v, w}
13. For the graph (v)--x--(w), operation getEdgeElem on the edge should return x.
14. For a graph that contains edge (v)--x--(w), operation replaceEdgeElem on this edge
with an argument y should successfully replace x with y.
2
Solution:
Spec: Graph (Element);
Sort: Graph;
Imports: Boolean, Edge, Set;
Operations:
newgraph → Graph
vertices : Graph → Set
edges : Graph → Set
countAllV ertices : Graph → N
countAllEdges : Graph → N
getEdge : Graph × V ertex × V ertex → Edge
incidentEdges : Graph × V ertex → Set
opposite : Graph × V ertex × Edge → V ertex
endV ertices : Graph × Edge → Set
areAdjacent : Graph × V ertex × V ertex → Boolean
insertV ertex : Graph × V ertex → Graph
removeV ertex : Graph × V ertex → Graph
insertEdge : Graph × V ertex × V ertex × Element → Graph
removeEdge : Graph × V ertex × V ertex → Graph
getEdgeElem : Graph × Edge → Element
replaceEdgeElem : Graph × Edge × Element → Graph
Variables:
g: Graph; v, w, u: Vertex; x, y: Element;
Axioms:
vertices(newgraph) = { }.
edges(newgraph) = { }.
countAllVertices(newgraph) = 0.
countAllEdges(newgraph) = 0.
size(vertices(insertVertex(insertVertex(newgraph, v), w))) = 2.
size(vertices(insertVertex(insertVertex(newgraph, v), v))) = 1.
size(vertices(removeVertex(insertVertex(newgraph, v), v))) = 0.
3
size
(edges
(removeEdge
(insertEdge
(insertVertex
(insertVertex
(newgraph, v), w), v, w, x), v, w))) = 0.
areAdjacent
(insertEdge
(insertVertex
(insertVertex
(newgraph, v), w), v, w, x), v, w) = true.
incidentEdges
(insertEdge
(insertEdge
(insertVertex
(insertVertex
(insertVertex
(newgraph, v), w), u), v, w , x), w, u, y), w) = { {{v, w}, x}, {{w, u}, y} }.
opposite
(insertEdge
(insertEdge
(insertVertex
(insertVertex
(insertVertex
(g, v), w), u), v, w , x), w, u, y), v, getEdge(g, v, w)) = w.
endVertices
(insertEdge
(insertVertex
(insertVertex(g, v), w), v, w, x), getEdge(g, v, w)) = { v, w }.
getEdgeElem
(insertEdge
(insertVertex
(insertVertex(g, v), w), v, w, x), v, w), getEdge(g, v, w)) = x.
4
getEdgeElem
(g, getEdge
(replaceEdgeElem
(insertEdge
(insertVertex
(insertVertex(g, v), w), v, w, x), getEdge(g, v, w), y), v, w)) = y.
5
A Directed Graph ADT subclassifies Graph and adds the following operations to the API:
1. newdirectedgraph: Create and return an empty directed graph.
2. insertDirectedEdge(v, w, x): Create and insert a new directed edge with origin v
and destination w and storing element x, and return an updated graph.
3. incomingEdgesOf(v): Return the set of all edges incoming into vertex v.
4. inDegreeOf(v): Return the indegree of vertex v.
5. outDegreeOf(v): Return the outdegree of vertex v.
6. outgoingEdgesOf(v): Return the set of all edges outgoing from the specified vertex.
Notes: A directed edge is modeled as a set {⟨v1 , v2 ⟩, x} of its end vertices v1 and v2 with a
direction from v1 to v2 , with an element x.
Axioms: Your axioms should correspond to the following principles:
1. Operation newdirectedgraph produces a directed graph with an empty collection of
vertices.
2. Operation newdirectedgraph produces a directed graph with an empty collection of
edges.
3. The number of vertices in a newly created directed graph is zero.
4. The number of edges in a newly created directed graph is zero.
6
For each of the axioms 5-8, you should first consider creating the following graph:
x
v
w
y
5. The set of incoming edges to vertex v in the graph is {{⟨w, v⟩, y}}.
6. The indegree of vertex v in the graph is 1.
7. The outdegree of vertex v in the graph is 1.
8. The set of outgoing edges of vertex v in the graph is {{⟨v, w⟩, x}}.
9. For the graph (v)--x-->(w)<--y--(u), the indegree of vertex w is 2.
10. For the graph (v)--x-->(w)<--y--(u), the outdegree of vertex v is 1.
7
Solution:
Spec: Directed Graph (Element);
Sort: DGraph;
extend Graph (Element) by
Operations:
newdirectedgraph → DGraph
insertDirectedEdge : DGraph × V ertex × V ertex × Element → Graph
incomingEdgesOf : DGraph × V ertex → Set.
inDegreeOf : DGraph × V ertex → N0
outDegreeOf : DGraph × V ertex → N0
outgoingEdgesOf : DGraph × V ertex → Set
Variables:
v, w, u: Vertex; x, y: Element;
Axioms:
vertices(newdirectedgraph) = { }.
edges(newdirectedgraph) = { }.
countAllVertices(newdirectedgraph) = 0.
countAllEdges(newdirectedgraph) = 0.
incomingEdgesOf
(insertDirectedEdge
(insertDirectedEdge
(insertVertex
(insertVertex
(newdirectedgraph, v), w),
v, w, x), w, v, y), v) = { {<w, v>, y} }.
inDegreeOf
(insertDirectedEdge
(insertDirectedEdge
(insertVertex
(insertVertex(newdirectedgraph, v), w) v, w, x), w, v, y), v) = 1.
outDegreeOf
(insertDirectedEdge
(insertDirectedEdge
(insertVertex
(insertVertex(newdirectedgraph, v), w) v, w, x), w, v, y), v) = 1.
8
outgoingEdgesOf
(insertDirectedEdge
(insertDirectedEdge
(insertVertex
(insertVertex
(newdirectedgraph, v), w), v, w, x), w, v, y), v) =
{ {<v, w>, x} }.
inDegreeOf
(insertDirectedEdge
(insertDirectedEdge
(insertVertex
(insertVertex
(insertVertex
(newdirectedgraph, v), w), u), v, w, x), u, w, y), w) = 2.
outDegreeOf
(insertDirectedEdge
(insertDirectedEdge
(insertVertex
(insertVertex
(insertVertex
(newdirectedgraph, v), w), u), v, w, x), u, w, y), v) = 1.
2
Your assignment
Your task is to a) deploy an Algebraic Specification to define Graph and Directed Graph and
b) implement your specification in any language. For the formal specification, there is no
need to reproduce the description of the operations. Each axiom must be demonstrated by
a statement in the main body of your implementation.
You must work strictly between the two of you and you may not seek assistance on this
assignment from anyone, including the instructor or the Teaching Assistants. You will only
receive feedback upon submission.
3
What to submit
Your should package your implementation in a zip file that will contain all files. For the
formal specification, plain text is acceptable. Provide internal documentation in the form of
comments to clearly indicate your names and id’s. Your zip file should have an 8-digit name
that is the concatenation of the first 4 digits of each partner’s id. You should electronically
submit the zip file.
9
4
Due date and late submissions
Your assignment is due by February 13 at 21:00. Any late submission by one day will get a
50% penalty and it will subsequently receive a 10% penalty per day.
5
Disputes
If you wish to dispute the grade that you receive on your assignment, please email me, providing all details. I will discuss your dispute with the Teaching Assistants and respond to
your email. My decision in my response will be final.
10
Download