0% found this document useful (0 votes)
56 views4 pages

STD Printmat Mat, N I, J Setw I I N I Setw I: // A Function To Print The Adjacency Matrix

The document contains C++ code to represent graphs using adjacency lists. It takes the number of vertices and edges as input, then takes the vertex pairs for each edge to build the adjacency list representation and print it out. It includes two examples - a graph with 5 vertices and 8 edges, and another with 4 vertices and 6 edges. The code takes the edge inputs, builds the adjacency lists with each vertex pointing to its neighbors, and prints out the representation.

Uploaded by

sirisha dasari
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)
56 views4 pages

STD Printmat Mat, N I, J Setw I I N I Setw I: // A Function To Print The Adjacency Matrix

The document contains C++ code to represent graphs using adjacency lists. It takes the number of vertices and edges as input, then takes the vertex pairs for each edge to build the adjacency list representation and print it out. It includes two examples - a graph with 5 vertices and 8 edges, and another with 4 vertices and 6 edges. The code takes the edge inputs, builds the adjacency lists with each vertex pointing to its neighbors, and prints out the representation.

Uploaded by

sirisha dasari
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/ 4

#include<iostream>

#include<iomanip>
using namespace std;
// A function to print the adjacency matrix.
void PrintMat(int mat[][20], int n)
{
int i, j;

cout<<"\n\n"<<setw(4)<<"";
for(i = 0; i < n; i++)
cout<<setw(3)<<"("<<i+1<<")";
cout<<"\n\n";
// Print 1 if the corresponding vertexes are connected otherwise 0.
for(i = 0; i < n; i++)
{
cout<<setw(3)<<"("<<i+1<<")";
for(j = 0; j < n; j++)
{
cout<<setw(4)<<mat[i][j];
}
cout<<"\n\n";
}
}

int main()
{
int i, j, v;

cout<<"Enter the number of vertexes: ";


cin>>v;

int mat[20][20];

cout<<"\n";
// Take input of the adjacency of each pair of vertexes.
for(i = 0; i < v; i++)
{
for(j = i; j < v; j++)
{
if(i != j)
{
cout<<"Enter 1 if the vertex "<<i+1<<" is adjacent t
o "<<j+1<<", otherwise 0: ";
cin>>mat[i][j];

mat[j][i] = mat[i][j];
}
else
mat[i][j] = 0;
}
}

PrintMat(mat, v);
}
#include<iostream>

using namespace std;

int main()
{
int i, v, e, j, count;

// take the input of the number of vertex and edges.


cout<<"Enter the number of vertexes of the graph: ";
cin>>v;
cout<<"\nEnter the number of edges of the graph: ";
cin>>e;
int edge[e][2];

// Take the input of the adjacent vertex pairs of the given graph.
for(i = 0; i < e; i++)
{
cout<<"\nEnter the vertex pair for edge "<<i+1;
cout<<"\nV(1): ";
cin>>edge[i][0];
cout<<"V(2): ";
cin>>edge[i][1];
}

// Print the adjacency list representation of the graph.


cout<<"\n\nThe adjacency list representation for the given graph: ";
for(i = 0; i < v; i++)
{
count = 0;
// For each vertex print, its adjacent vertex.
cout<<"\n\t"<<i+1<<"-> { ";
for(j = 0; j < e; j++)
{
if(edge[j][0] == i+1)
{
cout<<edge[j][1]<<" ";
count++;
}
else if(edge[j][1] == i+1)
{
cout<<edge[j][0]<<" ";
count++;
}
else if(j == e-1 && count == 0)
cout<<"Isolated Vertex!";
}
cout<<" }";
}
}
Case 1:
Enter the number of vertexes of the graph: 5

Enter the number of edges of the graph: 8

Enter the vertex pair for edge 1


V(1): 1
V(2): 3

Enter the vertex pair for edge 2


V(1): 1
V(2): 4

Enter the vertex pair for edge 3


V(1): 1
V(2): 5

Enter the vertex pair for edge 4


V(1): 2
V(2): 3

Enter the vertex pair for edge 5


V(1): 2
V(2): 5

Enter the vertex pair for edge 6


V(1): 3
V(2): 4

Enter the vertex pair for edge 7


V(1): 3
V(2): 5

Enter the vertex pair for edge 8


V(1): 4
V(2): 5

The adjacency list representation for the given graph:


1-> { 3 4 5 }
2-> { 3 5 }
3-> { 1 2 4 5 }
4-> { 1 3 5 }
5-> { 1 2 3 4 }

Case 2:
Enter the number of vertexes of the graph: 4

Enter the number of edges of the graph: 6

Enter the vertex pair for edge 1


V(1): 1
V(2): 2

Enter the vertex pair for edge 2


V(1): 1
V(2): 3

Enter the vertex pair for edge 3


V(1): 1
V(2): 4

Enter the vertex pair for edge 4


V(1): 2
V(2): 3

Enter the vertex pair for edge 5


V(1): 2
V(2): 4

Enter the vertex pair for edge 6


V(1): 3
V(2): 4

The adjacency list representation for the given graph:


1-> { 2 3 4 }
2-> { 1 3 4 }
3-> { 1 2 4 }
4-> { 1 2 3 }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy