Skip to content

Commit 8dfc37f

Browse files
committed
add top k word and sparse matrix mul
1 parent 7884751 commit 8dfc37f

8 files changed

+586
-128
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
*.kdev4*
22
# Byte-compiled / optimized / DLL files
3+
*/.idea/*
34
.idea/*
5+
*/cmake-build-debug/*
46
__pycache__/
57
*.py[cod]
68
*$py.class

cpp/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ add_executable(String src/String.cpp)
66
add_executable(BinTree src/BinTree.cpp)
77
add_executable(LinkList src/LinkList.cpp)
88
add_executable(sort src/sort.cpp)
9+
add_executable(sparse_matrix src/sparse_matrix_mul.cpp)
10+
add_executable(top_k src/top_k_words.cpp)

cpp/ReadeMe.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## top k words
2+
Hashing is used in this solution. The words are readed from a file and the hash table is constructed at same time. The complexity of this part is O(M), where M is words' quantity. The complexity of sort part to find the top-k is O(nlog(k)). The total is (M+nlog(k)).
3+
4+
## sparse matrix multiplication
5+
The multiplication of two n×n matrices is one of the most basic algebraic problems and considerable effort was devoted to obtaining efficient algorithms for the task. The naive matrix multiplication algorithm performs O(n^3) operations. Strassen was the first to show that the naive algorithm is not optimal, giving an O(n^2.81) algorithm for the problem. Many improvements then followed. The currently fastest matrix multiplication algorithm, with a complexity of O(n^2.38 ), was obtained by Coppersmith and Winograd.
6+
7+
Considering that there are 5% elements are nonzero, so the algorithmic complexity of sparse matrix multiplication is O(max(m,n,k) + 0.05 * max(m*n,n*k) ).
8+
9+
After reading the question, it is obvious the data in matrix is float point, so I use float as the data precision, it is easy to change the data precision to double type.
10+
11+
12+
the running times of the two schemes (my code and the traditional one) when m = n = k = 1000 for the same generated matrices A and B is
13+
traditional matrix multiplication scheme use time 5.476 s
14+
sparse matrix multiplication scheme use time 1.701 s

cpp/data/example.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Welcome to the world of Geeks..,,,,,,,,,,,,, . . . . . .
2+
This portal has been created to provide well written well thought and well explained
3+
solutions for selected questions If you like Geeks for Geeks and would like to contribute
4+
here is your chance You can write article and mail your article to contribute at
5+
geeksforgeeks org See your article appearing on the Geeks for Geeks main page and helpthousands of other Geeks

cpp/data/example_2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Welcome to the world of Geeks
2+

cpp/src/String.cpp

+134-128
Original file line numberDiff line numberDiff line change
@@ -3,149 +3,155 @@
33
#include <limits.h>
44
#include <stack>
55
#include <string>
6+
#include <vector>
7+
8+
using namespace std;
69

710
class StringSolution {
811
public:
9-
10-
bool isValid(std::string const &s) {
11-
std::stack<char> st;
12-
for (auto c : s) {
13-
if (c == '[' || c == '(' || c == '{') {
14-
st.push(c);
15-
}
16-
if (c == ']' || c == ')' || c == '}') {
17-
if (st.empty()) {
18-
return false;
12+
bool isValid(std::string const &s) {
13+
std::stack<char> st;
14+
for (auto c : s) {
15+
if (c == '[' || c == '(' || c == '{') {
16+
st.push(c);
17+
}
18+
if (c == ']' || c == ')' || c == '}') {
19+
if (st.empty()) {
20+
return false;
21+
}
22+
char tmp = st.top();
23+
if ((tmp == '[' && c == ']') || (tmp == '(' && c == ')') ||
24+
(tmp == '{' && c == '}')) {
25+
st.pop();
26+
} else {
27+
return false;
28+
}
29+
}
1930
}
20-
char tmp = st.top();
21-
if ((tmp == '[' && c == ']') || (tmp == '(' && c == ')') ||
22-
(tmp == '{' && c == '}')) {
23-
st.pop();
24-
} else {
25-
return false;
31+
if (!st.empty()) {
32+
return false;
2633
}
27-
}
28-
}
29-
if (!st.empty()) {
30-
return false;
34+
return true;
3135
}
32-
return true;
33-
}
34-
std::string addBinary(std::string a, std::string b) {
35-
std::string result;
36-
int carry = 0;
37-
reverse(a.begin(), a.end());
38-
reverse(b.begin(), b.end());
39-
unsigned int alen = a.size();
40-
unsigned int blen = b.size();
41-
std::string short_str = b;
42-
std::string long_str = a;
43-
if (alen < blen) {
44-
short_str = a;
45-
long_str = b;
46-
}
47-
for (size_t i = 0; i < short_str.size(); i++) {
48-
unsigned int acc, val;
4936

50-
acc = short_str[i] + long_str[i] - 2 * '0' + carry;
51-
carry = acc / 2;
52-
val = acc % 2;
53-
result.insert(result.begin(), val + '0');
54-
}
55-
size_t idx = short_str.size();
56-
while (idx < long_str.size()) {
57-
unsigned int acc, val;
58-
acc = long_str[idx] - '0' + carry;
59-
carry = acc / 2;
60-
val = acc % 2;
61-
idx++;
62-
result.insert(result.begin(), val + '0');
63-
}
64-
if (carry) {
65-
result.insert(result.begin(), '1');
66-
}
67-
return result;
68-
}
69-
int myAtoi(const std::string &str) {
70-
auto start = str.begin();
71-
int result = 0;
72-
int sign = 1;
73-
while (*start == ' ' && start != str.end()) {
74-
start++;
37+
std::string addBinary(std::string a, std::string b) {
38+
std::string result;
39+
int carry = 0;
40+
reverse(a.begin(), a.end());
41+
reverse(b.begin(), b.end());
42+
unsigned int alen = a.size();
43+
unsigned int blen = b.size();
44+
std::string short_str = b;
45+
std::string long_str = a;
46+
if (alen < blen) {
47+
short_str = a;
48+
long_str = b;
49+
}
50+
for (size_t i = 0; i < short_str.size(); i++) {
51+
unsigned int acc, val;
52+
53+
acc = short_str[i] + long_str[i] - 2 * '0' + carry;
54+
carry = acc / 2;
55+
val = acc % 2;
56+
result.insert(result.begin(), val + '0');
57+
}
58+
size_t idx = short_str.size();
59+
while (idx < long_str.size()) {
60+
unsigned int acc, val;
61+
acc = long_str[idx] - '0' + carry;
62+
carry = acc / 2;
63+
val = acc % 2;
64+
idx++;
65+
result.insert(result.begin(), val + '0');
66+
}
67+
if (carry) {
68+
result.insert(result.begin(), '1');
69+
}
70+
return result;
7571
}
76-
if (*start == '+') {
77-
sign = 1;
78-
start++;
79-
} else if (*start == '-') {
80-
sign = -1;
81-
start++;
72+
73+
int myAtoi(const std::string &str) {
74+
auto start = str.begin();
75+
int result = 0;
76+
int sign = 1;
77+
while (*start == ' ' && start != str.end()) {
78+
start++;
79+
}
80+
if (*start == '+') {
81+
sign = 1;
82+
start++;
83+
} else if (*start == '-') {
84+
sign = -1;
85+
start++;
86+
}
87+
while (start != str.end()) {
88+
if (*start > '9' || *start < '0') {
89+
break;
90+
}
91+
if (result > INT_MAX / 10 ||
92+
(result == INT_MAX && (*start - '0') > INT_MAX % 10)) {
93+
return (sign == -1) ? INT_MIN : INT_MAX;
94+
}
95+
result *= 10;
96+
result += *start - '0';
97+
start++;
98+
}
99+
return result * sign;
82100
}
83-
while (start != str.end()) {
84-
if (*start > '9' || *start < '0') {
85-
break;
86-
}
87-
if (result > INT_MAX / 10 ||
88-
(result == INT_MAX && (*start - '0') > INT_MAX % 10)) {
89-
return (sign == -1) ? INT_MIN : INT_MAX;
90-
}
91-
result *= 10;
92-
result += *start - '0';
93-
start++;
101+
102+
bool isPalindrome(std::string s) {
103+
transform(s.begin(), s.end(), s.begin(), ::tolower);
104+
auto left = s.begin(), right = prev(s.end());
105+
while (left < right) {
106+
if (!::isalnum(*left))
107+
left++;
108+
else if (!::isalnum(*right))
109+
right--;
110+
else if (*left != *right)
111+
return false;
112+
else {
113+
left++;
114+
right--;
115+
}
116+
}
117+
return true;
94118
}
95-
return result * sign;
96-
}
97-
bool isPalindrome(std::string s) {
98-
transform(s.begin(), s.end(), s.begin(), ::tolower);
99-
auto left = s.begin(), right = prev(s.end());
100-
while (left < right) {
101-
if (!::isalnum(*left))
102-
left++;
103-
else if (!::isalnum(*right))
104-
right--;
105-
else if (*left != *right)
106-
return false;
107-
else {
108-
left++;
109-
right--;
110-
}
119+
120+
bool isMatch(const std::string &s, const std::string &p) {
121+
return isMatch(s.c_str(), p.c_str());
111122
}
112-
return true;
113-
}
114-
bool isMatch(const std::string &s, const std::string &p) {
115-
return isMatch(s.c_str(), p.c_str());
116-
}
117123

118124
private:
119-
bool isMatch(const char *s, const char *p) {
120-
if (*p == '\0')
121-
return *s == '\0';
122-
// next char is not '*', then must match current character
123-
if (*(p + 1) != '*') {
124-
if (*p == *s || (*p == '.' && *s != '\0'))
125-
return isMatch(s + 1, p + 1);
126-
else
127-
return false;
128-
} else { // next char is '*'
129-
while (*p == *s || (*p == '.' && *s != '\0')) {
130-
if (isMatch(s, p + 2))
131-
return true;
132-
s++;
133-
}
134-
return isMatch(s, p + 2);
125+
bool isMatch(const char *s, const char *p) {
126+
if (*p == '\0')
127+
return *s == '\0';
128+
// next char is not '*', then must match current character
129+
if (*(p + 1) != '*') {
130+
if (*p == *s || (*p == '.' && *s != '\0'))
131+
return isMatch(s + 1, p + 1);
132+
else
133+
return false;
134+
} else { // next char is '*'
135+
while (*p == *s || (*p == '.' && *s != '\0')) {
136+
if (isMatch(s, p + 2))
137+
return true;
138+
s++;
139+
}
140+
return isMatch(s, p + 2);
141+
}
135142
}
136-
}
137143
};
138144

139145
int main(int argc, char const *argv[]) {
140-
StringSolution sol;
141-
std::string s = "AASS,DD.****SSAA";
142-
std::string p = "ac*a";
143-
std::cout << sol.isMatch(s, p) << std::endl;
144-
std::cout << "is Palindrome" << std::endl;
145-
std::cout << sol.isPalindrome(".,") << std::endl;
146-
std::cout << "atoi\t" << sol.myAtoi("125") << std::endl;
147-
std::cout << " add Binary\t" << sol.addBinary("1010", "1011") << std::endl;
148-
printf("const std::string &)))");
149-
std::cout << "is valid closure " << sol.isValid("[(11111]") << '\n';
150-
return 0;
146+
StringSolution sol;
147+
std::string s = "AASS,DD.****SSAA";
148+
std::string p = "ac*a";
149+
std::cout << sol.isMatch(s, p) << std::endl;
150+
std::cout << "is Palindrome" << std::endl;
151+
std::cout << sol.isPalindrome(".,") << std::endl;
152+
std::cout << "atoi\t" << sol.myAtoi("125") << std::endl;
153+
std::cout << " add Binary\t" << sol.addBinary("1010", "1011") << std::endl;
154+
printf("const std::string &)))");
155+
std::cout << "is valid closure " << sol.isValid("[(11111]") << '\n';
156+
return 0;
151157
}

0 commit comments

Comments
 (0)