Contents

1. Introduction To Graphs
2. Terms Associated With Graphs
3. Breadth First Search

Complete Guide To Graphs

Graph Data Structure

Graph is a non-linear data structure defined as a collection of vertices(nodes) and edges(links). The edges in a graph are used to connect the nodes. The nodes in the graph can be used to store data. Each edge of the graph can also store data in the form of weights as we will see below

A graph is represented as G = (V, E) where,

  • V is the set of vertices or nodes
    • E is the set of edges in the Graph G . It is represented as ordered pairs of vertices (i,j)

Example :

https://res.cloudinary.com/dc0mjpwf8/image/upload/v1590067922/ArticleImages/Graph/Graph_e1oudh.png

Here:

V = {1, 2, 3, 4, 5}
E = {(1,2), (1,3), (1,4), (2,3), (3,4), (3,5), (4,5)}

For the purpose of coding interviews, we will only consider Simple Graphs (definition given below)

Types of Graphs

Simple Graph

A simple graph is a graph which satisifes two conditions :

  1. No self loops: A self loop is defined as an edge from the node to itself. A simple graph does not contain any self loops.
  2. No multiple edges: There cannot be multiple edges between two nodes. For example, if there is already an edge b/w node A and node B, there cannot be another edge in the same direction b/w node A and node B.

For the purpose of coding interviews and this tutorial, we will only consider simple Graphs.

Undirected Graphs

Undirected graph is a graph in which the edges don't have a fixed direction. For example, in the below graph the edge b/w node A and B can be used to traverse from both A to B as well as from B to A. Therefore, it is an undirected graph.

Directed Graphs

A graph in which all the edges have a fixed direction is called as a directed graph. For example, in the below graph the edge b/w node A and B can be only used to traverse b/w node A to node B and not the other way round. This is because the edge originates at node A and terminates at node B.

Directed and undirected Graph

Weighted Graphs

A weighted graph is a graph in which the edges will have weights with them. For example, in the graph given below you will see a number on top of the edges which represents the weight of that edge.

Unweighted Graphs

An unweighted graph is a graph in which the edges don't have any weight associated with them. You can also consider all of the edges to have a weight of 1 in an unweighted graph.

Binary Weighted Graphs

Binary weighted graphs are a special case of weighted graphs in which each edge will have zero or one weight.

Cyclic Graph

A cyclic graph is a graph which can contain a cycle within itself.

Directed Acyclic Graph

A directed acyclic graph is a graph which is a directed graph as well as it cannot contain any cycle. Directed acyclic graphs are used a lot with problems involving topological sorting.

Connected Graph

A connected graph is a graph where there is a path from each node to any other node.

Disconnected Graph

A disconnected graph is a graph where there maybe certain nodes which are not reachable from other nodes. Opposite of connected graphs.

Weighted graph

Connected Graph

Graphs Implementation

In this section we will cover the ways in which we can implement graphs in code. The graph implementations covered below can be used to implement both directed as well as undirected graphs. They can also be used for implementing both weighted as well as unweighted graphs. There are majorly two ways to represent graphs

Adjacency Matrix

An adjacency matrix is a 2D array of V x V vertices. Each row and column represent a vertex. ith row in a adjacency matrix represents the edges originating from node i. Similarly, ith column in an adjacency matrix represnets the edges incident towards node i

a[i][j] = 1 – If there is an edge originating from node i to node j a[j][i] = 1 - if there is an edge originating from node j to node i

a[i][j] = 0 – If there is no edge originating from node i to node j a[j][i] = 0 - if there is no edge originating from node j to node i

https://res.cloudinary.com/dc0mjpwf8/image/upload/v1590073349/ArticleImages/Graph/adjacency_matrix_x4yjgh.png

Since the graph creates a matrix of size V*V, with each cell storing a number, it ends up using O(V*V) space. This is one downside of the adjacency matrix where it ends up using V*V space even if no of edges are less. Therefore, it should not be used for sparse graphs

Adjacency List

Unlike adjacency matrix, adjacency list is a more optimized way of storing the graphs. In this representation, we create an array of linked lists where each linked list represents the edges originiating from the corresponding node.

For example, if there are 5 nodes in the graph there would be 5 linked lists, with the 0th linked list stores the vertices to which the edges originate from node 0, 1st linked list represents the vertices to which the edges originate from node 1 and so on.

Here, the total space used is O(V + E). This is because even if there are no edges, we end up creating V empty lists which would take up at least V space. Each edge will be added in the corresponding linked list (at least once for directed graphs and at most twice for undirected graphs).

https://res.cloudinary.com/dc0mjpwf8/image/upload/v1590073349/ArticleImages/Graph/adjacency_list_ml5jso.png

Practice Problems

Breadth First Search

    Depth First Search

      Topological Sorting