Graph as an Abstract Data Type (ADT)

 

Objectives of this lecture

q       View graph as an ADT and learn its operations and how they could be realized.

 

Another view of Graph?

q       So far we have used graph as a means of describing problems, which we then solve using ADT’s such as queue, linked list and stack.

q       However, graphs can themselves be regarded as a data structures.  Thus, in addition to being used for problem description, they can also be used as a computational tool for solving problems.

q       Because of their generality and flexibility, graphs are powerful data structures that are used in advanced applications where other data structures are usually inadequate, such as database management system design.

q       In this concluding lecture, we view graph as an ADT and try to identify the operations that can be performed.

 

A Graph ADT

q       An abstract datatype for a graph should provide operations for constructing a graph (adding and removing edges and vertices) and for checking connections in a graph.

q       Some of the possible operations are:

 
CreateGraph(Graph graph, int n)
// Creates and initialises a graph of given size (no. of nodes)
// Pre: None
// Post: Graph is initialized.
 
void AddEdge(int v, int w) 
// Adds edge from v to w
// Pre: v and w are vertices in a graph
// Post: Number of edges is incremented by 1
 
void RemoveEdge(int v, int w)
// Removes an edge}
// Pre: There is an edge in graph from v to w
// Post: Number of edges is decremented by 1
 
Boolean IsAdjacent(int v, int w)
/* returns true if there is an edge in graph from v to w, else returns false */
// Pre: Both v and w are valid vertices in the graph
 
int edgeSize()
//Returns:  The number of edges |E|.
// Pre: None.
// Post: Number of edges is returned.
 
int vertexSize()
//Returns the number of vertices in graph, |V|
// Pre:  None.
// Post:  Number of vertices is retured.
 

q       Other operations might be useful, but this provides a reasonable minimal set.

 

Implementation

q       The implementation of these operations depends on the graph representation being used.

 

Adjacency Table representation.

q       If the graph is represented as an Ajacency table, then all the ADT operations specified above can be straightforwardly implemented.

  1 2 3 4 5
  1 F T F T F
  2 F F T F F
  3 F F F F F
  4 F F T F T
  5 F F F F F

 

Adjacency Lists

q       Adjacency Table representation is very simple, but it is inneficient (in terms of space) for sparse graphs, ie, those without many edges compared with nodes (vertices).

q       Such a graph would still need an NxN matrix, but would be almost full of 'False's. So an alternative is to have associated with each node a list (or set) of all the nodes it is linked to via an edge.

q       As we saw before, there are three ways of representing Adjacency list

q       The implementation of the operations above depends on the particular implementation.

q       For example, if the representation is mixed, the to implement the following operation:


 

IsAdjacent(v, w)
 

q       We need to go to vertex v and then search through its list of edges.  If any of the edges has w as its end-node, then true is returned, else, false is returned.

 

Which to use?

q       As is so often the case, the best implementation for a graph depends on properties of the graphs we want to operate on, and on which operations are likely to be used most often. The adjacency matrix representation is not space efficient for sparse graphs, but certain operations such as Is Adjascent(v, w) are likely to be much more efficient

 

Exercises

q       Try the problems on Exercises 11.6 from page 530 of your book.