0% completed
As discussed earlier in the course, an abstract data type (ADT) is a theoretical concept that defines a set of operations and their behavior without specifying the internal representation of the data or the algorithms used to implement those operations. It provides a high-level description of the data and the functions that can be performed on it.
Here are some of the operations can be performed on graphs:
- Adding a new vertex
- Removing a vertex
- Adding an edge between two vertices
- Removing an edge between two vertices
- Getting a list of all the vertices
- Checking if two graph nodes are adjacent to each other or not
- Getting count of the total vertices in the graph
- Getting count of the total edges in the graph
- Getting a list of the graph edges
- Getting neighbors of a graph vertex
There are a few other ADT operations involving graph searching and traversal. Searching and traversal operations require detailed illustrations. Thereby, we will discuss those in the Graph Traversals section.
Implementing the ADT operations
In the beginning of this section, we mentioned ten major and the most common ADT operations associated with the graphs. Let's discuss each operation individually - understanding how is it implemented.
1. add_vertex(vertex)
The function add_vertex(vertex)
is a common operation in graph theory, where it adds a new vertex (also called a node or point) to a graph data structure. The vertex is a fundamental unit of a graph and represents an entity or an element. Adding a vertex expands the graph and creates potential connections (edges) between this new vertex and existing vertices.
In an adjacency list, each vertex is associated with a list of its adjacent vertices. We are using a dictionary or map for implementing the adjacency list. Thereby, we are able to store node key and value (s) associated with it.
For this course, we assume that graphs don't have any extra satellite data or values associated with it. Therefore, we will add an empty vector or list as a value attribute for the new graph node or vertex.
You can add a new vertex following the two main steps:
-
Check if vertex already exists
- Use predefined map or dictionary search methods
-
If not, add it by inserting new key-value pair
- Keep value of the new inserted vertex as empty list
Here is a generalized pseudocode:
Graph is represented as: adjacencyList (map/dictionary): key: vertex value: list of adjacent vertices Function addVertex(vertex) if vertex does not exist in adjacencyList add new key-value pair: key: vertex value: empty list end if end Function
Let's now see the actual implementation of the add_vertex()
method.
2. add_edge(vertex1, vertex2)
Pseudocode
Function add_edge(vertex1, vertex2): if vertex1 is not present in adjacencyList: initialize adjacencyList[vertex1] as an empty list add vertex2 to adjacencyList[vertex1] if vertex2 is not present in adjacencyList: initialize adjacencyList[vertex2] as an empty list add vertex1 to adjacencyList[vertex2] End Function
Explanation
To implement the add_edge()
function, we need to add an undirected edge between vertex1
and vertex2
in the graph's adjacency list representation.
-
For
vertex1
:- Check if
vertex1
is already present in the adjacency list. - If not, initialize its entry as an empty list (or vector in C++).
- Append
vertex2
tovertex1
's adjacency list, indicating an edge.
- Check if
-
For
vertex2
:- Similarly, check if
vertex2
exists in the adjacency list. - If not, initialize its entry as an empty list (or vector in C++).
- Append
vertex1
tovertex2
's adjacency list, ensuring the graph is undirected.
- Similarly, check if
A complete executable example
Table of Contents
Implementing the ADT operations
- add_vertex(vertex)
- add_edge(vertex1, vertex2)
A complete executable example