The meaning of the square operation of the adjacency matrix of a direction graph
This is an assignment in the first chapter of graph theory algorithm application. Now, the linear algebra is too bad. Hey
First code:
#include <iostream> #include <string.h> #include <stdlib.h> #include <stdio.h> #define MAXN 100/
The multiplicative operation of the adjacency matrix of a direction graph using namespace std;
int EDGE[MAXN][MAXN];
int NEWEDGE[MAXN][MAXN]; int main (int argc, char *argv[]) {int num; Number of test cases int i,j, k,q; loop variable int vertexnum; The number of vertices of a direction graph int arcnum; The number int start,end of the direction graph edge;
Start and end scanf ("%d", &num);
while (num--) {scanf ("%d%d", &vertexnum,&arcnum);
if (Vertexnum = = 0 && arcnum ==0) {break;
} memset (Edge) (edge,0,sizeof);
memset (newedge,0,sizeof (NewEdge));
for (i=0 i < arcnum;i++) {scanf ("%d%d", &start,&end); EDGE[START-1][END-1] = 1; Note number minus one} for (I =0 < vertexnum;i++) {for (j = 0; J < vertexnum;j++) {if (j = = 0) {printf ("%d", Edge I
[j]);
}else{printf ("%d", edge[i][j]);
printf ("\ n");
printf ("\ n \ nthe");
for (i = 0;i< vertexnum; i++) {int temparray [100]; memset (TemparraY,0,sizeof (Temparray));
for (k = 0; k < vertexnum;k++) {Temparray[k] = edge[i][k];
for (q = 0; Q < vertexnum;q + +) {int sum = 0;
for (j = 0; J < Vertexnum J + +) {sum = Sum+edge[j][q]*temparray[j];
} newedge[i][q] = sum; (I =0 i < vertexnum;i++) {for (j = 0; J < vertexnum;j++) {if (j = = 0) {printf ("%d", newedge[i][
J]);
}else{printf ("%d", newedge[i][j]);
printf ("\ n");
} return 0;
}
The title requires the matrix to be written after the square of the adjacency matrix of the direction graph, then say the meaning of it, write code nearly one hours, want to meaning will be nearly half a bit, during the resort to Baidu, 360 no fruit, linear algebra data too little, almost to see teaching video, I was drunk people ah.
After the square, we still get a two-dimensional matrix, in which the meaning of each element value is whether the direct adjacency point of the node in the directed graph can be reached.
Describe by diagram
Fig. 1 A direction graph
Fig. 2 The adjacency matrix representation of a direction graph
Fig. 3 The matrix after the square of a direction graph
is to 1 nodes of the adjacent point 2 as the standard, this adjacency point all the adjacency point 3, that is, figure 3 in the first row of the third element of the value of 1 means 1 nodes can be accessed through its adjacency point 2 to 3, the same reason the last element of the second row 1 means that Node 2 can access to 3 through 4, Of course the element of 0 means that the node cannot be reached indirectly, when the element value of 1 indicates that a path can be reached, the element value is 2 when there are two paths to reach.
The square of the adjacency matrix is equivalent to doing a breadth-first search traversal, and if the resulting adjacency matrix is squared again, it means to test whether the node is up to two times indirectly. That is, whether a third adjacency point can be reached from the beginning node through two adjacency points.
The completion of the work appraisal.