-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistances.c
170 lines (155 loc) · 4.42 KB
/
distances.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Copyright (c) 2020 Robert M. Vunabandi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "./distances.h"
#include "./alphabet.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
//
// First, defining functions to be used by the API functions in distance.h
//
/**
* A data structure to hold two characters in sorted order using the
* ALPHABET defined in `alphabet.h` for ordering.
*
* Invariants:
* - Both `first` and `second` appear in the ALPHABET
* - `first` appears before or in the same place as `second` in the ALPHABET
* - `second` appears after or in the same place as `first` in the ALPHABET
*/
typedef struct SortedChar {
char first;
char second;
} SortedChar;
/**
* This method is very self explanatory.
*
* Precondition:
* - s1 and s2 are in ALPHABET (i.e., lowercase alphanumeric)
*/
SortedChar getSortedChar(char s1, char s2) {
// Time Complexity: ALPHABET_LENGTH
char first = '\0';
char second = '\0';
#ifdef DEBUG
bool broke = false;
#endif
for (int i = 0; i < ALPHABET_LENGTH; i++) {
if (s1 == ALPHABET[i]) {
first = s1;
second = s2;
#ifdef DEBUG
broke = true;
#endif
break;
}
if (s2 == ALPHABET[i]) {
first = s2;
second = s1;
#ifdef DEBUG
broke = true;
#endif
break;
}
}
#ifdef DEBUG
assert(broke);
#endif
return (SortedChar) {.first = first, .second = second};
}
/**
* This computes the number of letters it takes to go from `first` to `second`
* in the alphabet. In case second appears before first in the alphabet, it
* will wrap around and compute the distance from `first` to "z" + 1 + distance
* from "a" to `second`. Each character counts as 1.
*/
int relativeCharDistance(char first, char second) {
// Time Complexity: WRAPPED_ALPHABET_LENGTH
int firstIdx = -1;
int secondIdx = -1;
for (int i = 0; i < WRAPPED_ALPHABET_LENGTH; i++) {
if (firstIdx == -1) {
assert(secondIdx == -1);
if (first == ALPHABET[i]) {
firstIdx = i;
if (second == ALPHABET[i]) {
secondIdx = i;
break;
}
}
continue;
}
assert(secondIdx == -1);
if (second == ALPHABET[i]) {
secondIdx = i;
break;
}
}
assert(firstIdx != -1);
assert(secondIdx != -1);
int result = secondIdx - firstIdx;
assert(result >= 0);
return result;
}
//
// Definitions for the API in distance.h
//
int hammingDistance(char *s1, char *s2, int m) {
// Time Complexity: m
int result = 0;
for (int i = 0; i < m; i++) {
if (s1[i] != s2[i]) {
result += 1;
}
}
return result;
}
int relativeDistance(char *s1, char *s2, int m) {
// Time Complexity: m * (getSortedChar + relativeCharDIstance)
int result = 0;
for (int i = 0; i < m; i++) {
SortedChar sc = getSortedChar(s1[i], s2[i]);
result += relativeCharDistance(sc.first, sc.second);
}
return result;
}
int rotRelativeDistance(char *s1, char *s2, int m) {
// Time Complexity: 2 * m * relativeCharDistance
int result = 0;
for (int i = 0; i < m; i++) {
int foward = relativeCharDistance(s1[i], s2[i]);
int backward = relativeCharDistance(s2[i], s1[i]);
result += foward < backward ? foward : backward;
}
return result;
}
int pairwiseHammingDistance(char *s1, char *s2, int m) {
int result = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (s1[i] != s2[j]) {
result += abs(j - i) + 1;
}
}
}
return result;
}