forked from Shailendra-Java/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphUsingAdjacanceyMatrix.cpp
More file actions
56 lines (46 loc) · 962 Bytes
/
GraphUsingAdjacanceyMatrix.cpp
File metadata and controls
56 lines (46 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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 to "<<j+1<<", otherwise 0: ";
cin>>mat[i][j];
mat[j][i] = mat[i][j];
}
else
mat[i][j] = 0;
}
}
PrintMat(mat, v);
}