Skip to content
Extraits de code Groupes Projets
Valider 3366bbca rédigé par Louis Fourcade's avatar Louis Fourcade
Parcourir les fichiers

Initial commit

parent
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
# README
## Info:
Feel free to use these lines as you wish. These programs load a graph in main memory as (i) a list of edges, (ii) an adjacency matrix or (iii) an adjacency list.
## To compile:
"gcc edgelist.c -O9 -o edgelist"
"gcc adjmatrix.c -O9 -o adjmatrix"
"gcc adjarray.c -O9 -o adjarray"
## To execute:
"./edgelist edgelist.txt"
"./adjmatrix edgelist.txt"
"./adjarray edgelist.txt"
"edgelist.txt" should contain the graph: one edge on each line (two unsigned long (nodes' ID) separated by a space).
The program will load the graph in main memory and then terminate.
## Performance:
- edgelist: up to 500 million edges on my laptop with 8G of RAM. Takes more or less 1.6G of RAM and 25 seconds (I have an SSD hardrive) for 100M edges.
- adjmatrix: up to 200.000 nodes on my laptop with 8G of RAM. Takes more or less 4G of RAM and 10 seconds for 100.000 nodes.
- adjlist: up to 200 million edges on my laptop with 8G of RAM: takes more or less 4G of RAM and 30 seconds for 100M edges.
adjmatrix is much less scallable than the two other programs for sparse graphs. adjmatrix uses O(n^2) memory (n^2 boolean values (note that adjmatrix uses 1 byte to encode a boolean value and not 1 bit...)), while edgelist uses O(m) (2m unsigned) and adjlist uses O(m+n) (4m+2n unsigned).
## Note:
If the graph is directed (and weighted) with selfloops and you want to make it undirected unweighted without selfloops, use the following linux command line.
awk '{if ($1<$2) print $1" "$2;else if ($2<$1) print $2" "$1}' net.txt | sort -n -k1,2 -u > net2.txt
## Initial contributors
Maximilien Danisch
September 2017
http://bit.ly/danisch
maximilien.danisch@gmail.com
/*
Maximilien Danisch
September 2017
http://bit.ly/danisch
maximilien.danisch@gmail.com
Info:
Feel free to use these lines as you wish. This program loads a graph in main memory.
To compile:
"gcc adjlist.c -O9 -o adjlist".
To execute:
"./adjlist edgelist.txt".
"edgelist.txt" should contain the graph: one edge on each line (two unsigned long (nodes' ID)) separated by a space.
The prograph will load the graph in main memory and then terminate.
Note:
If the graph is directed (and weighted) with selfloops and you want to make it undirected unweighted without selfloops, use the following linux command line.
awk '{if ($1<$2) print $1" "$2;else if ($2<$1) print $2" "$1}' net.txt | sort -n -k1,2 -u > net2.txt
Performance:
Up to 200 million edges on my laptop with 8G of RAM: takes more or less 4G of RAM and 30 seconds (I have an SSD hardrive) for 100M edges.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>//to estimate the runing time
#define NLINKS 100000000 //maximum number of edges for memory allocation, will increase if needed
typedef struct {
unsigned long s;
unsigned long t;
} edge;
//edge list structure:
typedef struct {
unsigned long n;//number of nodes
unsigned long e;//number of edges
edge *edges;//list of edges
unsigned long *cd;//cumulative degree cd[0]=0 length=n+1
unsigned long *adj;//concatenated lists of neighbors of all nodes
} adjlist;
//compute the maximum of three unsigned long
inline unsigned long max3(unsigned long a,unsigned long b,unsigned long c){
a=(a>b) ? a : b;
return (a>c) ? a : c;
}
//reading the edgelist from file
adjlist* readedgelist(char* input){
unsigned long e1=NLINKS;
FILE *file=fopen(input,"r");
adjlist *g=malloc(sizeof(adjlist));
g->n=0;
g->e=0;
g->edges=malloc(e1*sizeof(edge));//allocate some RAM to store edges
while (fscanf(file,"%lu %lu", &(g->edges[g->e].s), &(g->edges[g->e].t))==2) {
g->n=max3(g->n,g->edges[g->e].s,g->edges[g->e].t);
if (++(g->e)==e1) {//increase allocated RAM if needed
e1+=NLINKS;
g->edges=realloc(g->edges,e1*sizeof(edge));
}
}
fclose(file);
g->n++;
g->edges=realloc(g->edges,g->e*sizeof(edge));
return g;
}
//building the adjacency matrix
void mkadjlist(adjlist* g){
unsigned long i,u,v;
unsigned long *d=calloc(g->n,sizeof(unsigned long));
for (i=0;i<g->e;i++) {
d[g->edges[i].s]++;
d[g->edges[i].t]++;
}
g->cd=malloc((g->n+1)*sizeof(unsigned long));
g->cd[0]=0;
for (i=1;i<g->n+1;i++) {
g->cd[i]=g->cd[i-1]+d[i-1];
d[i-1]=0;
}
g->adj=malloc(2*g->e*sizeof(unsigned long));
for (i=0;i<g->e;i++) {
u=g->edges[i].s;
v=g->edges[i].t;
g->adj[ g->cd[u] + d[u]++ ]=v;
g->adj[ g->cd[v] + d[v]++ ]=u;
}
free(d);
//free(g->edges);
}
//freeing memory
void free_adjlist(adjlist *g){
free(g->edges);
free(g->cd);
free(g->adj);
free(g);
}
int main(int argc,char** argv){
adjlist* g;
time_t t1,t2;
t1=time(NULL);
printf("Reading edgelist from file %s\n",argv[1]);
g=readedgelist(argv[1]);
printf("Number of nodes: %lu\n",g->n);
printf("Number of edges: %lu\n",g->e);
printf("Building the adjacency list\n");
mkadjlist(g);
free_adjlist(g);
t2=time(NULL);
printf("- Overall time = %ldh%ldm%lds\n",(t2-t1)/3600,((t2-t1)%3600)/60,((t2-t1)%60));
return 0;
}
/*
Maximilien Danisch
September 2017
http://bit.ly/danisch
maximilien.danisch@gmail.com
Info:
Feel free to use these lines as you wish. This program loads an unweighetd graph in main memory as an adjacency matrix.
To compile:
"gcc adjmatrix.c -O9 -o adjmatrix".
To execute:
"./adjmatrix edgelist.txt".
"edgelist.txt" should contain the graph: one edge on each line (two unsigned long (nodes' ID)) separated by a space.
The prograph will load the graph in main memory and then terminate.
Note:
If the graph is directed (and weighted) with selfloops and you want to make it undirected unweighted without selfloops, use the following linux command line.
awk '{if ($1<$2) print $1" "$2;else if ($2<$1) print $2" "$1}' net.txt | sort -n -k1,2 -u > net2.txt
Performence:
Up to 200.000 nodes on my laptop with 8G of RAM.
Takes more or less 4G of RAM and 10 seconds (I have an SSD hardrive) for 100.000 nodes.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>//to estimate the runing time
#define NLINKS 100000000 //maximum number of edges for memory allocation, will increase if needed
typedef struct {
unsigned long s;
unsigned long t;
} edge;
//edge list structure:
typedef struct {
unsigned long n;//number of nodes
unsigned long e;//number of edges
edge *edges;//list of edges
bool *mat;//adjacency matrix
} adjmatrix;
//compute the maximum of three unsigned long
inline unsigned long max3(unsigned long a,unsigned long b,unsigned long c){
a=(a>b) ? a : b;
return (a>c) ? a : c;
}
//reading the edgelist from file
adjmatrix* readedgelist(char* input){
unsigned long e1=NLINKS;
FILE *file=fopen(input,"r");
adjmatrix *g=malloc(sizeof(adjmatrix));
g->n=0;
g->e=0;
g->edges=malloc(e1*sizeof(edge));//allocate some RAM to store edges
while (fscanf(file,"%lu %lu", &(g->edges[g->e].s), &(g->edges[g->e].t))==2) {
g->n=max3(g->n,g->edges[g->e].s,g->edges[g->e].t);
if (++(g->e)==e1) {//increase allocated RAM if needed
e1+=NLINKS;
g->edges=realloc(g->edges,e1*sizeof(edge));
}
}
fclose(file);
g->n++;
g->edges=realloc(g->edges,g->e*sizeof(edge));
return g;
}
//building the adjacency matrix
void mkmatrix(adjmatrix* g){
unsigned long i,u,v;
g->mat=calloc(g->n*g->n,sizeof(bool));
for (i=0;i<g->e;i++){
u=g->edges[i].s;
v=g->edges[i].t;
g->mat[u+g->n*v]=1;
g->mat[v+g->n*u]=1;
}
}
void free_adjmatrix(adjmatrix *g){
free(g->edges);
free(g->mat);
free(g);
}
int main(int argc,char** argv){
adjmatrix* g;
time_t t1,t2;
t1=time(NULL);
printf("Reading edgelist from file %s\n",argv[1]);
g=readedgelist(argv[1]);
printf("Number of nodes: %lu\n",g->n);
printf("Number of edges: %lu\n",g->e);
printf("Building the adjacency matrix\n");
mkmatrix(g);
free_adjmatrix(g);
t2=time(NULL);
printf("- Overall time = %ldh%ldm%lds\n",(t2-t1)/3600,((t2-t1)%3600)/60,((t2-t1)%60));
return 0;
}
/*
Maximilien Danisch
September 2017
http://bit.ly/danisch
maximilien.danisch@gmail.com
Info:
Feel free to use these lines as you wish. This program loads a graph in main memory as a list of edges.
To compile:
"gcc edgelist.c -O9 -o edgelist".
To execute:
"./edgelist edgelist.txt".
"edgelist.txt" should contain the graph: one edge on each line (two unsigned long (nodes' ID)) separated by a space.
The prograph loads the graph in main memory and then it terminates.
Note:
If the graph is directed (and weighted) with selfloops and you want to make it undirected unweighted without selfloops, use the following linux command line.
awk '{if ($1<$2) print $1" "$2;else if ($2<$1) print $2" "$1}' net.txt | sort -n -k1,2 -u > net2.txt
Performance:
Up to 500 million edges on my laptop with 8G of RAM:
Takes more or less 1.6G of RAM and 25 seconds (I have an SSD hardrive) for 100M edges.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>//to estimate the runing time
#define NLINKS 100000000 //maximum number of edges for memory allocation, will increase if needed
typedef struct {
unsigned long s;
unsigned long t;
} edge;
//edge list structure:
typedef struct {
unsigned long n;//number of nodes
unsigned long e;//number of edges
edge *edges;//list of edges
} edgelist;
//compute the maximum of three unsigned long
inline unsigned long max3(unsigned long a,unsigned long b,unsigned long c){
a=(a>b) ? a : b;
return (a>c) ? a : c;
}
//reading the edgelist from file
edgelist* readedgelist(char* input){
unsigned long e1=NLINKS;
FILE *file=fopen(input,"r");
edgelist *g=malloc(sizeof(edgelist));
g->n=0;
g->e=0;
g->edges=malloc(e1*sizeof(edge));//allocate some RAM to store edges
while (fscanf(file,"%lu %lu", &(g->edges[g->e].s), &(g->edges[g->e].t))==2) {
g->n=max3(g->n,g->edges[g->e].s,g->edges[g->e].t);
if (++(g->e)==e1) {//increase allocated RAM if needed
e1+=NLINKS;
g->edges=realloc(g->edges,e1*sizeof(edge));
}
}
fclose(file);
g->n++;
g->edges=realloc(g->edges,g->e*sizeof(edge));
return g;
}
void free_edgelist(edgelist *g){
free(g->edges);
free(g);
}
int main(int argc,char** argv){
edgelist* g;
time_t t1,t2;
t1=time(NULL);
printf("Reading edgelist from file %s\n",argv[1]);
g=readedgelist(argv[1]);
printf("Number of nodes: %lu\n",g->n);
printf("Number of edges: %lu\n",g->e);
free_edgelist(g);
t2=time(NULL);
printf("- Overall time = %ldh%ldm%lds\n",(t2-t1)/3600,((t2-t1)%3600)/60,((t2-t1)%60));
return 0;
}
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