-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower_matrix_and_path_matrix.cpp
More file actions
114 lines (102 loc) · 2.03 KB
/
power_matrix_and_path_matrix.cpp
File metadata and controls
114 lines (102 loc) · 2.03 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<bits/stdc++.h>
using namespace std;
int main()
{
int vertex, edge;
cout<<"Enter the number of vertex: "<<endl;
cin>>vertex;
cout<<"Enter the number of edge: "<<endl;
cin>>edge;
int adjacentmatrix[vertex+1][vertex+1];
memset(adjacentmatrix,0,sizeof(adjacentmatrix));
cout<<"enter edges"<<endl;
for(int i = 1; i<=edge; i++)
{
int u,v;
cin>>u>>v;
adjacentmatrix[u][v] = 1;
}
cout<<"Adjacency matrix is: "<<endl;
for(int i= 1; i<=vertex; i++)
{
for(int j = 1; j<=vertex; j++)
{
cout<<adjacentmatrix[i][j]<<" ";
}
cout<<endl;
}
int powmatrix[vertex+1][vertex+1][vertex+1];
for(int i=1; i<=vertex; i++)
{
for(int j = 0; j<=vertex; j++)
{
powmatrix[1][i][j] = adjacentmatrix[i][j];
}
}
for(int i = 2; i<=vertex; i++)
{
for(int j = 1; j<=vertex; j++)
{
for(int k = 1; k<=vertex; k++)
{
int temp = 0;
for(int l = 1; l<=vertex; l++)
{
temp = temp + powmatrix[i-1][j][l]*adjacentmatrix[l][k];
}
powmatrix[i][j][k] = temp;
}
}
}
cout<<endl<<endl;
cout<<"power matrix are: "<<endl;
for(int i = 1; i<=vertex; i++)
{
for(int j = 1; j<=vertex; j++)
{
for(int k = 1; k<=vertex; k++)
{
cout<<powmatrix[i][j][k]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
}
//Br matrix
int Br[vertex+1][vertex+1];
memset(Br,0,sizeof(Br));
for(int i= 1; i<vertex; i++)
{
for(int j = 0; j<vertex; j++)
{
for(int k = 1; k<vertex; k++)
{
Br[j][k] += powmatrix[i][j][k];
}
}
}
cout<<"path matrix is: "<<endl;
int path_matrix[vertex+1][vertex+1];
int count = 0;
for(int i = 1; i<vertex; i++)
{
for(int j = 0; j<vertex; j++)
{
cout<<Br[i][j]<<" ";
if(Br[i][j]==0)
{
count++;
}
}
cout<<endl;
}
cout<<endl;
if(count==0)
{
cout<<"strongly connected"<<endl;
}
else{
cout<<"not strongly connected"<<endl;
}
return 0;
}