mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-05 16:36:54 +08:00
Compare commits
25 Commits
14d64cc0a2
...
9e270b7425
Author | SHA1 | Date | |
---|---|---|---|
9e270b7425 | |||
5fd74a4d15 | |||
9d5f71250e | |||
5a2c2ceccc | |||
5e83dc2917 | |||
43b237487a | |||
43706105bb | |||
98eb005a4a | |||
ce973043d8 | |||
3ac92de18e | |||
c411ee96eb | |||
f63aa4db0a | |||
7ad5631aec | |||
c3ed2ab00d | |||
628df4c1a0 | |||
49bc8b0cc4 | |||
6965fa1e25 | |||
0520bb99ae | |||
238c16c3ec | |||
163f3c55da | |||
46f1f365fb | |||
af1a3703ba | |||
b788dd74ee | |||
c661ebaea6 | |||
4fb9ed9d58 |
23
README.md
23
README.md
@ -36,14 +36,23 @@
|
|||||||
|
|
||||||
|
|
||||||
## 性能比较
|
## 性能比较
|
||||||
|项目| 数组| 链表|
|
| 数据结构 | < | 时 |间 | | 复 | 杂 |度 | > | <空间复杂度> |
|
||||||
|---|---|---|
|
|---|---|---|---|---|---|---|---|---|---|
|
||||||
|查找| $O(1)$ | $O(n)$ |
|
|---|(|**平**|**均**|) | (|**最**|**坏**| ) |**最坏**|
|
||||||
|插入| $O(n)$ | $O(1)$ |
|
|---|访问|搜索|插入|删除|访问|搜索|插入|删除|---|
|
||||||
|删除| $O(n)$ | $O(1)$ |
|
| 数组 | $O(1)$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(1)$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$ |
|
||||||
|
| 栈 | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ |
|
||||||
**【A1】** 若链表的操作流程为,先查找元素再删除元素。那么时间复杂度确实是$O(n)$。但是链表的增删优势,在其他应用有体现。比如双向队列,插入和删除效率都为$O(1)$。
|
| 队列 | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ |
|
||||||
|
| 单向链表 | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ |
|
||||||
|
| 双向链表 | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ |
|
||||||
|
| 跳表 | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n*log(n))$ |
|
||||||
|
| 哈希表 | $N/A$ | $O(1)$ | $O(1)$ | $O(1)$ | $N/A$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$
|
||||||
|
| 二叉搜索树 | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$ |
|
||||||
|
| AVL树 | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(n)$ |
|
||||||
|
| 红黑树 | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(n)$ |
|
||||||
|
| B树 | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(n)$ |
|
||||||
|
|
||||||
|
**【答疑】** 若链表的操作流程为,先查找元素再删除元素。那么时间复杂度确实是$O(n)$。但是链表的增删优势,在其他应用有体现。比如双向队列,插入和删除效率都为$O(1)$。
|
||||||
|
|
||||||
## 规范
|
## 规范
|
||||||
### 版本说明
|
### 版本说明
|
||||||
|
107
include/graph.h
107
include/graph.h
@ -1,62 +1,109 @@
|
|||||||
/**
|
/**
|
||||||
* @file graph.h
|
* @file graph.h
|
||||||
* @author wenjf (Orig5826@163.com)
|
* @author wenjf (Orig5826@163.com)
|
||||||
* @brief
|
* @brief
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @date 2024-09-03
|
* @date 2024-09-03
|
||||||
*
|
*
|
||||||
* @copyright Copyright (c) 2024
|
* @copyright Copyright (c) 2024
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#ifndef _GRAPH_H
|
#ifndef _GRAPH_H
|
||||||
#define _GRAPH_H
|
#define _GRAPH_H
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
struct _graph_node {
|
#include "stack.h"
|
||||||
void *obj;
|
#include "queue.h"
|
||||||
uint32_t **edge;
|
|
||||||
uint8_t *visited;
|
enum _graph_type
|
||||||
|
{
|
||||||
|
GRAPH_UNDIRECTED,
|
||||||
|
GRAPH_DIRECTED,
|
||||||
|
// GRAPH_UNDIRECTED_WEIGHT,
|
||||||
|
// GRAPH_DIRECTED_WEIGHT,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _graph {
|
enum _graph_search
|
||||||
struct _graph_node *_head;
|
{
|
||||||
uint32_t **edges;
|
GRAPH_DFS,
|
||||||
|
GRAPH_BFS,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct _graph_edge
|
||||||
|
{
|
||||||
|
uint32_t weight;
|
||||||
|
struct _graph_edge* next;
|
||||||
|
void *target;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct _graph_node
|
||||||
|
{
|
||||||
|
void* obj;
|
||||||
|
struct _graph_node* next;
|
||||||
|
|
||||||
|
bool visited;
|
||||||
|
struct _graph_edge* edgehead;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct _graph
|
||||||
|
{
|
||||||
|
// -------------------- private --------------------
|
||||||
|
struct _graph_node* _head;
|
||||||
|
|
||||||
uint32_t _size;
|
uint32_t _size;
|
||||||
uint32_t _obj_size;
|
uint32_t _obj_size;
|
||||||
uint32_t _capacity;
|
uint32_t _capacity;
|
||||||
uint32_t _ratio;
|
uint32_t _ratio;
|
||||||
|
|
||||||
// init
|
enum _graph_type _type;
|
||||||
void (*init)(struct _graph *self);
|
enum _graph_search _search;
|
||||||
bool (*from_matrix)(struct _graph *self, void *obj, uint32_t *edges, uint32_t size);
|
|
||||||
|
|
||||||
|
stack_t stack;
|
||||||
|
queue_t queue;
|
||||||
|
|
||||||
|
struct _iterator _iter;
|
||||||
|
|
||||||
|
void (*_destory)(struct _graph* self);
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
// kernel
|
// kernel
|
||||||
bool (*add)(struct _graph *self, void *obj);
|
// -> vertex
|
||||||
bool (*get)(struct _graph *self, uint32_t idx, void *obj);
|
bool (*add_vertex)(struct _graph* self, void* obj);
|
||||||
bool (*remove)(struct _graph *self, uint32_t idx);
|
bool (*del_vertex)(struct _graph* self, void* obj);
|
||||||
|
bool (*find_vertex)(struct _graph* self, void* obj);
|
||||||
|
// -> edge
|
||||||
|
bool (*add_edge)(struct _graph* self, void* from, void* to, uint32_t weight);
|
||||||
|
bool (*del_edge)(struct _graph* self, void* from, void* to);
|
||||||
|
bool (*find_edge)(struct _graph* self, void* from, void* to);
|
||||||
|
|
||||||
|
bool (*empty)(struct _graph* self);
|
||||||
|
bool (*full)(struct _graph* self);
|
||||||
|
|
||||||
// base
|
// base
|
||||||
uint32_t (*size)(struct _graph *self);
|
uint32_t(*size)(struct _graph* self);
|
||||||
uint32_t (*capacity)(struct _graph *self);
|
uint32_t(*capacity)(struct _graph* self);
|
||||||
|
bool (*clear)(struct _graph* self);
|
||||||
|
|
||||||
bool (*empty)(struct _graph *self);
|
// iter
|
||||||
bool (*full)(struct _graph *self);
|
iterator_t (*iter)(struct _graph* self, enum _graph_search search, void *obj);
|
||||||
|
|
||||||
bool (*dfs)(struct _graph *self, uint32_t idx);
|
// config
|
||||||
bool (*bfs)(struct _graph *self, uint32_t idx);
|
compare_fun_t compare; // !!! you have to implement this function
|
||||||
|
|
||||||
//
|
// others
|
||||||
bool (*clear)(struct _graph *self);
|
bool (*from_matrix)(struct _graph* self, void* obj, uint32_t* edges, uint32_t size);
|
||||||
void (*destory)(struct _graph *self);
|
|
||||||
|
|
||||||
void (*print)(struct _graph *self);
|
// -------------------- debug --------------------
|
||||||
void (*print_obj)(void *obj);
|
void (*print)(struct _graph* self);
|
||||||
|
void (*print_obj)(void* obj);
|
||||||
};
|
};
|
||||||
typedef struct _graph* graph_t;
|
typedef struct _graph* graph_t;
|
||||||
|
|
||||||
graph_t graph_new2(uint32_t obj_size, uint32_t capacity);
|
graph_t graph_new(uint32_t obj_size);
|
||||||
void graph_free(graph_t *graph);
|
// graph_t graph_new2(uint32_t obj_size, uint32_t capacity);
|
||||||
|
|
||||||
|
void graph_free(graph_t* graph);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
1015
src/graph.c
1015
src/graph.c
File diff suppressed because it is too large
Load Diff
@ -12,12 +12,171 @@
|
|||||||
|
|
||||||
void test_graph_new(void)
|
void test_graph_new(void)
|
||||||
{
|
{
|
||||||
graph_t graph = graph_new2(sizeof(int), 10);
|
graph_t graph = graph_new(sizeof(int));
|
||||||
TEST_ASSERT_NOT_NULL(graph);
|
TEST_ASSERT_NOT_NULL(graph);
|
||||||
graph_free(&graph);
|
graph_free(&graph);
|
||||||
TEST_ASSERT_NULL(graph);
|
TEST_ASSERT_NULL(graph);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_graph_add_vertex(void)
|
||||||
|
{
|
||||||
|
const int size = 10;
|
||||||
|
int data[10] = {
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
};
|
||||||
|
int temp = 11;
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
graph_t graph = graph_new(sizeof(int));
|
||||||
|
TEST_ASSERT_NOT_NULL(graph);
|
||||||
|
graph->compare = compare_num;
|
||||||
|
graph->print_obj = print_num;
|
||||||
|
|
||||||
|
// test add_vertex
|
||||||
|
for(i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i]));
|
||||||
|
}
|
||||||
|
// graph->print(graph);
|
||||||
|
|
||||||
|
// test find_vertex
|
||||||
|
TEST_ASSERT_TRUE(graph->find_vertex(graph, &data[0]));
|
||||||
|
TEST_ASSERT_TRUE(graph->find_vertex(graph, &data[9]));
|
||||||
|
TEST_ASSERT_FALSE(graph->find_vertex(graph, &temp));
|
||||||
|
|
||||||
|
// test del_vertex
|
||||||
|
TEST_ASSERT_TRUE(graph->del_vertex(graph, &data[0]));
|
||||||
|
TEST_ASSERT_FALSE(graph->find_vertex(graph, &data[0]));
|
||||||
|
// graph->print(graph);
|
||||||
|
|
||||||
|
for(i = 1; i < size; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(graph->del_vertex(graph, &data[i]));
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(graph->empty(graph));
|
||||||
|
|
||||||
|
// test clear
|
||||||
|
for(i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i]));
|
||||||
|
}
|
||||||
|
// graph->print(graph);
|
||||||
|
|
||||||
|
graph->clear(graph);
|
||||||
|
// graph->print(graph);
|
||||||
|
TEST_ASSERT_TRUE(graph->empty(graph));
|
||||||
|
|
||||||
|
graph_free(&graph);
|
||||||
|
TEST_ASSERT_NULL(graph);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void test_graph_add_edge(void)
|
||||||
|
{
|
||||||
|
const int size = 10;
|
||||||
|
int data[10] = {
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
};
|
||||||
|
int temp = 11;
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
graph_t graph = graph_new(sizeof(int));
|
||||||
|
TEST_ASSERT_NOT_NULL(graph);
|
||||||
|
graph->compare = compare_num;
|
||||||
|
graph->print_obj = print_num;
|
||||||
|
|
||||||
|
// test add_vertex
|
||||||
|
for(i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i]));
|
||||||
|
}
|
||||||
|
//graph->print(graph);
|
||||||
|
|
||||||
|
// test add_edge
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 12));
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 13));
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 24));
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[5], &data[6], 67));
|
||||||
|
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
|
||||||
|
//graph->print(graph);
|
||||||
|
|
||||||
|
// test find_edge
|
||||||
|
TEST_ASSERT_TRUE(graph->find_edge(graph, &data[0], &data[1]));
|
||||||
|
TEST_ASSERT_TRUE(graph->find_edge(graph, &data[1], &data[3]));
|
||||||
|
// TEST_ASSERT_TRUE(graph->find_edge(graph, &data[6], &data[5])); // undirected graph
|
||||||
|
TEST_ASSERT_FALSE(graph->find_edge(graph, &data[4], &data[3]));
|
||||||
|
TEST_ASSERT_FALSE(graph->find_edge(graph, &data[0], &temp));
|
||||||
|
|
||||||
|
// test del_edge
|
||||||
|
TEST_ASSERT_TRUE(graph->del_edge(graph, &data[0], &data[1]));
|
||||||
|
//graph->print(graph);
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(graph->find_edge(graph, &data[0], &data[1]));
|
||||||
|
TEST_ASSERT_TRUE(graph->del_edge(graph, &data[5], &data[6]));
|
||||||
|
//graph->print(graph);
|
||||||
|
|
||||||
|
graph_free(&graph);
|
||||||
|
TEST_ASSERT_NULL(graph);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_graph_iter(void)
|
||||||
|
{
|
||||||
|
const int size = 10;
|
||||||
|
int data[10] = {
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
};
|
||||||
|
int temp = 11;
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
graph_t graph = graph_new(sizeof(int));
|
||||||
|
TEST_ASSERT_NOT_NULL(graph);
|
||||||
|
graph->compare = compare_num;
|
||||||
|
graph->print_obj = print_num;
|
||||||
|
|
||||||
|
// test add_vertex
|
||||||
|
for(i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i]));
|
||||||
|
}
|
||||||
|
// graph->print(graph);
|
||||||
|
|
||||||
|
// test add_edge
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 12));
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 13));
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 24));
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[5], &data[6], 67));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[7], &data[6], 87));
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[8], &data[2], 92));
|
||||||
|
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
|
||||||
|
graph->print(graph);
|
||||||
|
|
||||||
|
iterator_t iter_vertex = NULL;
|
||||||
|
|
||||||
|
iter_vertex = graph->iter(graph, GRAPH_BFS, &data[0]);
|
||||||
|
TEST_ASSERT_NOT_NULL(iter_vertex);
|
||||||
|
while(iter_vertex->hasnext(iter_vertex))
|
||||||
|
{
|
||||||
|
temp = *(int *)iter_vertex->next(iter_vertex);
|
||||||
|
graph->print_obj(&temp);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
iter_vertex = graph->iter(graph, GRAPH_DFS, &data[0]);
|
||||||
|
TEST_ASSERT_NOT_NULL(iter_vertex);
|
||||||
|
while(iter_vertex->hasnext(iter_vertex))
|
||||||
|
{
|
||||||
|
temp = *(int *)iter_vertex->next(iter_vertex);
|
||||||
|
graph->print_obj(&temp);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
graph_free(&graph);
|
||||||
|
TEST_ASSERT_NULL(graph);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
void test_graph_print(void)
|
void test_graph_print(void)
|
||||||
{
|
{
|
||||||
graph_t graph = graph_new2(sizeof(int), 10);
|
graph_t graph = graph_new2(sizeof(int), 10);
|
||||||
@ -56,12 +215,17 @@ void test_graph_from_matrix(void)
|
|||||||
graph_free(&graph);
|
graph_free(&graph);
|
||||||
TEST_ASSERT_NULL(graph);
|
TEST_ASSERT_NULL(graph);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void test_graph(void)
|
void test_graph(void)
|
||||||
{
|
{
|
||||||
UnitySetTestFile(__FILE__);
|
UnitySetTestFile(__FILE__);
|
||||||
|
|
||||||
RUN_TEST(test_graph_new);
|
RUN_TEST(test_graph_new);
|
||||||
|
RUN_TEST(test_graph_add_vertex);
|
||||||
|
RUN_TEST(test_graph_add_edge);
|
||||||
|
RUN_TEST(test_graph_iter);
|
||||||
|
|
||||||
// RUN_TEST(test_graph_print);
|
// RUN_TEST(test_graph_print);
|
||||||
// RUN_TEST(test_graph_from_matrix);
|
// RUN_TEST(test_graph_from_matrix);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user