Initial commit

This commit is contained in:
Jose
2025-12-29 09:16:47 +01:00
parent 8fa3dba5f5
commit a680d83b4e
9 changed files with 374 additions and 0 deletions

9
.clang-format Normal file
View File

@@ -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

10
.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
bin/
build/
.settings/
.cproject
.project
.clangd
*.o
*.obj

38
Makefile Normal file
View File

@@ -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

150
src/graph.c Normal file
View File

@@ -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;
}

62
src/graph.h Normal file
View File

@@ -0,0 +1,62 @@
/*
* graphs.h
*
* Created on: 29 dic 2025
* Author: jomaa
*/
#ifndef GRAPH_H_
#define GRAPH_H_
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <limits.h>
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_ */

30
src/graphs.c Normal file
View File

@@ -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;
}

14
src/graphs.h Normal file
View File

@@ -0,0 +1,14 @@
/*
* graphs.h
*
* Created on: 29 dic 2025
* Author: jomaa
*/
#ifndef GRAPHS_H_
#define GRAPHS_H_
#include <stdio.h>
#include "graph.h"
#endif /* GRAPHS_H_ */

38
src/metric_dimension.c Normal file
View File

@@ -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);
}

23
src/metric_dimension.h Normal file
View File

@@ -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_ */