Which operations does the graph ADT offer?
Accessors: endVertices(e), opposite(v, e), areAdjacent(v, w) and replace(v, x) / replace(e, x). Updates: insertVertex(o), insertEdge(v, w, o), removeVertex(v), which also removes all of v's edges, and removeEdge(e). Iterators: incidentEdges(v), vertices() and edges().
| Group | Method | Does |
|---|---|---|
| access | endVertices(e) |
the two end vertices of e |
| access | opposite(v, e) |
the vertex at the other end of e |
| access | areAdjacent(v, w) |
whether an edge joins v and w |
| access | replace(v, x), replace(e, x) |
swap the element stored at a vertex or edge |
| update | insertVertex(o) |
add a vertex storing o, return it |
| update | insertEdge(v, w, o) |
add an edge from v to w storing o, return it |
| update | removeVertex(v) |
remove v and every edge incident on it |
| update | removeEdge(e) |
remove e |
| iterate | incidentEdges(v), vertices(), edges() |
all edges at v, all vertices, all edges |
On the airport graph: endVertices of the 1843-mile edge returns the array [SFO, ORD]; for the 337-mile edge e, opposite(LAX, e) returns SFO; areAdjacent(SFO, ORD) is true and areAdjacent(SFO, PVD) false; replace corrects a distance or a renamed airport code.
Vertices and edges are positions: objects the graph hands out and later accepts back, each storing one element. insertVertex returns the new vertex, the same idea as an adaptable priority queue whose insert returns the new entry: the caller keeps the handle to pass it to insertEdge later, so the graph never has to search for a vertex by its element. Building a graph therefore starts with vertices: both ends must exist before an edge between them, which is why code often inserts all vertices first and then all edges.
removeVertex takes the incident edges along because an edge is defined by its two ends. Without one of them it would point at a vertex that no longer exists, and the next opposite call on it would fail, in Java with a NullPointerException.
Go deeper:
Graph (abstract data type): operations — the usual operations of a graph type, with slightly different names.