Skip to content

Commit d30a1ba

Browse files
author
Carlos Miranda
committed
Multiple additions.
Restructured a bit and mostly added new files.
1 parent 7f46c56 commit d30a1ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+601
-34
lines changed

.gitignore

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
# Ignore eclipse files
1+
# Editors files
2+
## Eclipse
23
/java/.metadata
34
/java/**/.project
45
/java/**/.settings
56
/java/**/.classpath
67

7-
# Ignore binaries
8+
## Vi
9+
**/*.swp
10+
11+
# Binaries
812
**/bin/
13+
14+
# Residual objects
15+
**/*.o
16+
17+
# Notes to myself
18+
**/notes
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4-
typedef struct Node {
5-
int dest;
6-
struct Node *next;
7-
} Node;
8-
9-
typedef struct AdjList {
10-
Node *head;
11-
} AdjList;
12-
13-
typedef struct Graph {
14-
int V;
15-
AdjList *array;
16-
} Graph;
17-
184
Node *newNode(int dest) {
195
Node *node = (Node *) malloc(sizeof(Node));
206
node->dest = dest;
217
node->next = NULL;
228
return node;
239
}
2410

25-
Graph *createGraph(int V) {
26-
Graph *graph = (Graph *) malloc(sizeof(Graph));
11+
graph *create_graph(int V) {
12+
graph *graph = (graph *) malloc(sizeof(graph));
2713
graph->V = V;
2814

2915
graph->array = (AdjList *) malloc(V * sizeof(AdjList));
@@ -36,7 +22,7 @@ Graph *createGraph(int V) {
3622
return graph;
3723
}
3824

39-
void addEdge(Graph *graph, int src, int dest) {
25+
void add_edge(graph *graph, int src, int dest) {
4026
Node *node = newNode(dest);
4127
node->next = graph->array[src].head;
4228
graph->array[src].head = node;
@@ -46,10 +32,10 @@ void addEdge(Graph *graph, int src, int dest) {
4632
graph->array[dest].head = node;
4733
}
4834

49-
void printGraph(Graph *graph) {
50-
int v;
51-
for (v = 0; v < graph->V; v += 1) {
52-
Node *pCrawl = graph->array[v].head;
35+
void print_graph(graph *g) {
36+
int i;
37+
for (i = 0; i < g->nb_vertices; i += 1) {
38+
node *pcrawl = graph->list[i].head;
5339
printf("\n Adjacency list of vertex %d\n head ", v);
5440
while (pCrawl) {
5541
printf("-> %d", pCrawl->dest);
@@ -60,16 +46,18 @@ void printGraph(Graph *graph) {
6046
}
6147

6248
int main(void) {
63-
int V = 5;
64-
Graph *graph = createGraph(V);
65-
addEdge(graph, 0, 1);
66-
addEdge(graph, 0, 4);
67-
addEdge(graph, 1, 2);
68-
addEdge(graph, 1, 3);
69-
addEdge(graph, 1, 4);
70-
addEdge(graph, 2, 3);
71-
addEdge(graph, 3, 4);
49+
int nb_vertices = 5;
50+
51+
graph *g = create_graph(nb_vertices);
52+
53+
add_edge(g, 0, 1);
54+
add_edge(g, 0, 4);
55+
add_edge(g, 1, 2);
56+
add_edge(g, 1, 3);
57+
add_edge(g, 1, 4);
58+
add_edge(g, 2, 3);
59+
add_edge(g, 3, 4);
7260

73-
printGraph(graph);
61+
print_graph(g);
7462
return 0;
7563
}

c/adjacency_list/adjacency_list.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef ADJACENCY_LIST_H
2+
#define ADJACENCY_LIST_H
3+
4+
#include "../common/linked_list.h"
5+
6+
typedef struct graph {
7+
size_t nb_vertices;
8+
linked_list *vertices;
9+
} graph;
10+
11+
graph *create_graph(size_t);
12+
void destroy_graph(graph *);
13+
14+
label add_vertex(graph *, vertex_val);
15+
void add_edge(graph *, unsigned int, unsigned int);
16+
void print_graph(graph *);
17+
18+
#endif

c/adjacency_matrix/adjacencymatrix.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "adjacencymatrix.h"
2+
3+
4+
graph *init_graph_with_weights(size_t nb_vertices, double **weights) {
5+
6+
unsigned int i, j;
7+
graph *g = (graph *) malloc(sizeof(graph));
8+
g->nb_vertices = nb_vertices;
9+
g->weights = (double **) malloc(sizeof(double *) * nb_vertices);
10+
for (i = 0; i < nb_vertices; i += 1) {
11+
g->weights[i] = (double *) malloc(sizeof(double) * nb_vertices);
12+
for (j = 0; j < nb_vertices; j += 1) {
13+
g->weights[i][j] = weights[i][j];
14+
}
15+
}
16+
return g;
17+
18+
}
19+
20+
graph *init_graph(size_t nb_vertices) {
21+
22+
unsigned int i, j;
23+
graph *g = (graph *) malloc(sizeof(graph));
24+
g->nb_vertices = nb_vertices;
25+
g->weights = (double **) malloc(sizeof(double *) * nb_vertices);
26+
for (i = 0; i < nb_vertices; i += 1) {
27+
g->weights[i] = (double *) malloc(sizeof(double) * nb_vertices);
28+
for (j = 0; j < nb_vertices; j += 1) {
29+
g->weights[i][j] = 0.0;
30+
}
31+
}
32+
return g;
33+
34+
}
35+
36+
void add_edge(graph * const g, unsigned int v1, unsigned int v2, double weight) {
37+
if (v1 < g->nb_vertices && v2 < g->nb_vertices) {
38+
g->weights[v1][v2] = weight;
39+
}
40+
}
41+
42+
void remove_edge(graph * const g, unsigned int v1, unsigned int v2) {
43+
add_edge(g, v1, v2, 0.0);
44+
}
45+
46+
47+
void print_graph(const graph * const g) {
48+
49+
unsigned int i, j;
50+
for (i = 0; i < g->nb_vertices; i+= 1) {
51+
for (j = 0; j < g->nb_vertices; j += 1) {
52+
printf("\t%lf", g->weights[i][j]);
53+
}
54+
printf("\n");
55+
}
56+
57+
}

c/adjacency_matrix/adjacencymatrix.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef ADJACENCY_MATRIX_H
2+
#define ADJACENCY_MATRIX_H
3+
4+
typedef struct graph {
5+
size_t nb_vertices;
6+
double **weights;
7+
} graph;
8+
9+
graph *init_graph_with_weights(size_t nb_vertices, double **weights);
10+
graph *init_graph(size_t nb_vertices);
11+
void add_edge(graph * const g, unsigned int v1, unsigned int v2, double weight);
12+
void remove_edge(graph * const g, unsigned int v1, unsigned v2);
13+
void print_graph(const graph * const g);
14+
15+
#endif

c/common/linked_list.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "list.h"
2+
3+
void insert_first(linked_list, node_val);
4+
void insert_last(linked_list, node_val);
5+
void insert_before(linked_list, node_val, node_val);
6+
void insert_after(linked_list, node_val, node_val);
7+
8+
void remove_first(linked_list);
9+
void remove_last(linked_list);
10+
void remove_once(linked_list, node_val);
11+
void remove_all(linked_list, node_val);
12+
13+
int contains(linked_list, node_val);
14+
int count(linked_list, node_val);

c/common/linked_list.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef LINKED_LIST_H
2+
#define LINKED_LIST_H
3+
4+
typedef struct node {
5+
double value;
6+
unsigned int id;
7+
struct node *next;
8+
} node;
9+
10+
typedef struct linked_list {
11+
size_t id_count;
12+
node *head;
13+
} linked_list;
14+
15+
linked_list *init_ll();
16+
void destroy_ll(linked_list*);
17+
18+
void insert_first(linked_list, unsigned int, double);
19+
void insert_last(linked_list, unsigned int, double);
20+
void insert_before(linked_list, unsigned int, unsigned int, double);
21+
void insert_after(linked_list, unsigned int, unsigned int, double);
22+
23+
void remove_first(linked_list);
24+
void remove_last(linked_list);
25+
void remove_by_id(linked_list, unsigned int);
26+
void remove_by_value(linked_list, double);
27+
28+
int contains(linked_list, double);
29+
int count(linked_list, double);
30+
31+
#endif
File renamed without changes.
File renamed without changes.

c/incidence_matrix/incidence_matrix.c

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include "incidence_matrix.h"
5+
6+
// Private prototypes
7+
int check_edge(const matrix * const m, index v1, index v2);
8+
9+
// Interface implementation
10+
11+
matrix *load(FILE * const inf) {
12+
13+
// Check input argument
14+
if (inf == NULL) return NULL;
15+
16+
// Allocate matrix struct and helper variables
17+
matrix *m = (matrix *) malloc(sizeof(matrix));
18+
unsigned int i, j, v1, v2, nb_vertices, nb_edges;
19+
float tmp;
20+
21+
// Read first line of the file as number of vertices and edges
22+
fscanf(inf, "%u %u", &nb_vertices, &nb_edges);
23+
if (nb_vertices > MAX_VERTICES || nb_edges > MAX_EDGES) return NULL;
24+
25+
// Set up matrix structure
26+
m->lines = nb_vertices;
27+
m->columns = nb_edges;
28+
m->values = (float **) malloc(sizeof(float *));
29+
for (i = 0; i < nb_vertices; i += 1) {
30+
m->values[i] = (float *) malloc(sizeof(float) * nb_edges);
31+
//memset(m->values[i], 0.0f, nb_edges * sizeof(float));
32+
}
33+
m->unused_edges = NULL;
34+
m->nb_unused = 0;
35+
36+
// Set up matrix values
37+
for (i = 0; i < nb_edges; i += 1) {
38+
fscanf(inf, "%u %u %f", &v1, &v2, &tmp);
39+
m->values[v1][i] = m->values[v2][i] = tmp;
40+
for (j = 0; j < nb_vertices; j += 1) {
41+
if (j != v1 && j != v2) {
42+
m->values[j][i] = 0.0;
43+
}
44+
}
45+
}
46+
47+
return m;
48+
49+
}
50+
51+
void save(FILE * const outf, const matrix * const m) {
52+
53+
// Check output file
54+
if (outf == NULL) return;
55+
56+
// Helper variables
57+
unsigned int i, v1 = 0, v2;
58+
59+
// Print first line as number of vertices and number of edges
60+
fprintf(outf, "%u\t%u\n", m->lines, m->columns);
61+
62+
// Print all (vertex1, vertex2, value) triples
63+
for (i = 0; i < m->columns; i += 1) {
64+
while (v1 < m->lines && m->values[v1][i] == 0.0) v1 += 1;
65+
// Case of an unused edge entry
66+
if (v1 == m->lines) continue;
67+
v2 = v1 + 1;
68+
while (m->values[v2][i] == 0.0) v2 += 1;
69+
fprintf(outf, "%u\t%u\t%f\n", v1, v2, m->values[v1][i]);
70+
fprintf(outf, "%u\t%u\t%f\n", v2, v1, m->values[v2][i]);
71+
}
72+
}
73+
74+
75+
bool adjacent(const matrix * const m, index v1, index v2) {
76+
77+
unsigned int i = 0;
78+
int found = 0, v1_found = 0;
79+
while (!found) {
80+
while (i < m->columns && !m->values[v1][i]) i += 1;
81+
if (i < m->columns && m->values[v2][i] != 0.0) {
82+
found = 1;
83+
} else if (i >= m->columns) {
84+
found = -1;
85+
}
86+
}
87+
88+
return (found == 1 ? true : false);
89+
90+
}
91+
92+
index add_vertex(matrix * const m) {
93+
94+
m->lines += 1;
95+
m->values = (float **) realloc(m->values, m->lines * sizeof(float *));
96+
97+
return m->lines - 1;
98+
99+
}
100+
101+
void remove_vertex(matrix * const m, index vertex) {
102+
103+
// Remove all edges from/to the vertex to remove
104+
105+
// Swap last vertex with vertex to remove
106+
107+
// Truncate matrix
108+
109+
}
110+
111+
void add_edge(matrix * const m, index v1, index v2, float value) {
112+
if (!check_edge(m, v1, v2) || adjacent(m, v1, v2)) return;
113+
114+
unsigned int i;
115+
116+
if (m->unused_edges == NULL) {
117+
m->columns += 1;
118+
for (i = 0; i < m->lines; i += 1) {
119+
m->values[i] = (float *) realloc(m->values[i], m->columns * sizeof(float));
120+
m->values[i][m->columns - 1] = 0.0f;
121+
}
122+
} else {
123+
m->nb_unused -= 1;
124+
m->values[v1][m->unused_edges[m->nb_unused]] = value;
125+
m->values[v2][m->unused_edges[m->nb_unused]] = value;
126+
if (m->nb_unused == 0) {
127+
free(m->unused_edges);
128+
} else {
129+
m->unused_edges = (unsigned int *) realloc(m->unused_edges, m->nb_unused * sizeof(unsigned int));
130+
}
131+
}
132+
133+
}
134+
135+
void remove_edge(matrix * const m, index v1, index v2) {
136+
if (!check_edge(m, v1, v2)) return;
137+
}
138+
139+
140+
int check_edge(const matrix * const m, index v1, index v2) {
141+
return v1 >= m->lines && v2 >= m->lines;
142+
}
143+

0 commit comments

Comments
 (0)