Breaking the Ice with Graphs
By: Leander Melroy Maben
Tags: Mathematics, Data Structures, Computer Science, Graph Theory, Programming

What is a Graph Data Structure?

A graph is a tool in mathematics that is used to depict the relationship between entities.

A simple directed graph

The circles are called vertices and represent some entities. These could be anything ranging from stations in a rail network to members of a family.

The lines are called edges and represent some relation.

There are 2 types of graphs — directed graphs and undirected graphs. The one shown above is directed, which means that the edges go in 1 direction. So I can go from 1 to 2 but not 2 to 1. Getting rid of the arrows would make it an undirected graph which would mean all edges are 2-way.

Graphs are extremely useful wherever we have to solve problems involving objects related to each other. In the real world computer networks, social networks, cities connected by highways can all be analyzed as graph problems. The applications are endless — from calculating the similarity between 2 twitter users or calculating the optimal path a delivery truck must take to city planning and water supply management.

Basic Terms:

Walk: Any sequence of edges joining a sequence of vertices. Suppose you go from node 1 to 2 to 4 — it would constitute a walk 1->2,2->4.

Trail: A walk in which all edges are distinct. So, you do not travel a given edge more than once.

Cycle: A non-empty trail starting and ending at the same vertex.

Representing a Graph in a Computer

There are 2 popular ways to represent a graph in code:

Adjacency matrix

2. Adjacency List

Adjacency Matrix

Suppose a graph has n vertices. We can represent the connections between them by using an n x n matrix.

Follow these steps:

Initialize an n x n matrix with 0s( if the vertices are numbered from 1 it would be convenient to use a (n+1)x(n+1) matrix ignoring the 0th row and column). If there is an edge from vertex u to v set matrix[u][v]=1. Do this for each edge.

Your adjacency matrix is ready✨.

Thus every connection is represented by a 1 at the appropriate position.

Adjacency Matrix for the Graph shown above.

Adjacency List

An Adjacency list is a list of lists.

You have a main list with n sub-lists corresponding to the n vertices.

The ‘ith’ sub-list contains vertices that vertex ‘i’ is connected to.

For Example, if there edges from vertex 1 to 2,3 and 4, then the first sub-list will contain 2,3 and 4.

Adjacency List for the Graph shown above.

In C++ this structure can easily be implemented by using 2-d vectors.

Matrix Vs List:

(|V| stands for number of vertices and |E| stands for number of edges.)

A matrix has certain advantages:

We can check if an edge exists between 2 vertices u and v by checking mat[u][v] ie. in O(1) time. But in an adjacency list, you would have to traverse the whole sub-list of u and hence the worst case could take O(|V|) time if u is connected to every other vertex(Adjacency lists can be implemented in other ways to eliminate this problem).

Removing or inserting an edge can be done in constant time by changing one value again O(1) while an adjacency list could take O(|V|).

A List has the following advantage: