0% found this document useful (0 votes)
6 views2 pages

Adjacency List and Matrix in C++

The document contains a C++ program that constructs and displays both an adjacency list and an adjacency matrix representation of a graph with 8 vertices and 16 edges. It initializes the edges, populates the adjacency list and matrix, and then prints both representations to the console. The program demonstrates basic graph data structures and their implementations in C++.

Uploaded by

Hrithik Raj
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)
6 views2 pages

Adjacency List and Matrix in C++

The document contains a C++ program that constructs and displays both an adjacency list and an adjacency matrix representation of a graph with 8 vertices and 16 edges. It initializes the edges, populates the adjacency list and matrix, and then prints both representations to the console. The program demonstrates basic graph data structures and their implementations in C++.

Uploaded by

Hrithik Raj
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

#include <iostream>

#include <vector>

using namespace std;

int main() {

int V = 8;

int E = 16;

vector<pair<int, int>> edges = {

{1, 2}, {1, 3}, {1, 5}, {1, 6},

{2, 3}, {2, 4}, {2, 7}, {3, 4},

{3, 8}, {4, 5}, {4, 7}, {5, 6},

{5, 8}, {6, 7}, {6, 8}, {7, 8}

};

vector<int> adjList[9];

for (auto e : edges) {

adjList[[Link]].push_back([Link]);

adjList[[Link]].push_back([Link]);

cout << "Adjacency List Representation:\n";


for (int i = 1; i <= V; i++) {

cout << i << " -> ";

for (int j : adjList[i])

cout << j << " ";

cout << "\n";

int adjMatrix[9][9] = {0};

for (auto e : edges) {

adjMatrix[[Link]][[Link]] = 1;

adjMatrix[[Link]][[Link]] = 1;

cout << "\nAdjacency Matrix Representation:\n";

for (int i = 1; i <= V; i++) {

for (int j = 1; j <= V; j++)

cout << adjMatrix[i][j] << " ";

cout << "\n";

return 0;

You might also like