Professional Documents
Culture Documents
Theoretical Background
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:
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
Another path is
Another path is
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
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.