-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_am.c
More file actions
54 lines (44 loc) · 1 KB
/
graph_am.c
File metadata and controls
54 lines (44 loc) · 1 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
#include <stdlib.h>
#include <stdio.h>
#include "graph_am.h"
// Constructor
void init_graph_am(struct graph_am* G, int n) {
G->n = n;
G->M = (float*) malloc(n*n*sizeof(float));
int i;
int j;
for (i=0;i<G->n;i++) {
for (j=0;j<G->n;j++) {
G->M[i+i*(G->n-1)+j] = -1;
}
}
}
// Add an edge between i and j
void add_edge_am(struct graph_am* G, int i, int j, int w) {
G->M[i+i*(G->n-1)+j] = w;
}
// Printer
void print_graph_am(struct graph_am* G) {
int i;
int j;
float w;
printf("digraph G {\n\trankdir=LR;\n\n");
for (i=0;i<G->n;i++) {
printf("\t%d [shape=circle];\n", i);
}
printf("\n");
for (i=0;i<G->n;i++) {
for (j=0;j<G->n;j++) {
w = G->M[i+i*(G->n-1)+j];
if (w > 0) {
printf("\t%d -> %d ", i, j);
printf("[label=\"%2.1f\"];\n", w);
}
}
}
printf("}\n");
}
// Destructor
void clear_graph_am(struct graph_am* G) {
free(G->M);
}