Skip to content
Extraits de code Groupes Projets
Valider 9297bd9a rédigé par Samuh's avatar Samuh
Parcourir les fichiers

distance entre 2 communautés

parent 024eac94
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -2,7 +2,7 @@ CC = gcc
OPTIONS = -Wall -Wextra -O3
MATH = -lm
EXECUTABLE = TP1 TP2 TP3 TP4 TP4_q4
EXECUTABLE = TP1 TP2 TP3 TP4 TP4_q4 TP4_metric
all: $(EXECUTABLE)
......@@ -30,6 +30,8 @@ TP4: TP4.o
TP4_q4: TP4_q4.o
$(CC) $(OPTIONS) -o $@ $^ $(MATH) -g
TP4_metric: TP4_metric.o
$(CC) $(OPTIONS) -o $@ $^ $(MATH) -g
# TP1.o: implems.c
......
#include "./implems.c"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
void aff_tab(unsigned long *t, int size)
{
for (int i = 0; i < size; i++)
{
printf("%li ", t[i]);
}
printf("\n");
}
void swap(unsigned long *tab, unsigned long num1, unsigned long num2)
{
unsigned long temp = tab[num1];
tab[num1] = tab[num2];
tab[num2] = temp;
}
unsigned long partition(unsigned long *tab, unsigned long *tab_value, unsigned long left, unsigned long right, unsigned long pivot)
{
unsigned long leftPointer = left;
unsigned long rightPointer = right;
while (true)
{
while (tab_value[tab[leftPointer]] > tab_value[pivot])
{
leftPointer++;
//do nothing
}
while (rightPointer > 0 && tab_value[tab[--rightPointer]] < tab_value[pivot])
{
//do nothing
}
if (leftPointer >= rightPointer)
{
break;
}
else
{
swap(tab, leftPointer, rightPointer);
}
}
swap(tab, leftPointer, right);
return leftPointer;
}
void quickSort(unsigned long *tab, unsigned long *tab_value, unsigned long left, unsigned long right)
{
if (right <= left)
{
return;
}
else
{
unsigned long pivot = tab[right];
unsigned long partitionPoint = partition(tab, tab_value, left, right, pivot);
if (partitionPoint > 0)
{
quickSort(tab, tab_value, left, partitionPoint - 1);
}
quickSort(tab, tab_value, partitionPoint + 1, right);
}
}
unsigned long nb_commu(edgelist *g)
{
unsigned long nb = 0;
for (unsigned long i = 0; i < g->e; i++)
{
nb = (g->edges[i].t > nb) ? g->edges[i].t : nb;
}
return nb;
}
unsigned long *size_commu(edgelist *g, unsigned long size)
{
unsigned long *res = (unsigned long *)malloc(sizeof(unsigned long) * size);
for (unsigned long i = 0; i < size; i++)
{
res[i] = 0;
}
for (unsigned long i = 0; i < g->e; i++)
{
res[g->edges[i].t]++;
}
return res;
}
unsigned long metric(edgelist *g1, edgelist *g2)
{
unsigned long distance = 0;
unsigned long nb_commu_1 = nb_commu(g1);
unsigned long nb_commu_2 = nb_commu(g2);
if (nb_commu_2 > nb_commu_1)
{
unsigned long tmp = nb_commu_1;
nb_commu_1 = nb_commu_2;
nb_commu_2 = tmp;
edgelist *gtmp = g1;
g1 = g2;
g2 = gtmp;
}
unsigned long *size_commu_1 = size_commu(g1, nb_commu_1);
unsigned long *size_commu_2 = size_commu(g2, nb_commu_2);
unsigned long *ordre_1 = (unsigned long *)malloc(sizeof(unsigned long) * nb_commu_1);
for (unsigned long i = 0; i < nb_commu_1; i++)
{
ordre_1[i] = i;
}
unsigned long *ordre_2 = (unsigned long *)malloc(sizeof(unsigned long) * nb_commu_2);
for (unsigned long i = 0; i < nb_commu_2; i++)
{
ordre_2[i] = i;
}
quickSort(ordre_1, size_commu_1, 0, nb_commu_1 - 1);
quickSort(ordre_2, size_commu_2, 0, nb_commu_2 - 1);
unsigned long ecart = 0;
for (unsigned long i = 0; i < nb_commu_2; i++)
{
ecart = (size_commu_1[ordre_1[i]] > size_commu_2[ordre_2[i]]) ? (size_commu_1[ordre_1[i]] - size_commu_2[ordre_2[i]]) : (size_commu_2[ordre_2[i]] - size_commu_1[ordre_1[i]]);
distance += ecart * ecart;
}
for (unsigned long i = nb_commu_2; i < nb_commu_1; i++)
{
ecart = size_commu_1[ordre_1[i]];
distance += ecart * ecart;
}
return distance;
}
int main(int argc, char **argv)
{
if (argc < 3)
{
printf("deux arguments sont attendus\n");
return 1;
}
edgelist *g1;
edgelist *g2;
// PARSING
printf("Reading edgelist from file %s\n", argv[1]);
g1 = el_readedgelist(argv[1]);
printf("Reading edgelist from file %s\n", argv[2]);
g2 = el_readedgelist(argv[2]);
printf("done\n");
unsigned long res = metric(g1, g2);
printf("distance : %li\n", res);
free_edgelist(g1);
free_edgelist(g2);
return 0;
}
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 2
10 2
11 2
12 3
13 3
14 3
15 3
16 3
17 4
18 4
19 4
20 4
21 4
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter