CS210-DSA-Lab-12
CS210-DSA-Lab-12
12
GRAPHS
Following are the lab objectives:
Theory Overview: Graphs as a mathematical abstraction comprising vertices (nodes) and edges
connecting them. It covers different types of graphs such as directed and undirected, weighted and
unweighted, and discusses fundamental concepts like adjacency matrices and adjacency lists. The
overview also explores common graph algorithms including traversal methods like depth-first
search (DFS) and breadth-first search (BFS), as well as shortest path algorithms
Instructions:
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
PROCEDURE
1. System/Tools/Programming Language
A computer system with visual studio in C++.
2. Definition and Explanation :
A graph is a collection of nodes called vertices, and the connections between them,
called edges. A graph data structure may also associate to each edge some edge
value, such as a symbolic label or a numeric attribute (cost, capacity, length, etc.).
LAB TASKS
Task 1 (20 mark)
Adjacency matrix is a way to represent a graph inside computer memory. Write a class
MatrixGraph which maintains adjacency matrix of above graph. 2-dimension dynamic array will
help us to create adjacency matrix. Use following sample class as reference and implement
mentioned methods. (Note: Remember this is weighted graph. Assumptions are encouraged but do
mention your assumptions.)
class MatrixGraph
{
private:
short ** adjMatrix; // 2-D array
short matSize;
public:
II. Constructor initializes adjMatrix pointer and matSize...
MatrixGraph();
KK. This method read a text file containing graph entries and
populate adjacency matrix...
void createGraph();
LL. This method takes two vertices and return true if both
vertices are connected otherwise returns false...
bool verifyConnection(short vertex1, short vertex2);
Task 2
Adjacency List is another way of representing a graph inside computer memory. Write a class
ListGraph which maintains adjacency List of above graph. Linked List will help us to create
adjacency list. Use following sample class as reference and implement mentioned methods. (Note:
Remember this is weighted graph. Assumptions are encouraged but do mention your assumptions.)
class ListGraph
{
private:
list <node> * adjList; // array of list
public:
Task 3
Task 4
Now we try to add / remove vertices to / from our graph. Implement following methods for both
classes (i.e. MatrixGraph & ListGraph).
removeVertex: This method accepts a vertex value and removes it from adjacency matrix/list and
updates other edges accordingly.
Sample Output: