You are on page 1of 5

Lab Journal – Lab 11

Data Structures and Algorithms

Lab Journal - Lab 11


Name: _________________________________

Enrollment #: _________________________________

Class/Section: _________________________________

Objective

This lab session is intended to introduce students to the Graphs and concepts related to Graphs.

Task 1 :

Give answers to the following.

1. Consider the following directed graph.

3
B D
0
5 10
C
4
A

E
6

Represent the given graph using an adjacency matrix and adjacency list.

Data Structures and Algorithms Page 1


Lab Journal – Lab 11

Adjacency Matrix Adjacency List

0 1 2 3 4
A
B

2. Starting at vertex ‘A’, give the depth- fi rst and breadth-fi rst traversal of the graph in
Question 1.

3. Generate a graph from the given adjacency list.

Data Structures and Algorithms Page 2


Lab Journal – Lab 11

A
B C D

B
A D F
A
E
C
A D
B C

D
A B C
F D

E
F

F
B E

Task 2 :

Implement the following exercises.

Data Structures and Algorithms Page 3


Lab Journal – Lab 11

Exercise 1

Consider the adjacency list implementation of a graph as illustrated in Figure 1.


Each node of such a list can be represented by the following class.
class AdjListNode
{
public:
int dest;
AdjListNode* next;
};

Likewise, an array of Adjacency lists can be maintained having the same size as the number of
vertices in the graph.

class AdjList
{
public:
AdjListNode *head;
};
Given the above classes, implement the ‘Graph’ class outlined in the following.

class Graph
{
public:
int V; \\Number of vertices
AdjList* arr; \\ An Array of adj lists
Graph(int V);
\\ Create a new node of the list with value ‘d’
AdjListNode* newAdjListNode(int d);
\\ Create an edge from ‘src’ to ‘dest’
void addEdge(int src, int dest);
\\ Print the vertices in the adjacency list of each vertex
void printGraph();
};

Data Structures and Algorithms Page 4


Lab Journal – Lab 11

Figure 1 : Adajcency list implementation of a graph

Exercise 2

Complete the given ‘Graph’ class that is basedon adjacency matrix representation.

class Graph {
private:
      bool** adjacencyMatrix;
      int vertexCount;
public:
      Graph(int vertexCount);
void addEdge(int i, int j) ;
      void removeEdge(int i, int j);
      bool isEdge(int i, int j) ;
      ~Graph() ;
};

Implement the given exercises and get them checked by your instructor. If you are unable to
complete the tasks in the lab session, deposit this journal alongwith your programs (printed
or handwritten) before the start of the next lab session.

S No. Exercise Checked By:


1. Exercise 1

2. Exercise 2

+++++++++++++++++++++++++

Data Structures and Algorithms Page 5

You might also like