You are on page 1of 3

University of Engineering and Technology Lahore

Data Structures and Algorithms


Lab 12: Graphs - Adjacency Matrix Representation

Theoretical Background

A graph is a data structure that consists of the following two components: 


1. A finite set of vertices also called as nodes. 
2. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v) is not
the same as (v, u) in case of a directed graph (di-graph). The pair of the form (u, v) indicates that there is
an edge from vertex u to vertex v. The edges may contain weight/value/cost.
Graphs are used to represent many real-life applications: Graphs are used to represent networks. The
networks may include paths in a city or telephone network or circuit network. Graphs are also used in
social networks like LinkedIn and Facebook. For example, in Facebook, each person is represented with
a vertex (or node). Each node is a structure and contains information like person id, name, gender, and
locale. Google to see more applications of graphs.
Following is an example of an undirected graph with 5 vertices.

The following two are the most commonly used representations of a graph. 
1. Adjacency Matrix 
2. Adjacency List 
There are other representations also like, Incidence Matrix and Incidence List. The choice of graph
representation is situation-specific. It depends on the type of operations to be performed and ease of
use.

Adjacency Matrix: 
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array
be M[][], a slot M[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for
undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs. If
adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w. 

The adjacency matrix for the above example graph, assuming all edges’ cost is 1, is: 
University of Engineering and Technology Lahore
Data Structures and Algorithms
Lab 12: Graphs - Adjacency Matrix Representation

If we assign weights i.e. costs to the edges, the graph would look like this:

And the adjacency matrix would be:

The cost of a path from one node to another is the sum of the costs of all edges on that path. In above
graph, there are multiple paths from one node to another. For example, from node 0 to 3, one path is

0 -> 4 -> 3, with cost = 9 + 5 = 14

Another path is

0 -> 1 -> 3, with cost = 1 + 7 = 8


University of Engineering and Technology Lahore
Data Structures and Algorithms
Lab 12: Graphs - Adjacency Matrix Representation

Another path is

0 -> 4 -> 1 -> 3, with cost = 9 + 3 + 7 = 19

Lab Task:

In the code given to you, a structure for a graph, containing number of nodes (vertices) and a pointer to
a 2D matrix, is defined. Functions CreateGraph(), DisposeGraph(), SetEdgeCost() and PrintGraph() have
also been defined. The main() function creates the graph shown above, but with random edge costs
between 1 and 9, using roll no. as seed. Also given is a function FindAPath() that prints a single path
between the two given nodes, along with the cost of the path. You are required to go through the given
code, set the value of variable roll_no to your roll no., and then write a function FindAllPaths() that finds
and prints all paths between two given nodes, along with the cost of each path, and at the end, prints
the path with the smallest cost. Run it for any pair of nodes in the graph.

Bonus task: Google Dijkstra’s algorithm; it finds the shortest path tree from a given node to all other
nodes in a graph. Study the algorithm and write C code for it, then print the shortest path tree from node
0 to all other nodes in the graph of the above program.

Write comments in the code to briefly explain the lines of code you added/changed (no need to write
comments for the code given to you).

Marks: FindAllPaths() (4 marks) + neat and correct output (1 mark) [+ bonus mark from Dijkstra’s
algorithm] = 5 [+ 1] marks

Submission Instructions for Teams assignment of Part (a)

1) Submit “lab12.c”. If the code or comments of any students are found to be too similar, they may
get negative marks.
2) Submit a PDF file containing a screenshot of the output, with the name “2019EE###.pdf”, where
### is your roll no. (so if your roll no. is 1, name the file 2019EE001.pdf; if it is 140, name the file
2019EE140.pdf, and so on).
3) Do not create zip; instead just submit each file separately (in this case, 2 files: 1 file of code and
1 PDF containing screenshot of output).
4) You have to submit this in the assignment of Lab 12 within due date. If you are unable to
complete it by then, submit it in the “Late Assignments” Teams assignment. This lab will be
treated as a “part (b) lab” w.r.t. late submission penalty. Late submission of part (b) has a
cumulative penalty of -0.5 marks per late submission, so while your first late submission will be
forgiven, your second late submission will get -0.5 marks, your third late submission will get -1
marks, fourth will get -1.5 marks and so on.
a. I advise submitting at least 5 to 10 minutes early; if you submit at the last minute and
there is a delay of a few seconds in the internet, Teams may refuse to submit your
assignment in part (a); then you would have to submit it in “Late Assignments”.
5) Rigged code (showing desired output without the required code) may result in negative marks.

This is no viva evaluation of this lab.

You might also like