Graph Representations: Adjacency Matrix & Adjacency List

Representations of Graph

Executive Summary

In this comprehensive exploration of sequential and linked representations of graphs, we traverse the depths of both structures, their strengths, weaknesses, and applicability. We kick-off by establishing the core problem—efficiently storing and manipulating graph data, where both representations find their relevance. This thesis uncovers the basics before diving into complex facets like adjacency matrix and list, incidence matrix, and list, alongside the C++ code implementation. Further, we cross-examine these concepts with Object-Oriented Programming (OOP), revealing surprising links. Lastly, we steer our lens to real-world applications, clarifying the theoretical fog with practical examples. Be prepared to grapple with advanced-level concepts, carefully broken down for seamless comprehension. The curtain will rise on lesser-known topics like multi-graphs and hyper-graphs in subsequent discussions.

Introduction

Imagine you are building a large-scale social network. You find yourself facing a computational challenge—how do you efficiently store and process information about connections between millions of users? One potential solution would be to leverage data structures known as graphs, more specifically, the sequential or linked representation of graphs.

A graph, in computer science parlance, is an abstract data type that aims to implement the graph concept from mathematics—a graph is composed of vertices (also called nodes) and edges (connections between nodes). Two main types of graph representation, sequential and linked, offer different trade-offs regarding memory usage and operation speed. Understanding these can dramatically impact how you design and implement your social network.

Before diving deep, let's lay the foundation by briefly examining the basics of graphs, their types, and components. After this, we'll progress towards the complexities of sequential and linked representation in graphs, spiced up with appropriate C++ implementation. Ready to embark on this exhilarating journey? Let's begin!

1. Graph Basics

A graph G is an ordered pair G := (V, E), comprising a set V of vertices or nodes, together with a set E of edges or arcs. Each edge is a 2-element subset of V. Every graph must have at least one vertex. The vertices may be part of the graph structure, or may be external entities represented by integer indices or references. If the edges in a graph are all one-way, the graph is a directed graph or a digraph.

Graphs are used to represent many real-world things such as systems of roads, airline flights from city to city, how the Internet is connected, etc. They can be used to model many types of relations and processes in various scientific structures.

2. Graph Representations

There are two common ways to represent a graph, sequential (also known as adjacency matrix) and linked (also known as adjacency list). Each has its advantages and disadvantages, and the right choice depends on the specific parameters of the problem at hand.

2.1 Adjacency Matrix

Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. A slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. The adjacency matrix for an undirected graph is always symmetric. This representation is also used for representing weighted graphs.

An adjacency matrix is a way of representing a graph as a matrix of booleans. If the value of matrix[i][j] is '1', it means there is an edge connecting vertices i and j, otherwise, there is no edge. It is particularly convenient for dense graphs, where the number of edges is close to the number of vertices squared.

The adjacency matrix provides constant-time access to determine if there is an edge between two nodes, but it requires O(V2) space, where V is the number of vertices. It's ideal for dense graphs, which contain a large number of edges.

Advantages:

Disadvantages:

class Graph {
  int V;
  int** adj;

public:
  Graph(int V);
  void addEdge(int v, int w);
  void printGraph();
};

Graph::Graph(int V) {
  this->V = V;
  adj = new int*[V];
  for (int i = 0; i < V; i++) {
    adj[i] = new int[V];
    for (int j = 0; j < V; j++)
      adj[i][j] = 0;
  }
}

void Graph::addEdge(int v, int w) {
  adj[v][w] = 1;
  adj[w][v] = 1;
}

void Graph::printGraph() {
  for (int v = 0; v < V; ++v) {
    for (int w = 0; w < V; ++w)
      cout << adj[v][w] << " ";
    cout << "\n";
  }
}

This C++ code creates an adjacency matrix and defines methods to add edges and print the graph. Notice the nested loops in the printGraph method, reflecting the O(V2) time complexity.

C Implementation

#include <cstdio>
int main(){
    int n, m;
    scanf("%d %d", &n, &m);
    int adjMat[n + 1][n + 1];
    for (int i = 0; i < m; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        adjMat[u][v] = 1;
        adjMat[v][u] = 1;
    }
    return 0;
}

2.2 Adjacency List

Contrarily, an adjacency list represents a graph as an array of linked lists. The index of the array represents a vertex and each element in its linked list represents the other vertices that form an edge with the vertex. The adjacency list is preferable for sparse graphs, where the number of edges is far less than the number of vertices squared.

Adjacency List is an array of linked lists. The size of the array is equal to the number of vertices. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This representation can also be used torepresent a weighted graph where the weights of edges can be stored as lists of pairs.

The adjacency list requires less space, O(V + E), where V is the number of vertices and E is the number of edges. However, to find out whether an edge exists between two vertices requires traversing a linked list, taking O(V) time in the worst case.

Advantages:

Disadvantages:

class Graph {
  int V;
  list* adj;

public:
  Graph(int V);
  void addEdge(int v, int w);
  void printGraph();
};

Graph::Graph(int V) {
  this->V = V;
  adj = new list[V];
}

void Graph::addEdge(int v, int w) {
  adj[v].push_back(w);
  adj[w].push_back(v);
}

void Graph::printGraph() {
  for (int v = 0; v < V; ++v) {
    cout << "\n Adjacency list of vertex " << v << "\n head ";
    for (auto x : adj[v])
      cout << "-> " << x;
    printf("\n");
  }
}

This C++ code creates an adjacency list and defines methods to add edges and print the graph. Observe the O(V + E) space complexity in the printGraph method.

3. Incidence Matrix and Incidence List

Besides adjacency matrix and adjacency list, graphs can also be represented using incidence matrix and incidence list. An incidence matrix is a 2D boolean matrix, where the row represents vertices and the column represents edges. The entry of row i and column j is '1' if the vertex i is part of edge j, otherwise '0'. Incidence list, on the other hand, is similar to adjacency list, but instead of lists of vertices, we have lists of edges for every vertex.

4. Graphs and Object-Oriented Programming

Now, let's shift our lens to view graphs from an Object-Oriented Programming (OOP) perspective. In OOP, a graph can be seen as an object, with vertices and edges being its member variables. The operations like adding an edge, deleting a vertex, etc., can be member functions. This encapsulation of data and operations within an object aligns well with the graph data structure. The adjacency list and adjacency matrix can be considered different strategies for implementing the same object - the graph. This is a classical example of the Strategy Pattern in OOP.

5. Applications of Graphs

Graphs have widespread applications in various domains. From computer networks, where nodes represent computers and edges represent connections, to transportation networks, where nodes represent intersections and edges represent roads. In fact, social networks like Facebook and LinkedIn use graphs to represent users as nodes and friendships as edges. Here, efficient graph representations, traversal, and manipulation algorithms are critical for performance and scalability.

Conclusion

After this whirlwind exploration of sequential and linked representations of graphs, we hope you're left with a sense of awe and curiosity. Understanding these representations is like unlocking a new level in the game of data structures, opening the doors to more advanced algorithms and real-world applications.

What Lies Beyond?

In the subsequent article, we'll be charting our course through the intriguing waters of multi-graphs and hyper-graphs. Buckle up, because this journey will expand your horizons, deepening your understanding of the vast and complex world of graphs. Stay tuned and keep exploring!