-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseGraph.c
More file actions
80 lines (69 loc) · 1.75 KB
/
parseGraph.c
File metadata and controls
80 lines (69 loc) · 1.75 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
#include "parseGraph.h"
#include <stdlib.h>
GRAPH* allocateGraph(int n_nodes,int n_edges){
GRAPH* g = (GRAPH*)malloc(sizeof(GRAPH));
g->n_edges = n_edges;
g->n_nodes = n_nodes;
g->nodes = (NODE*)malloc(sizeof(NODE)*g->n_nodes);
for(int i=0; i<g->n_nodes; i++){
g->nodes[i].index = i;
g->nodes[i].edges = (NODE**)malloc(sizeof(NODE*)*g->n_edges);
}
return g;
}
GRAPH* getGraph(FILE* file)
{
int n_edges;
int n_nodes;
fscanf(file, "%d%d", &n_nodes, &n_edges);
GRAPH* g = allocateGraph(n_nodes,n_edges);
for(int i=0; i<g->n_nodes; i++)
{
int currentNumber;
fscanf(file, "%d", ¤tNumber);
for(int j=0; j<g->n_edges; j++)
{
int link;
fscanf(file, "%d", &link);
g->nodes[currentNumber].edges[j] = &g->nodes[link];
}
}
return g;
}
void printGraphAlt(GRAPH* graph)
{
for(int i=0; i<graph->n_nodes; i++)
{
printf("%d ", i);
for(int j=0; j<graph->n_edges; j++)
{
printf("%d ", graph->nodes[i].edges[j]->index);
}
printf("spin %d\n", graph->nodes[i].spin);
}
}
GRAPH* getGraphFromPython(int nodes, int edges)
{
char command[50];
sprintf(command, "python graf_console.py %d %d", nodes, edges);
FILE* pythonPipe = popen(command, "r");
if(!pythonPipe)
{
printf("Error opening pipe to python");
return 0;
}
int n_edges = edges;
int n_nodes = nodes;
GRAPH* g = allocateGraph(n_nodes,n_edges);
for(int i=0; i<g->n_nodes; i++)
{
for(int j=0; j<g->n_edges; j++)
{
int link;
fscanf(pythonPipe, "%d", &link);
g->nodes[i].edges[j] = &g->nodes[link];
}
}
pclose(pythonPipe);
return g;
}