diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..367e78c --- /dev/null +++ b/.clang-format @@ -0,0 +1,9 @@ +# We'll use defaults from the LLVM style, but with some modifications so that it's close to the CDT K&R style. +BasedOnStyle: LLVM +UseTab: Always +IndentWidth: 4 +TabWidth: 4 +PackConstructorInitializers: NextLineOnly +BreakConstructorInitializers: AfterColon +IndentAccessModifiers: false +AccessModifierOffset: -4 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..861170d --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +bin/ +build/ + +.settings/ +.cproject +.project +.clangd + +*.o +*.obj diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..108f89c --- /dev/null +++ b/Makefile @@ -0,0 +1,38 @@ +# Compilador +CC = gcc +CFLAGS = -Wall -Wextra -g -Iincludes $(shell sdl2-config --cflags) +LDFLAGS = $(shell sdl2-config --libs) -lm + +# Carpetas +SRC_DIR = src +OBJ_DIR = build +BIN_DIR = bin + +# Fuentes y objetos +SRC = $(wildcard $(SRC_DIR)/*.c) +OBJ = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC)) +TARGET = $(BIN_DIR)/graphs + +# Regla por defecto +all: $(TARGET) + +# Linkar el ejecutable +$(TARGET): $(OBJ) | $(BIN_DIR) + $(CC) -o $@ $^ $(LDFLAGS) + +# Compilar cada .c a .o en build/ +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) + $(CC) -c $(CFLAGS) $< -o $@ + +# Crear carpetas si no existen +$(OBJ_DIR): + mkdir -p $(OBJ_DIR) + +$(BIN_DIR): + mkdir -p $(BIN_DIR) + +# Limpiar objetos y binario +clean: + rm -rf $(OBJ_DIR)/* $(BIN_DIR)/* + +.PHONY: all clean diff --git a/src/graph.c b/src/graph.c new file mode 100644 index 0000000..fb2cbe0 --- /dev/null +++ b/src/graph.c @@ -0,0 +1,150 @@ +/* + * graphs.c + * + * Created on: 29 dic 2025 + * Author: jomaa + */ + +#include "graph.h" + +Graph_t *create_graph(int num_vertices, GraphDirection_t direction, + GraphMultiplicity_t multiplicity, GraphType_t type) +{ + Graph_t *graph = malloc(sizeof(Graph_t)); + if(!graph) return NULL; + + graph->num_vertices = num_vertices; + + graph->vertices = malloc(num_vertices * sizeof(*graph->vertices)); + if(!graph->vertices) { + free(graph); + return NULL; + } + + for(int i = 0; i < num_vertices; i++) { + graph->vertices[i].id = i; + graph->vertices[i].edges = NULL; + } + + graph->direction = direction; + graph->multiplicity = multiplicity; + graph->type = type; + + return graph; +} + +int add_edge(Graph_t *g, int u, int v) +{ + Edge_t *new_edge; + if(!g) return -EINVAL; + + if(u < 0 || v < 0 + || u >= g->num_vertices || v >= g->num_vertices) + return -EINVAL; + + new_edge = malloc(sizeof(Edge_t)); + if(!new_edge) return -ENOMEM; + + new_edge->to = &g->vertices[v]; + new_edge->next = g->vertices[u].edges; + g->vertices[u].edges = new_edge; + + return 0; +} + +int get_num_edges(Graph_t *g) +{ + if (!g) return -EINVAL; + + int num = 0; + int n = g->num_vertices; + bool **visited = malloc(n * sizeof(bool*)); + for (int i = 0; i < n; i++) + visited[i] = calloc(n, sizeof(bool)); + + for (int i = 0; i < n; i++) { + Edge_t *e = g->vertices[i].edges; + while (e) { + int u = g->vertices[i].id; + int v = e->to->id; + if (g->direction == GRAPH_DIRECTED || !visited[u][v]) { + num++; + if (g->direction == GRAPH_UNDIRECTED) + visited[u][v] = visited[v][u] = true; + } + e = e->next; + } + } + + for (int i = 0; i < n; i++) + free(visited[i]); + free(visited); + + return num; +} + +int distance(Graph_t *g, int u, int v) +{ + if (!g) return -EINVAL; + if (u < 0 || u >= g->num_vertices || v < 0 || v >= g->num_vertices) return -EINVAL; + + int n = g->num_vertices; + int *dist = malloc(n * sizeof(int)); + bool *visited = malloc(n * sizeof(bool)); + + // inicialización de la matriz binaria y de las distancias + for (int i = 0; i < n; i++) { + dist[i] = INT_MAX; + visited[i] = false; + } + + dist[u] = 0; + visited[u] = true; + + // cola para BFS + int *queue = malloc(n * sizeof(int)); + int front = 0, back = 0; + queue[back++] = u; + + while (front < back) { + int curr = queue[front++]; + Edge_t *e = g->vertices[curr].edges; + while (e) { + int nei = e->to->id; + if (!visited[nei]) { + visited[nei] = true; + dist[nei] = dist[curr] + 1; + queue[back++] = nei; + } + e = e->next; + } + } + + int steps = dist[v]; + if (steps == INT_MAX) steps = -1; // no hay camino + + free(dist); + free(visited); + free(queue); + + return steps; +} + +int free_graph(Graph_t *g) +{ + if (!g) return -EINVAL; + + for(int i = 0; i < g->num_vertices; i++) { + Edge_t *e = g->vertices[i].edges; + while (e) { + Edge_t *next = e->next; + free(e); + e = next; + } + } + + free(g->vertices); + free(g); + + return 0; +} \ No newline at end of file diff --git a/src/graph.h b/src/graph.h new file mode 100644 index 0000000..93798e1 --- /dev/null +++ b/src/graph.h @@ -0,0 +1,62 @@ +/* + * graphs.h + * + * Created on: 29 dic 2025 + * Author: jomaa + */ + +#ifndef GRAPH_H_ +#define GRAPH_H_ + +#include +#include +#include +#include + +typedef enum GraphDirection_t { + GRAPH_DIRECTED, + GRAPH_UNDIRECTED +} GraphDirection_t; + +typedef enum GraphMultiplicity_t { + GRAPH_SIMPLE, + GRAPH_MULTI +} GraphMultiplicity_t; + +typedef enum { + GRAPH_TYPE_PETERSEN, + GRAPH_TYPE_CYCLE, + GRAPH_TYPE_COMPLETE, + GRAPH_TYPE_BIPARTITE +} GraphType_t; + +typedef struct Edge_t { + struct Vertex_t *to; + struct Edge_t *next; +} Edge_t; + +typedef struct Vertex_t { + int id; + struct Edge_t *edges; +} Vertex_t; + +typedef struct Graph_t { + int num_vertices; + Vertex_t *vertices; + GraphDirection_t direction; + GraphMultiplicity_t multiplicity; + GraphType_t type; +} Graph_t; + +Graph_t *create_graph(int num_vertices, GraphDirection_t direction, + GraphMultiplicity_t multiplicity, GraphType_t type); + +int add_edge(Graph_t *g, int u, int v); + +int get_num_edges(Graph_t *g); + +int distance(Graph_t *g, int u, int v); + +int free_graph(Graph_t *g); + +#endif /* GRAPH_H_ */ diff --git a/src/graphs.c b/src/graphs.c new file mode 100644 index 0000000..77656f6 --- /dev/null +++ b/src/graphs.c @@ -0,0 +1,30 @@ +/* + * graphs.c + * + * Created on: 29 dic 2025 + * Author: jomaa + */ + +#include "graphs.h" + +int main(void) +{ + Graph_t *petersen_graph = create_graph(10, GRAPH_UNDIRECTED, GRAPH_SIMPLE, + GRAPH_TYPE_PETERSEN); + if (!petersen_graph) return EXIT_FAILURE; + + int edges[15][2] = { + {0,1},{0,4},{0,5},{1,2},{1,6},{2,3},{2,7},{3,4},{3,8}, + {4,9},{5,7},{5,8},{6,8},{6,9},{7,9} + }; + for (int i=0;i<15;i++) { + add_edge(petersen_graph, edges[i][0], edges[i][1]); + add_edge(petersen_graph, edges[i][1], edges[i][0]); + } + + printf("El número de aristas de P es: %d\r\n", get_num_edges(petersen_graph)); + printf("La distancia entre los vértices 2 y 8 es: %d\r\n", distance(petersen_graph, 2, 8)); + + free_graph(petersen_graph); + return EXIT_SUCCESS; +} diff --git a/src/graphs.h b/src/graphs.h new file mode 100644 index 0000000..0b3a4a8 --- /dev/null +++ b/src/graphs.h @@ -0,0 +1,14 @@ +/* + * graphs.h + * + * Created on: 29 dic 2025 + * Author: jomaa + */ + +#ifndef GRAPHS_H_ +#define GRAPHS_H_ + +#include +#include "graph.h" + +#endif /* GRAPHS_H_ */ diff --git a/src/metric_dimension.c b/src/metric_dimension.c new file mode 100644 index 0000000..03dc2f0 --- /dev/null +++ b/src/metric_dimension.c @@ -0,0 +1,38 @@ +/* + * metric_dimension.c + * + * Created on: 29 dic 2025 + * Author: jomaa + */ + +#include "metric_dimension.h" + +VertexSet_t *metric_base(Graph_t *g) +{ + return NULL; +} + +int metric_dimension(Graph_t *g) +{ + return 0; +} + +int is_resolving_set(Graph_t *g, VertexSet_t *set) +{ + return 0; +} + +void free_vertex_set(VertexSet_t *set) +{ + int i; + for(i = 0; i < set->size; i++) { + Edge_t *e = set->vertices[i].edges; + while (e) { + Edge_t *next = e->next; + free(e); + e = next; + } + } + free(set->vertices); + free(set); +} \ No newline at end of file diff --git a/src/metric_dimension.h b/src/metric_dimension.h new file mode 100644 index 0000000..517f34f --- /dev/null +++ b/src/metric_dimension.h @@ -0,0 +1,23 @@ +/* + * metric_dimension.h + * + * Created on: 29 dic 2025 + * Author: jomaa + */ + +#ifndef METRIC_DIMENSION_H_ +#define METRIC_DIMENSION_H_ + +#include "graph.h" + +typedef struct { + int size; + Vertex_t *vertices; +} VertexSet_t; + +VertexSet_t *metric_base(Graph_t *g); +int metric_dimension(Graph_t *g); +int is_resolving_set(Graph_t *g, VertexSet_t *set); +void free_vertex_set(VertexSet_t *set); + +#endif /* METRIC_DIMENSION_H_ */