-
Notifications
You must be signed in to change notification settings - Fork 0
/
rede_social.c
executable file
·129 lines (107 loc) · 2.53 KB
/
rede_social.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <stdlib.h>
int** criaMatriz(int n){
int** matriz = (int**)malloc(sizeof(int*)*n);
for(int i=0; i<n; i++){
matriz[i] = (int*)malloc(sizeof(int)*n);
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
matriz[i][j] = 0;
}
}
return matriz;
}
void mostraMatriz(int** matriz, int n){
for(int i =0; i<n; i++){
for(int j=0; j<n; j++){
printf("%d ", matriz[i][j]);
}
printf("\n");
}
}
void amizade(int** matriz, int a, int b, int n){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(i == a && j == b){
matriz[i][j] = 1;
}
else if(i == b && j == a){
matriz[i][j] = 1;
}
}
}
}
float geraRand(){
float scale = rand() / (float) RAND_MAX;
return scale;
}
void popularRedeSocialAleatoriamente (float p, int ** matriz, int n){
float r = 0;
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
r = geraRand();
//printf("%f\n", r);
if(r < p){
amizade(matriz, i, j, n);
}
}
}
}
int numAmigosEmComum (int** matriz, int n, int a, int b){
int cont=0;
printf("Id amigos em comum: ");
for(int i=0; i<n; i++){
if(matriz[a][i] == 1 && matriz[b][i] == 1){
cont++;
printf("%d ", i);
}
}
printf("\n");
return cont;
}
float coeficienteAglomeracao (int ** matriz, int n, int a){
int amigos = 0;
int cont = 0;
for(int i=0; i<n; i++){
if(matriz[a][i] == 1){
amigos++;
cont+= numAmigosEmComum(matriz,n,a,i);
}
}
return cont/(amigos*(amigos-1)/2);
}
int main(){
// Criando a matriz
int n;
printf("Numero de usuarios: ");
scanf("%d", &n);
int** matriz = criaMatriz(n);
mostraMatriz(matriz, n);
// Relação de amizade entre a e b
int a, b;
printf("Amigos: ");
scanf("%d %d", &a, &b);
amizade(matriz, a, b, n);
mostraMatriz(matriz, n);
// Numero aleatório entre 0 e 1
float aleatorio = geraRand();
printf("Rand: %f\n", aleatorio);
// Popular aleatoriamente
float p;
printf("Valor de P: ");
scanf("%f", &p);
popularRedeSocialAleatoriamente (p, matriz, n);
mostraMatriz(matriz, n);
// Amigos em comum
printf("Amigos: ");
scanf("%d %d", &a, &b);
int comum = numAmigosEmComum(matriz, n, a, b);
printf("%d amigos em comum \n", comum);
// Coeficiente de aglomeração
printf("Usuario: ");
scanf("%d", &a);
float coeficiente = coeficienteAglomeracao(matriz, n, a);
printf("Coeficiente de aglomeracao: %f\n", coeficiente);
return 0;
}