dijkstra_fast 1.4.2 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,83 +0,0 @@
1
- #ifndef DIJKSTRA_GRAPH_H
2
- #define DIJKSTRA_GRAPH_H
3
-
4
- #include <ruby.h>
5
-
6
- struct VertexListStruct;
7
- typedef struct VertexListStruct* VertexList;
8
-
9
- typedef struct VertexListStruct {
10
- struct VertexStruct *vertex;
11
- struct VertexListStruct *next;
12
- } VertexListStruct;
13
-
14
- //////////////////////////////////////////////////////////////////////////////////////
15
-
16
- struct EdgeListStruct;
17
- typedef struct EdgeListStruct* EdgeList;
18
-
19
- typedef struct EdgeListStruct {
20
- struct EdgeStruct *edge;
21
- struct EdgeListStruct *next;
22
- } EdgeListStruct;
23
-
24
- //////////////////////////////////////////////////////////////////////////////////////
25
-
26
- struct VertexStruct;
27
- typedef struct VertexStruct* Vertex;
28
-
29
- typedef struct VertexStruct {
30
- EdgeList edges;
31
- int id;
32
- VALUE label;
33
- } VertexStruct;
34
-
35
- //////////////////////////////////////////////////////////////////////////////////////
36
-
37
- struct EdgeStruct;
38
- typedef struct EdgeStruct* Edge;
39
-
40
- typedef struct EdgeStruct {
41
- Vertex source;
42
- Vertex dest;
43
- int distance;
44
- } EdgeStruct;
45
-
46
- //////////////////////////////////////////////////////////////////////////////////////
47
-
48
- struct GraphStruct;
49
- typedef struct GraphStruct* Graph;
50
-
51
- typedef struct GraphStruct {
52
- unsigned long vertex_count;
53
- VertexList vertices;
54
- st_table *vertex_lookup;
55
- } GraphStruct;
56
-
57
- //////////////////////////////////////////////////////////////////////////////////////
58
-
59
- void free_graph(void *data);
60
- void free_vertex(Vertex n);
61
- void free_vertex_list(VertexList vertices, void (*free_item)(Vertex));
62
- void free_edge(Edge e);
63
- void free_edge_list(EdgeList edges, void (*free_item)(Edge));
64
-
65
- //////////////////////////////////////////////////////////////////////////////////////
66
-
67
- Vertex add_vertex(Graph g, VALUE label);
68
- Edge add_edge(Vertex source, Vertex dest, int distance);
69
- Edge add_edge_with_labels(Graph g, VALUE source_label, VALUE dest_label, int distance);
70
- Vertex lookup_vertex(Graph g, VALUE label, bool create_if_missing);
71
-
72
- //////////////////////////////////////////////////////////////////////////////////////
73
-
74
- int shortest_path(Graph g, Vertex source, Vertex dest, VALUE best_path);
75
-
76
- //////////////////////////////////////////////////////////////////////////////////////
77
-
78
- void Init_dijkstra_graph();
79
- VALUE dijkstra_graph_allocate(VALUE self);
80
- VALUE dijkstra_graph_add_edge(VALUE self, VALUE source_label, VALUE dest_label, VALUE distance);
81
- VALUE dijkstra_graph_shortest_path(VALUE self, VALUE source_label, VALUE dest_label, VALUE best_path);
82
-
83
- #endif
@@ -1,128 +0,0 @@
1
- #include <prioritized_item_list.h>
2
-
3
- PrioritizedItemList make_prioritized_item_list(int capacity) {
4
- int i;
5
-
6
- PrioritizedItemList list = malloc(sizeof(PrioritizedItemListStruct));
7
- list->priorities = malloc(capacity * sizeof(PrioritizedItem));
8
- list->indices = malloc(capacity * sizeof(int));
9
- list->capacity = capacity;
10
- list->last = capacity - 1;
11
- for (i = 0; i < capacity; i++) {
12
- list->priorities[i] = malloc(sizeof(PrioritizedItemStruct));
13
- list->priorities[i]->item = i;
14
- list->priorities[i]->priority = INT_MAX;
15
- list->indices[i] = i;
16
- }
17
- return list;
18
- }
19
-
20
- bool empty_prioritized_item_list(PrioritizedItemList list) {
21
- return list->last < 0;
22
- }
23
-
24
- bool in_prioritized_item_list(PrioritizedItemList list, int item) {
25
- return list->indices[item] <= list->last;
26
- }
27
-
28
- void swap_prioritized_items(PrioritizedItemList list, int i, int j) {
29
- PrioritizedItem tmp = list->priorities[i];
30
- list->priorities[i] = list->priorities[j];
31
- list->priorities[j] = tmp;
32
- list->indices[list->priorities[i]->item] = i;
33
- list->indices[list->priorities[j]->item] = j;
34
- }
35
-
36
- void reprioritize_right(PrioritizedItemList list, int i) {
37
- int orig_i = i;
38
- int wi, wj_left, wj_right;
39
- int j_left, j_right;
40
- wi = list->priorities[i]->priority;
41
-
42
- while (true) {
43
- j_left = (i << 1) + 1;
44
- j_right = j_left + 1;
45
-
46
- wj_left = j_left <= list->last ? list->priorities[j_left]->priority : INT_MAX;
47
- wj_right = j_right <= list->last ? list->priorities[j_right]->priority : INT_MAX;
48
-
49
- if (wj_right < wi && wj_right < wj_left) {
50
- swap_prioritized_items(list, i, j_right);
51
- i = j_right;
52
-
53
- } else if (wj_left < wi) {
54
- swap_prioritized_items(list, i, j_left);
55
- i = j_left;
56
-
57
- } else {
58
- return;
59
- }
60
- }
61
- }
62
-
63
- void reprioritize_left(PrioritizedItemList list, int i) {
64
- int wi, wj;
65
- int j;
66
- wi = list->priorities[i]->priority;
67
-
68
- while (i > 0) {
69
- j = (i - 1) >> 1;
70
- wj = list->priorities[j]->priority;
71
-
72
- if (wj > wi) {
73
- swap_prioritized_items(list, i, j);
74
- i = j;
75
-
76
- } else {
77
- return;
78
- }
79
- }
80
- }
81
-
82
- int next_prioritized_item(PrioritizedItemList list) {
83
- PrioritizedItem item;
84
-
85
- if (empty_prioritized_item_list(list)) {
86
- return -1;
87
- }
88
-
89
- item = list->priorities[0];
90
- swap_prioritized_items(list, 0, list->last);
91
- list->last--;
92
- reprioritize_right(list, 0);
93
-
94
- return item->item;
95
- }
96
-
97
- void update_prioritized_item(PrioritizedItemList list, int item, int priority) {
98
- int i = list->indices[item];
99
- list->priorities[i]->priority = priority;
100
-
101
- if (i <= list->last) {
102
- reprioritize_left(list, i);
103
- }
104
- }
105
-
106
- int get_priority(PrioritizedItemList list, int item) {
107
- int index = list->indices[item];
108
- return list->priorities[index]->priority;
109
- }
110
-
111
- // For debugging only
112
- void print_prioritized_item_list(PrioritizedItemList list) {
113
- for (int i = 0; i <= list->last; i++) {
114
- if (i > 0) printf(", ");
115
- printf("%d (%d)", list->priorities[i]->item, list->priorities[i]->priority);
116
- }
117
- printf("\n");
118
- }
119
-
120
- void free_prioritized_item_list(PrioritizedItemList list) {
121
- for (int i = 0; i < list->capacity; i++) {
122
- free(list->priorities[i]);
123
- }
124
-
125
- free(list->priorities);
126
- free(list->indices);
127
- free(list);
128
- }
@@ -1,39 +0,0 @@
1
- #ifndef PRIORITIZED_ITEM_LIST_H
2
- #define PRIORITIZED_ITEM_LIST_H
3
-
4
- #include <ruby.h>
5
-
6
- struct PrioritizedItemStruct;
7
- typedef struct PrioritizedItemStruct* PrioritizedItem;
8
-
9
- typedef struct PrioritizedItemStruct {
10
- int item;
11
- int priority;
12
- } PrioritizedItemStruct;
13
-
14
- //////////////////////////////////////////////////////////////////////////////////////
15
-
16
- struct PrioritizedItemListStruct;
17
- typedef struct PrioritizedItemListStruct* PrioritizedItemList;
18
-
19
- typedef struct PrioritizedItemListStruct {
20
- PrioritizedItem *priorities;
21
- int *indices;
22
- int capacity;
23
- int last;
24
- } PrioritizedItemListStruct;
25
-
26
- //////////////////////////////////////////////////////////////////////////////////////
27
-
28
- PrioritizedItemList make_prioritized_item_list(int capacity);
29
- bool empty_prioritized_item_list(PrioritizedItemList list);
30
- bool in_prioritized_item_list(PrioritizedItemList list, int item);
31
- int next_prioritized_item(PrioritizedItemList list);
32
- void update_prioritized_item(PrioritizedItemList list, int item, int priority);
33
- void print_prioritized_item_list(PrioritizedItemList list);
34
- int get_priority(PrioritizedItemList list, int item);
35
- void free_prioritized_item_list(PrioritizedItemList list);
36
-
37
- //////////////////////////////////////////////////////////////////////////////////////
38
-
39
- #endif
@@ -1,17 +0,0 @@
1
- module DijkstraFast
2
- ##
3
- # The "best path" from applying Dijkstra's algorithm contains both the ordered
4
- # list of nodes travelled as well as the total distance travelled.
5
- ##
6
- class BestPath
7
-
8
- attr_reader :path
9
- attr_accessor :distance
10
-
11
- def initialize
12
- @path = []
13
- @distance = 0
14
- end
15
-
16
- end
17
- end