0% found this document useful (0 votes)
4 views

CS210-DSA-Lab-12

The document outlines Lab No. 12 focused on implementing graph data structures using C++. It includes objectives such as creating adjacency matrices and lists, implementing graph traversal algorithms, and methods for adding/removing vertices. The lab tasks are structured sequentially and emphasize individual work with specific coding requirements and guidelines.

Uploaded by

msohaib9685055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CS210-DSA-Lab-12

The document outlines Lab No. 12 focused on implementing graph data structures using C++. It includes objectives such as creating adjacency matrices and lists, implementing graph traversal algorithms, and methods for adding/removing vertices. The lab tasks are structured sequentially and emphasize individual work with specific coding requirements and guidelines.

Uploaded by

msohaib9685055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB NO.

12
GRAPHS
Following are the lab objectives:

1. Implement Graph Data Structure


Lab Objective

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.).

Graph could be represented using three ways.

3. Binary Relation List


4. Adjacency matrix
5. Adjacency List

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();

JJ. Destructor deletes/de-allocates adjacency matrix...


~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);

MM. This methods displays adjacency matrix at console...


void diplayAdjMatrix();

}; // end of class MatrixGraph

(Paste your code here)

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:

1. Constructor initializes adjList pointer and no_Nodes...


ListGraph();

2. Destructor deletes/de-allocates adjacency list...


~ListGraph();
3. This method read a text file containing graph entries and populate adjacency list...
void createGraph();
4. This method takes two vertices and return true if both vertices are connected
otherwise returns false...
bool verifyConnection(short vertex1, short vertex2);

5. This methods displays adjacency matrix at console...


void diplayAdjMatrix();
}; // end of class ListGraph

(Paste your code here)


Sample output:

Task 3

Implement Depth First and Breadth First graph traversal algorithms.

(Paste your code here)

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.

Prototype of the method is

bool removeVertex (short vertexValue);


insertVertex: This method accepts a vertex value and inserts it into adjacency matrix/list. (Be
careful while inserting a vertex, corresponding edges have to be tackled.)

Prototype of the method is

bool insertVertex (short vertexValue);

There would be four methods which you have to develop.

1. Adjacency Matrix RemoveVertex


2. Adjacency Matrix InsertVertex
3. Adjacency List RemoveVertex
4. Adjacency List InsertVertex

(Paste your code here)

Sample Output:

You might also like