-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartesian_product.c
81 lines (64 loc) · 1.71 KB
/
cartesian_product.c
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
#include <stdio.h>
#include <stdlib.h>
// Print the adjacency matrix M with V vertices
void printAdjMat (int * M, int V){
int i, j;
for (i = 0; i < V; i++){
for (j = 0; j < V; j++)
printf ("%d ", *(M + i*V + j) );
printf("\n");
}
printf("\n");
return;
}
// Calculates the cartesian product G x H of the
// adjacency matrices G and H with VG and VH vertices, respectively
int* cartProd (int* G, int *H, int VG, int VH){
int VP = VG*VH;
int i, j, m, line, column;
int *P;
// Calculating the cartesian product G x H
// P is a VP x VP adj. matrix with aij = (vi, uj) as its vertexes, a pair of vi in G and uj in H
// Each element elc in P is a (a l/VH l%VH, a c/VG c%VG) edge
P = calloc (VP*VP, sizeof(int));
if (!P) return NULL;
// For each edge (vi, vj) in G, make (aim, ajm) for m from 0 to VH:
for (i = 0; i < VG; i++){
for (j = 0; j < VG; j++){
if ( *(G+i*VG+j) ){ // (vi, vj) is in EG, equal to G[i][j] != 0
for (m = 0; m < VH; m++){
line = i*VH + m; //aim
column = j*VH + m; //ajm
*(P + line*VP + column) = 1;
}
}
}
}
// For each edge (ui, uj) in H, make (ami, amj) for m from 0 to VG:
for (i = 0; i < VH; i++){
for (j = 0; j < VH; j++){
if ( *(H+i*VH+j)){ // (ui, uj) is in EH, equal to H[i][j] != 0
for (m = 0; m < VG; m++){
line = m*VH + i; //ami
column = m*VH + j; //amj
*(P + line*VP + column) = 1;
}
}
}
}
return P;
}
int main(){
int VH = 3, VG = 3;
int G[3][3] = {{0,1,1},{1,0,1},{1,1,0}}, H[3][3] = {{0,1,0},{1,0,1},{0,1,0}};
int *P;
printf("G:\n");
printAdjMat (G, VG);
printf("H:\n");
printAdjMat (H, VH);
P = cartProd (G, H, VG, VH);
printf("P:\n");
printAdjMat (P, VG*VH);
free (P);
return 0;
}