-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphVis.c
More file actions
97 lines (91 loc) · 2.44 KB
/
GraphVis.c
File metadata and controls
97 lines (91 loc) · 2.44 KB
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
/*
* Graph Visulaiser
* Written By Zain Afzal for cs2521 2018 s1
*
* Creates a JSON rep of a graph for use
* with Sigma.js
*
* 13/5/18 - zain.afz@gmail.com
*/
#include "GraphVis.h"
#include "CentralityMeasures.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#define PI 3.14159265
char* nodeTmp = " {id:'%d',label:'%d',x:%f,y:%f, size:0.2, color: getColor(%f,gradient)}";
char* edgeTmp = " {source:'%d',target:'%d',id:'%d',label:'%d',type: 'curvedArrow'}";
NodeValues makeBlankNodes(int nV) {
NodeValues result;
result.noNodes = nV;
result.values = calloc(sizeof(double),nV);
int i = 0;
for(i=0; i < result.noNodes; i++) {
result.values[i] = 1;
}
return result;
}
NodeValues normalise(NodeValues nodes) {
int i = 0;
double max = -1;
for(i=0; i < nodes.noNodes; i++) {
if(nodes.values[i] > max) max = nodes.values[i];
}
for(i=0; i < nodes.noNodes; i++) {
nodes.values[i] = nodes.values[i]/max;
}
return nodes;
}
NodeValues getNodes(Graph g, int mode) {
if (mode == DEGREE)
return normalise(degreeCentrality(g));
if (mode == DEGREE_IN)
return normalise(outDegreeCentrality(g));
if (mode == DEGREE_OUT)
return normalise(inDegreeCentrality(g));
if (mode == BETWEENNESS)
return normalise(betweennessCentrality(g));
if (mode == CLOSENESS)
return normalise(closenessCentrality(g));
return makeBlankNodes(numVerticies(g));
}
void graphVis(Graph g, int mode) {
FILE *f = fopen("graphVisFiles/data.js","w");
fprintf(f, "g = {\n nodes: [\n");
int i = 0;
float x = 0;
float y = 0;
float a = 0;
int numVert = numVerticies(g);
float deltaAngle = 360/numVert;
float r = 0.3;
NodeValues nodes = getNodes(g,mode);
for(i=0; i < numVert; i++) {
x = r * cos(a*(PI / 180.0));
y = r * sin(a*(PI / 180.0));
a += deltaAngle;
fprintf(f, nodeTmp,i,i,x,y,nodes.values[i]);
if(i != numVert-1) fprintf(f, ",\n");
}
fprintf(f, "\n ],\n");
fprintf(f, " edges: [\n");
int c = 0;
for(i=0; i < numVert; i++) {
AdjList org = outIncident(g,i);
AdjList l = outIncident(g,i);
while(l != NULL) {
fprintf(f,edgeTmp,i,l->w,c,l->weight);
c++;
l = l->next;
if(l != NULL) fprintf(f, ",\n");
}
if (i != numVert-1 && org != NULL) fprintf(f, ",\n");
}
fprintf(f, "\n ]\n}");
char cwd[1024];
char * res = getcwd(cwd, 1024);
free(nodes.values);
fclose(f);
printf("See Graph: file://%s/graphVisFiles/seeGraph.html\n",res);
}