0% found this document useful (0 votes)
34 views6 pages

Experiment No 7

This document describes an experiment to write and execute C++ programs for representing graphs using adjacency matrices and adjacency lists. It includes the code for programs that create adjacency matrices and lists for a sample graph with 4 vertices and 4 edges. The results show that the programs executed correctly and produced the expected outputs, representing the graph using the two different representations.

Uploaded by

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

Experiment No 7

This document describes an experiment to write and execute C++ programs for representing graphs using adjacency matrices and adjacency lists. It includes the code for programs that create adjacency matrices and lists for a sample graph with 4 vertices and 4 edges. The results show that the programs executed correctly and produced the expected outputs, representing the graph using the two different representations.

Uploaded by

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

Experiment no:7

7. C++ program for adjacency matrix representation of a graph


Aim/objective: to write and execute C++ program for adjacency matrix representation of the
given graph
Pre lab Questions :
1. List the applications of graph data structure.

2. The number of edges present in a complete graph having n vertices is _____

3. Define spanning tree.

4. What is a directed graph?


Program 1: Write a C++ program to create an adjacency matrix representation for the
graphshown in fig 1.

//RA2311043010090
#include <iostream>
#include <vector>
using namespace std;
class Graph {
private:
int numVertices;
vector<vector<int>> adjacencyMatrix;
public:
Graph(int n) : numVertices(n) {
adjacencyMatrix.resize(n, vector<int>(n, 0));
}
void addEdge(int from, int to) {
if (from >= 0 && from < numVertices && to >= 0 && to < numVertices) {
adjacencyMatrix[from][to] = 1;
adjacencyMatrix[to][from] = 1; // Uncomment if graph is undirected
} else {
cout << "Invalid edge!\n";
}
}
void display() {
for (int i = 0; i < numVertices; ++i) {
for (int j = 0; j < numVertices; ++j) {
cout << adjacencyMatrix[i][j] << " ";
}
cout << endl;
}
}
};
int main() {
Graph graph(4);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(0, 3);
graph.addEdge(1, 2);
cout << "Adjacency Matrix:\n";
graph.display();
return 0;
}

Output:
Program 2: Write a C++ program to create an adjacency matrix representation for the
graphshown in fig 1.
//RA2311043010090
#include <iostream>
#include <list>
#include <vector>
using namespace std;
class Graph {
private:
int numVertices;
vector<list<int>> adjacencyList;
public:
Graph(int n) : numVertices(n) {
adjacencyList.resize(n); }
void addEdge(int from, int to) {
adjacencyList[from].push_back(to);
adjacencyList[to].push_back(from); }
void display() {
for (int i = 0; i < numVertices; ++i) {
cout << "Vertex " << i << " -> ";
for (int neighbor : adjacencyList[i]) {
cout << neighbor << " "; }
cout << endl;
}}};
int main() {
Graph graph(4);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(0, 3);
graph.addEdge(1, 2);
cout << "Adjacency List:\n";
graph.display();
return 0;}
Post lab questions :
1. In a simple graph, the number of edges is equal to twice the sum of the degrees of the
vertices. a) True b) False

2. Which of the two ways that can be used to represent a graph?

Results and Conclusion :


The C++ Program on adjacency matrix and adjacency list representation of graphs was
executed and desired outputwas obtained

You might also like