Representing a graph using adjacency matrix

Representing a graph using adjacency matrix

Graphs are useful tools for visualizing or representing real world problems, but in order to use graphs for computing problems, we need to represent a graph in a programming paradigm. The most common form of representing a graph in programming terms is using adjacency matrices. Consider this simple non directed graph.






we can represent the vertices and edges using a 2 dimensional Boolean array.
As we have 5 vertices in the above graph namely A,B,C,D and E
the array will be a 5x5 matrix.

lets initialize the array as:

boolean arr[5][5];

let us assume i and j as the starting and ending vertex.

if arr[i][j]=true, that means there is a connection from i to j.

as it is a non directed graph we assume that

if arr[i][j]=true then arr[j][i] is also true;

the matrix for the above graph will look like this:

     A B C D E

  A 0  1 1  0 1     

  B 1 0  0  1 1    

  C 1 0  0  0 0

  D 0 1  0  0 0

  E 1 1  0  0 0

 

So the array can be initialized as:

int arr[5][5]= {{0,1,1,0,1},
                         {1,0,0,1,1},
                         {1,0,0,0,0},
                         {0,1,0,0,0},
                         {1,1,0,0,0}}


 

Similarly a weighted and or directed graph can also be represented in a similar manner, but the difference is that instead of Boolean values, the matrix will hold the weights. lets take an example to explain how a directed and weighted graph can be represented using adjacency matrix.



 The adjacency matrix for the above graph will look like this

            Bob   Alice   Charlie
Bob       0        0          10
Alice     20      0           5
Charlie  0       0           0

So the array can be initialized as:

int arr[3][3]= {{0,0,10},
                         {20,0,5},
                         {0,0,0}}

This is how different types of graphs are represented using an adjacency matrix. Once we are able to represent a graph in a matrix format, the programming part becomes easy. Once we have the adjacency matrix we can apply different algorithms to identify the properties of a given graph.

Importance of Graph Theory in Computer Science


Graph theory is one of the important field of mathematics which is closely related to computer science. Many of the real world problems which are difficult to visualize can be easily represented using graphs. Thus graphs are used extensively in computer science to represent communication networks, computational semantics and so on.

The famous PageRank algorithm of Google uses Graph theory, where each webpage is considered as a vertex of a graph and each vertex is weighted based on the number of quality links to that web page. Even though PageRank is not the only algorithm used by Google to rank webpages, it is the most popular one.

Another important area in Computer Science where Graph theory is used is Semantic networks. It is a part of NLP( Natural Language Processing) or Knowledge representation. A semantic network is a network which represents the semantic relationship between concepts, or in simple words, how different concepts are connected to each other. It uses vertices to represent concepts and the edges to represent relationships.

As complicated problems can be represented easily using graphs, formulating new algorithms for graphs is a major interest in computer science. There are several algorithms used to solve problems using graphs.

These are some of the important algorithms used in Graph theory.

  • Bellman–Ford algorithm
  • Dijkstra's algorithm
  • Ford–Fulkerson algorithm
  • Kruskal's algorithm
  • Nearest neighbor algorithm
  • Prim's algorithm
  • Depth-first search
  • Breadth-first search
Graphs are represented in terms of vertices and edges. In the next post, we will see how a graph can be represented using a 2 dimensional array (matrix).