From fb2a1a00e88f6a6409cd83cb78d7fd2cbfe4c6c2 Mon Sep 17 00:00:00 2001 From: ZhangYou0122 Date: Wed, 21 May 2014 14:41:26 +0800 Subject: [PATCH 01/53] 0 warning --- include/suffix_tree.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/suffix_tree.h b/include/suffix_tree.h index 05bffa33..00c9ac6a 100644 --- a/include/suffix_tree.h +++ b/include/suffix_tree.h @@ -21,7 +21,7 @@ class SuffixTree { public: // active point is initialized as (root, None, 0), remainder initialized as 1 - SuffixTree(string str):test_str(str), pos(0), root(test_str), active_point(&root, 0, 0), remainder(0), ls() {} + SuffixTree(string str):test_str(str), root(test_str), active_point(&root, 0, 0), remainder(0), pos(0), ls() {} int construct(void); // return -1 if no such sub exist, return the beginning postion of this substring in thr original string if it exist @@ -82,13 +82,13 @@ class SuffixTree struct Edge{ // the begin and end pos of this edge, note that INT_MAX stands for #(the changing end pos of this entire string) - int begin, end; + unsigned int begin, end; // Is there a better way to find test_str? string& test_node_str; Node * endpoint; - Edge(int b, int e, string& str): + Edge(unsigned int b, unsigned int e, string& str): test_node_str(str) { begin = b; @@ -97,7 +97,7 @@ class SuffixTree //std::cout << "Edge initialized" << std::endl; } - void change_edge(int b, int e) + void change_edge(unsigned int b, unsigned int e) { begin = b; end = e; @@ -118,7 +118,7 @@ class SuffixTree return me.begin < other.begin; } - char operator[](int i) + char operator[](unsigned int i) { i += begin; if (i > end) @@ -129,12 +129,12 @@ class SuffixTree friend ostream& operator<<(ostream& os, Edge& edge) { - int end = edge.test_node_str.size()-1; + unsigned int end = edge.test_node_str.size()-1; if (end >= edge.end) end = edge.end; char c; - for (int i=edge.begin; i<=end; i++) { + for (unsigned int i=edge.begin; i<=end; i++) { c = edge.test_node_str[i]; os << c; } @@ -246,7 +246,7 @@ class SuffixTree // how many suffixes is to be inserted? int remainder; // how many characters inserted? - int pos; + unsigned int pos; char get_ele(int i) { return test_str[i]; } // insert a char from pos to suffix tree int insert(); From b7dcd001887274d1b96f72465c625d2dbbc153a0 Mon Sep 17 00:00:00 2001 From: ZhangYou0122 Date: Sat, 31 May 2014 13:35:51 +0800 Subject: [PATCH 02/53] debugging --- include/suffix_tree.h | 53 +++++--- src/suffix_tree_demo.cpp | 271 +++++++++++++++------------------------ 2 files changed, 135 insertions(+), 189 deletions(-) diff --git a/include/suffix_tree.h b/include/suffix_tree.h index 00c9ac6a..3b429581 100644 --- a/include/suffix_tree.h +++ b/include/suffix_tree.h @@ -21,7 +21,7 @@ class SuffixTree { public: // active point is initialized as (root, None, 0), remainder initialized as 1 - SuffixTree(string str):test_str(str), root(test_str), active_point(&root, 0, 0), remainder(0), pos(0), ls() {} + SuffixTree(string str):test_str(str), root(test_str), active_point(&root, NULL, 0), remainder(0), pos(0), base_pos(0), ls() {} int construct(void); // return -1 if no such sub exist, return the beginning postion of this substring in thr original string if it exist @@ -94,7 +94,7 @@ class SuffixTree begin = b; end = e; endpoint = NULL; - //std::cout << "Edge initialized" << std::endl; + std::cout << "Edge initialized" << std::endl; } void change_edge(unsigned int b, unsigned int e) @@ -167,7 +167,7 @@ class SuffixTree make_pair(edge, true); edges.insert(make_pair(edge, true)); findedges.insert(make_pair(test_node_str[edge->begin], edge)); - //cout << "edge added. Now we have " << edges.size() << "edges." << endl; + cout << "edge added. Now we have " << edges.size() << "edges." << endl; } void del_edge(Edge* edge) { @@ -178,9 +178,9 @@ class SuffixTree else { // note we should erase the findedges too edges.erase(edge); - //cout << "delete" << (*edge)[0] << endl; + cout << "delete" << (*edge)[0] << endl; findedges.erase((*edge)[0]); - //cout << "edge deleted. Now we have " << edges.size() << "edges." << endl; + cout << "edge deleted. Now we have " << edges.size() << "edges." << endl; } } @@ -188,13 +188,18 @@ class SuffixTree // find edge by the first char Edge* find_edge(char c) { - //cout << "finding edge"; + cout << "finding edge char " << c; map::iterator iter = findedges.find(c); - //cout << "founded?" << endl; - if (iter != findedges.end()) + cout << " founded? "; + + if (iter != findedges.end()) { + cout << "yes." << endl; return iter->second; - else + } + else { + cout << "no." << endl; return NULL; + } } bool isleaf() { return edges.empty(); } @@ -224,10 +229,10 @@ class SuffixTree class ActivePoint{ public: Node* active_node; - char active_edge; + Edge* active_edge; int active_length; - ActivePoint(Node* node, char edge, int length): + ActivePoint(Node* node, Edge* edge, int length): active_node(node), active_edge(edge), active_length(length) { std::cout << "ActivePoint initialized" << std::endl; } }; @@ -236,8 +241,8 @@ class SuffixTree Node* get_active_node(void) { return active_point.active_node; } void set_active_node(Node* node) { active_point.active_node = node; cout << "Active node set as " << node << endl; } - char get_active_edge(void) { return active_point.active_edge; } - void set_active_edge(char edge) { active_point.active_edge = edge; } + Edge* get_active_edge(void) { return active_point.active_edge; } + void set_active_edge(Edge* edge) { active_point.active_edge = edge; } int get_active_length(void) { return active_point.active_length; } void set_active_length(int len) { active_point.active_length = len; } void inc_active_len() { active_point.active_length++; } @@ -247,6 +252,7 @@ class SuffixTree int remainder; // how many characters inserted? unsigned int pos; + unsigned int base_pos; // the beginnig position of suffixes need to be inserted char get_ele(int i) { return test_str[i]; } // insert a char from pos to suffix tree int insert(); @@ -258,21 +264,32 @@ class SuffixTree Node* seperate_edge(Node * node, Edge* edge, int rule); // check if we can change active node - void check_an(void) + void check_active_node(void) { Node* node = get_active_node(); - Edge* edge = node->find_edge(get_active_edge()); + Edge* edge = get_active_edge(); if (edge == NULL) return; - int edge_size = edge->end - edge->begin + 1; + unsigned int edge_size = edge->end - edge->begin + 1; + unsigned int length = get_active_length(); // update - if (edge_size == get_active_length()) { + if (edge_size == length) { set_active_node(edge->endpoint); set_active_edge(0); set_active_length(0); + base_pos += edge_size; + } + else if (length > edge_size) { + set_active_length(length-edge_size); + set_active_node(edge->endpoint); + int new_length = get_active_length(); + base_pos += edge_size; + Edge *new_active_edge = edge->endpoint->find_edge(get_ele(base_pos)); + set_active_edge(new_active_edge); + check_active_node(); } } @@ -292,7 +309,7 @@ class SuffixTree prev = curr; curr = node; - if (!first) { + if (first == false) { prev->suffix_link = curr; cout << "Suffix link added from prev " << prev << " to curr " << curr << endl; } diff --git a/src/suffix_tree_demo.cpp b/src/suffix_tree_demo.cpp index e3ce387c..e22fd57f 100644 --- a/src/suffix_tree_demo.cpp +++ b/src/suffix_tree_demo.cpp @@ -54,219 +54,148 @@ int SuffixTree::construct(void) while (pos < test_str.size()) { ls.clear(); remainder++; - //cout << "Char: " << test_str[pos] << endl; + cout << "Char: " << test_str[pos] << endl; + cout << "Suffix yet inserted begin at " << base_pos << " " << test_str[base_pos] << endl; - bool flag = true; - while (flag) - flag = insert(); + int move = 0; + while (move == 0) + move = insert(); pos++; } return 0; } int SuffixTree::insert(void) -{ - int result = 0; - - Node* node = active_point.active_node; - if (node == (&root)) { - //cout << "ActiveNode is root." << endl; - result = insert_rule1(); - } - else { - //cout << "ActiveNode isn't root." << endl; - result = insert_rule3(); - } - - return result; -} - -// rule1 applies when the active node is root -int SuffixTree::insert_rule1(void) { using std::numeric_limits; - - //cout << "Rule 1" << endl; - Node* node = &root; - Edge* a_edge = node->find_edge(get_active_edge()); + int move = 1; - // next active edge - char active_char = 0; + Node* node = active_point.active_node; + int length = get_active_length(); - // can we find a match at active node? - Edge* possible = NULL; - bool will_insert = false; - if (get_active_length() != 0 && a_edge != NULL) { - // shouldn't throw out_of_range here, e.g. abcabc* - char match_char = (*a_edge)[get_active_length()]; - if (match_char == get_ele(pos)) - possible = a_edge; - else - will_insert = true; // will insert while active length is not 0 and activechar don't match - //cout << "Active char is " << active_char << endl; + Edge* a_edge = get_active_edge(); - // node for insertion - } - else if (get_active_length() == 0) { - //cout << "Active char is NULL." << endl; - possible = node->find_edge(get_ele(pos)); - - // new active edge here and only here! - if (possible) - active_char = get_ele(pos); - else - active_char = 0; + if (a_edge == NULL) { + Edge *search_edge = node->find_edge(get_ele(pos)); + + if (search_edge == NULL) { + // insert new suffix + Edge *new_edge = new Edge(pos, numeric_limits::max(), test_str); + node->add_edge(new_edge); + remainder--; + if (node->suffix_link) { + // move to suffix link + set_active_node(node->suffix_link); + Edge *new_active_edge = node->suffix_link->find_edge(base_pos); + set_active_edge(new_active_edge); + move = 0; + } + else { + // move to root + set_active_node(&root); + + if (base_pos != pos) { + Edge *new_active_edge = root.find_edge(get_ele(base_pos)); + set_active_edge(new_active_edge); + dec_active_len(); + } + base_pos++; + + if (remainder > 0) { + move = 0; + } + else + move = 1; + } + } + else { + // set new active edge + set_active_edge(search_edge); + inc_active_len(); + move = 1; + } } else { - cout << "Error!!!!!!!!!!!!!!!!!!!1" << endl; - //throw; - } - - - if (possible) { - remainder++; + char expected_ele = (*a_edge)[get_active_length()]; - // if not 0, then it's not a new edge, should not set - if (get_active_length() == 0) - set_active_edge(active_char); - - inc_active_len(); - check_an(); - } - else { - // seperate the old edge, set new active edge - if (a_edge != NULL) { - node = seperate_edge(node, a_edge, 1); + cout << (*a_edge) << endl; + cout << get_active_length() << endl; + cout << expected_ele << " vs " << get_ele(pos) << endl; + if (expected_ele == get_ele(pos)) { + // expand active length + inc_active_len(); + check_active_node(); + move = 1; } - else - set_active_edge(0); - - //cout << "append a new edge at endpoint" << endl; - Edge* new_edge2 = new Edge(pos, numeric_limits::max(), test_str); - //cout << node << endl; - node->add_edge(new_edge2); - } + else { + // seperate edge and insert new suffix + Node * new_node = seperate_edge(node, a_edge, length); + Edge *new_edge = new Edge(pos, numeric_limits::max(), test_str); + new_node->add_edge(new_edge); + remainder--; + ls.ins_link(new_node); + + if (new_node->suffix_link) { + // move to suffix link + set_active_node(node->suffix_link); + Edge *new_active_edge = node->suffix_link->find_edge(get_ele(base_pos)); + set_active_edge(new_active_edge); + move = 0; + } + else { + // fall back to root + set_active_node(&root); + dec_active_len(); + base_pos++; + if (base_pos != pos) { + Edge *new_active_edge = root.find_edge(get_ele(pos-remainder+1)); + set_active_edge(new_active_edge); + base_pos = pos - remainder + 1; + + set_active_length(pos-base_pos); + check_active_node(); + + move = 0; + } + else { + set_active_edge(NULL); + move = 0; + } + } - remainder--; + } - return will_insert; + } + return move; } SuffixTree::Node* SuffixTree::seperate_edge(Node * node, Edge* a_edge, int rule) { - //cout << "seperate the old edge here: " << (*a_edge) << endl; - - char active_char; - - if (remainder > 2) - active_char = (*a_edge)[1]; - else - active_char = get_ele(pos); - + cout << "seperate the old edge here: " << (*a_edge) << endl; int new_begin = a_edge->begin + get_active_length(); int new_end = a_edge->end; int old_begin = a_edge->begin; int old_end = new_begin - 1; - //cout << node->find_edge(active_char) << "|||||||||||||||||||||||||| char " << active_char << endl; - //cout << (*node); + cout << (*node); node->del_edge(a_edge); a_edge->change_edge(new_begin, new_end); Edge* old_edge1 = new Edge(old_begin, old_end, test_str); node->add_edge(old_edge1); - //cout << node->find_edge(active_char) << "||||||||||||||||||||||||||2 char " << active_char << endl; old_edge1->endpoint->add_edge(a_edge); - //cout << (*node); -// old_edge1->endpoint->suffix_link = a_edge->endpoint->suffix_link; -// a_edge->endpoint->suffix_link = NULL; -/*----------------------------------------------------------------------- - Edge* new_edge1 = new Edge(new_begin, new_end, test_str); - a_edge->endpoint->add_edge(new_edge1); -------------------------------------------------------------------*/ - - //cout << "change edge" << endl; - //cout << "What's wrong?" << endl; cout << "The old edge split as -- " << (*a_edge) << " and -- " << (*old_edge1) << endl; - //cout << "What's wrong?" << endl; - - if (rule == 1) { - set_active_edge(active_char); - dec_active_len(); - } - else if (rule == 3) { - Node* n = &root; // new active node - //cout << node; - if (node->suffix_link) { - n = node->suffix_link; - cout << " Moved to suffix link!--------------" << endl; - } - else - cout << " Moved to root!------------------" << endl; - set_active_node(n); - } cout << "root " << (&root) << endl; - //cout << node << endl; + cout << node << endl; Node* new_node = old_edge1->endpoint; - ls.ins_link(new_node); - //cout << node << endl; - - return new_node; -} - -// applies when the active is not root -int SuffixTree::insert_rule3() -{ - //cout << "Rule3" << endl; - Node * node = get_active_node(); - cout << "Active node " << node << endl; - Edge * edge = node->find_edge(get_active_edge()); - - // input match a suffix? - bool match = false; - if (get_active_length() == 0) { - if (node->find_edge(get_ele(pos))) { - match = true; - - set_active_edge(get_ele(pos)); - inc_active_len(); - check_an(); - } - } - else { - // assert edge is not NULL - char match_char = (*edge)[get_active_length()]; - if (match_char == get_ele(pos)) { - match = true; - - inc_active_len(); - check_an(); - } - } - - if (match) - return 0; - - if (edge != NULL) { - node = seperate_edge(node, edge, 3); - } - - using std::numeric_limits; - - //cout << "append a new edge at endpoint" << endl; - Edge* new_edge2 = new Edge(pos, numeric_limits::max(), test_str); cout << node << endl; - node->add_edge(new_edge2); - remainder--; - - return 1; // should insert again at a different node - + return new_node; } int SuffixTree::print_tree() @@ -313,7 +242,7 @@ using namespace std; int main() { cout << "Begining" << endl; - SuffixTree st("BANANAS"); + SuffixTree st("mississippi"); cout << "Constructing..." << endl; st.construct(); From a021ec6ea3633a6604c535ca76d704477d075e16 Mon Sep 17 00:00:00 2001 From: cdkr <775481991@qq.com> Date: Sat, 21 Jun 2014 03:10:26 +0800 Subject: [PATCH 03/53] Optimized test_prime --- include/prime.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/prime.h b/include/prime.h index 8780dfd3..fd7b73ef 100644 --- a/include/prime.h +++ b/include/prime.h @@ -35,7 +35,7 @@ namespace alg { if (n%2 == 0) return false; unsigned sqrtn = sqrt(n); - for (unsigned int i = 2; i <= sqrtn; ++i) { + for (unsigned int i = 3; i <= sqrtn; i+=2) { if (n % i == 0) { return false; } From 1400e7c5207fd2a36d981badeb082860e124244d Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 25 Jul 2014 17:09:44 +0800 Subject: [PATCH 04/53] fix a bug in quicksort --- include/quick_sort.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/quick_sort.h b/include/quick_sort.h index 35caf1dd..a446f7af 100644 --- a/include/quick_sort.h +++ b/include/quick_sort.h @@ -74,8 +74,8 @@ namespace alg { quicksort(list, begin, pivot_idx-1); quicksort(list, pivot_idx+1, end); } else if ( begin + 1 == end) { - if (list[begin + 1] > list[end]) - swap(list[begin + 1], list[end]); + if (list[begin] > list[end]) + swap(list[begin], list[end]); } } } From f1473fa85ee067efa35244c230aaac8bd291ddd5 Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 25 Jul 2014 17:16:30 +0800 Subject: [PATCH 05/53] rollback quicksort to random pivot version --- include/quick_sort.h | 51 +++++++++++++------------------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/include/quick_sort.h b/include/quick_sort.h index a446f7af..b8962216 100644 --- a/include/quick_sort.h +++ b/include/quick_sort.h @@ -19,49 +19,31 @@ #define __QUICKSORT_H__ #include -#include namespace alg { - - /** - * Return median of begin, middle, and end. - * Order these and hide the pivot. - */ - template - static const T & __median3(T list[], int begin, int end) { - assert(begin + 2 <= end); - int middle = end - (end - begin) / 2; - if (list[middle] < list[begin]) - swap(list[middle], list[begin]); - if (list[end] < list[begin]) - swap(list[end], list[begin]); - if (list[end] < list[middle]) - swap(list[end], list[middle]); - - //Place pivot at position [end - 1] - swap(list[middle], list[end - 1]); - return list[end - 1]; - } - /** * the quick-sort partition routine */ template static int __partition(T list[],int begin, int end) { - T pivot = __median3(list, begin, end); - - int i = begin; - int j = end - 1; - - while(i < j) { - while(list[++i] < pivot) {} - while(pivot < list[--j]) {} + int pivot_idx = RANDOM(begin,end); + T pivot = list[pivot_idx]; + swap(list[begin], list[pivot_idx]); + + int i = begin + 1; + int j = end; + + while(i <= j) { + while((i <= end) && (list[i] <= pivot)) + i++; + while((j >= begin) && (list[j] > pivot)) + j--; if(i < j) swap(list[i],list[j]); } - swap(list[i],list[end - 1]); - return i; // final pivot position + swap(list[begin],list[j]); + return j; // final pivot position } /** @@ -69,13 +51,10 @@ namespace alg { */ template static void quicksort(T list[],int begin,int end) { - if( begin + 1 < end) { + if( begin < end) { int pivot_idx = __partition(list, begin, end); quicksort(list, begin, pivot_idx-1); quicksort(list, pivot_idx+1, end); - } else if ( begin + 1 == end) { - if (list[begin] > list[end]) - swap(list[begin], list[end]); } } } From 62761362a454e8cd48c148059f32c6fddda92da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Su=C3=A1rez?= Date: Tue, 9 Sep 2014 20:00:50 +0200 Subject: [PATCH 06/53] AVL tree added, but there's still a bug in the erasure method --- Makefile | 6 +- README.md | 20 +-- include/avl.h | 230 +++++++++++++++++++++++++++++++++++ include/binary_search_tree.h | 2 +- src/avl_demo.cpp | 59 +++++++++ 5 files changed, 305 insertions(+), 12 deletions(-) create mode 100644 include/avl.h create mode 100644 src/avl_demo.cpp diff --git a/Makefile b/Makefile index ed8cccc0..73ca5f8a 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CC=gcc CPP=g++ AR=ar RANLIB=ranlib -CFLAGS= -g -Wall -Wno-unused-function -std=gnu++0x +CFLAGS= -g -Wall -Wno-unused-function -std=c++11 SRCDIR = ./src INCLUDEDIR = -I./include -I. DEPS = @@ -70,7 +70,8 @@ PROGRAMS = m_based_demo \ 8queue_demo \ palindrome_demo \ suffix_array_demo \ - suffix_tree_demo + suffix_tree_demo \ + avl_demo all: $(PROGRAMS) @@ -79,3 +80,4 @@ all: $(PROGRAMS) clean: rm -rf $(PROGRAMS) *.dSYM + diff --git a/README.md b/README.md index e907418f..9f2ddb6f 100644 --- a/README.md +++ b/README.md @@ -35,11 +35,11 @@ Queue Stack Binary Heap - Fibonacci Heap + Fibonacci Heap Priority Queue (list based) - Bubble sort - Selection sort + Bubble sort + Selection sort Insertion sort Radix sort Quick sort @@ -51,13 +51,14 @@ Largest common sequence Binary search tree + AVL tree Dynamic order statistics Red-black tree Interval tree Prefix Tree(Trie) Suffix Tree B-Tree - Suffix Array + Suffix Array Hash by multiplication Hash table @@ -72,7 +73,7 @@ Base64 Graph data structure - Strongly Connected Components(SCC) + Strongly Connected Components(SCC) Prim's minimum spanning tree Kruskal MST Directed/Undirected graph ops @@ -89,13 +90,14 @@ K-Means Knuth–Morris–Pratt algorithm Disjoint-Set - 8-Queue Problem - Palindrome + 8-Queue Problem + Palindrome ####贡献者 ( Contributors ) : - Samana : for heavy work of MSVC compatability + Samana: for heavy work of MSVC compatability wycg1984: for K-Means xmuliang: for HeapSort, Kruskal MST wyh267: for base64, LRU, bubble sort, selection sort ZhangYou0122: Push-Relabel algorithm, Suffix Tree - UsingtcNower: Suffix Array + UsingtcNower: Suffix Array + afernandez90: AVL trees diff --git a/include/avl.h b/include/avl.h new file mode 100644 index 00000000..9dc6d621 --- /dev/null +++ b/include/avl.h @@ -0,0 +1,230 @@ +/******************************************************************************* + * ALGORITHM IMPLEMENTAIONS + * + * /\ | _ _ ._ o _|_ |_ ._ _ _ + * /--\ | (_| (_) | | |_ | | | | | _> + * _| + * + * Adelson-Velskii and Landis' (AVL) tree + * + * Features, being N the number of elements in the tree: + * 1. Guaranteed search time is O(log(N)). + * 2. Dynamically updated/balanced tree structure O(N) storage. + * 3. Exportable to GraphViz format for easy visualization and verification + * + * http://en.wikipedia.org/wiki/AVL_tree + * + ******************************************************************************/ + +#ifndef __AVL_H__ +#define __AVL_H__ + +#include +#include +#include + +namespace alg { + +template +class AVL { + + public: + + AVL() : tree(0), numNodes(0) {} + + T root () const { return tree->value; } + unsigned height() const { return Node::getHeight(tree); } + unsigned size() const { return numNodes; } + bool isEmpty() const { return numNodes == 0; } + + bool contains(const T &x) const { + if (!isEmpty()) { + return tree->contains(x); + } else return false; + } + + void insert(const T &x) { + if (isEmpty()) tree = new Node(x); + else tree = tree->insert(x); + numNodes++; + } + + void erase(const T &x) { + if (!isEmpty()) { + tree = tree->erase(x); + numNodes--; + } + } + + void toGraphViz(std::ostream &stream, std::string name) const { + if (!isEmpty()) { + stream << "digraph " << name << " {" << std::endl; + tree->toGraphViz(stream); + stream << "}" << std::endl; + } + } + + private: + + struct Node { + Node *left, *right; + T value; + unsigned height; + + Node(const T &x) : left(0), right(0), value(x), height(1) {} + + bool contains(const T &x) const { + if (value == x) return true; + else if (x < value && left != 0) return left->contains(x); + else if (right != 0) return right->contains(x); + else return false; + } + + Node *insert(const T &x) { + if (x <= value) { + if (left == 0) left = new Node(x); + else left = left->insert(x); + } + else { + if (right == 0) right = new Node(x); + else right = right->insert(x); + } + + return update(); + } + + Node *erase(const T &x) { + if (value == x) { + if (left == 0 && right == 0) { + delete this; + return 0; + } else if (left == 0) { + *this = *right; + delete right; + } else if (right == 0) { + *this = *left; + delete left; + } else { + // Tracing path to rightmost leaf of the left subtree + std::stack trace; + + Node *current = left; + while (current->right != 0) { + trace.push(current); + current = current->right; + } + + value = current->value; + Node *lsubtree = current->left; + delete current; + + if (trace.empty()) trace.push(left); + + trace.top()->right = lsubtree; + + do { + trace.top()->update(); + trace.pop(); + } while (!trace.empty()); + } + return update(); + } + else if (x < value) { + if (left != 0) { + left = left->erase(x); + return update(); + } else return this; + } + else { + if (right != 0) { + right = right->erase(x); + return update(); + } else return this; + } + } + + Node *update() { + updateHeight(); + + if (getBF(this) >= 2) { + if (getBF(left) <= -1) LR(); + return LL(); + } else if (getBF(this) <= -2) { + if (getBF(right) >= 1) RL(); + return RR(); + } else return this; + } + + void updateHeight() { height = std::max(getHeight(left), getHeight(right)) + 1; } + + void LR() { + Node *lrcopy = left->right; + left->right = lrcopy->left; + lrcopy->left = left; + left = lrcopy; + left->left->updateHeight(); + left->updateHeight(); + updateHeight(); + } + + void RL() { + Node *rlcopy = right->left; + right->left = rlcopy->right; + rlcopy->right = right; + right = rlcopy; + right->right->updateHeight(); + right->updateHeight(); + updateHeight(); + } + + Node *LL() { + Node *lcopy = left; + left = left->right; + lcopy->right = this; + lcopy->left->updateHeight(); + lcopy->right->updateHeight(); + lcopy->updateHeight(); + return lcopy; + } + + Node *RR() { + Node *rcopy = right; + right = right->left; + rcopy->left = this; + rcopy->left->updateHeight(); + rcopy->right->updateHeight(); + rcopy->updateHeight(); + return rcopy; + } + + static int getBF(const Node *t) { + return getHeight(t->left) - getHeight(t->right); + } + + static int getHeight(const Node *t) { + return t == 0 ? 0 : t->height; + } + + void toGraphViz(std::ostream &stream) const { + stream << value << ";" << std::endl; + if (left != 0) { + stream << left->value << ";" << std::endl; + stream << value << "->" << left->value << ";" << std::endl; + left->toGraphViz(stream); + } + if (right != 0) { + stream << right->value << ";" << std::endl; + stream << value << "->" << right->value << ";" << std::endl; + right->toGraphViz(stream); + } + } + }; + + Node *tree; + unsigned numNodes; +}; + +} // namespace alg + +#endif // _ALG_AVL_HPP + diff --git a/include/binary_search_tree.h b/include/binary_search_tree.h index 21ff297a..5066c771 100644 --- a/include/binary_search_tree.h +++ b/include/binary_search_tree.h @@ -8,7 +8,7 @@ * BINARY SEARCH TREE * * Features: - * 1. Expected search time is O(nlogn). + * 1. Expected search time is O(log(n)), with worst case O(n). * 2. Data should be !!!SHUFFLED!!! first before tree creation. * 3. First initialize the value of the root (pointer to the * structure treeNode) with NULL. eg: diff --git a/src/avl_demo.cpp b/src/avl_demo.cpp new file mode 100644 index 00000000..9eb66173 --- /dev/null +++ b/src/avl_demo.cpp @@ -0,0 +1,59 @@ +#include +#include "avl.h" + +using namespace std; +using namespace alg; + +const unsigned N = 4096*32; +const unsigned N_ELEMS_TO_REMOVE = 4096*8; + +template +void printTreeStatus(const AVL &t) { + cout << "----------------------------------------" << endl; + if (t.isEmpty()) cout << "The tree is empty" << endl; + else { + cout << "Tree root is: " << t.root() << endl; + cout << "Tree height is: " << t.height() << endl; + cout << "Tree contains " << t.size() << " elements" << endl; + } + cout << "----------------------------------------" << endl; +} + +int main() +{ + int values[N]; + + AVL avl; + + cout << "Populating the tree with " << N << " random values... "; + for (unsigned i = 0; i < N; ++i) { + values[i] = rand(); + avl.insert(values[i]); + } + cout << "Done" << endl; + + printTreeStatus(avl); + + for (unsigned i = 0; i < N; ++i) { + unsigned idx = rand() % N; + if (!avl.contains(values[idx])) + cout << "ERROR: Value " << values[idx] << " was inserted and not found!" << endl; + } + + cout << "Now removing " << N_ELEMS_TO_REMOVE << " random elements for the tree... "; + for (unsigned i = 0; i < N_ELEMS_TO_REMOVE; ++i) { + unsigned idx = rand() % N; + avl.erase(values[idx]); + } + cout << "Done" << endl; + + printTreeStatus(avl); + + cout << "Do you want to see the GraphViz representation of the Tree (Y/n)? "; + char usrInput; + cin >> usrInput; + if (usrInput == 'Y' || usrInput == 'y') avl.toGraphViz(cout, "AVL"); + + return 0; +} + From 3171767d370f75b099550c7d10590987d6e5b074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Su=C3=A1rez?= Date: Wed, 10 Sep 2014 18:39:42 +0200 Subject: [PATCH 07/53] bug fixed, AVL trees now working properly --- include/avl.h | 40 ++++++++++++++++++++++++---------------- src/avl_demo.cpp | 21 ++++++++++++++------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/include/avl.h b/include/avl.h index 9dc6d621..e9ffe90c 100644 --- a/include/avl.h +++ b/include/avl.h @@ -51,8 +51,9 @@ class AVL { void erase(const T &x) { if (!isEmpty()) { - tree = tree->erase(x); - numNodes--; + bool found = false; + tree = tree->erase(x, found); + if (found) numNodes--; } } @@ -64,7 +65,7 @@ class AVL { } } - private: + public: struct Node { Node *left, *right; @@ -93,51 +94,58 @@ class AVL { return update(); } - Node *erase(const T &x) { + Node *erase(const T &x, bool &found) { if (value == x) { + found = true; if (left == 0 && right == 0) { delete this; return 0; } else if (left == 0) { + Node *aux = right; *this = *right; - delete right; + delete aux; } else if (right == 0) { + Node *aux = left; *this = *left; - delete left; + delete aux; } else { // Tracing path to rightmost leaf of the left subtree std::stack trace; Node *current = left; - while (current->right != 0) { + while (current != 0) { trace.push(current); current = current->right; } + current = trace.top(); value = current->value; Node *lsubtree = current->left; delete current; + trace.pop(); - if (trace.empty()) trace.push(left); - - trace.top()->right = lsubtree; - - do { - trace.top()->update(); + if (trace.empty()) { left = lsubtree; } + else { + trace.top()->right = lsubtree; trace.pop(); - } while (!trace.empty()); + while (!trace.empty()) { + current = trace.top(); + current->right = current->right->update(); + trace.pop(); + } + } } return update(); } else if (x < value) { if (left != 0) { - left = left->erase(x); + left = left->erase(x, found); return update(); } else return this; } else { if (right != 0) { - right = right->erase(x); + right = right->erase(x, found); return update(); } else return this; } diff --git a/src/avl_demo.cpp b/src/avl_demo.cpp index 9eb66173..444185f0 100644 --- a/src/avl_demo.cpp +++ b/src/avl_demo.cpp @@ -5,7 +5,7 @@ using namespace std; using namespace alg; const unsigned N = 4096*32; -const unsigned N_ELEMS_TO_REMOVE = 4096*8; +const unsigned N_ELEMS_TO_REMOVE = N-128; // Must be between 0 and N-1 template void printTreeStatus(const AVL &t) { @@ -40,20 +40,27 @@ int main() cout << "ERROR: Value " << values[idx] << " was inserted and not found!" << endl; } - cout << "Now removing " << N_ELEMS_TO_REMOVE << " random elements for the tree... "; + cout << "Now removing a random element from the tree... "; + unsigned idx = rand() % N; + avl.erase(values[idx]); + cout << "Done" << endl; + + printTreeStatus(avl); + + cout << "Now removing the root of the tree " << N_ELEMS_TO_REMOVE << " times... "; for (unsigned i = 0; i < N_ELEMS_TO_REMOVE; ++i) { - unsigned idx = rand() % N; - avl.erase(values[idx]); + avl.erase(avl.root()); } cout << "Done" << endl; printTreeStatus(avl); - cout << "Do you want to see the GraphViz representation of the Tree (Y/n)? "; + // Outputting to cerr so the output can be redirected with ./avl_demo 2> .gvz + cout << "Do you want to output the GraphViz representation of the tree to the cerr stream (Y/n)? "; char usrInput; cin >> usrInput; - if (usrInput == 'Y' || usrInput == 'y') avl.toGraphViz(cout, "AVL"); - + if (usrInput == 'Y' || usrInput == 'y') avl.toGraphViz(cerr, "AVL"); + return 0; } From 3ae4d87ca5d626174a3154896d44d57621c73291 Mon Sep 17 00:00:00 2001 From: fuli Date: Thu, 11 Sep 2014 14:09:41 +0800 Subject: [PATCH 08/53] update Makefile --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 73ca5f8a..9f25fa8c 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CC=gcc CPP=g++ AR=ar RANLIB=ranlib -CFLAGS= -g -Wall -Wno-unused-function -std=c++11 +CFLAGS= -g -Wall -Wno-unused-function SRCDIR = ./src INCLUDEDIR = -I./include -I. DEPS = @@ -69,7 +69,6 @@ PROGRAMS = m_based_demo \ selection_sort_demo \ 8queue_demo \ palindrome_demo \ - suffix_array_demo \ suffix_tree_demo \ avl_demo From 1312092d77a93f0571b4910fad06b21d33d1362d Mon Sep 17 00:00:00 2001 From: fuli Date: Thu, 11 Sep 2014 14:59:17 +0800 Subject: [PATCH 09/53] fix Makefile for compatibility of cxx11 --- Makefile | 218 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 202 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 9f25fa8c..04870219 100644 --- a/Makefile +++ b/Makefile @@ -4,25 +4,25 @@ CPP=g++ AR=ar RANLIB=ranlib CFLAGS= -g -Wall -Wno-unused-function +C11FLAGS= -g -Wall -Wno-unused-function -std=c++11 SRCDIR = ./src INCLUDEDIR = -I./include -I. DEPS = LIBS = -lm -PROGRAMS = m_based_demo \ + +PROGRAMS = m_based_demo \ integer_demo \ - insertion_sort_demo \ - radix_sort_demo \ - shuffle_demo \ - quick_sort_demo \ - merge_sort_demo \ - random_select_demo \ - hash_multi_demo \ - hash_table_demo \ - double_linked_list_demo \ - stack_demo \ - queue_demo \ - priority_queue_demo \ - prime_demo \ + insertion_sort_demo \ + radix_sort_demo \ + shuffle_demo \ + quick_sort_demo \ + merge_sort_demo \ + random_select_demo \ + hash_multi_demo \ + hash_table_demo \ + double_linked_list_demo \ + stack_demo \ + queue_demo \ universal_hash_demo \ perfect_hash_demo \ binary_search_tree_demo \ @@ -74,8 +74,194 @@ PROGRAMS = m_based_demo \ all: $(PROGRAMS) -%: $(SRCDIR)/%.cpp $(DEPS) - $(CPP) $(CFLAGS) -o $@ $< $(INCLUDEDIR) $(LIBS) +m_based_demo: $(SRCDIR)/m_based_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +integer_demo: $(SRCDIR)/integer_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +insertion_sort_demo: $(SRCDIR)/insertion_sort_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +radix_sort_demo: $(SRCDIR)/radix_sort_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +shuffle_demo: $(SRCDIR)/shuffle_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +quick_sort_demo: $(SRCDIR)/quick_sort_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +merge_sort_demo: $(SRCDIR)/merge_sort_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +random_select_demo: $(SRCDIR)/random_select_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +hash_multi_demo: $(SRCDIR)/hash_multi_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +hash_table_demo: $(SRCDIR)/hash_table_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +double_linked_list_demo: $(SRCDIR)/double_linked_list_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +stack_demo: $(SRCDIR)/stack_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +queue_demo: $(SRCDIR)/queue_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +priority_queue_demo: $(SRCDIR)/priority_queue_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +prime_demo: $(SRCDIR)/prime_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +universal_hash_demo: $(SRCDIR)/universal_hash_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +perfect_hash_demo: $(SRCDIR)/perfect_hash_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +binary_search_tree_demo: $(SRCDIR)/binary_search_tree_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +rbtree_demo: $(SRCDIR)/rbtree_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +heap_demo: $(SRCDIR)/heap_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +interval_tree_demo: $(SRCDIR)/interval_tree_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +dos_tree_demo: $(SRCDIR)/dos_tree_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +skiplist_demo: $(SRCDIR)/skiplist_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +lcs_demo: $(SRCDIR)/lcs_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +prim_mst_demo: $(SRCDIR)/prim_mst_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +directed_graph_demo: $(SRCDIR)/directed_graph_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +undirected_graph_demo: $(SRCDIR)/undirected_graph_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +dijkstra_demo: $(SRCDIR)/dijkstra_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +bellman_ford_demo: $(SRCDIR)/bellman_ford_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +graph_search_demo: $(SRCDIR)/graph_search_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +edmonds_karp_demo: $(SRCDIR)/edmonds_karp_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +astar_demo: $(SRCDIR)/astar_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +hash_string_demo: $(SRCDIR)/hash_string_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +bitset_demo: $(SRCDIR)/bitset_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +bloom_filter_demo: $(SRCDIR)/bloom_filter_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +sha1_demo: $(SRCDIR)/sha1_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +huffman_demo: $(SRCDIR)/huffman_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +word_seg_demo: $(SRCDIR)/word_seg_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +md5_demo: $(SRCDIR)/md5_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +trie_demo: $(SRCDIR)/trie_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +simhash_demo: $(SRCDIR)/simhash_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +imath_demo: $(SRCDIR)/imath_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +random_demo: $(SRCDIR)/random_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +k-means_demo: $(SRCDIR)/k-means_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +kmp_demo : $(SRCDIR)/kmp_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +heap_sort_demo: $(SRCDIR)/heap_sort_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +kruskal_mst_demo: $(SRCDIR)/kruskal_mst_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +LRU_cache_demo: $(SRCDIR)/LRU_cache_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +base64_demo: $(SRCDIR)/base64_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +max_subarray_demo: $(SRCDIR)/max_subarray_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +disjoint-set_demo: $(SRCDIR)/disjoint-set_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +relabel_to_front_demo: $(SRCDIR)/relabel_to_front_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +btree_demo: $(SRCDIR)/btree_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +sort_demo: $(SRCDIR)/sort_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +fib-heap_demo: $(SRCDIR)/fib-heap_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +scc_demo: $(SRCDIR)/scc_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +bubble_sort_demo: $(SRCDIR)/bubble_sort_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +selection_sort_demo: $(SRCDIR)/selection_sort_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +8queue_demo: $(SRCDIR)/8queue_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +palindrome_demo: $(SRCDIR)/palindrome_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +suffix_tree_demo: $(SRCDIR)/suffix_tree_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +avl_demo: $(SRCDIR)/avl_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + +suffix_array_demo: $(SRCDIR)/suffix_array_demo.cpp + $(CPP) $(C11FLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) clean: rm -rf $(PROGRAMS) *.dSYM From 2179038643760950a1c75b3dfbb33f51845f765b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Su=C3=A1rez?= Date: Thu, 11 Sep 2014 14:36:42 +0200 Subject: [PATCH 10/53] Author information added to AVL headerfile --- include/avl.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/avl.h b/include/avl.h index e9ffe90c..b339739c 100644 --- a/include/avl.h +++ b/include/avl.h @@ -13,6 +13,9 @@ * 3. Exportable to GraphViz format for easy visualization and verification * * http://en.wikipedia.org/wiki/AVL_tree + * + * @author Alejandro Fernandez (alejandro.fernandez.suarez@gmail.com) + * @github afernandez90 * ******************************************************************************/ From d81d1a693c6fa1d5fb1ce903b05faf9d7267158d Mon Sep 17 00:00:00 2001 From: Samana Date: Wed, 26 Nov 2014 22:33:21 -0500 Subject: [PATCH 11/53] + Added .Net style Dictionary ( cache friendly hash table ) + Minor VC2013 compliance fixes --- include/avl.h | 2 + include/dictionary.h | 562 ++++++++++++++++++++++++++++++++++++++++ include/hash_multi.h | 4 +- include/prime.h | 2 +- include/suffix_array.h | 3 +- include/suffix_tree.h | 2 + msvc/alg_vs.h | 4 +- src/dictionary_demo.cpp | 49 ++++ 8 files changed, 622 insertions(+), 6 deletions(-) create mode 100644 include/dictionary.h create mode 100644 src/dictionary_demo.cpp diff --git a/include/avl.h b/include/avl.h index b339739c..4b8f00cd 100644 --- a/include/avl.h +++ b/include/avl.h @@ -25,6 +25,8 @@ #include #include #include +#include +#include namespace alg { diff --git a/include/dictionary.h b/include/dictionary.h new file mode 100644 index 00000000..4c38a9cf --- /dev/null +++ b/include/dictionary.h @@ -0,0 +1,562 @@ +/******************************************************************************* +* DANIEL'S ALGORITHM IMPLEMENTAIONS +* +* /\ | _ _ ._ o _|_ |_ ._ _ _ +* /--\ | (_| (_) | | |_ | | | | | _> +* _| +* +* .Net Dictionary Implementation (Cache friendly hash table) +* +******************************************************************************/ + +#pragma once + +#include +#include +#include +#include "hash_code.h" +#include "prime.h" + +namespace alg +{ + +template< typename TKey, typename TValue, typename THash = hash_code > +class Dictionary +{ +public: + struct KeyValuePair + { + TKey Key; + TValue Value; + }; + +private: + struct Entry : public KeyValuePair + { + int32_t HashCode; + int32_t Next; + + Entry() + : HashCode(-1) + , Next(-1) + { + } + + void Reset() + { + HashCode = -1; + Next = -1; + Key = TKey(); + Value = TValue(); + } + }; + +private: + std::vector m_Buckets; + std::vector m_Entries; + int32_t m_Count; + int32_t m_FreeList; + int32_t m_FreeCount; + + friend class Iterator; +public: + + template + class IteratorBase + { + protected: + DictType* Dict; + int32_t Index; + EntryType* Current; + + friend class Dictionary; + + IteratorBase(DictType* dict) + : Dict(dict) + , Index(0) + , Current(nullptr) + { + } + + public: + TIter& operator++() + { + while ((uint32_t) Index < (uint32_t) Dict->m_Count) + { + if (Dict->m_Entries[Index].HashCode >= 0) + { + Current = &Dict->m_Entries[Index]; + Index++; + return *static_cast(this); + } + Index++; + } + + Index = Dict->m_Count + 1; + Current = nullptr; + return *static_cast(this); + } + + TIter operator++(int32_t) + { + TIter tmp = *static_cast(this); + ++(*this); + return tmp; + } + + bool operator == (const TIter& other) const + { + return Dict == other.Dict + && Index == other.Index + && Current == other.Current; + } + + bool operator != (const TIter& other) const + { + return !(*this == other); + } + }; + + class Iterator : public IteratorBase + { + friend class Dictionary; + private: + Iterator(Dictionary* dict) + : IteratorBase(dict) + { + } + public: + KeyValuePair& operator*() const + { + return *Current; + } + }; + + class ConstIterator : public IteratorBase + { + friend class Dictionary; + private: + ConstIterator(const Dictionary* dict) + : IteratorBase(dict) + { + } + public: + const KeyValuePair& operator*() const + { + return *Current; + } + }; + +public: + typedef Iterator iterator; + typedef ConstIterator const_iterator; + +public: + Dictionary(int32_t capacity = 0) + : m_Count(0) + , m_FreeList(-1) + , m_FreeCount(0) + { + _Init(capacity); + } + + ~Dictionary() + { + Clear(); + } + + int32_t Size() const + { + return m_Count - m_FreeCount; + } + + TValue& operator[](const TKey& key) + { + int32_t i = _FindEntry(key); + if (i >= 0) + { + return m_Entries[i].Value; + } + throw MxKeyNotFoundException(); + } + const TValue& operator[](const TKey& key) const + { + int32_t i = _FindEntry(key); + if (i >= 0) + { + return m_Entries[i].Value; + } + throw MxKeyNotFoundException(); + } + + bool TryGetValue(const TKey& key, TValue& outValue) const + { + int32_t i = _FindEntry(key); + if (i >= 0) + { + outValue = m_Entries[i].Value; + return true; + } + else + { + return false; + } + } + + TValue TryGetValueOrDefault(const TKey& key, const TValue& defaultValue) const + { + int32_t i = _FindEntry(key); + if (i >= 0) + { + return m_Entries[i].Value; + } + else + { + return defaultValue; + } + } + + const TValue& TryGetValueRefOrDefault(const TKey& key, const TValue& defaultValue) const + { + int32_t i = _FindEntry(key); + if (i >= 0) + { + return m_Entries[i].Value; + } + else + { + return defaultValue; + } + } + + TValue* TryGetValuePtr(const TKey& key) + { + int32_t i = _FindEntry(key); + if (i >= 0) + { + return &m_Entries[i].Value; + } + else + { + return nullptr; + } + } + + const TValue* TryGetValuePtr(const TKey& key) const + { + int32_t i = _FindEntry(key); + if (i >= 0) + { + return &m_Entries[i].Value; + } + else + { + return nullptr; + } + } + + void AddOrUpdate(const TKey& key, const TValue& value) + { + _Insert(key, value, false); + } + + bool ContainsKey(const TKey& key) const + { + int32_t i = _FindEntry(key); + return i >= 0; + } + + bool Contains(const std::pair& pair) const + { + int32_t i = _FindEntry(pair.first); + return i >= 0 && pair.second == m_Entries[i].Value; + } + + bool Add(const TKey& key, const TValue& value) + { + int32_t i = _FindEntry(key); + if (i >= 0) + { + return false; + } + + return _Insert(key, value, true); + } + + bool Add(const TKey& key, TValue&& value) + { + int32_t i = _FindEntry(key); + if (i >= 0) + { + return false; + } + + return _Insert(key, value, true); + } + + bool Remove(const TKey& key) + { + int32_t hashCode = THash()(key) & 0x7FFFFFFF; + int32_t bucket = hashCode % m_Buckets.size(); + int32_t last = -1; + for (int i = m_Buckets[bucket]; i >= 0; last = i, i = m_Entries[i].Next) + { + if (m_Entries[i].HashCode == hashCode && m_Entries[i].Key == key) + { + if (last < 0) + { + m_Buckets[bucket] = m_Entries[i].Next; + } + else + { + m_Entries[last].Next = m_Entries[i].Next; + } + m_Entries[i].HashCode = -1; + m_Entries[i].Next = m_FreeList; + m_Entries[i].Key = TKey(); + m_Entries[i].Value = TValue(); + m_FreeList = i; + m_FreeCount++; + + return true; + } + } + return false; + } + + void Clear() + { + if (m_Count > 0) + { + memset(m_Buckets.data(), -1, m_Buckets.size() * sizeof(m_Buckets[0])); + for (auto& entry : m_Entries) + { + entry.Reset(); + } + m_FreeList = -1; + m_FreeCount = 0; + m_Count = 0; + } + } + + Iterator Begin() + { + return ++Iterator(this); + } + + ConstIterator Begin() const + { + return CBegin(); + } + + ConstIterator CBegin() const + { + return ++ConstIterator(this); + } + + Iterator End() + { + Iterator ret(this); + ret.Index = m_Count + 1; + ret.Current = nullptr; + return ret; + } + + ConstIterator End() const + { + return CEnd(); + } + + ConstIterator CEnd() const + { + ConstIterator ret(this); + ret.Index = m_Count + 1; + ret.Current = nullptr; + return ret; + } + + //STL style + iterator begin() + { + return Begin(); + } + + const_iterator begin() const + { + return CBegin(); + } + + const_iterator cbegin() const + { + return CBegin(); + } + + iterator end() + { + return End(); + } + + const_iterator end() const + { + return CEnd(); + } + + const_iterator cend() const + { + return CEnd(); + } +private: + int32_t _FindEntry(const TKey& key) const + { + if (m_Buckets.size() > 0) + { + int32_t hashCode = THash()(key) & 0x7FFFFFFF; + for (int32_t i = m_Buckets[hashCode % m_Buckets.size()]; i >= 0; i = m_Entries[i].Next) + { + if (m_Entries[i].HashCode == hashCode && m_Entries[i].Key == key) + { + return i; + } + } + } + return -1; + } + + void _Init(int32_t capacity) + { + int32_t size = GetNextPrime(capacity); + m_Buckets.clear(); + m_Buckets.resize(size, -1); + m_Entries.clear(); + m_Entries.resize(size); + m_FreeList = -1; + } + + template + bool _Insert(const TKey& key, TValueRef value, bool add) + { + if (m_Buckets.size() == 0) + { + _Init(3); + } + + int32_t hashCode = THash()(key) & 0x7FFFFFFF; + int32_t targetBucket = hashCode % m_Buckets.size(); + + for (int32_t i = m_Buckets[targetBucket]; i >= 0; i = m_Entries[i].Next) + { + if (m_Entries[i].HashCode == hashCode && m_Entries[i].Key == key) + { + if (add) + { + return false; + } + m_Entries[i].Value = value; + return true; + } + } + + int32_t index; + if (m_FreeCount > 0) + { + index = m_FreeList; + m_FreeList = m_Entries[index].Next; + m_FreeCount--; + } + else + { + if (m_Count == m_Entries.size()) + { + _Resize(); + targetBucket = hashCode % m_Buckets.size(); + } + index = m_Count; + m_Count++; + } + + m_Entries[index].HashCode = hashCode; + m_Entries[index].Next = m_Buckets[targetBucket]; + m_Entries[index].Key = key; + m_Entries[index].Value = value; + + m_Buckets[targetBucket] = index; + + return true; + } + + void _Resize() + { + _Resize(GetNextPrime(m_Count * 2), false); + } + + void _Resize(int32_t newSize, bool forceNewHashCodes) + { + assert(newSize >= m_Entries.size()); + + m_Buckets.resize(0); + m_Buckets.resize(newSize, -1); + m_Entries.resize(newSize); + + if (forceNewHashCodes) + { + for (int32_t i = 0; i < m_Count; i++) + { + if (m_Entries[i].HashCode != -1) + { + m_Entries[i].HashCode = (THash()(m_Entries[i].Key) & 0x7FFFFFFF); + } + } + } + for (int32_t i = 0; i < m_Count; i++) + { + if (m_Entries[i].HashCode >= 0) + { + int32_t bucket = m_Entries[i].HashCode % newSize; + m_Entries[i].Next = m_Buckets[bucket]; + m_Buckets[bucket] = i; + } + } + } + + + static int GetNextPrime(int n) + { + static const int c_PrimeArraySize = 72; + static const int c_Primes[c_PrimeArraySize] = + { + 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919, + 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591, + 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437, + 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, + 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369 + }; + static const int c_HashPrime = 101; + + if (n < 0) + { + return -1; + } + + for (int i = 0; i < c_PrimeArraySize; i++) + { + int prime = c_Primes[i]; + if (prime >= n) + { + return prime; + } + } + + //outside of our predefined table. + //compute the hard way. + for (int i = (n | 1); i < INT32_MAX; i += 2) + { + if (is_prime(i) && ((i - 1) % c_HashPrime != 0)) + { + return i; + } + } + return n; + } +}; + +} \ No newline at end of file diff --git a/include/hash_multi.h b/include/hash_multi.h index bc75cb37..4639e229 100644 --- a/include/hash_multi.h +++ b/include/hash_multi.h @@ -42,7 +42,7 @@ namespace alg { } #ifdef _MSC_VER -#define log2(x) (log(x) / log(2)) +#define log2(x) (log(x) / log(2.0)) #endif /** @@ -50,7 +50,7 @@ namespace alg { */ static MultiHash * multi_hash_init(uint32_t size) { // find prime larger than log2(size) - uint32_t r = ceil(log2(size)); + uint32_t r = ceil(log2((double)size)); int i; for (i = r; ;i++) { if (is_prime(i)) { diff --git a/include/prime.h b/include/prime.h index fd7b73ef..81f772e4 100644 --- a/include/prime.h +++ b/include/prime.h @@ -34,7 +34,7 @@ namespace alg { if (n%2 == 0) return false; - unsigned sqrtn = sqrt(n); + unsigned sqrtn = sqrt((double)n); for (unsigned int i = 3; i <= sqrtn; i+=2) { if (n % i == 0) { return false; diff --git a/include/suffix_array.h b/include/suffix_array.h index 92fd67e9..c078bd0a 100644 --- a/include/suffix_array.h +++ b/include/suffix_array.h @@ -27,6 +27,7 @@ #include #include #include +#include using namespace std; @@ -69,7 +70,7 @@ namespace alg { for(size_t k=0;k>1) + +using namespace alg; +using namespace std::chrono; + +int main(void) { + + Dictionary dict; + + dict.Add(0, 1); + dict.Add(1, 2); + dict.Add(5, 2); + dict.Add(3, 3); + dict.Remove(5); + dict.AddOrUpdate(3, 4); + + for (auto x : dict) + { + printf("%d - %d\n", x.Key, x.Value); + } + + static const uint32_t TEST_LENGTH = 1000000; + Dictionary d(TEST_LENGTH); + HashTable h(TEST_LENGTH); + + auto t0 = high_resolution_clock::now(); + + for (uint32_t i = 0; i < TEST_LENGTH; i++) + { + d.AddOrUpdate(alg::LCG(), alg::LCG()); + } + + auto t1 = high_resolution_clock::now(); + + for (uint32_t i = 0; i < TEST_LENGTH; i++) + { + h[alg::LCG()] = alg::LCG(); + } + + auto t2 = high_resolution_clock::now(); + + auto dt0 = duration_cast(t1 - t0).count(); + auto dt1 = duration_cast(t2 - t1).count(); + + printf("Dictionary: %lld ms, HashTable: %lld ms\n", dt0, dt1); +} From bc16e44e9453e41f2bc67443f1b918ba8b3d688b Mon Sep 17 00:00:00 2001 From: fuli Date: Wed, 21 Jan 2015 10:27:36 +0800 Subject: [PATCH 12/53] fix a bug in RANDOM macro --- include/generic.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/generic.h b/include/generic.h index c55487c6..8671f740 100644 --- a/include/generic.h +++ b/include/generic.h @@ -24,7 +24,7 @@ #define Min(a, b) ( (a < b) ? a : b ) #define RANDOM_INIT() srand(time(NULL)) -#define RANDOM(L, R) (L + rand() % ((R) - (L))) // gen a random integer in [L, R] +#define RANDOM(L, R) (L + rand() % ((R) - (L) + 1)) // gen a random integer in [L, R] namespace alg { /** From b6e62b4e3567fa50814cf37a854307263e23c0da Mon Sep 17 00:00:00 2001 From: orthographic-pedant Date: Wed, 30 Sep 2015 18:51:52 -0400 Subject: [PATCH 13/53] Fixed typographical error, changed arbitary to arbitrary in README. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9f2ddb6f..f347d414 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Prime test(trial division) Prime test(Miller-Rabin's method) 2D Array - Arbitary Integer + Arbitrary Integer Linear congruential generator Maximum subarray problem From a5c52404a3f716b9582faa5c4265b804f1a3b40b Mon Sep 17 00:00:00 2001 From: xtaci Date: Thu, 22 Oct 2015 15:52:26 +0800 Subject: [PATCH 14/53] fix a bug in dijkstra --- include/dijkstra.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dijkstra.h b/include/dijkstra.h index c7b9c9c3..f1831c18 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -76,7 +76,7 @@ namespace alg { list_for_each_entry(v, &u->v_head, v_node){ uint32_t alt = dist_u + v->weight; uint32_t dist_v = dist[v->id]; - if (alt < dist_v && !visited[v->id]) { + if (alt < dist_v) { /* uint32_t tmp = dist[v->id]; if (tmp != INT_MAX) { From 8caabf1bcb10cb89ad7a1df80af29f4abf2edd2a Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 23 Oct 2015 14:55:03 +0800 Subject: [PATCH 15/53] a better implementation of heap inspired by golang/heap --- Makefile | 5 -- README.md | 1 - include/astar.h | 10 +-- include/dijkstra.h | 31 +++---- include/heap.h | 190 +++++++++++++++++++---------------------- include/heap_sort.h | 54 ------------ include/kruskal_mst.h | 2 +- include/prim_mst.h | 20 ++--- include/scc.h | 8 +- src/heap_demo.cpp | 21 +++-- src/heap_sort_demo.cpp | 34 -------- 11 files changed, 133 insertions(+), 243 deletions(-) delete mode 100644 include/heap_sort.h delete mode 100644 src/heap_sort_demo.cpp diff --git a/Makefile b/Makefile index 04870219..e9ba280c 100644 --- a/Makefile +++ b/Makefile @@ -53,8 +53,6 @@ PROGRAMS = m_based_demo \ random_demo \ k-means_demo \ kmp_demo \ - heap_sort_demo \ - kruskal_mst_demo \ LRU_cache_demo \ base64_demo \ max_subarray_demo \ @@ -209,9 +207,6 @@ k-means_demo: $(SRCDIR)/k-means_demo.cpp kmp_demo : $(SRCDIR)/kmp_demo.cpp $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) -heap_sort_demo: $(SRCDIR)/heap_sort_demo.cpp - $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) - kruskal_mst_demo: $(SRCDIR)/kruskal_mst_demo.cpp $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) diff --git a/README.md b/README.md index 9f2ddb6f..1a431a9f 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ Radix sort Quick sort Merge sort - Heap sort Double linked list Skip list Self-organized linked-list ops (move-to-front, move-ahead-one) diff --git a/include/astar.h b/include/astar.h index 78fa276f..03474b34 100644 --- a/include/astar.h +++ b/include/astar.h @@ -93,7 +93,7 @@ namespace alg { // initialy containing the start node // encoding [x,y] to [x*ncol + y] // using binary heap ... - m_openset.insert(0, x1*ncol+y1); + m_openset.push(0, x1*ncol+y1); // record the starting point in openset_grid m_openset_grid(x1,y1) = true; @@ -109,7 +109,8 @@ namespace alg { // the main A*algorithm while(!m_openset.is_empty()) { - uint32_t value = m_openset.min_value(); + Heap::elem e = m_openset.pop(); + uint32_t value = e.data; int cx = value/ncol; int cy = value%ncol; @@ -136,8 +137,7 @@ namespace alg { return as; } - // delete current positon from openset and move it into closed set. - m_openset.delete_min(); + // move it into closed set. m_closedset(cx, cy) = true; m_openset_grid(cx, cy) = false; @@ -168,7 +168,7 @@ namespace alg { g_score(nx,ny) = tentative; // update path cost for current position f_score(nx,ny) = tentative + estimate(nx,ny,x2,y2); // record path cost to this neighbour if (!m_openset_grid(nx,ny)) { // only insert the neighbour if it hasn't been add to the openset. - m_openset.insert(f_score(nx,ny), nx*ncol+ny); + m_openset.push(f_score(nx,ny), nx*ncol+ny); m_openset_grid(nx,ny) = true; } } diff --git a/include/dijkstra.h b/include/dijkstra.h index f1831c18..d0bebe7d 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -37,6 +37,7 @@ namespace alg { class Dijkstra { public: static const int UNDEFINED = -1; + static const int LARGE_NUMBER = 999999; // run dijkstra algorithm, and return the previous table static HashTable * run(const Graph & g, uint32_t src_id) { // a binary heap @@ -51,43 +52,35 @@ namespace alg { // all vertices Graph::Adjacent * a; list_for_each_entry(a, &g.list(), a_node){ - dist[a->v.id] = INT_MAX; // set inital distance to each vertex as INT_MAX + dist[a->v.id] = LARGE_NUMBER; // set inital distance to each vertex to a large number (*previous)[a->v.id] = UNDEFINED; // clear path to UNDEFINED visited[a->v.id] = false; // all vertices are not visited + Q.push(LARGE_NUMBER, a->v.id); // push all vertices to heap } // source vertex, the first vertex in Heap-Q - Q.insert(0, src_id); dist[src_id] = 0; + // decrease-key the source vertex to 0 + Q.decrease_key(src_id,0); while(!Q.is_empty()) { // for every un-visited vertex, try relaxing the path - int32_t id = Q.min_value(); - Q.delete_min(); // remove u from Q - if (visited[id]) { // jump visited vertex, it means a closer vertex has found - // printf("visted:%d %d\n", id, dist[id]); + Heap::elem e = Q.pop(); + uint32_t id = e.data; + if (visited[id]) { // ignore visited vertex continue; } Graph::Adjacent * u = g[id]; // the vertex to process int dist_u = dist[id]; // current known shortest distance to u - visited[id] = true; // mark the vertex as visited. + visited[id] = true; // mark the vertex as visited. Graph::Vertex * v; list_for_each_entry(v, &u->v_head, v_node){ uint32_t alt = dist_u + v->weight; - uint32_t dist_v = dist[v->id]; - if (alt < dist_v) { - /* - uint32_t tmp = dist[v->id]; - if (tmp != INT_MAX) { - printf("old %d %d\n", v->id, tmp); - printf("new %d %d\n", v->id, dist[v->id]); - } - */ - + if (alt < dist[v->id]) { dist[v->id] = alt; - (*previous)[v->id] = u->v.id; - Q.insert(alt, v->id); + (*previous)[v->id] = id; + Q.decrease_key(v->id, alt); // decrease-key } } } diff --git a/include/heap.h b/include/heap.h index 3462660a..959d6a3e 100644 --- a/include/heap.h +++ b/include/heap.h @@ -8,8 +8,8 @@ * Heap Data structure * * Heaps can be used as an array. For any key at array position I, - I left child is at ( 2i ), right child is at ( 2i+1 ) and parent is - I at (int) (i / 2). Heap size is stored at index 0. + * left child is at ( 2i ), right child is at ( 2i+1 ) and parent is + * at (int) (i / 2). Heap size is stored at index 0. * * Basic operations of a heap are: * @@ -27,8 +27,7 @@ #include #include #include -#include "hash_code.h" -#include "hash_table.h" +#include "generic.h" namespace alg { /** @@ -36,33 +35,29 @@ namespace alg { */ template class Heap { - private: + public: /** * define key-value pair of heap struct. */ - struct KV { + struct elem { public: - int32_t key; - T value; + int key; + T data; }; - int32_t m_size; // current heap size. - int32_t m_max; // max heap size. - KV * m_kvs; // key value pairs. - - HashTable * m_idx; // key -> idx + private: + int m_size; // current heap size. + int m_max; // max heap size. + elem * m_heap; // key value pairs. public: Heap(int max) { m_size = 0; m_max = max+1; - m_kvs = new KV[m_max]; - m_kvs[0].key = INT_MIN; - m_idx = new HashTable(m_max); + m_heap = new elem[m_max]; }; ~Heap() { - delete [] m_kvs; - delete m_idx; + delete [] m_heap; }; private: @@ -70,37 +65,20 @@ namespace alg { Heap& operator=(const Heap&); public: - - inline int min_key() const { return m_kvs[1].key; }; - inline const T & min_value() const { return m_kvs[1].value; }; - // for loop through the kvs - inline uint32_t count() const { return m_size; }; - inline const T & operator[] (uint32_t idx) const { return m_kvs[idx+1].value; }; + inline int count() const { return m_size; }; /** * insert a 'key'->'value' pair into the heap. */ - void insert(int key, const T & value) { + void push(int key, const T & data) { // heap full, just return; if(m_size == m_max) return; - + // put in the back, and try move upward the heap + m_heap[m_size].key = key; + m_heap[m_size].data= data; + up(m_size); m_size++; - m_kvs[m_size].key = key; - m_kvs[m_size].value = value; - (*m_idx)[value] = m_size; - - // Adjust its position - int now = m_size; - while(m_kvs[now/2].key > key) { - m_kvs[now] = m_kvs[now/2]; - (*m_idx)[m_kvs[now/2].value] = now; - now /= 2; - } - - m_kvs[now].key = key; - m_kvs[now].value = value; - (*m_idx)[value] = now; } /** @@ -113,84 +91,94 @@ namespace alg { */ inline void clear() { m_size = 0; } + bool contains(const T & data) { + for(int i=1;i<=m_size;i++) { + if(m_heap[i].data== data) return true; + } + return false; + } + /** - * contains test + * pop the min element */ - bool contains(const T & value) { - for(int32_t i=1;i<=m_size;i++) { - if(m_kvs[i].value == value) return true; - } + elem pop() { + int n = m_size-1; + swap(m_heap[0],m_heap[n]); + down(0, n); + m_size--; + return m_heap[m_size]; + } + /** + * remove the given data + */ + bool remove(T data) { + for (int i=0;i heap top. + * decrease key + * simpliy implemented as remove then push */ - void delete_min() { - // heap[1] is the minimum key. So we remove heap[1]. - // Size of the heap is decreased. Now heap[1] has to be filled. - // We put the last key in its place and see if it fits. If it - // does not fit, take minimum key among both its children and - // replaces parent with it. Again See if the last key fits - //in that place. - int32_t lastKey; - T lastValue; - int32_t child,now; - - // empty heap, just return - if (m_size == 0) return; - - lastKey = m_kvs[m_size].key; - lastValue = m_kvs[m_size].value; - m_size--; + void decrease_key(T data, int newkey) { + if (remove(data)) { + push(newkey, data); + } + } - // now refers to the index at which we are now - for(now = 1; now*2 <= m_size ;now = child) { - // child is the index of the key which is minimum among - // both the children, Indexes of children are i*2 and i*2 + 1 - child = now*2; - // child!=heapSize beacuse heap[heapSize+1] does not exist, - // which means it has only one child - if(child != m_size && m_kvs[child+1].key < m_kvs[child].key) { - child++; // choose the minium one. + void up(int j) { + for (;;) { + int i = (j-1)/2; // parent + if (i==j || !less(j,i)) { // j not smaller than i + break; } - // To check if the last key fits or not it suffices to check - // if the last key is less than the minimum key among both the children - if(lastKey > m_kvs[child].key) { - m_kvs[now] = m_kvs[child]; - (*m_idx)[m_kvs[now].value] = now; // record index + swap(m_heap[i], m_heap[j]); + j=i; + } + } + + void down(int i, int n) { + for(;;) { + int j1 = 2*i+1; // left child + if (j1 >=n || j1 < 0) { // j1 < 0 after int overflow + break; + } + + int j = j1; + int j2 = j1+1; // left child + if (j2 < n && !less(j1,j2)) { + j = j2; // choose the minium one. } - else { // It fits there + + if (!less(j,i)) { break; } + swap(m_heap[i], m_heap[j]); + i=j; } - - m_kvs[now].key = lastKey; - m_kvs[now].value= lastValue; - (*m_idx)[lastValue] = now; // record index } - /** - * so called DECREASE KEY operation. - * step 1. find the value - * step 2. decrease the key to the newkey - */ - void decrease_key(T value, int32_t newkey) { - int32_t index = (*m_idx)[value]; - if (index > m_size || index == 0) return; // value not found - if (newkey >= m_kvs[index].key) return; // violate DECREASE meanning. - T oldvalue = m_kvs[index].value; - - int now = index; - while(m_kvs[now/2].key > newkey) { - m_kvs[now] = m_kvs[now/2]; - (*m_idx)[m_kvs[now].value] = now; // record index - now /= 2; + void print_heap() { + for (int i=0;i - * _| - * - * HEAPSORT - * - * Features: - * 1. Although somewhat slower in practice on most machines than a well-implemented quicksort, - it has the advantage of a more favorable worst-case O(n log n) runtime - * - * http://en.wikipedia.org/wiki/Heapsort - * - ******************************************************************************/ - -#ifndef __HEAPSORT_H__ -#define __HEAPSORT_H__ - -#include - -namespace alg { - /** - * heap sort an array - */ - template - static void heapsort(T *array,int number_of_elements) { - Heap heap(number_of_elements); - int i; - - // In order to build a heap structure from input array - for(i=0;iheap.insert(v->weight, v); // weight->vertex + pa->heap.push(v->weight, v); // weight->vertex } } diff --git a/include/prim_mst.h b/include/prim_mst.h index b3bf5069..8c4892fa 100644 --- a/include/prim_mst.h +++ b/include/prim_mst.h @@ -30,10 +30,12 @@ #include "undirected_graph.h" #include "double_linked_list.h" #include "heap.h" +#include "hash_table.h" namespace alg { class Prim { public: + static const int LARGE_NUMBER = 999999; /** * Prim's Algorithm. * @@ -62,24 +64,20 @@ namespace alg { // all vertices Graph::Adjacent * a; list_for_each_entry(a, &g.list(), a_node){ - if (a->v.id != src_id) { - Q.insert(INT_MAX, a->v.id); - keys[a->v.id] = INT_MAX; - } + Q.push(LARGE_NUMBER, a->v.id); + keys[a->v.id] = LARGE_NUMBER; } - - Q.insert(0, src_id); + + Q.decrease_key(src_id, 0); keys[src_id] = 0; while (!Q.is_empty()) { - int32_t id = Q.min_value(); - Q.delete_min(); // remove u from Q - - Graph::Adjacent * u = g[id]; // the vertex to process + Heap::elem e = Q.pop(); + Graph::Adjacent * u = g[e.data]; // the vertex to process Graph::Vertex * v; list_for_each_entry(v, &u->v_head, v_node) { if (Q.contains(v->id) && v->weight < keys[v->id]) { - pi[v->id] = id; + pi[v->id] = e.data; Q.decrease_key(v->id, v->weight); keys[v->id] = v->weight; } diff --git a/include/scc.h b/include/scc.h index 23e34e0e..1d641a1b 100644 --- a/include/scc.h +++ b/include/scc.h @@ -39,7 +39,7 @@ namespace alg { Heap Q(g.vertex_count()) ; Graph::Adjacent * a; list_for_each_entry(a, &g.list(), a_node) { - Q.insert(INT_MAX - a->f, a->v.id); // descending order of a->f + Q.push(INT_MAX - a->f, a->v.id); // descending order of a->f } // step 2. discover @@ -51,9 +51,9 @@ namespace alg { // step 3. call DFS(GT), but in the main loop of DFS, consider the vertices // in order of decreasing u.f (as computed in line 1) while(!Q.is_empty()) { - int32_t key = Q.min_key(); - int32_t id = Q.min_value(); - Q.delete_min(); + Heap::elem e = Q.pop(); + int32_t key = e.key; + int32_t id = e.data; if ((*GT)[id]->color == Graph::WHITE) { printf("component:%d %d\n",id, INT_MAX - key); _DFS_VISIT(*GT, (*GT)[id]); diff --git a/src/heap_demo.cpp b/src/heap_demo.cpp index bf761a66..ccae4aea 100644 --- a/src/heap_demo.cpp +++ b/src/heap_demo.cpp @@ -9,20 +9,25 @@ int main() int MAXELEMENTS=10; Heap heap(MAXELEMENTS); - int32_t i; + int i; srand(time(NULL)); for (i=0;i < MAXELEMENTS; i++) { - int32_t value = i; - heap.insert(i, value); - printf("inserting: %d->%d\n", i, value); + heap.push(100-i, i); + printf("push: key:%d->value:%d\n", 100-i, i); } + heap.print_heap(); + + for (i=0;i%d\n", heap.min_key(), heap.min_value()); - heap.delete_min(); + Heap::elem e = heap.pop(); + printf("pop: key:%d->value:%d\n", e.key, e.data); } + heap.print_heap(); return 0; } diff --git a/src/heap_sort_demo.cpp b/src/heap_sort_demo.cpp deleted file mode 100644 index 7a8423c4..00000000 --- a/src/heap_sort_demo.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include - -#include "generic.h" -#include "heap_sort.h" - -using namespace alg; - -int main() -{ - const int MAX_ELEMENTS = 10; - int list[MAX_ELEMENTS]; - - int i = 0; - - srand(time(NULL)); - // generate random numbers and fill them to the list - for(i = 0; i < MAX_ELEMENTS; i++ ){ - list[i] = rand()%100; - } - printf("The list before sorting is:\n"); - printlist(list,MAX_ELEMENTS); - - // sort the list using heap sort - heapsort(&list[0],MAX_ELEMENTS); - - // print the result - printf("The list after sorting using heapsort algorithm:\n"); - printlist(list,MAX_ELEMENTS); - return 0; -} - - From 2fb4c5ab2755c6814e180a23e8b6378f1bab12e8 Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 23 Oct 2015 15:13:08 +0800 Subject: [PATCH 16/53] clean --- include/prim_mst.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/prim_mst.h b/include/prim_mst.h index 8c4892fa..3949e895 100644 --- a/include/prim_mst.h +++ b/include/prim_mst.h @@ -73,11 +73,12 @@ namespace alg { while (!Q.is_empty()) { Heap::elem e = Q.pop(); - Graph::Adjacent * u = g[e.data]; // the vertex to process + uint32_t id = e.data; + Graph::Adjacent * u = g[id]; // the vertex to process Graph::Vertex * v; list_for_each_entry(v, &u->v_head, v_node) { if (Q.contains(v->id) && v->weight < keys[v->id]) { - pi[v->id] = e.data; + pi[v->id] = id; Q.decrease_key(v->id, v->weight); keys[v->id] = v->weight; } From b5756134050f5aeab5b430674cc27ad1984ae88c Mon Sep 17 00:00:00 2001 From: Daniel Fu Date: Fri, 23 Oct 2015 17:05:22 +0800 Subject: [PATCH 17/53] Update heap.h --- include/heap.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/include/heap.h b/include/heap.h index 959d6a3e..c82f4d06 100644 --- a/include/heap.h +++ b/include/heap.h @@ -7,14 +7,23 @@ * * Heap Data structure * - * Heaps can be used as an array. For any key at array position I, - * left child is at ( 2i ), right child is at ( 2i+1 ) and parent is - * at (int) (i / 2). Heap size is stored at index 0. + * In computer science, a heap is a specialized tree-based data structure that + * satisfies the heap property: If A is a parent node of B then the key of node + * A is ordered with respect to the key of node B with the same ordering applying + * across the heap. Heaps can be classified further as either a "max heap" or + * a "min heap". In a max heap, the keys of parent nodes are always greater + * than or equal to those of the children and the highest key is in the root node. + * In a min heap, the keys of parent nodes are less than or equal to those of + * the children and the lowest key is in the root node. Heaps are crucial in + * several efficient graph algorithms such as Dijkstra's algorithm, and in + * the sorting algorithm heapsort. A common implementation of a heap is the + * binary heap, in which the tree is a complete binary tree (see figure). * * Basic operations of a heap are: * - * 1. Insert – Insert an key. - * 2. Delete minimum – Delete and return the smallest item in the heap. + * 1. Push – Insert an key. + * 2. Pop – Delete and return the smallest item in the heap. + * 3. Remove - Remove an element * * http://en.wikipedia.org/wiki/Binary_heap ******************************************************************************/ From e95583a3d8adccfddc6b5737dc6389014a796105 Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 23 Oct 2015 17:06:53 +0800 Subject: [PATCH 18/53] fix ident --- include/heap.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/heap.h b/include/heap.h index c82f4d06..d6343ef7 100644 --- a/include/heap.h +++ b/include/heap.h @@ -57,7 +57,7 @@ namespace alg { private: int m_size; // current heap size. int m_max; // max heap size. - elem * m_heap; // key value pairs. + elem * m_heap; // key value pairs. public: Heap(int max) { m_size = 0; @@ -122,8 +122,8 @@ namespace alg { * remove the given data */ bool remove(T data) { - for (int i=0;i Date: Fri, 23 Oct 2015 17:09:48 +0800 Subject: [PATCH 19/53] add qualifier --- include/heap.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/heap.h b/include/heap.h index d6343ef7..8712109b 100644 --- a/include/heap.h +++ b/include/heap.h @@ -121,7 +121,7 @@ namespace alg { /** * remove the given data */ - bool remove(T data) { + bool remove(const T &data) { for (int i=0;i Date: Sun, 8 Nov 2015 23:19:15 -0200 Subject: [PATCH 20/53] Add Fenwick Tree algorithm - Gabriel123Duarte --- include/fenwick_tree.h | 44 +++++++++++++++++++++++++++++++++++++++ src/fenwick_tree_demo.cpp | 16 ++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 include/fenwick_tree.h create mode 100644 src/fenwick_tree_demo.cpp diff --git a/include/fenwick_tree.h b/include/fenwick_tree.h new file mode 100644 index 00000000..ba3f581c --- /dev/null +++ b/include/fenwick_tree.h @@ -0,0 +1,44 @@ +#ifndef __FENWICK_H__ +#define __FENWICK_H__ + +#include + +#define LSONE(x) (x & (-x)) + +class Fenwick +{ + private: + std::vector fen; + public: + Fenwick() {} + + // We don't use the index 0 + Fenwick(int n) + { + fen.assign(n + 1, 0); + } + + // RSQ 1..a + int rsq(int a) + { + int ans = 0; + for(; a; a -= LSONE(a)) + ans += fen[a]; + return ans; + } + + // RSQ a..b + inline int rsq(int a, int b) + { + return rsq(b) - (a == 1 ? 0 : rsq(a - 1)); + } + + // Update the value of the k-th element by x + void update(int k, int x) + { + for(; k < (int)fen.size(); k += LSONE(k)) + fen[k] += x; + } +}; + +#endif \ No newline at end of file diff --git a/src/fenwick_tree_demo.cpp b/src/fenwick_tree_demo.cpp new file mode 100644 index 00000000..d5e294de --- /dev/null +++ b/src/fenwick_tree_demo.cpp @@ -0,0 +1,16 @@ +#include +#include "fenwick_tree.h" + +int main() +{ + Fenwick ft(5); + + ft.update(2, 1); + ft.update(4, 10); + + printf("%d\n", ft.rsq(1)); + + ft.update(1, 5); + printf("%d\n", ft.rsq(1)); + return 0; +} \ No newline at end of file From 99defa0c2139648cc551509bf583457137598da5 Mon Sep 17 00:00:00 2001 From: Gabriel Duarte Date: Mon, 9 Nov 2015 08:53:33 -0200 Subject: [PATCH 21/53] Added some comments in the header file --- include/.goutputstream-UUQE8X | 61 +++++++++++++++++++++++++++++++++++ include/fenwick_tree.h | 23 +++++++++++-- 2 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 include/.goutputstream-UUQE8X diff --git a/include/.goutputstream-UUQE8X b/include/.goutputstream-UUQE8X new file mode 100644 index 00000000..e92d1ab5 --- /dev/null +++ b/include/.goutputstream-UUQE8X @@ -0,0 +1,61 @@ +/******************************************************************************* + * Fenwick Tree + * + * Data structure providing prefix sums and modify the table in O(log n) - n is the size o the table. + * + * In this algorithm we use two functions: + * - RSQ - This function calculates the range sum query in O(log n) + * - Update - This function adjusts the values in the given range in O(log n) + * + * https://en.wikipedia.org/wiki/Fenwick_tree + * + * @author Gabriel Duarte (gabriellagoa10@yahoo.com.br) + * @github Gabriel123Duarte + * + ******************************************************************************/ + +#ifndef __FENWICK_H__ +#define __FENWICK_H__ + +#include + +#define LSONE(x) (x & (-x)) + +class Fenwick +{ + private: + // Vector representing the table + std::vector fen; + public: + Fenwick() {} + + // We don't use the index 0, because it is the base case + Fenwick(int n) + { + fen.assign(n + 1, 0); + } + + // Calculate the + int rsq(int a) + { + int ans = 0; + for(; a; a -= LSONE(a)) + ans += fen[a]; + return ans; + } + + // RSQ a..b + inline int rsq(int a, int b) + { + return rsq(b) - (a == 1 ? 0 : rsq(a - 1)); + } + + // Update the value of the k-th element by x + void update(int k, int x) + { + for(; k < (int)fen.size(); k += LSONE(k)) + fen[k] += x; + } +}; + +#endif diff --git a/include/fenwick_tree.h b/include/fenwick_tree.h index ba3f581c..e92d1ab5 100644 --- a/include/fenwick_tree.h +++ b/include/fenwick_tree.h @@ -1,3 +1,19 @@ +/******************************************************************************* + * Fenwick Tree + * + * Data structure providing prefix sums and modify the table in O(log n) - n is the size o the table. + * + * In this algorithm we use two functions: + * - RSQ - This function calculates the range sum query in O(log n) + * - Update - This function adjusts the values in the given range in O(log n) + * + * https://en.wikipedia.org/wiki/Fenwick_tree + * + * @author Gabriel Duarte (gabriellagoa10@yahoo.com.br) + * @github Gabriel123Duarte + * + ******************************************************************************/ + #ifndef __FENWICK_H__ #define __FENWICK_H__ @@ -8,17 +24,18 @@ class Fenwick { private: + // Vector representing the table std::vector fen; public: Fenwick() {} - // We don't use the index 0 + // We don't use the index 0, because it is the base case Fenwick(int n) { fen.assign(n + 1, 0); } - // RSQ 1..a + // Calculate the int rsq(int a) { int ans = 0; @@ -41,4 +58,4 @@ class Fenwick } }; -#endif \ No newline at end of file +#endif From 943b52a8eb2d42b3fb1f92dd00a05f963fbd361f Mon Sep 17 00:00:00 2001 From: Yanzhe Chen Date: Wed, 30 Mar 2016 20:16:12 +0800 Subject: [PATCH 22/53] Add shell sort --- Makefile | 4 ++++ README.md | 1 + include/shell_sort.h | 44 +++++++++++++++++++++++++++++++++++++++++ src/shell_sort_demo.cpp | 32 ++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 include/shell_sort.h create mode 100644 src/shell_sort_demo.cpp diff --git a/Makefile b/Makefile index e9ba280c..c0c50190 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ LIBS = -lm PROGRAMS = m_based_demo \ integer_demo \ insertion_sort_demo \ + shell_sort_demo \ radix_sort_demo \ shuffle_demo \ quick_sort_demo \ @@ -81,6 +82,9 @@ integer_demo: $(SRCDIR)/integer_demo.cpp insertion_sort_demo: $(SRCDIR)/insertion_sort_demo.cpp $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) +shell_sort_demo: $(SRCDIR)/shell_sort_demo.cpp + $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) + radix_sort_demo: $(SRCDIR)/radix_sort_demo.cpp $(CPP) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) diff --git a/README.md b/README.md index 04958eaf..5bb32f29 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Bubble sort Selection sort Insertion sort + Shell sort Radix sort Quick sort Merge sort diff --git a/include/shell_sort.h b/include/shell_sort.h new file mode 100644 index 00000000..d3d2db2e --- /dev/null +++ b/include/shell_sort.h @@ -0,0 +1,44 @@ +/******************************************************************************* + * DANIEL'S ALGORITHM IMPLEMENTAIONS + * + * /\ | _ _ ._ o _|_ |_ ._ _ _ + * /--\ | (_| (_) | | |_ | | | | | _> + * _| + * + * SHELL SORT + * + * 1. sort array in O(n^(3/2)) time. + * + * https://en.wikipedia.org/wiki/Shellsort + * + ******************************************************************************/ + +#ifndef __SHELL_SORT_H__ +#define __SHELL_SORT_H__ + +namespace alg { + /** + * shell sort an array + */ + template + static void shell_sort(T *array, int len) { + int h = 1; + while (h < len / 3) { + h = 3 * h + 1; // 1, 4, 13, 40, 121, ... + } + while (h >= 1) { + for (int i = h; i < len; i++) { + int cur = array[i]; + int j = i - h; + while (j >= 0 && array[j] > cur) { + array[j + h] = array[j]; + j = j - h; + } + array[j + h] = cur; + } + h = h / 3; + } + } +} + +#endif // diff --git a/src/shell_sort_demo.cpp b/src/shell_sort_demo.cpp new file mode 100644 index 00000000..e284bd8d --- /dev/null +++ b/src/shell_sort_demo.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +#include "generic.h" +#include "shell_sort.h" + +using namespace alg; + +int main() +{ + const int MAX_ELEMENTS = 10; + int list[MAX_ELEMENTS]; + + int i = 0; + + srand(time(NULL)); + // generate random numbers and fill them to the list + for(i = 0; i < MAX_ELEMENTS; i++ ){ + list[i] = rand()%100; + } + printf("The list before sorting is:\n"); + printlist(list,MAX_ELEMENTS); + + // sort the list using shell sort + shell_sort(&list[0],MAX_ELEMENTS); + + // print the result + printf("The list after sorting using shell sort algorithm:\n"); + printlist(list,MAX_ELEMENTS); + return 0; +} From cc7793cf77d550bd47d18dd55de8c97390f7af01 Mon Sep 17 00:00:00 2001 From: Yanzhe Chen Date: Wed, 30 Mar 2016 22:04:42 +0800 Subject: [PATCH 23/53] Fix bugs in heap --- include/heap.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/heap.h b/include/heap.h index 8712109b..351ba0db 100644 --- a/include/heap.h +++ b/include/heap.h @@ -61,7 +61,7 @@ namespace alg { public: Heap(int max) { m_size = 0; - m_max = max+1; + m_max = max; m_heap = new elem[m_max]; }; @@ -101,7 +101,7 @@ namespace alg { inline void clear() { m_size = 0; } bool contains(const T & data) { - for(int i=1;i<=m_size;i++) { + for(int i=0;i Date: Thu, 31 Mar 2016 09:47:50 +0800 Subject: [PATCH 24/53] indent --- include/shell_sort.h | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/include/shell_sort.h b/include/shell_sort.h index d3d2db2e..22b04de9 100644 --- a/include/shell_sort.h +++ b/include/shell_sort.h @@ -22,22 +22,22 @@ namespace alg { */ template static void shell_sort(T *array, int len) { - int h = 1; - while (h < len / 3) { - h = 3 * h + 1; // 1, 4, 13, 40, 121, ... - } - while (h >= 1) { - for (int i = h; i < len; i++) { - int cur = array[i]; - int j = i - h; - while (j >= 0 && array[j] > cur) { - array[j + h] = array[j]; - j = j - h; - } - array[j + h] = cur; - } - h = h / 3; - } + int h = 1; + while (h < len / 3) { + h = 3 * h + 1; // 1, 4, 13, 40, 121, ... + } + while (h >= 1) { + for (int i = h; i < len; i++) { + int cur = array[i]; + int j = i - h; + while (j >= 0 && array[j] > cur) { + array[j + h] = array[j]; + j = j - h; + } + array[j + h] = cur; + } + h = h / 3; + } } } From c3c99c40033d0e3a6030f1a699a1b233504f85e2 Mon Sep 17 00:00:00 2001 From: xtaci Date: Tue, 26 Apr 2016 17:40:14 +0800 Subject: [PATCH 25/53] update --- README.md | 4 ++++ donate_alg.png | Bin 0 -> 4349 bytes 2 files changed, 4 insertions(+) create mode 100644 donate_alg.png diff --git a/README.md b/README.md index 5bb32f29..b2203990 100644 --- a/README.md +++ b/README.md @@ -101,3 +101,7 @@ ZhangYou0122: Push-Relabel algorithm, Suffix Tree UsingtcNower: Suffix Array afernandez90: AVL trees + +####支持此项目 ( Donations ) : +![donate](donate_alg.png) +欢迎使用支付宝手扫描上面的二维码,对该项目进行捐赠。捐赠款项将用于持续优化补全及完善。 diff --git a/donate_alg.png b/donate_alg.png new file mode 100644 index 0000000000000000000000000000000000000000..04e73bb091a433492a5a564e25cec76445518861 GIT binary patch literal 4349 zcmZWtdpy(o|9`vW(p{7&m!Bdf9CRwT(Kiy!kr26*UlQe3G_2XIi*k2Ja>@OekWE6a z!_i5sOf80CxvVwAVrI6Peb+hlJDv0Us{fc^`dW zt0bXde2^)%bku>ekk(+wvZIU*=M0Yx!`rv56^-TRG`uLJ_I34b4y;~h{a7Y96rewC z1=cU+faXPMu+>Ns#P5^fk|`yYd?%K@Ce-cQLEnfeC2}oDLdc)YA@uMOWl0oAJ-JP;lx)$!J0O48}m1_y&n_bl9$wR6VjN`BfW@sm+ysDeLiLF1$`GAF44ghYJ~hb?wI zUOi!BQ0eh<_`VB=oepguYS15)xVG2L@SQ{Qu9CV|c(iwHR|87(kU5TQXFIK;C$dVf zEDTCda_l{=rK|zZyU9q!G4eu2E-C|o8})X)v(%@D7D4pvwNke>>-!rGV>9R#{Pg(N z+FAI9r0@r`?!raqHjnzyD)3t4*SOm51L=FZav*OpkRn0`wB6wne0|$*s}ko<9vmDv zyBTfaMl0Ww*%}lLjeU02#~PXetx9lJd#82WKb7bL3&>z>sQlm`A$z*=KV`Jxt{nW^ z5_L_puAFhBXHH|K^^aX9zp$$YUG{_C@e*C_u*pf;N)>eyV$szx>Gg7nAKR62plY4a zazhafKLL#;c=4>IGoV{?ljXCGzpicKpL*&QC+E{!iGIDFx&w-m@Je+DV`QTJu-iOn z*sk_#a7Oj^-gPyb^%oJ^i;r)2CF9Jke9&cao560Co}~!njdjUi0}}ks=((0xE9)Y# zIhin8YP%eK16oj7YL9U;@WL?EtpB&O#Tfaf1`4z{2JlC>MM-!;dvA=bTft2&g6Qc( z;FCr=+dE6P=zf;G^ZYXThAJ>`uh{qXi(O%>Q(bb)0ql*h&^oZZB9;3vnSrBMs8T;? z#}qc3sgMc}x)k)Nb(G0;O9sZj53h(nx*2FYVMjCp=be_*e#AkRjPyUS|F6_II0R1j zQXyA@-tk{gFhHhnsJ~8VG0xEt-QX0CmnA8BD+nWGb{x7vY zq!GVngOAraU*QI8>u&6Xh;0QYmfYNs^ zFjE8}dHW*U4pXCe4eWV5^93@pI79>)Df!Wi9l_u(-=JYTRxpa8M)}~GMt9C*kZb)S zX0ne5NCE5bob=R0;v=88 zis8O}kFs<$(Y(n|PH0=w;(|8dQi2jJ6pSNlq7*1@C5$hDFwpEUWUa7&$un{P@P zur0-MVnc?0Fa*YWMka6zV0i8@i^d;A!C3Y~1davD{gs^9Oz`HrgF9u=Oh`Seo{FXO zl2Q#Pn^BZyrR!}5zb~y~mw1SE;$Y1F(3b_D#|WaaOzs#xN_g-d%V-HxA59i)sB^0kgF;=RKiu=tTG6cVr7^a5>8>$n z|3Jc)-aajH@WesMEx3;roV(ND$!qel^7_c!`6aT(Pl@TcU3uHPs&0L%RYhbS1m3@vh079}Ds4~Du7 z7ZSJ#iC|b6k*YTY;@!ML1>};=(D!O`8^41!2V4_kA!PRQ4i4g+VLzg3zAEV1xEWdt z&%JbL;2|Uy*C!1?#5W{*)JS}6ar)Fu^mNy#ojK$^%&tt`eyFXxD1?Wh#cBP8i_h7$ zh5fL%l0{=*<0Ozx9 zf%2)SOb#gB><+QZ3zf7;&;cH5O61WV zF_W$O;T+RE^6I!ttcAYFqPcN`=f!tgje;!Q*&h&cT(A|WIbZ%D(!lMum=F*wsq*mCOo35nzOIv@GaXMoRyHZbCZa=^1p}9)yTxS~OC6cGc?n8?Dm{5@2 z*VQr}(HFVp_eyr3p0a4nWJCD4qe)yhE~>mAv5*w+&GKEM4xm6oEKZEx7yFd*<y@A2nxf)&hA+9o09g|(4BE_ zRX^w|ZQb!oBm2QSpS)zE7xQA<@<$`yD=YdhE4f#;YFda}-&?)u{fu=i5!={9&C9s2 ze0Y99SK*xZ)Wvb(bCo7;C+EVEhHcUsLv80ENEFXVs_udqU;^H3bD=D5q&sHZP(73C z(>k}Jy2fdiVj>e+s}psr^r2UTJMP|yFWFzORa(X|Q+6hPQt>SL0=tC|6)ZInH{Ufo zu{9X@_Eg(_q9qqX2|}EQYQIU7503x(%Q-8b-1V(rKA@U}+N}ylFG*X=t|B9yKism< zfzMUchWJixDWR@~oi7j(Wb;2Dx<5!xN#EK7%q|JIPBGzOr`iwfR=yUeDd$on;X4xW z9EqpG5Q%tJiPJgq9~bU1h1(Nf)|MAm`$_?{<)3nF>&~{QC-sI4MIwy?U#SjG!8Ht# z+j71t^t~N&WIuB8#r@v5V`SRnxiqSi@6Tj`UZZ-%{;Of`;-ofe34`w|Y#5dQ(xYJ@ zsp;z-U1KYjbz;(O9Ph$CiE;54Jb|?<7a8EGUA~^N&OD!iVE|4igFw@=!L_3Bw)ZJV z7%vmVTrKAF50+bk&BKBzbXfJgB{!Jjhls&pVnrbe!2s0QeN&4t9WaAJjdIza@Z?I$ zd<7jb*cqvYW-Y^P10(_3^qasGSJ5w)MAl6;Ym%Rm?VOP%HFHiqrOg{xb*+thT=(CV zlOl-03cg@yf_aj1eK#ljE>kEKOg?tkOs79bTuf5`E+&mA$Y@ky+Y?KSH!kIfP`IM* zw;(il<*~eRR{Wc~e~Vhe3<^)>w8)0go0urW+gErYBEj9nA9SzSeRys)*K*5jGcn8) z&!RJu8rKB8&_5mfBi&n%A&*k%-ATQAd=%xCbS^6F7>OR`?_>QJga4(b2Xnv^1hJ)r zkHl5K7*oW}b0wAP%jb=DkfvIiLyLYao!=AyKN)fVai5}KT$Y-}f?p@GlTwd_6b2Rd zv6UJBH09@oHXNP>Xn9iyOkfZ`0z+_(1MT10z=E2 z>1B)Be(2>9Ak}drr2RTCm0SE`EP{ALA0Hy{jt~alW?HTeDQ>l^1 z$c@{w7}L(|uS1chm}`Q%e+M}bq49KndBF0r?@jz1cf#&Rm;4=zzxN*xK=!uueY~r3 z;K`ZgH2#!RsjQ@C^5w~_q1Sl^ z7`lF^poesuHX?8g2Iq56@1~OK09)@-Dn| z#sGcMbNXl7?!7q{oxef54A6sT4oMb!cuO}PPOnURWPWB>NUA68qK{HKK4tT3m$C=R zYUhD_-D|sEwac%Lf($5^eq~|DoBMVH^b@Z*2uFhKwGv};Ui`@n#wM$5^5k5k&;F*# zHNwD}36@O4&%{GdM!8nsv^)NK(RJ+e^Hm3lUz1N=fHW`o7RA4AFwNOk#<$7ioC5LF z6*tr3TbtiJ>uFrFzkJ@V-W*idD^;)e&PwZ@46eKv>EFC0wcdJG=M2YR0ghlO9zeZ5 zJ1@C6=d8a#$IDiS?aYb0G_4Cs0%qb9PCYEN^0CL#W0rurmbmUc%LGlb|}xRTGIE+shh^s za+J2`d5`7Cw3d5udr41~1B>eoR*$dhlH*Wg{%)B1b-`1ddr1-+!$s1(&FHwO$O1|CL6?g~|r z3^baT8To^DG?#>036~q)t7Ql~XVA->>%y%yGveM@`!Yu}E`8l<#%LP!ED>}J-S4m? zXh(GJYB1xDozqip4Kb}bqBrTUpYkYurMQgNenVchj!(+na5UH+<2G3ljB~kek9nwD z)_ZW*iI&WBdZ0by`T{*>T)Mo-6%9JH+!+=w8wZb6(^7p{uRq8Y=~a518po zyzKZ^&&=dQqyrHS(;lRd_|XZ>uG3kA8Qi4N6>90?GfV2b2*($8zuB^6yw~DlqIp(D znR`i2!Ofd6Pj|4)2Xn<`!0P{<2K?Sxd-dpds{bl@*S36!(1I7Izs(mPYXE!OUu>}d H@xA?Dd2w2* literal 0 HcmV?d00001 From 50035c9941f8fca3d7e810ad8455536f3d5d7bc5 Mon Sep 17 00:00:00 2001 From: xtaci Date: Tue, 26 Apr 2016 18:28:38 +0800 Subject: [PATCH 26/53] update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b2203990..33744dd0 100644 --- a/README.md +++ b/README.md @@ -104,4 +104,4 @@ ####支持此项目 ( Donations ) : ![donate](donate_alg.png) -欢迎使用支付宝手扫描上面的二维码,对该项目进行捐赠。捐赠款项将用于持续优化补全及完善。 +欢迎使用支付宝扫描上面的二维码,对该项目进行捐赠。捐赠款项将用于持续优化补全及完善。 From b8c632b6352e06fdd990fa2c012ebe7eb188cf0c Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:39:55 +0900 Subject: [PATCH 27/53] definiton -> definition --- include/binary_search_tree.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/binary_search_tree.h b/include/binary_search_tree.h index 5066c771..2fc544c0 100644 --- a/include/binary_search_tree.h +++ b/include/binary_search_tree.h @@ -31,7 +31,7 @@ namespace alg { class BST { private: /** - * binary search tree definiton. + * binary search tree definition. */ struct treeNode { KeyT key; // key From 841a1bdbba7cd5a52f33fa3fc3e017937e4d2f28 Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:44:45 +0900 Subject: [PATCH 28/53] h:55 inital -> initial --- include/dijkstra.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dijkstra.h b/include/dijkstra.h index d0bebe7d..5013f14f 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -52,7 +52,7 @@ namespace alg { // all vertices Graph::Adjacent * a; list_for_each_entry(a, &g.list(), a_node){ - dist[a->v.id] = LARGE_NUMBER; // set inital distance to each vertex to a large number + dist[a->v.id] = LARGE_NUMBER; // set initial distance to each vertex to a large number (*previous)[a->v.id] = UNDEFINED; // clear path to UNDEFINED visited[a->v.id] = false; // all vertices are not visited Q.push(LARGE_NUMBER, a->v.id); // push all vertices to heap From 70a90bdde9638c1f5c21b172272409608591e27c Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:47:51 +0900 Subject: [PATCH 29/53] h:39 definiton -> definition --- include/hash_table.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/hash_table.h b/include/hash_table.h index 0fcdb713..1c5abd6e 100644 --- a/include/hash_table.h +++ b/include/hash_table.h @@ -36,7 +36,7 @@ namespace alg { typedef _HashCode hash_code_fn; private: /** - * definiton of Key-Value pair. + * definition of Key-Value pair. */ struct HashKV { key_type key; // 32-bit key From a2554338602c9e3c49f0a1e147eb20cd7c5185cc Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:51:02 +0900 Subject: [PATCH 30/53] h:16 seperately -> separately h:17 fucntion -> function --- include/merge_sort.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/merge_sort.h b/include/merge_sort.h index 8a45aece..5eab291d 100644 --- a/include/merge_sort.h +++ b/include/merge_sort.h @@ -13,8 +13,8 @@ * and right part. * Example: Say the input is -10 32 45 -78 91 1 0 -16 then the left part will be * -10 32 45 -78 and the right part will be 91 1 0 6. - * (2) Sort Each of them seperately. Note that here sort does not mean to sort it using some other - * method. We already wrote fucntion to sort it. Use the same. + * (2) Sort Each of them separately. Note that here sort does not mean to sort it using some other + * method. We already wrote function to sort it. Use the same. * (3) Then merge the two sorted parts. * * ------------ From 0556913a27d355357b45172a35a26b5c88dd0848 Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:54:31 +0900 Subject: [PATCH 31/53] h:78 apropriate -> appropriate --- include/priority_queue.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/priority_queue.h b/include/priority_queue.h index 4a8dbe28..e81edf35 100644 --- a/include/priority_queue.h +++ b/include/priority_queue.h @@ -75,7 +75,7 @@ namespace alg { list_add(&n->node, &m_head); m_count++; } else { - // sequentially find the apropriate position + // sequentially find the appropriate position PQNode * pos; bool found = false; list_for_each_entry(pos, &m_head, node) { From a0ce506be525bb55aad2cf1779adcfd07cd594ed Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 10:57:31 +0900 Subject: [PATCH 32/53] h:100 higer -> higher --- include/skiplist.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/skiplist.h b/include/skiplist.h index 1c330ba7..f45d4f33 100644 --- a/include/skiplist.h +++ b/include/skiplist.h @@ -97,7 +97,7 @@ namespace alg { if(x == NULL || x->key != key) { int lvl = random_level(); // random promotion - // for nodes higer than current max level + // for nodes higher than current max level // make 'header node' as it's prev if(lvl > m_level) { for(int i = m_level + 1; i <= lvl; i++) { From 4cf920ed7d76cbdb2e02a3389fea8609c97499cf Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 11:01:26 +0900 Subject: [PATCH 33/53] h:27: postion -> position h:260: beginnig -> beginning h:269: seperate -> separate --- include/suffix_tree.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/suffix_tree.h b/include/suffix_tree.h index 23bd04d6..d738d9c3 100644 --- a/include/suffix_tree.h +++ b/include/suffix_tree.h @@ -24,7 +24,7 @@ class SuffixTree SuffixTree(string str):test_str(str), root(test_str), active_point(&root, 0, 0), remainder(0), pos(0), active_e(0), ls() {} int construct(void); - // return -1 if no such sub exist, return the beginning postion of this substring in thr original string if it exist + // return -1 if no such sub exist, return the beginning position of this substring in thr original string if it exist int search(string sub); // return the length of the longest prefix of sub which can be matched in suffix tree @@ -257,7 +257,7 @@ class SuffixTree int remainder; // how many characters inserted? unsigned int pos; - unsigned int active_e; // the beginnig position of suffixes need to be inserted + unsigned int active_e; // the beginning position of suffixes need to be inserted char get_ele(int i) { return test_str[i]; } // insert a char from pos to suffix tree int insert(); @@ -266,7 +266,7 @@ class SuffixTree int print_node(Node* node, int level); - Node* seperate_edge(Node * node, Edge* edge); + Node* separate_edge(Node * node, Edge* edge); // check if we can change active node bool check_active_node(void) From 56e957685304afbff7d110821288ffd893e367f9 Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 11:03:08 +0900 Subject: [PATCH 34/53] h:66: arbitary -> arbitrary --- include/universal_hash.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/universal_hash.h b/include/universal_hash.h index 29afcf7d..1b162857 100644 --- a/include/universal_hash.h +++ b/include/universal_hash.h @@ -63,7 +63,7 @@ namespace alg { } /** - * hash an arbitary length integer. + * hash an arbitrary length integer. * len, number of 32-bit integer, max len is 32 */ static uint32_t uhash_bigint(const struct UHash * params, uint32_t * key, uint32_t len) { From 20ecf38f3b78fe96e65b538abf26d0e2e7014096 Mon Sep 17 00:00:00 2001 From: tmdrnjs54 Date: Thu, 19 May 2016 11:06:34 +0900 Subject: [PATCH 35/53] 85: seperate -> separate --- src/suffix_tree_demo.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/suffix_tree_demo.cpp b/src/suffix_tree_demo.cpp index 8862e9b2..d4425c68 100644 --- a/src/suffix_tree_demo.cpp +++ b/src/suffix_tree_demo.cpp @@ -82,7 +82,7 @@ int SuffixTree::construct(void) ls.ins_link(node); break; } - Node *newnode = seperate_edge(node, a_edge); + Node *newnode = separate_edge(node, a_edge); Edge* newedge = new Edge(pos, numeric_limits::max(), test_str); newnode->add_edge(newedge); ls.ins_link(newnode); @@ -106,7 +106,7 @@ int SuffixTree::construct(void) SuffixTree::Node* SuffixTree::seperate_edge(Node * node, Edge* a_edge) { - cout << "seperate the old edge here: " << (*a_edge) << endl; + cout << "separate the old edge here: " << (*a_edge) << endl; int new_begin = a_edge->begin + get_active_length(); int new_end = a_edge->end; @@ -175,7 +175,7 @@ using namespace std; int main() { - cout << "Begining" << endl; + cout << "Beginning" << endl; SuffixTree st("mississippi"); cout << "Constructing..." << endl; From e9a800739c10c2bbc4c2617cd07b74a62a72f9da Mon Sep 17 00:00:00 2001 From: "Hesham.Safi" Date: Sat, 21 May 2016 15:06:47 +0200 Subject: [PATCH 36/53] issue #46 : include/suffix_tree.h : - fixed build errors ( access modifiers issue) - removed typedef struct ... as it is unnecessary in C++ src/suffic_tree_demo.cpp : - fixed typo in function declaration name (seperate -> separate) --- include/suffix_tree.h | 16 +++------------- src/suffix_tree_demo.cpp | 2 +- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/include/suffix_tree.h b/include/suffix_tree.h index d738d9c3..7fd684e6 100644 --- a/include/suffix_tree.h +++ b/include/suffix_tree.h @@ -31,8 +31,6 @@ class SuffixTree template Iterator inc_search(Iterator sub) { - typedef typename Iterator::value_type T; // extract real type - Iterator result = sub; Node* node = &root; Edge* edge = NULL; @@ -73,13 +71,7 @@ class SuffixTree return result; } - int print_tree(void); -private: - string test_str; - struct Node; - typedef struct Node Node; - struct Edge{ // the begin and end pos of this edge, note that INT_MAX stands for #(the changing end pos of this entire string) unsigned int begin, end; @@ -146,7 +138,6 @@ class SuffixTree bool is_none(void) { return begin == 0 && end == 0; } }; - typedef struct Edge Edge; struct Node{ string& test_node_str; @@ -224,10 +215,9 @@ class SuffixTree return os; } }; - //typedef struct Node Node; - - friend struct Node; - + int print_tree(void); +private: + string test_str; class ActivePoint{ public: Node* active_node; diff --git a/src/suffix_tree_demo.cpp b/src/suffix_tree_demo.cpp index d4425c68..4a5170b5 100644 --- a/src/suffix_tree_demo.cpp +++ b/src/suffix_tree_demo.cpp @@ -104,7 +104,7 @@ int SuffixTree::construct(void) return 0; } -SuffixTree::Node* SuffixTree::seperate_edge(Node * node, Edge* a_edge) +SuffixTree::Node* SuffixTree::separate_edge(Node * node, Edge* a_edge) { cout << "separate the old edge here: " << (*a_edge) << endl; int new_begin = a_edge->begin + get_active_length(); From 7f8d1ab173f3f1d1e9016c39745fb53d7b67134f Mon Sep 17 00:00:00 2001 From: ahmetince Date: Sat, 6 Aug 2016 12:55:28 +0300 Subject: [PATCH 37/53] rearranged I tried to rearrange the code with cpp codes. --- src/random_demo.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/random_demo.cpp b/src/random_demo.cpp index 3c5788a5..d2df1d8c 100644 --- a/src/random_demo.cpp +++ b/src/random_demo.cpp @@ -1,9 +1,9 @@ -#include +#include #include "random.h" int main(void) { - printf("generate random numbers\n"); + std::cout <<"generate random numbers\n"; for (int i=0;i<100;i++) { - printf("%u\n",alg::LCG()); + std::cout< Date: Sun, 7 Aug 2016 21:47:05 +0800 Subject: [PATCH 38/53] add .travis.yml --- .travis.yml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..e4d9ce7f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: cpp + +compiler: + - gcc + - clang + +script: + - make From 788277858072c74ed13c9c6ee14abdb7d8b98bfe Mon Sep 17 00:00:00 2001 From: xtaci Date: Sun, 7 Aug 2016 21:47:59 +0800 Subject: [PATCH 39/53] upd readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 33744dd0..d9b7eb9f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ ###Algorithms & Data Structures in C++ +[![Build Status][1]][2] +[1]: https://travis-ci.org/xtaci/algorithms.svg?branch=master +[2]: https://travis-ci.org/xtaci/algorithms ####目标 ( goal ) : From e30dd73ee55ca26558288b37cf79198a97c3b938 Mon Sep 17 00:00:00 2001 From: Vic Luo Date: Thu, 1 Sep 2016 14:05:37 +0800 Subject: [PATCH 40/53] Correct import guard macro name --- include/2darray.h | 4 ++-- include/8queen.h | 6 +++--- include/astar.h | 4 ++-- include/avl.h | 4 ++-- include/bellman_ford.h | 4 ++-- include/binary_search_tree.h | 4 ++-- include/bitset.h | 4 ++-- include/bloom_filter.h | 4 ++-- include/btree.h | 4 ++-- include/dijkstra.h | 4 ++-- include/directed_graph.h | 4 ++-- include/disjoint-set.h | 4 ++-- include/dos_tree.h | 4 ++-- include/double_linked_list.h | 4 ++-- include/edmonds_karp.h | 4 ++-- include/fenwick_tree.h | 4 ++-- include/fib-heap.h | 4 ++-- include/generic.h | 4 ++-- include/graph_defs.h | 4 ++-- include/graph_search.h | 4 ++-- include/hash_code.h | 4 ++-- include/hash_multi.h | 4 ++-- include/hash_string.h | 4 ++-- include/hash_table.h | 4 ++-- include/heap.h | 4 ++-- include/huffman.h | 4 ++-- include/imath.h | 4 ++-- include/insertion_sort.h | 4 ++-- include/integer.h | 4 ++-- include/interval_tree.h | 4 ++-- include/k-means.h | 4 ++-- include/kmp.h | 4 ++-- include/kruskal_mst.h | 4 ++-- include/lcs.h | 4 ++-- include/md5.h | 4 ++-- include/merge_sort.h | 4 ++-- include/perfect_hash.h | 4 ++-- include/prim_mst.h | 4 ++-- include/prime.h | 4 ++-- include/priority_queue.h | 4 ++-- include/queue.h | 4 ++-- include/quick_sort.h | 4 ++-- include/radix_sort.h | 4 ++-- include/random.h | 4 ++-- include/random_select.h | 4 ++-- include/rbtree.h | 4 ++-- include/rbtree_defs.h | 4 ++-- include/relabel_to_front.h | 4 ++-- include/scc.h | 4 ++-- include/selection_sort.h | 6 +++--- include/sha1.h | 4 ++-- include/shell_sort.h | 4 ++-- include/shuffle.h | 4 ++-- include/simhash.h | 4 ++-- include/skiplist.h | 4 ++-- include/sol.h | 4 ++-- include/stack.h | 4 ++-- include/suffix_array.h | 6 +++--- include/trie.h | 4 ++-- include/undirected_graph.h | 4 ++-- include/universal_hash.h | 4 ++-- include/word_seg.h | 4 ++-- msvc/alg_vs.h | 6 +++--- utils/byteorder.h | 4 ++-- utils/gb18030.h | 4 ++-- 65 files changed, 134 insertions(+), 134 deletions(-) diff --git a/include/2darray.h b/include/2darray.h index 1383e242..929a286a 100644 --- a/include/2darray.h +++ b/include/2darray.h @@ -10,8 +10,8 @@ * Simulated by 1-dimension array. ******************************************************************************/ -#ifndef __2D_ARRAY_H__ -#define __2D_ARRAY_H__ +#ifndef 2D_ARRAY_H__ +#define 2D_ARRAY_H__ #include #include diff --git a/include/8queen.h b/include/8queen.h index 27062e3a..17eaefef 100644 --- a/include/8queen.h +++ b/include/8queen.h @@ -9,8 +9,8 @@ * http://en.wikipedia.org/wiki/Eight_queens_puzzle ******************************************************************************/ -#ifndef __8QUEEN_H__ -#define __8QUEEN_H__ +#ifndef 8QUEEN_H__ +#define 8QUEEN_H__ #include #include @@ -84,4 +84,4 @@ namespace alg { }; } -#endif //__8QUEEN_H__ +#endif //8QUEEN_H__ diff --git a/include/astar.h b/include/astar.h index 03474b34..6dc68f02 100644 --- a/include/astar.h +++ b/include/astar.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef __ASTAR_H__ -#define __ASTAR_H__ +#ifndef ASTAR_H__ +#define ASTAR_H__ #include #include diff --git a/include/avl.h b/include/avl.h index 4b8f00cd..aa1ef6ca 100644 --- a/include/avl.h +++ b/include/avl.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef __AVL_H__ -#define __AVL_H__ +#ifndef AVL_H__ +#define AVL_H__ #include #include diff --git a/include/bellman_ford.h b/include/bellman_ford.h index 2d6df417..d859d72a 100644 --- a/include/bellman_ford.h +++ b/include/bellman_ford.h @@ -44,8 +44,8 @@ * ******************************************************************************/ -#ifndef __BELLMAN_FORD_H__ -#define __BELLMAN_FORD_H__ +#ifndef BELLMAN_FORD_H__ +#define BELLMAN_FORD_H__ #include #include diff --git a/include/binary_search_tree.h b/include/binary_search_tree.h index 2fc544c0..254cb0f2 100644 --- a/include/binary_search_tree.h +++ b/include/binary_search_tree.h @@ -18,8 +18,8 @@ * ******************************************************************************/ -#ifndef __BINARY_SEARCH_TREE_H__ -#define __BINARY_SEARCH_TREE_H__ +#ifndef BINARY_SEARCH_TREE_H__ +#define BINARY_SEARCH_TREE_H__ #include #include diff --git a/include/bitset.h b/include/bitset.h index 83171c12..fd6ae57b 100644 --- a/include/bitset.h +++ b/include/bitset.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef __BIT_SET_H__ -#define __BIT_SET_H__ +#ifndef BIT_SET_H__ +#define BIT_SET_H__ #include #include diff --git a/include/bloom_filter.h b/include/bloom_filter.h index 810badd5..952d44ab 100644 --- a/include/bloom_filter.h +++ b/include/bloom_filter.h @@ -18,8 +18,8 @@ * ******************************************************************************/ -#ifndef __BLOOM_FILTER_H__ -#define __BLOOM_FILTER_H__ +#ifndef BLOOM_FILTER_H__ +#define BLOOM_FILTER_H__ #include #include diff --git a/include/btree.h b/include/btree.h index bec96048..5f50b6e2 100644 --- a/include/btree.h +++ b/include/btree.h @@ -24,8 +24,8 @@ * http://en.wikipedia.org/wiki/B-tree ******************************************************************************/ -#ifndef __BTREE_H__ -#define __BTREE_H__ +#ifndef BTREE_H__ +#define BTREE_H__ #include #include diff --git a/include/dijkstra.h b/include/dijkstra.h index 5013f14f..73b6c2ee 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef __DIJKSTRA_H__ -#define __DIJKSTRA_H__ +#ifndef DIJKSTRA_H__ +#define DIJKSTRA_H__ #include #include diff --git a/include/directed_graph.h b/include/directed_graph.h index 0eb8e56e..7694e5a3 100644 --- a/include/directed_graph.h +++ b/include/directed_graph.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __DIRECTED_GRAPH_H__ -#define __DIRECTED_GRAPH_H__ +#ifndef DIRECTED_GRAPH_H__ +#define DIRECTED_GRAPH_H__ #include #include diff --git a/include/disjoint-set.h b/include/disjoint-set.h index c13a002e..1d1568c4 100644 --- a/include/disjoint-set.h +++ b/include/disjoint-set.h @@ -18,8 +18,8 @@ * http://en.wikipedia.org/wiki/Disjoint-set_data_structure ******************************************************************************/ -#ifndef __DISJOINTSET_H__ -#define __DISJOINTSET_H__ +#ifndef DISJOINTSET_H__ +#define DISJOINTSET_H__ namespace alg { template diff --git a/include/dos_tree.h b/include/dos_tree.h index 7440990f..f754f180 100644 --- a/include/dos_tree.h +++ b/include/dos_tree.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef __DOS_TREE_H__ -#define __DOS_TREE_H__ +#ifndef DOS_TREE_H__ +#define DOS_TREE_H__ #include #include diff --git a/include/double_linked_list.h b/include/double_linked_list.h index 7b8a8637..be595e7e 100644 --- a/include/double_linked_list.h +++ b/include/double_linked_list.h @@ -16,8 +16,8 @@ * http://en.wikipedia.org/wiki/Double_linked_list ******************************************************************************/ -#ifndef __DOUBLE_LINKED_LIST_H__ -#define __DOUBLE_LINKED_LIST_H__ +#ifndef DOUBLE_LINKED_LIST_H__ +#define DOUBLE_LINKED_LIST_H__ struct list_head { struct list_head *next, *prev; diff --git a/include/edmonds_karp.h b/include/edmonds_karp.h index 396c9fbc..f9ca4b56 100644 --- a/include/edmonds_karp.h +++ b/include/edmonds_karp.h @@ -21,8 +21,8 @@ * ******************************************************************************/ -#ifndef __EDMONDS_KARP_H__ -#define __EDMONDS_KARP_H__ +#ifndef EDMONDS_KARP_H__ +#define EDMONDS_KARP_H__ #include #include diff --git a/include/fenwick_tree.h b/include/fenwick_tree.h index e92d1ab5..cc12d602 100644 --- a/include/fenwick_tree.h +++ b/include/fenwick_tree.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __FENWICK_H__ -#define __FENWICK_H__ +#ifndef FENWICK_H__ +#define FENWICK_H__ #include diff --git a/include/fib-heap.h b/include/fib-heap.h index 68100b8b..b5303bf6 100644 --- a/include/fib-heap.h +++ b/include/fib-heap.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Fibonacci_heap ******************************************************************************/ -#ifndef __FIB_HEAP_H__ -#define __FIB_HEAP_H__ +#ifndef FIB_HEAP_H__ +#define FIB_HEAP_H__ #include #include #include diff --git a/include/generic.h b/include/generic.h index 8671f740..16d50e84 100644 --- a/include/generic.h +++ b/include/generic.h @@ -9,8 +9,8 @@ * ******************************************************************************/ -#ifndef __ALG_INC_H__ -#define __ALG_INC_H__ +#ifndef ALG_INC_H__ +#define ALG_INC_H__ #include #include #include diff --git a/include/graph_defs.h b/include/graph_defs.h index 8860c9b1..f2b61ada 100644 --- a/include/graph_defs.h +++ b/include/graph_defs.h @@ -1,5 +1,5 @@ -#ifndef __GRAPH_DEFS_H__ -#define __GRAPH_DEFS_H__ +#ifndef GRAPH_DEFS_H__ +#define GRAPH_DEFS_H__ #include "double_linked_list.h" diff --git a/include/graph_search.h b/include/graph_search.h index 438ea660..7605aed6 100644 --- a/include/graph_search.h +++ b/include/graph_search.h @@ -20,8 +20,8 @@ * ******************************************************************************/ -#ifndef __BREADTH_FIRST_SEARCH_H__ -#define __BREADTH_FIRST_SEARCH_H__ +#ifndef BREADTH_FIRST_SEARCH_H__ +#define BREADTH_FIRST_SEARCH_H__ #include #include diff --git a/include/hash_code.h b/include/hash_code.h index 89f22b2b..7773a108 100644 --- a/include/hash_code.h +++ b/include/hash_code.h @@ -1,5 +1,5 @@ -#ifndef __HASH_CODE_H__ -#define __HASH_CODE_H__ +#ifndef HASH_CODE_H__ +#define HASH_CODE_H__ #include #include "hash_string.h" namespace alg { diff --git a/include/hash_multi.h b/include/hash_multi.h index 4639e229..73040cef 100644 --- a/include/hash_multi.h +++ b/include/hash_multi.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef __HASH_MULTIPLICATION_H__ -#define __HASH_MULTIPLICATION_H__ +#ifndef HASH_MULTIPLICATION_H__ +#define HASH_MULTIPLICATION_H__ #include #include diff --git a/include/hash_string.h b/include/hash_string.h index 52d1b7cf..9a95d4fa 100644 --- a/include/hash_string.h +++ b/include/hash_string.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef __STRING_HASH_H__ -#define __STRING_HASH_H__ +#ifndef STRING_HASH_H__ +#define STRING_HASH_H__ #include diff --git a/include/hash_table.h b/include/hash_table.h index 1c5abd6e..9ed41eb3 100644 --- a/include/hash_table.h +++ b/include/hash_table.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __HASH_TABLE_H__ -#define __HASH_TABLE_H__ +#ifndef HASH_TABLE_H__ +#define HASH_TABLE_H__ #include #include diff --git a/include/heap.h b/include/heap.h index 351ba0db..dec4f9bc 100644 --- a/include/heap.h +++ b/include/heap.h @@ -28,8 +28,8 @@ * http://en.wikipedia.org/wiki/Binary_heap ******************************************************************************/ -#ifndef __HEAP_H__ -#define __HEAP_H__ +#ifndef HEAP_H__ +#define HEAP_H__ #include #include diff --git a/include/huffman.h b/include/huffman.h index b47a4d12..c95fc9b5 100644 --- a/include/huffman.h +++ b/include/huffman.h @@ -21,8 +21,8 @@ * ******************************************************************************/ -#ifndef __HUFFMAN_CODING_H__ -#define __HUFFMAN_CODING_H__ +#ifndef HUFFMAN_CODING_H__ +#define HUFFMAN_CODING_H__ #include #include diff --git a/include/imath.h b/include/imath.h index 6a23ab52..00e1e472 100644 --- a/include/imath.h +++ b/include/imath.h @@ -9,8 +9,8 @@ * ******************************************************************************/ -#ifndef __IMATH_H__ -#define __IMATH_H__ +#ifndef IMATH_H__ +#define IMATH_H__ #include #include diff --git a/include/insertion_sort.h b/include/insertion_sort.h index 42427e4b..8fcf6f42 100644 --- a/include/insertion_sort.h +++ b/include/insertion_sort.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef __INSERTION_SORT_H__ -#define __INSERTION_SORT_H__ +#ifndef INSERTION_SORT_H__ +#define INSERTION_SORT_H__ namespace alg { /** diff --git a/include/integer.h b/include/integer.h index f177d5ad..3c8e7d1b 100644 --- a/include/integer.h +++ b/include/integer.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef __INTEGER_H__ -#define __INTEGER_H__ +#ifndef INTEGER_H__ +#define INTEGER_H__ #include #include diff --git a/include/interval_tree.h b/include/interval_tree.h index c4edca19..5b6848e2 100644 --- a/include/interval_tree.h +++ b/include/interval_tree.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef __INTERVAL_TREE_H__ -#define __INTERVAL_TREE_H__ +#ifndef INTERVAL_TREE_H__ +#define INTERVAL_TREE_H__ #include #include diff --git a/include/k-means.h b/include/k-means.h index 953430de..966c085e 100644 --- a/include/k-means.h +++ b/include/k-means.h @@ -12,8 +12,8 @@ * https://github.com/wycg1984 ******************************************************************************/ -#ifndef __KMEANS_H__ -#define __KMEANS_H__ +#ifndef KMEANS_H__ +#define KMEANS_H__ #include #include #include diff --git a/include/kmp.h b/include/kmp.h index ada982a1..acb38297 100644 --- a/include/kmp.h +++ b/include/kmp.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef __KMP_H__ -#define __KMP_H__ +#ifndef KMP_H__ +#define KMP_H__ #include namespace alg { diff --git a/include/kruskal_mst.h b/include/kruskal_mst.h index d6ed961e..b4f8078e 100644 --- a/include/kruskal_mst.h +++ b/include/kruskal_mst.h @@ -25,8 +25,8 @@ * By Contibutor:xmuliang ******************************************************************************/ -#ifndef __KRUSKAL_MST_H__ -#define __KRUSKAL_MST_H__ +#ifndef KRUSKAL_MST_H__ +#define KRUSKAL_MST_H__ #include #include diff --git a/include/lcs.h b/include/lcs.h index a3c3e9d9..93d6fa5f 100644 --- a/include/lcs.h +++ b/include/lcs.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef __LCS_H__ -#define __LCS_H__ +#ifndef LCS_H__ +#define LCS_H__ #include "generic.h" #include "2darray.h" diff --git a/include/md5.h b/include/md5.h index 5ff52525..3d8c1410 100644 --- a/include/md5.h +++ b/include/md5.h @@ -23,8 +23,8 @@ */ -#ifndef __MD5_H__ -#define __MD5_H__ +#ifndef MD5_H__ +#define MD5_H__ #include /* Data structure for MD5 (Message Digest) computation */ diff --git a/include/merge_sort.h b/include/merge_sort.h index 5eab291d..519c19a2 100644 --- a/include/merge_sort.h +++ b/include/merge_sort.h @@ -32,8 +32,8 @@ * ******************************************************************************/ -#ifndef __MERGE_SORT_H__ -#define __MERGE_SORT_H__ +#ifndef MERGE_SORT_H__ +#define MERGE_SORT_H__ namespace alg { /** diff --git a/include/perfect_hash.h b/include/perfect_hash.h index 52dd9801..28b23191 100644 --- a/include/perfect_hash.h +++ b/include/perfect_hash.h @@ -10,8 +10,8 @@ * http://en.wikipedia.org/wiki/Perfect_hash * ******************************************************************************/ -#ifndef __PERFECT_HASH_H__ -#define __PERFECT_HASH_H__ +#ifndef PERFECT_HASH_H__ +#define PERFECT_HASH_H__ #include #include #include diff --git a/include/prim_mst.h b/include/prim_mst.h index 3949e895..97d8e0a7 100644 --- a/include/prim_mst.h +++ b/include/prim_mst.h @@ -22,8 +22,8 @@ * ******************************************************************************/ -#ifndef __PRIM_MST_H__ -#define __PRIM_MST_H__ +#ifndef PRIM_MST_H__ +#define PRIM_MST_H__ #include #include diff --git a/include/prime.h b/include/prime.h index 81f772e4..593fb81a 100644 --- a/include/prime.h +++ b/include/prime.h @@ -11,8 +11,8 @@ * http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test * ******************************************************************************/ -#ifndef __PRIME_H__ -#define __PRIME_H__ +#ifndef PRIME_H__ +#define PRIME_H__ #include #include diff --git a/include/priority_queue.h b/include/priority_queue.h index e81edf35..10214a83 100644 --- a/include/priority_queue.h +++ b/include/priority_queue.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef __PRIORITY_QUEUE_H__ -#define __PRIORITY_QUEUE_H__ +#ifndef PRIORITY_QUEUE_H__ +#define PRIORITY_QUEUE_H__ #include #include diff --git a/include/queue.h b/include/queue.h index 1de7b292..7e322aec 100644 --- a/include/queue.h +++ b/include/queue.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef __QUEUE_H__ -#define __QUEUE_H__ +#ifndef QUEUE_H__ +#define QUEUE_H__ #include #include diff --git a/include/quick_sort.h b/include/quick_sort.h index b8962216..936d0860 100644 --- a/include/quick_sort.h +++ b/include/quick_sort.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef __QUICKSORT_H__ -#define __QUICKSORT_H__ +#ifndef QUICKSORT_H__ +#define QUICKSORT_H__ #include diff --git a/include/radix_sort.h b/include/radix_sort.h index 4272ce48..16298355 100644 --- a/include/radix_sort.h +++ b/include/radix_sort.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef __RADIX_SORT_H__ -#define __RADIX_SORT_H__ +#ifndef RADIX_SORT_H__ +#define RADIX_SORT_H__ #include #include diff --git a/include/random.h b/include/random.h index 456ab698..2ae56aa3 100644 --- a/include/random.h +++ b/include/random.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef __RANDOM_H__ -#define __RANDOM_H__ +#ifndef RANDOM_H__ +#define RANDOM_H__ #include #include diff --git a/include/random_select.h b/include/random_select.h index 3577bdd9..500d0c9a 100644 --- a/include/random_select.h +++ b/include/random_select.h @@ -17,8 +17,8 @@ * ******************************************************************************/ -#ifndef __RANDOM_SELECT_H__ -#define __RANDOM_SELECT_H__ +#ifndef RANDOM_SELECT_H__ +#define RANDOM_SELECT_H__ #include diff --git a/include/rbtree.h b/include/rbtree.h index c9b8a318..bace1061 100644 --- a/include/rbtree.h +++ b/include/rbtree.h @@ -15,8 +15,8 @@ * http://en.literateprograms.org/Red-black_tree_(C) ******************************************************************************/ -#ifndef __RBTREE_H__ -#define __RBTREE_H__ +#ifndef RBTREE_H__ +#define RBTREE_H__ #include #include #include diff --git a/include/rbtree_defs.h b/include/rbtree_defs.h index ab5ef4ea..00395bb4 100644 --- a/include/rbtree_defs.h +++ b/include/rbtree_defs.h @@ -15,8 +15,8 @@ * http://en.literateprograms.org/Red-black_tree_(C) ******************************************************************************/ -#ifndef __RBTREE_DEFS_H__ -#define __RBTREE_DEFS_H__ +#ifndef RBTREE_DEFS_H__ +#define RBTREE_DEFS_H__ #include #include diff --git a/include/relabel_to_front.h b/include/relabel_to_front.h index c8a4d950..8e1ecf37 100644 --- a/include/relabel_to_front.h +++ b/include/relabel_to_front.h @@ -9,8 +9,8 @@ * * */ -#ifndef __RELABEL_TO_FRONT_H__ -#define __RELABEL_TO_FRONT_H__ +#ifndef RELABEL_TO_FRONT_H__ +#define RELABEL_TO_FRONT_H__ #include #include diff --git a/include/scc.h b/include/scc.h index 1d641a1b..5e4b9f44 100644 --- a/include/scc.h +++ b/include/scc.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Strongly_connected_component ******************************************************************************/ -#ifndef __SCC_H__ -#define __SCC_H__ +#ifndef SCC_H__ +#define SCC_H__ #include #include #include diff --git a/include/selection_sort.h b/include/selection_sort.h index a956ad9a..b08f9d96 100644 --- a/include/selection_sort.h +++ b/include/selection_sort.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Selection_sort ******************************************************************************/ -#ifndef __SELECTION_SORT_H__ -#define __SELECTION_SORT_H__ +#ifndef SELECTION_SORT_H__ +#define SELECTION_SORT_H__ #include #include @@ -49,4 +49,4 @@ namespace alg { } } -#endif //__SELECTION_SORT_H__ +#endif //SELECTION_SORT_H__ diff --git a/include/sha1.h b/include/sha1.h index 3079e20c..d68c0a4a 100644 --- a/include/sha1.h +++ b/include/sha1.h @@ -37,8 +37,8 @@ * http://en.wikipedia.org/wiki/SHA-1 */ -#ifndef __SHA1_H__ -#define __SHA1_H__ +#ifndef SHA1_H__ +#define SHA1_H__ #include diff --git a/include/shell_sort.h b/include/shell_sort.h index 22b04de9..c6a8c717 100644 --- a/include/shell_sort.h +++ b/include/shell_sort.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef __SHELL_SORT_H__ -#define __SHELL_SORT_H__ +#ifndef SHELL_SORT_H__ +#define SHELL_SORT_H__ namespace alg { /** diff --git a/include/shuffle.h b/include/shuffle.h index fe8b97ab..16f5deab 100644 --- a/include/shuffle.h +++ b/include/shuffle.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __SHUFFLE_H__ -#define __SHUFFLE_H__ +#ifndef SHUFFLE_H__ +#define SHUFFLE_H__ #include #include diff --git a/include/simhash.h b/include/simhash.h index f68e9e58..d98b87c0 100644 --- a/include/simhash.h +++ b/include/simhash.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef __SIMHASH_H__ -#define __SIMHASH_H__ +#ifndef SIMHASH_H__ +#define SIMHASH_H__ #include #include diff --git a/include/skiplist.h b/include/skiplist.h index f45d4f33..e34d8f04 100644 --- a/include/skiplist.h +++ b/include/skiplist.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef __SKIP_LIST_H__ -#define __SKIP_LIST_H__ +#ifndef SKIP_LIST_H__ +#define SKIP_LIST_H__ #include #include #include diff --git a/include/sol.h b/include/sol.h index 070ee4c5..a2e148b5 100644 --- a/include/sol.h +++ b/include/sol.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef __SOL_H__ -#define __SOL_H__ +#ifndef SOL_H__ +#define SOL_H__ #include "double_linked_list.h" namespace alg { diff --git a/include/stack.h b/include/stack.h index acc75414..8b664f5e 100644 --- a/include/stack.h +++ b/include/stack.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __STACK_H__ -#define __STACK_H__ +#ifndef STACK_H__ +#define STACK_H__ #include #include diff --git a/include/suffix_array.h b/include/suffix_array.h index c078bd0a..a47fe2fc 100644 --- a/include/suffix_array.h +++ b/include/suffix_array.h @@ -20,8 +20,8 @@ * AUTHOR: nowerzt@gmail.com ******************************************************************************/ -#ifndef __SUFFIX_ARRAY_H__ -#define __SUFFIX_ARRAY_H__ +#ifndef SUFFIX_ARRAY_H__ +#define SUFFIX_ARRAY_H__ #include #include @@ -100,4 +100,4 @@ namespace alg { } } -#endif // __SUFFIX_ARRAY_H__ +#endif // SUFFIX_ARRAY_H__ diff --git a/include/trie.h b/include/trie.h index c9990f28..ac253704 100644 --- a/include/trie.h +++ b/include/trie.h @@ -10,8 +10,8 @@ * http://en.wikipedia.org/wiki/Trie ******************************************************************************/ -#ifndef __TRIE_H__ -#define __TRIE_H__ +#ifndef TRIE_H__ +#define TRIE_H__ #include #include #include diff --git a/include/undirected_graph.h b/include/undirected_graph.h index 5570267c..c1e4cd08 100644 --- a/include/undirected_graph.h +++ b/include/undirected_graph.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef __UNDIRECTED_GRAPH_H__ -#define __UNDIRECTED_GRAPH_H__ +#ifndef UNDIRECTED_GRAPH_H__ +#define UNDIRECTED_GRAPH_H__ #include #include diff --git a/include/universal_hash.h b/include/universal_hash.h index 1b162857..1a8abe20 100644 --- a/include/universal_hash.h +++ b/include/universal_hash.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef __UNIVERSAL_HASH_H__ -#define __UNIVERSAL_HASH_H__ +#ifndef UNIVERSAL_HASH_H__ +#define UNIVERSAL_HASH_H__ #include #include diff --git a/include/word_seg.h b/include/word_seg.h index eb0f65c7..00528827 100644 --- a/include/word_seg.h +++ b/include/word_seg.h @@ -17,8 +17,8 @@ * ******************************************************************************/ -#ifndef __WORD_SEG_H__ -#define __WORD_SEG_H__ +#ifndef WORD_SEG_H__ +#define WORD_SEG_H__ #include #include diff --git a/msvc/alg_vs.h b/msvc/alg_vs.h index 13b5aa67..78e56b88 100644 --- a/msvc/alg_vs.h +++ b/msvc/alg_vs.h @@ -1,5 +1,5 @@ -#ifndef __ALGVS_H__ -#define __ALGVS_H__ +#ifndef ALGVS_H__ +#define ALGVS_H__ #ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS @@ -15,4 +15,4 @@ #define typeof decltype #endif//_MSC_VER -#endif//__ALGVS_H__ \ No newline at end of file +#endif//ALGVS_H__ \ No newline at end of file diff --git a/utils/byteorder.h b/utils/byteorder.h index ca2fada9..1cfcaca9 100644 --- a/utils/byteorder.h +++ b/utils/byteorder.h @@ -1,5 +1,5 @@ -#ifndef __BYTEORDER_H__ -#define __BYTEORDER_H__ +#ifndef BYTEORDER_H__ +#define BYTEORDER_H__ #include #include diff --git a/utils/gb18030.h b/utils/gb18030.h index 88d28e85..fdc2803d 100644 --- a/utils/gb18030.h +++ b/utils/gb18030.h @@ -1,5 +1,5 @@ -#ifndef __GB18030_H__ -#define __GB18030_H__ +#ifndef GB18030_H__ +#define GB18030_H__ /** * Read from the string encoded in GB18030 into WORD From b8b2ed5b72c9e8ab81d99c0e57999d7900f2a92e Mon Sep 17 00:00:00 2001 From: Vic Luo Date: Thu, 1 Sep 2016 14:14:36 +0800 Subject: [PATCH 41/53] Rename all identifiers with 2 underscores at the beginning --- include/2darray.h | 4 ++-- include/8queen.h | 6 +++--- include/astar.h | 4 ++-- include/avl.h | 4 ++-- include/bellman_ford.h | 4 ++-- include/binary_search_tree.h | 12 ++++++------ include/bitset.h | 4 ++-- include/bloom_filter.h | 4 ++-- include/btree.h | 4 ++-- include/dijkstra.h | 4 ++-- include/directed_graph.h | 4 ++-- include/disjoint-set.h | 4 ++-- include/dos_tree.h | 4 ++-- include/double_linked_list.h | 26 +++++++++++++------------- include/edmonds_karp.h | 4 ++-- include/fenwick_tree.h | 4 ++-- include/fib-heap.h | 4 ++-- include/generic.h | 4 ++-- include/graph_defs.h | 4 ++-- include/graph_search.h | 4 ++-- include/hash_code.h | 4 ++-- include/hash_multi.h | 4 ++-- include/hash_string.h | 4 ++-- include/hash_table.h | 4 ++-- include/heap.h | 4 ++-- include/huffman.h | 4 ++-- include/imath.h | 4 ++-- include/insertion_sort.h | 4 ++-- include/integer.h | 4 ++-- include/interval_tree.h | 4 ++-- include/k-means.h | 4 ++-- include/kmp.h | 4 ++-- include/kruskal_mst.h | 4 ++-- include/lcs.h | 4 ++-- include/max_subarray.h | 4 ++-- include/md5.h | 4 ++-- include/merge_sort.h | 8 ++++---- include/perfect_hash.h | 4 ++-- include/prim_mst.h | 4 ++-- include/prime.h | 4 ++-- include/priority_queue.h | 6 +++--- include/queue.h | 4 ++-- include/quick_sort.h | 8 ++++---- include/radix_sort.h | 14 +++++++------- include/random.h | 4 ++-- include/random_select.h | 8 ++++---- include/rbtree.h | 4 ++-- include/rbtree_defs.h | 4 ++-- include/relabel_to_front.h | 4 ++-- include/scc.h | 4 ++-- include/selection_sort.h | 6 +++--- include/sha1.h | 4 ++-- include/shell_sort.h | 4 ++-- include/shuffle.h | 4 ++-- include/simhash.h | 4 ++-- include/skiplist.h | 4 ++-- include/sol.h | 12 ++++++------ include/stack.h | 4 ++-- include/suffix_array.h | 6 +++--- include/trie.h | 4 ++-- include/undirected_graph.h | 4 ++-- include/universal_hash.h | 4 ++-- include/word_seg.h | 4 ++-- msvc/alg_vs.h | 6 +++--- src/lcs_demo.cpp | 6 +++--- utils/byteorder.h | 4 ++-- utils/gb18030.h | 4 ++-- 67 files changed, 170 insertions(+), 170 deletions(-) diff --git a/include/2darray.h b/include/2darray.h index 929a286a..2a565810 100644 --- a/include/2darray.h +++ b/include/2darray.h @@ -10,8 +10,8 @@ * Simulated by 1-dimension array. ******************************************************************************/ -#ifndef 2D_ARRAY_H__ -#define 2D_ARRAY_H__ +#ifndef ALGO_2D_ARRAY_H__ +#define ALGO_2D_ARRAY_H__ #include #include diff --git a/include/8queen.h b/include/8queen.h index 17eaefef..511ebe21 100644 --- a/include/8queen.h +++ b/include/8queen.h @@ -9,8 +9,8 @@ * http://en.wikipedia.org/wiki/Eight_queens_puzzle ******************************************************************************/ -#ifndef 8QUEEN_H__ -#define 8QUEEN_H__ +#ifndef ALGO_8QUEEN_H__ +#define ALGO_8QUEEN_H__ #include #include @@ -84,4 +84,4 @@ namespace alg { }; } -#endif //8QUEEN_H__ +#endif //ALGO_8QUEEN_H__ diff --git a/include/astar.h b/include/astar.h index 6dc68f02..a821488d 100644 --- a/include/astar.h +++ b/include/astar.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef ASTAR_H__ -#define ASTAR_H__ +#ifndef ALGO_ASTAR_H__ +#define ALGO_ASTAR_H__ #include #include diff --git a/include/avl.h b/include/avl.h index aa1ef6ca..697317ab 100644 --- a/include/avl.h +++ b/include/avl.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef AVL_H__ -#define AVL_H__ +#ifndef ALGO_AVL_H__ +#define ALGO_AVL_H__ #include #include diff --git a/include/bellman_ford.h b/include/bellman_ford.h index d859d72a..40771ae4 100644 --- a/include/bellman_ford.h +++ b/include/bellman_ford.h @@ -44,8 +44,8 @@ * ******************************************************************************/ -#ifndef BELLMAN_FORD_H__ -#define BELLMAN_FORD_H__ +#ifndef ALGO_BELLMAN_FORD_H__ +#define ALGO_BELLMAN_FORD_H__ #include #include diff --git a/include/binary_search_tree.h b/include/binary_search_tree.h index 254cb0f2..574914af 100644 --- a/include/binary_search_tree.h +++ b/include/binary_search_tree.h @@ -18,8 +18,8 @@ * ******************************************************************************/ -#ifndef BINARY_SEARCH_TREE_H__ -#define BINARY_SEARCH_TREE_H__ +#ifndef ALGO_BINARY_SEARCH_TREE_H__ +#define ALGO_BINARY_SEARCH_TREE_H__ #include #include @@ -57,7 +57,7 @@ namespace alg { BST():m_root(NULL){}; ~BST() { - __destruct(m_root); + destruct_(m_root); } /** @@ -159,10 +159,10 @@ namespace alg { } private: - void __destruct(treeNode *n) { + void destruct_(treeNode *n) { if (n==NULL) return; - __destruct(n->left); - __destruct(n->right); + destruct_(n->left); + destruct_(n->right); delete n; } diff --git a/include/bitset.h b/include/bitset.h index fd6ae57b..9e7b9bbc 100644 --- a/include/bitset.h +++ b/include/bitset.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef BIT_SET_H__ -#define BIT_SET_H__ +#ifndef ALGO_BIT_SET_H__ +#define ALGO_BIT_SET_H__ #include #include diff --git a/include/bloom_filter.h b/include/bloom_filter.h index 952d44ab..08253593 100644 --- a/include/bloom_filter.h +++ b/include/bloom_filter.h @@ -18,8 +18,8 @@ * ******************************************************************************/ -#ifndef BLOOM_FILTER_H__ -#define BLOOM_FILTER_H__ +#ifndef ALGO_BLOOM_FILTER_H__ +#define ALGO_BLOOM_FILTER_H__ #include #include diff --git a/include/btree.h b/include/btree.h index 5f50b6e2..6e17050f 100644 --- a/include/btree.h +++ b/include/btree.h @@ -24,8 +24,8 @@ * http://en.wikipedia.org/wiki/B-tree ******************************************************************************/ -#ifndef BTREE_H__ -#define BTREE_H__ +#ifndef ALGO_BTREE_H__ +#define ALGO_BTREE_H__ #include #include diff --git a/include/dijkstra.h b/include/dijkstra.h index 73b6c2ee..853376cc 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -19,8 +19,8 @@ * ******************************************************************************/ -#ifndef DIJKSTRA_H__ -#define DIJKSTRA_H__ +#ifndef ALGO_DIJKSTRA_H__ +#define ALGO_DIJKSTRA_H__ #include #include diff --git a/include/directed_graph.h b/include/directed_graph.h index 7694e5a3..9e46cfe0 100644 --- a/include/directed_graph.h +++ b/include/directed_graph.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef DIRECTED_GRAPH_H__ -#define DIRECTED_GRAPH_H__ +#ifndef ALGO_DIRECTED_GRAPH_H__ +#define ALGO_DIRECTED_GRAPH_H__ #include #include diff --git a/include/disjoint-set.h b/include/disjoint-set.h index 1d1568c4..be6bff7d 100644 --- a/include/disjoint-set.h +++ b/include/disjoint-set.h @@ -18,8 +18,8 @@ * http://en.wikipedia.org/wiki/Disjoint-set_data_structure ******************************************************************************/ -#ifndef DISJOINTSET_H__ -#define DISJOINTSET_H__ +#ifndef ALGO_DISJOINTSET_H__ +#define ALGO_DISJOINTSET_H__ namespace alg { template diff --git a/include/dos_tree.h b/include/dos_tree.h index f754f180..42689457 100644 --- a/include/dos_tree.h +++ b/include/dos_tree.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef DOS_TREE_H__ -#define DOS_TREE_H__ +#ifndef ALGO_DOS_TREE_H__ +#define ALGO_DOS_TREE_H__ #include #include diff --git a/include/double_linked_list.h b/include/double_linked_list.h index be595e7e..678c66b8 100644 --- a/include/double_linked_list.h +++ b/include/double_linked_list.h @@ -16,8 +16,8 @@ * http://en.wikipedia.org/wiki/Double_linked_list ******************************************************************************/ -#ifndef DOUBLE_LINKED_LIST_H__ -#define DOUBLE_LINKED_LIST_H__ +#ifndef ALGO_DOUBLE_LINKED_LIST_H__ +#define ALGO_DOUBLE_LINKED_LIST_H__ struct list_head { struct list_head *next, *prev; @@ -39,7 +39,7 @@ struct list_head { * the prev/next entries already! */ static inline void -__list_add(struct list_head *n, +list_add_(struct list_head *n, struct list_head *prev, struct list_head *next) { @@ -57,14 +57,14 @@ __list_add(struct list_head *n, * the prev/next entries already! */ static inline void -__list_del(struct list_head *prev, struct list_head *next) +list_del_(struct list_head *prev, struct list_head *next) { next->prev = prev; prev->next = next; } static inline void -__list_splice(struct list_head *list, struct list_head *head) +list_splice_(struct list_head *list, struct list_head *head) { struct list_head *first = list->next; struct list_head *last = list->prev; @@ -88,7 +88,7 @@ __list_splice(struct list_head *list, struct list_head *head) static inline void list_add(struct list_head *n, struct list_head *head) { - __list_add(n, head, head->next); + list_add_(n, head, head->next); } /** @@ -102,7 +102,7 @@ list_add(struct list_head *n, struct list_head *head) static inline void list_add_tail(struct list_head *n, struct list_head *head) { - __list_add(n, head->prev, head); + list_add_(n, head->prev, head); } /** @@ -113,7 +113,7 @@ list_add_tail(struct list_head *n, struct list_head *head) static inline void list_del(struct list_head *entry) { - __list_del(entry->prev, entry->next); + list_del_(entry->prev, entry->next); entry->next = NULL; entry->prev = NULL; } @@ -125,7 +125,7 @@ list_del(struct list_head *entry) static inline void list_del_init(struct list_head *entry) { - __list_del(entry->prev, entry->next); + list_del_(entry->prev, entry->next); INIT_LIST_HEAD(entry); } @@ -137,7 +137,7 @@ list_del_init(struct list_head *entry) static inline void list_move(struct list_head *list, struct list_head *head) { - __list_del(list->prev, list->next); + list_del_(list->prev, list->next); list_add(list, head); } @@ -149,7 +149,7 @@ list_move(struct list_head *list, struct list_head *head) static inline void list_move_tail(struct list_head *list, struct list_head *head) { - __list_del(list->prev, list->next); + list_del_(list->prev, list->next); list_add_tail(list, head); } @@ -172,7 +172,7 @@ static inline void list_splice(struct list_head *list, struct list_head *head) { if (!list_empty(list)) - __list_splice(list, head); + list_splice_(list, head); } /** @@ -186,7 +186,7 @@ static inline void list_splice_init(struct list_head *list, struct list_head *head) { if (!list_empty(list)) { - __list_splice(list, head); + list_splice_(list, head); INIT_LIST_HEAD(list); } } diff --git a/include/edmonds_karp.h b/include/edmonds_karp.h index f9ca4b56..295de725 100644 --- a/include/edmonds_karp.h +++ b/include/edmonds_karp.h @@ -21,8 +21,8 @@ * ******************************************************************************/ -#ifndef EDMONDS_KARP_H__ -#define EDMONDS_KARP_H__ +#ifndef ALGO_EDMONDS_KARP_H__ +#define ALGO_EDMONDS_KARP_H__ #include #include diff --git a/include/fenwick_tree.h b/include/fenwick_tree.h index cc12d602..4806536e 100644 --- a/include/fenwick_tree.h +++ b/include/fenwick_tree.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef FENWICK_H__ -#define FENWICK_H__ +#ifndef ALGO_FENWICK_H__ +#define ALGO_FENWICK_H__ #include diff --git a/include/fib-heap.h b/include/fib-heap.h index b5303bf6..44af130a 100644 --- a/include/fib-heap.h +++ b/include/fib-heap.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Fibonacci_heap ******************************************************************************/ -#ifndef FIB_HEAP_H__ -#define FIB_HEAP_H__ +#ifndef ALGO_FIB_HEAP_H__ +#define ALGO_FIB_HEAP_H__ #include #include #include diff --git a/include/generic.h b/include/generic.h index 16d50e84..785e5078 100644 --- a/include/generic.h +++ b/include/generic.h @@ -9,8 +9,8 @@ * ******************************************************************************/ -#ifndef ALG_INC_H__ -#define ALG_INC_H__ +#ifndef ALGO_ALG_INC_H__ +#define ALGO_ALG_INC_H__ #include #include #include diff --git a/include/graph_defs.h b/include/graph_defs.h index f2b61ada..06e77be2 100644 --- a/include/graph_defs.h +++ b/include/graph_defs.h @@ -1,5 +1,5 @@ -#ifndef GRAPH_DEFS_H__ -#define GRAPH_DEFS_H__ +#ifndef ALGO_GRAPH_DEFS_H__ +#define ALGO_GRAPH_DEFS_H__ #include "double_linked_list.h" diff --git a/include/graph_search.h b/include/graph_search.h index 7605aed6..e0d18f4d 100644 --- a/include/graph_search.h +++ b/include/graph_search.h @@ -20,8 +20,8 @@ * ******************************************************************************/ -#ifndef BREADTH_FIRST_SEARCH_H__ -#define BREADTH_FIRST_SEARCH_H__ +#ifndef ALGO_BREADTH_FIRST_SEARCH_H__ +#define ALGO_BREADTH_FIRST_SEARCH_H__ #include #include diff --git a/include/hash_code.h b/include/hash_code.h index 7773a108..a310580c 100644 --- a/include/hash_code.h +++ b/include/hash_code.h @@ -1,5 +1,5 @@ -#ifndef HASH_CODE_H__ -#define HASH_CODE_H__ +#ifndef ALGO_HASH_CODE_H__ +#define ALGO_HASH_CODE_H__ #include #include "hash_string.h" namespace alg { diff --git a/include/hash_multi.h b/include/hash_multi.h index 73040cef..ec22f65f 100644 --- a/include/hash_multi.h +++ b/include/hash_multi.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef HASH_MULTIPLICATION_H__ -#define HASH_MULTIPLICATION_H__ +#ifndef ALGO_HASH_MULTIPLICATION_H__ +#define ALGO_HASH_MULTIPLICATION_H__ #include #include diff --git a/include/hash_string.h b/include/hash_string.h index 9a95d4fa..6eee926c 100644 --- a/include/hash_string.h +++ b/include/hash_string.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef STRING_HASH_H__ -#define STRING_HASH_H__ +#ifndef ALGO_STRING_HASH_H__ +#define ALGO_STRING_HASH_H__ #include diff --git a/include/hash_table.h b/include/hash_table.h index 9ed41eb3..72018610 100644 --- a/include/hash_table.h +++ b/include/hash_table.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef HASH_TABLE_H__ -#define HASH_TABLE_H__ +#ifndef ALGO_HASH_TABLE_H__ +#define ALGO_HASH_TABLE_H__ #include #include diff --git a/include/heap.h b/include/heap.h index dec4f9bc..d793cdab 100644 --- a/include/heap.h +++ b/include/heap.h @@ -28,8 +28,8 @@ * http://en.wikipedia.org/wiki/Binary_heap ******************************************************************************/ -#ifndef HEAP_H__ -#define HEAP_H__ +#ifndef ALGO_HEAP_H__ +#define ALGO_HEAP_H__ #include #include diff --git a/include/huffman.h b/include/huffman.h index c95fc9b5..50088b53 100644 --- a/include/huffman.h +++ b/include/huffman.h @@ -21,8 +21,8 @@ * ******************************************************************************/ -#ifndef HUFFMAN_CODING_H__ -#define HUFFMAN_CODING_H__ +#ifndef ALGO_HUFFMAN_CODING_H__ +#define ALGO_HUFFMAN_CODING_H__ #include #include diff --git a/include/imath.h b/include/imath.h index 00e1e472..b3fa2430 100644 --- a/include/imath.h +++ b/include/imath.h @@ -9,8 +9,8 @@ * ******************************************************************************/ -#ifndef IMATH_H__ -#define IMATH_H__ +#ifndef ALGO_IMATH_H__ +#define ALGO_IMATH_H__ #include #include diff --git a/include/insertion_sort.h b/include/insertion_sort.h index 8fcf6f42..eb9f58ef 100644 --- a/include/insertion_sort.h +++ b/include/insertion_sort.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef INSERTION_SORT_H__ -#define INSERTION_SORT_H__ +#ifndef ALGO_INSERTION_SORT_H__ +#define ALGO_INSERTION_SORT_H__ namespace alg { /** diff --git a/include/integer.h b/include/integer.h index 3c8e7d1b..1d5ff609 100644 --- a/include/integer.h +++ b/include/integer.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef INTEGER_H__ -#define INTEGER_H__ +#ifndef ALGO_INTEGER_H__ +#define ALGO_INTEGER_H__ #include #include diff --git a/include/interval_tree.h b/include/interval_tree.h index 5b6848e2..ec107364 100644 --- a/include/interval_tree.h +++ b/include/interval_tree.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef INTERVAL_TREE_H__ -#define INTERVAL_TREE_H__ +#ifndef ALGO_INTERVAL_TREE_H__ +#define ALGO_INTERVAL_TREE_H__ #include #include diff --git a/include/k-means.h b/include/k-means.h index 966c085e..86fdef3c 100644 --- a/include/k-means.h +++ b/include/k-means.h @@ -12,8 +12,8 @@ * https://github.com/wycg1984 ******************************************************************************/ -#ifndef KMEANS_H__ -#define KMEANS_H__ +#ifndef ALGO_KMEANS_H__ +#define ALGO_KMEANS_H__ #include #include #include diff --git a/include/kmp.h b/include/kmp.h index acb38297..d75d207b 100644 --- a/include/kmp.h +++ b/include/kmp.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef KMP_H__ -#define KMP_H__ +#ifndef ALGO_KMP_H__ +#define ALGO_KMP_H__ #include namespace alg { diff --git a/include/kruskal_mst.h b/include/kruskal_mst.h index b4f8078e..6db3458f 100644 --- a/include/kruskal_mst.h +++ b/include/kruskal_mst.h @@ -25,8 +25,8 @@ * By Contibutor:xmuliang ******************************************************************************/ -#ifndef KRUSKAL_MST_H__ -#define KRUSKAL_MST_H__ +#ifndef ALGO_KRUSKAL_MST_H__ +#define ALGO_KRUSKAL_MST_H__ #include #include diff --git a/include/lcs.h b/include/lcs.h index 93d6fa5f..ce218022 100644 --- a/include/lcs.h +++ b/include/lcs.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef LCS_H__ -#define LCS_H__ +#ifndef ALGO_LCS_H__ +#define ALGO_LCS_H__ #include "generic.h" #include "2darray.h" diff --git a/include/max_subarray.h b/include/max_subarray.h index c791df3b..96b0d943 100644 --- a/include/max_subarray.h +++ b/include/max_subarray.h @@ -21,8 +21,8 @@ * http://en.wikipedia.org/wiki/Maximum_subarray_problem ******************************************************************************/ -#ifndef __MAX_SUBARRAY__ -#define __MAX_SUBARRAY__ +#ifndef MAX_SUBARRAY__ +#define MAX_SUBARRAY__ namespace alg { /** diff --git a/include/md5.h b/include/md5.h index 3d8c1410..284d8c70 100644 --- a/include/md5.h +++ b/include/md5.h @@ -23,8 +23,8 @@ */ -#ifndef MD5_H__ -#define MD5_H__ +#ifndef ALGO_MD5_H__ +#define ALGO_MD5_H__ #include /* Data structure for MD5 (Message Digest) computation */ diff --git a/include/merge_sort.h b/include/merge_sort.h index 519c19a2..b50d1976 100644 --- a/include/merge_sort.h +++ b/include/merge_sort.h @@ -32,15 +32,15 @@ * ******************************************************************************/ -#ifndef MERGE_SORT_H__ -#define MERGE_SORT_H__ +#ifndef ALGO_MERGE_SORT_H__ +#define ALGO_MERGE_SORT_H__ namespace alg { /** * Merge functions merges the two sorted parts. Sorted parts will be from [left, mid] and [mid+1, right]. */ template - static void __merge(T *array, int left, int mid, int right) { + static void merge_(T *array, int left, int mid, int right) { /*We need a Temporary array to store the new sorted part*/ T tempArray[right-left+1]; int pos=0,lpos = left,rpos = mid + 1; @@ -75,7 +75,7 @@ namespace alg { /* Sort the right part */ merge_sort(array,mid+1,right); /* Merge the two sorted parts */ - __merge(array,left,mid,right); + merge_(array,left,mid,right); } } diff --git a/include/perfect_hash.h b/include/perfect_hash.h index 28b23191..55af66c1 100644 --- a/include/perfect_hash.h +++ b/include/perfect_hash.h @@ -10,8 +10,8 @@ * http://en.wikipedia.org/wiki/Perfect_hash * ******************************************************************************/ -#ifndef PERFECT_HASH_H__ -#define PERFECT_HASH_H__ +#ifndef ALGO_PERFECT_HASH_H__ +#define ALGO_PERFECT_HASH_H__ #include #include #include diff --git a/include/prim_mst.h b/include/prim_mst.h index 97d8e0a7..be48ab58 100644 --- a/include/prim_mst.h +++ b/include/prim_mst.h @@ -22,8 +22,8 @@ * ******************************************************************************/ -#ifndef PRIM_MST_H__ -#define PRIM_MST_H__ +#ifndef ALGO_PRIM_MST_H__ +#define ALGO_PRIM_MST_H__ #include #include diff --git a/include/prime.h b/include/prime.h index 593fb81a..357f8b96 100644 --- a/include/prime.h +++ b/include/prime.h @@ -11,8 +11,8 @@ * http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test * ******************************************************************************/ -#ifndef PRIME_H__ -#define PRIME_H__ +#ifndef ALGO_PRIME_H__ +#define ALGO_PRIME_H__ #include #include diff --git a/include/priority_queue.h b/include/priority_queue.h index 10214a83..8795bc08 100644 --- a/include/priority_queue.h +++ b/include/priority_queue.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef PRIORITY_QUEUE_H__ -#define PRIORITY_QUEUE_H__ +#ifndef ALGO_PRIORITY_QUEUE_H__ +#define ALGO_PRIORITY_QUEUE_H__ #include #include @@ -80,7 +80,7 @@ namespace alg { bool found = false; list_for_each_entry(pos, &m_head, node) { if (n->priority <= pos->priority) { - __list_add(&n->node, pos->node.prev, &pos->node); + list_add_(&n->node, pos->node.prev, &pos->node); m_count++; found = true; break; diff --git a/include/queue.h b/include/queue.h index 7e322aec..55051228 100644 --- a/include/queue.h +++ b/include/queue.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef QUEUE_H__ -#define QUEUE_H__ +#ifndef ALGO_QUEUE_H__ +#define ALGO_QUEUE_H__ #include #include diff --git a/include/quick_sort.h b/include/quick_sort.h index 936d0860..b3a74489 100644 --- a/include/quick_sort.h +++ b/include/quick_sort.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef QUICKSORT_H__ -#define QUICKSORT_H__ +#ifndef ALGO_QUICKSORT_H__ +#define ALGO_QUICKSORT_H__ #include @@ -25,7 +25,7 @@ namespace alg { * the quick-sort partition routine */ template - static int __partition(T list[],int begin, int end) { + static int partition_(T list[],int begin, int end) { int pivot_idx = RANDOM(begin,end); T pivot = list[pivot_idx]; swap(list[begin], list[pivot_idx]); @@ -52,7 +52,7 @@ namespace alg { template static void quicksort(T list[],int begin,int end) { if( begin < end) { - int pivot_idx = __partition(list, begin, end); + int pivot_idx = partition_(list, begin, end); quicksort(list, begin, pivot_idx-1); quicksort(list, pivot_idx+1, end); } diff --git a/include/radix_sort.h b/include/radix_sort.h index 16298355..ec5e0733 100644 --- a/include/radix_sort.h +++ b/include/radix_sort.h @@ -15,8 +15,8 @@ * ******************************************************************************/ -#ifndef RADIX_SORT_H__ -#define RADIX_SORT_H__ +#ifndef ALGO_RADIX_SORT_H__ +#define ALGO_RADIX_SORT_H__ #include #include @@ -29,7 +29,7 @@ namespace alg { /** * couting sort */ - static void __radix(int byte, const unsigned N, const uint32_t *source, uint32_t *dest) { + static void radix_(int byte, const unsigned N, const uint32_t *source, uint32_t *dest) { unsigned count[256]; unsigned index[256]; memset(count, 0, sizeof (count)); @@ -51,10 +51,10 @@ namespace alg { */ static void radix_sort(uint32_t *source, const unsigned N) { uint32_t * temp = new uint32_t[N]; - __radix(0, N, source, temp); - __radix(1, N, temp, source); - __radix(2, N, source, temp); - __radix(3, N, temp, source); + radix_(0, N, source, temp); + radix_(1, N, temp, source); + radix_(2, N, source, temp); + radix_(3, N, temp, source); delete [] temp; } diff --git a/include/random.h b/include/random.h index 2ae56aa3..1b2fd82e 100644 --- a/include/random.h +++ b/include/random.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef RANDOM_H__ -#define RANDOM_H__ +#ifndef ALGO_RANDOM_H__ +#define ALGO_RANDOM_H__ #include #include diff --git a/include/random_select.h b/include/random_select.h index 500d0c9a..8893008d 100644 --- a/include/random_select.h +++ b/include/random_select.h @@ -17,8 +17,8 @@ * ******************************************************************************/ -#ifndef RANDOM_SELECT_H__ -#define RANDOM_SELECT_H__ +#ifndef ALGO_RANDOM_SELECT_H__ +#define ALGO_RANDOM_SELECT_H__ #include @@ -27,7 +27,7 @@ namespace alg { * the random_select partition routine */ template - static int __partition(T list[],int begin, int end) { + static int partition_(T list[],int begin, int end) { int pivot_idx = RANDOM(begin,end); T pivot = list[pivot_idx]; swap(list[begin],list[pivot_idx]); @@ -56,7 +56,7 @@ namespace alg { if(begin == end) return begin; - int pivot_idx = __partition(list, begin, end); + int pivot_idx = partition_(list, begin, end); int human_idx = pivot_idx - begin + 1; if(k < human_idx) diff --git a/include/rbtree.h b/include/rbtree.h index bace1061..61c129bd 100644 --- a/include/rbtree.h +++ b/include/rbtree.h @@ -15,8 +15,8 @@ * http://en.literateprograms.org/Red-black_tree_(C) ******************************************************************************/ -#ifndef RBTREE_H__ -#define RBTREE_H__ +#ifndef ALGO_RBTREE_H__ +#define ALGO_RBTREE_H__ #include #include #include diff --git a/include/rbtree_defs.h b/include/rbtree_defs.h index 00395bb4..a99a26b0 100644 --- a/include/rbtree_defs.h +++ b/include/rbtree_defs.h @@ -15,8 +15,8 @@ * http://en.literateprograms.org/Red-black_tree_(C) ******************************************************************************/ -#ifndef RBTREE_DEFS_H__ -#define RBTREE_DEFS_H__ +#ifndef ALGO_RBTREE_DEFS_H__ +#define ALGO_RBTREE_DEFS_H__ #include #include diff --git a/include/relabel_to_front.h b/include/relabel_to_front.h index 8e1ecf37..ef933e02 100644 --- a/include/relabel_to_front.h +++ b/include/relabel_to_front.h @@ -9,8 +9,8 @@ * * */ -#ifndef RELABEL_TO_FRONT_H__ -#define RELABEL_TO_FRONT_H__ +#ifndef ALGO_RELABEL_TO_FRONT_H__ +#define ALGO_RELABEL_TO_FRONT_H__ #include #include diff --git a/include/scc.h b/include/scc.h index 5e4b9f44..ef41af12 100644 --- a/include/scc.h +++ b/include/scc.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Strongly_connected_component ******************************************************************************/ -#ifndef SCC_H__ -#define SCC_H__ +#ifndef ALGO_SCC_H__ +#define ALGO_SCC_H__ #include #include #include diff --git a/include/selection_sort.h b/include/selection_sort.h index b08f9d96..7b0f53e3 100644 --- a/include/selection_sort.h +++ b/include/selection_sort.h @@ -17,8 +17,8 @@ * http://en.wikipedia.org/wiki/Selection_sort ******************************************************************************/ -#ifndef SELECTION_SORT_H__ -#define SELECTION_SORT_H__ +#ifndef ALGO_SELECTION_SORT_H__ +#define ALGO_SELECTION_SORT_H__ #include #include @@ -49,4 +49,4 @@ namespace alg { } } -#endif //SELECTION_SORT_H__ +#endif //ALGO_SELECTION_SORT_H__ diff --git a/include/sha1.h b/include/sha1.h index d68c0a4a..da529b94 100644 --- a/include/sha1.h +++ b/include/sha1.h @@ -37,8 +37,8 @@ * http://en.wikipedia.org/wiki/SHA-1 */ -#ifndef SHA1_H__ -#define SHA1_H__ +#ifndef ALGO_SHA1_H__ +#define ALGO_SHA1_H__ #include diff --git a/include/shell_sort.h b/include/shell_sort.h index c6a8c717..7d9285c2 100644 --- a/include/shell_sort.h +++ b/include/shell_sort.h @@ -13,8 +13,8 @@ * ******************************************************************************/ -#ifndef SHELL_SORT_H__ -#define SHELL_SORT_H__ +#ifndef ALGO_SHELL_SORT_H__ +#define ALGO_SHELL_SORT_H__ namespace alg { /** diff --git a/include/shuffle.h b/include/shuffle.h index 16f5deab..0bdd69cd 100644 --- a/include/shuffle.h +++ b/include/shuffle.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef SHUFFLE_H__ -#define SHUFFLE_H__ +#ifndef ALGO_SHUFFLE_H__ +#define ALGO_SHUFFLE_H__ #include #include diff --git a/include/simhash.h b/include/simhash.h index d98b87c0..650af6c2 100644 --- a/include/simhash.h +++ b/include/simhash.h @@ -12,8 +12,8 @@ * ******************************************************************************/ -#ifndef SIMHASH_H__ -#define SIMHASH_H__ +#ifndef ALGO_SIMHASH_H__ +#define ALGO_SIMHASH_H__ #include #include diff --git a/include/skiplist.h b/include/skiplist.h index e34d8f04..7469511d 100644 --- a/include/skiplist.h +++ b/include/skiplist.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef SKIP_LIST_H__ -#define SKIP_LIST_H__ +#ifndef ALGO_SKIP_LIST_H__ +#define ALGO_SKIP_LIST_H__ #include #include #include diff --git a/include/sol.h b/include/sol.h index a2e148b5..c8064fc8 100644 --- a/include/sol.h +++ b/include/sol.h @@ -16,8 +16,8 @@ * ******************************************************************************/ -#ifndef SOL_H__ -#define SOL_H__ +#ifndef ALGO_SOL_H__ +#define ALGO_SOL_H__ #include "double_linked_list.h" namespace alg { @@ -26,8 +26,8 @@ namespace alg { */ static inline void list_mtf(struct list_head *entry, struct list_head *head) { if (entry->prev == head) return; - __list_del(entry->prev, entry->next); - __list_add(entry, head, head->next); + list_del_(entry->prev, entry->next); + list_add_(entry, head, head->next); } @@ -38,8 +38,8 @@ namespace alg { // if the entry in the 1st position if (entry->prev == head) return; struct list_head * prev = entry->prev; - __list_del(entry->prev, entry->next); - __list_add(entry, prev->prev, prev); + list_del_(entry->prev, entry->next); + list_add_(entry, prev->prev, prev); } } #endif // diff --git a/include/stack.h b/include/stack.h index 8b664f5e..21ab1159 100644 --- a/include/stack.h +++ b/include/stack.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef STACK_H__ -#define STACK_H__ +#ifndef ALGO_STACK_H__ +#define ALGO_STACK_H__ #include #include diff --git a/include/suffix_array.h b/include/suffix_array.h index a47fe2fc..8c7e11fb 100644 --- a/include/suffix_array.h +++ b/include/suffix_array.h @@ -20,8 +20,8 @@ * AUTHOR: nowerzt@gmail.com ******************************************************************************/ -#ifndef SUFFIX_ARRAY_H__ -#define SUFFIX_ARRAY_H__ +#ifndef ALGO_SUFFIX_ARRAY_H__ +#define ALGO_SUFFIX_ARRAY_H__ #include #include @@ -100,4 +100,4 @@ namespace alg { } } -#endif // SUFFIX_ARRAY_H__ +#endif // ALGO_SUFFIX_ARRAY_H__ diff --git a/include/trie.h b/include/trie.h index ac253704..0cdba5ef 100644 --- a/include/trie.h +++ b/include/trie.h @@ -10,8 +10,8 @@ * http://en.wikipedia.org/wiki/Trie ******************************************************************************/ -#ifndef TRIE_H__ -#define TRIE_H__ +#ifndef ALGO_TRIE_H__ +#define ALGO_TRIE_H__ #include #include #include diff --git a/include/undirected_graph.h b/include/undirected_graph.h index c1e4cd08..51804de4 100644 --- a/include/undirected_graph.h +++ b/include/undirected_graph.h @@ -14,8 +14,8 @@ * ******************************************************************************/ -#ifndef UNDIRECTED_GRAPH_H__ -#define UNDIRECTED_GRAPH_H__ +#ifndef ALGO_UNDIRECTED_GRAPH_H__ +#define ALGO_UNDIRECTED_GRAPH_H__ #include #include diff --git a/include/universal_hash.h b/include/universal_hash.h index 1a8abe20..40c454ce 100644 --- a/include/universal_hash.h +++ b/include/universal_hash.h @@ -11,8 +11,8 @@ * ******************************************************************************/ -#ifndef UNIVERSAL_HASH_H__ -#define UNIVERSAL_HASH_H__ +#ifndef ALGO_UNIVERSAL_HASH_H__ +#define ALGO_UNIVERSAL_HASH_H__ #include #include diff --git a/include/word_seg.h b/include/word_seg.h index 00528827..ed99b763 100644 --- a/include/word_seg.h +++ b/include/word_seg.h @@ -17,8 +17,8 @@ * ******************************************************************************/ -#ifndef WORD_SEG_H__ -#define WORD_SEG_H__ +#ifndef ALGO_WORD_SEG_H__ +#define ALGO_WORD_SEG_H__ #include #include diff --git a/msvc/alg_vs.h b/msvc/alg_vs.h index 78e56b88..ca1d0163 100644 --- a/msvc/alg_vs.h +++ b/msvc/alg_vs.h @@ -1,5 +1,5 @@ -#ifndef ALGVS_H__ -#define ALGVS_H__ +#ifndef ALGO_ALGVS_H__ +#define ALGO_ALGVS_H__ #ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS @@ -15,4 +15,4 @@ #define typeof decltype #endif//_MSC_VER -#endif//ALGVS_H__ \ No newline at end of file +#endif//ALGO_ALGVS_H__ \ No newline at end of file diff --git a/src/lcs_demo.cpp b/src/lcs_demo.cpp index 838c5d4b..71c2db20 100644 --- a/src/lcs_demo.cpp +++ b/src/lcs_demo.cpp @@ -6,9 +6,9 @@ #define printlistC(list,n) \ do { \ - int __list_counter; \ - for(__list_counter=0;__list_counter #include diff --git a/utils/gb18030.h b/utils/gb18030.h index fdc2803d..5da8c9f3 100644 --- a/utils/gb18030.h +++ b/utils/gb18030.h @@ -1,5 +1,5 @@ -#ifndef GB18030_H__ -#define GB18030_H__ +#ifndef ALGO_GB18030_H__ +#define ALGO_GB18030_H__ /** * Read from the string encoded in GB18030 into WORD From 68ad0f22303aec9db74eb6b8dfccc4de2bcc697e Mon Sep 17 00:00:00 2001 From: xtaci Date: Sat, 22 Oct 2016 17:23:02 +0800 Subject: [PATCH 42/53] Update README.md --- README.md | 131 ++++++++++++++++++++++++++---------------------------- 1 file changed, 62 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index d9b7eb9f..dac3795d 100644 --- a/README.md +++ b/README.md @@ -26,75 +26,68 @@ ####已实现 ( Implemented ): - Array shuffle - Prime test(trial division) - Prime test(Miller-Rabin's method) - 2D Array - Arbitrary Integer - Linear congruential generator - Maximum subarray problem - - Bit-Set - Queue - Stack - Binary Heap - Fibonacci Heap - Priority Queue (list based) - - Bubble sort - Selection sort - Insertion sort - Shell sort - Radix sort - Quick sort - Merge sort - Double linked list - Skip list - Self-organized linked-list ops (move-to-front, move-ahead-one) - Largest common sequence - - Binary search tree - AVL tree - Dynamic order statistics - Red-black tree - Interval tree - Prefix Tree(Trie) - Suffix Tree - B-Tree - Suffix Array - - Hash by multiplication - Hash table - Universal hash function - Perfect hash - Java's string hash - FNV-1a string hash - SimHash - Bloom Filter - SHA-1 Message Digest Algorithm - MD5 - Base64 - - Graph data structure - Strongly Connected Components(SCC) - Prim's minimum spanning tree - Kruskal MST - Directed/Undirected graph ops - Breadth First Search - Depth First Search - Dijkstra's algorithm - Bellman-Ford algorithm - Edmonds-Karp Maximal Flow - Push–Relabel algorithm - - Huffman Coding - Word segementation(CHN/GB18030) using HMM and viterbi algorithm. - A* algorithm - K-Means - Knuth–Morris–Pratt algorithm - Disjoint-Set - 8-Queue Problem - Palindrome +| Name | File | +|------|------| +|Array shuffle|https://github.com/xtaci/algorithms/blob/master/include/shuffle.h | +|Prime test(trial division)|https://github.com/xtaci/algorithms/blob/master/include/prime.h| +|Prime test(Miller-Rabin's method)|https://github.com/xtaci/algorithms/blob/master/include/prime.h| +|2D Array|https://github.com/xtaci/algorithms/blob/master/include/2darray.h| +|Arbitrary Integer|https://github.com/xtaci/algorithms/blob/master/include/integer.h| +|Linear congruential generator|https://github.com/xtaci/algorithms/blob/master/include/random.h| +|Maximum subarray problem|https://github.com/xtaci/algorithms/blob/master/include/max_subarray.h| +|Bit-Set|https://github.com/xtaci/algorithms/blob/master/include/bitset.h| +|Queue|https://github.com/xtaci/algorithms/blob/master/include/queue.h| +|Stack|https://github.com/xtaci/algorithms/blob/master/include/stack.h| +|Binary Heap|https://github.com/xtaci/algorithms/blob/master/include/heap.h| +|Fibonacci Heap|https://github.com/xtaci/algorithms/blob/master/include/fib-heap.h| +|Priority Queue (list based)|https://github.com/xtaci/algorithms/blob/master/include/priority_queue.h| +|Bubble sort|https://github.com/xtaci/algorithms/blob/master/include/bubble_sort.h| +|Selection sort|https://github.com/xtaci/algorithms/blob/master/include/selection_sort.h| +|Insertion sort|https://github.com/xtaci/algorithms/blob/master/include/insertion_sort.h| +|Shell sort|https://github.com/xtaci/algorithms/blob/master/include/shell_sort.h| +|Radix sort|https://github.com/xtaci/algorithms/blob/master/include/radix_sort.h| +|Quicksort|https://github.com/xtaci/algorithms/blob/master/include/quick_sort.h| +|Merge sort|https://github.com/xtaci/algorithms/blob/master/include/merge_sort.h| +|Double linked list|https://github.com/xtaci/algorithms/blob/master/include/double_linked_list.h| +|Skip list|https://github.com/xtaci/algorithms/blob/master/include/skiplist.h| +|Largest common sequence|https://github.com/xtaci/algorithms/blob/master/include/lcs.h| +|Binary search tree|https://github.com/xtaci/algorithms/blob/master/include/binary_search_tree.h| +|AVL tree|https://github.com/xtaci/algorithms/blob/master/include/avl.h| +|Dynamic order statistics|https://github.com/xtaci/algorithms/blob/master/include/dos_tree.h| +|Red-black tree|https://github.com/xtaci/algorithms/blob/master/include/rbtree.h| +|Interval tree|https://github.com/xtaci/algorithms/blob/master/include/interval_tree.h| +|Prefix Tree(Trie)|https://github.com/xtaci/algorithms/blob/master/include/trie.h| +|Suffix Tree|https://github.com/xtaci/algorithms/blob/master/include/suffix_tree.h| +|B-Tree|https://github.com/xtaci/algorithms/blob/master/include/btree.h| +|Suffix Array|https://github.com/xtaci/algorithms/blob/master/include/suffix_array.h| +|Hash by multiplication|https://github.com/xtaci/algorithms/blob/master/include/hash_multi.h| +|Hash table|https://github.com/xtaci/algorithms/blob/master/include/hash_table.h| +|Universal hash function|https://github.com/xtaci/algorithms/blob/master/include/universal_hash.h| +|Perfect hash|https://github.com/xtaci/algorithms/blob/master/include/perfect_hash.h| +|Java's string hash|https://github.com/xtaci/algorithms/blob/master/include/hash_string.h| +|FNV-1a string hash|https://github.com/xtaci/algorithms/blob/master/include/hash_string.h| +|SimHash|https://github.com/xtaci/algorithms/blob/master/include/simhash.h| +|Bloom Filter|https://github.com/xtaci/algorithms/blob/master/include/bloom_filter.h| +|SHA-1 Message Digest Algorithm|https://github.com/xtaci/algorithms/blob/master/include/sha1.h| +|MD5|https://github.com/xtaci/algorithms/blob/master/include/md5.h| +|Base64|https://github.com/xtaci/algorithms/blob/master/include/base64.h| +|Strongly Connected Components(SCC)|https://github.com/xtaci/algorithms/blob/master/include/scc.h| +|Prim's minimum spanning tree|https://github.com/xtaci/algorithms/blob/master/include/prim_mst.h| +|Kruskal MST|https://github.com/xtaci/algorithms/blob/master/include/kruskal_mst.h| +|Breadth First Search|https://github.com/xtaci/algorithms/blob/master/include/graph_search.h| +|Depth First Search|https://github.com/xtaci/algorithms/blob/master/include/graph_search.h| +|Dijkstra's algorithm|https://github.com/xtaci/algorithms/blob/master/include/dijkstra.h| +|Bellman-Ford algorithm|https://github.com/xtaci/algorithms/blob/master/include/bellman_ford.h| +|Edmonds-Karp Maximal Flow|https://github.com/xtaci/algorithms/blob/master/include/edmonds_karp.h| +|Push–Relabel algorithm|https://github.com/xtaci/algorithms/blob/master/include/relabel_to_front.h| +|Huffman Coding|https://github.com/xtaci/algorithms/blob/master/include/huffman.h| +|Word segementation|https://github.com/xtaci/algorithms/blob/master/include/word_seg.h| +|A\* algorithm|https://github.com/xtaci/algorithms/blob/master/include/astar.h| +|K-Means|https://github.com/xtaci/algorithms/blob/master/include/k-means.h| +|Knuth–Morris–Pratt algorithm|https://github.com/xtaci/algorithms/blob/master/include/kmp.h| +|Disjoint-Set|https://github.com/xtaci/algorithms/blob/master/include/disjoint-set.h| +|8-Queue Problem|https://github.com/xtaci/algorithms/blob/master/include/8queen.h| +|Palindrome|https://github.com/xtaci/algorithms/blob/master/include/palindrome.h| ####贡献者 ( Contributors ) : Samana: for heavy work of MSVC compatability From c9fae93ad4eadff6b8b2006db5cbf8c560893e60 Mon Sep 17 00:00:00 2001 From: Mohamed Ayman Date: Mon, 26 Dec 2016 00:51:56 +0200 Subject: [PATCH 43/53] Add Lowest commen ancestor algorithm --- src/lca_demo.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/lca_demo.cpp diff --git a/src/lca_demo.cpp b/src/lca_demo.cpp new file mode 100644 index 00000000..7bbf548d --- /dev/null +++ b/src/lca_demo.cpp @@ -0,0 +1,99 @@ +#include + +int numberOfNodes, MAXLOG; +std::vector< std::vector > adjList; +int parent[50005], nodeHeight[50005]; +bool visited[50005]; +int binaryLiftDp[50005][27]; + +void dfs(int currentNode, int currentParent) +{ + visited[currentNode] = true; + parent[currentNode] = currentParent; + nodeHeight[currentNode] = nodeHeight[currentParent] + 1; + int adjacencySize = adjList[currentNode].size(); + for(int idx = 0; idx < adjacencySize; idx++){ + int nextNode = adjList[currentNode][idx]; + if(!visited[nextNode]) + { + dfs(nextNode, currentNode); + } + } +} + +int getMaxLog(){ + int curValue = 1; + int curLog = 1; + while(curValue < numberOfNodes) curValue *= 2, curLog++; + return curLog; +} + +void initializeDP() +{ + nodeHeight[-1] = -1; + MAXLOG = getMaxLog(); + dfs(0, -1); + for(int i = 0; i < numberOfNodes; i++) binaryLiftDp[i][0] = parent[i]; + for(int i = 1; i <= MAXLOG; i++) + { + for(int j = 0; j < numberOfNodes; j++) + { + if(binaryLiftDp[j][i - 1] + 1) + binaryLiftDp[j][i] = binaryLiftDp[binaryLiftDp[j][i - 1]][i - 1]; + else binaryLiftDp[j][i] = -1; + } + } +} + +int LCA(int a, int b) +{ + if(nodeHeight[a] < nodeHeight[b]) std::swap(a,b); + for(int i = MAXLOG; i >= 0; i--) + { + if(binaryLiftDp[a][i] + 1 && nodeHeight[binaryLiftDp[a][i]] >= nodeHeight[b]) + a = binaryLiftDp[a][i]; + } + if(!(a - b)) return a; + for(int i = MAXLOG; i >= 0; i--) + { + if(binaryLiftDp[a][i] + 1 && binaryLiftDp[a][i] - binaryLiftDp[b][i]) + a = binaryLiftDp[a][i], b = binaryLiftDp[b][i]; + } + return parent[a]; +} + +void buildTree() +{ + printf("Enter number of nodes of the tree: "); + scanf("%d", &numberOfNodes); + adjList.resize(numberOfNodes, std::vector ()); + for(int i = 0; i < numberOfNodes - 1; i++) + { + int firstNode, secondNode; + printf("Enter the two nodes to be connected: "); + scanf("%d %d", &firstNode, &secondNode); + adjList[firstNode].push_back(secondNode); + adjList[secondNode].push_back(firstNode); + } +} + +void answerQueries() +{ + int queryCount; + printf("Enter the number of queries: "); + scanf("%d", &queryCount); + for(int i = 0; i < queryCount; i++) + { + int firstNode, secondNode; + printf("Enter the two nodes : "); + scanf("%d %d", &firstNode, &secondNode); + printf("%d\n", LCA(firstNode, secondNode)); + } +} + +int main() +{ + buildTree(); + initializeDP(); + answerQueries(); +} From ffe13e93ee0e0408cd54bdcc1982f247e075d241 Mon Sep 17 00:00:00 2001 From: Mohamed Ayman Date: Mon, 26 Dec 2016 00:56:30 +0200 Subject: [PATCH 44/53] Add const size for array --- src/lca_demo.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/lca_demo.cpp b/src/lca_demo.cpp index 7bbf548d..19002d3e 100644 --- a/src/lca_demo.cpp +++ b/src/lca_demo.cpp @@ -1,10 +1,14 @@ -#include +#include +#include -int numberOfNodes, MAXLOG; +const int MAX_NODE = 5000; +const int MAX_LOG = 20; + +int numberOfNodes, maxLog; std::vector< std::vector > adjList; -int parent[50005], nodeHeight[50005]; -bool visited[50005]; -int binaryLiftDp[50005][27]; +int parent[MAX_NODE], nodeHeight[MAX_NODE]; +bool visited[MAX_NODE]; +int binaryLiftDp[MAX_NODE][MAX_LOG]; void dfs(int currentNode, int currentParent) { @@ -31,10 +35,10 @@ int getMaxLog(){ void initializeDP() { nodeHeight[-1] = -1; - MAXLOG = getMaxLog(); + maxLog = getMaxLog(); dfs(0, -1); for(int i = 0; i < numberOfNodes; i++) binaryLiftDp[i][0] = parent[i]; - for(int i = 1; i <= MAXLOG; i++) + for(int i = 1; i <= maxLog; i++) { for(int j = 0; j < numberOfNodes; j++) { @@ -48,13 +52,13 @@ void initializeDP() int LCA(int a, int b) { if(nodeHeight[a] < nodeHeight[b]) std::swap(a,b); - for(int i = MAXLOG; i >= 0; i--) + for(int i = maxLog; i >= 0; i--) { if(binaryLiftDp[a][i] + 1 && nodeHeight[binaryLiftDp[a][i]] >= nodeHeight[b]) a = binaryLiftDp[a][i]; } if(!(a - b)) return a; - for(int i = MAXLOG; i >= 0; i--) + for(int i = maxLog; i >= 0; i--) { if(binaryLiftDp[a][i] + 1 && binaryLiftDp[a][i] - binaryLiftDp[b][i]) a = binaryLiftDp[a][i], b = binaryLiftDp[b][i]; From e65421342902b29efdd39fd4e7d32086f4c7c261 Mon Sep 17 00:00:00 2001 From: xtaci Date: Fri, 20 Jan 2017 11:08:17 +0800 Subject: [PATCH 45/53] update makefile --- .travis.yml | 3 --- Makefile | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index e4d9ce7f..8226360b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,5 @@ language: cpp - compiler: - - gcc - clang - script: - make diff --git a/Makefile b/Makefile index c0c50190..ee02999c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: all clean -CC=gcc -CPP=g++ +CC=clang +CPP=clang++ AR=ar RANLIB=ranlib CFLAGS= -g -Wall -Wno-unused-function From 5c50c332114874e8e175a83fef12d0b24547d45b Mon Sep 17 00:00:00 2001 From: Mohamed Ayman Date: Tue, 21 Feb 2017 00:19:27 +0200 Subject: [PATCH 46/53] apply OOP principles to LCA --- include/LCA.h | 38 ++++++++++++++ src/lca_demo.cpp | 126 ++++++++++++++++++++++++++--------------------- 2 files changed, 108 insertions(+), 56 deletions(-) create mode 100644 include/LCA.h diff --git a/include/LCA.h b/include/LCA.h new file mode 100644 index 00000000..ef0eb098 --- /dev/null +++ b/include/LCA.h @@ -0,0 +1,38 @@ +/******************************************************************************* + * + * + * /\ | _ _ ._ o _|_ |_ ._ _ _ + * /--\ | (_| (_) | | |_ | | | | | _> + * _| + * + * LCA Finding using Binary Lifting and Dynamic Programming + * + * Features: + * 1. Answers Query about LCA of two nodes in O(log N) + * where N is the total number of nodes in a tree. + * + * https://en.wikipedia.org/wiki/Lowest_common_ancestor + * http://www.csegeek.com/csegeek/view/tutorials/algorithms/trees/tree_part12.php + ******************************************************************************/ + +#ifndef LCA_H +#define LCA_H +#include + +class LCA +{ + public: + LCA(std::vector< std::pair > edges); + int lcaQuery(int a, int b); + + private: + int getMaxLog(); + void initDP(); + void dfs(int currentNode, int currentParent); + std::vector< std::vector > adjList, binaryLiftDp; + std::vector parent, nodeHeight; + std::vector visited; + int _numberOfNodes, _maxLog; +}; + +#endif // LCA_H diff --git a/src/lca_demo.cpp b/src/lca_demo.cpp index 19002d3e..899cc1ba 100644 --- a/src/lca_demo.cpp +++ b/src/lca_demo.cpp @@ -1,16 +1,36 @@ +#include "LCA.h" #include #include +#include +/** +*Constructor is initialized with a Adjacency List that +*describe a tree and If It doesn't describe a tree it asserts failure. +*/ -const int MAX_NODE = 5000; -const int MAX_LOG = 20; - -int numberOfNodes, maxLog; -std::vector< std::vector > adjList; -int parent[MAX_NODE], nodeHeight[MAX_NODE]; -bool visited[MAX_NODE]; -int binaryLiftDp[MAX_NODE][MAX_LOG]; +LCA::LCA(std::vector< std::pair > edges): _numberOfNodes(edges.size() + 1), _maxLog(getMaxLog()) +{ + //First we initialize the needed vectors + parent.resize(_numberOfNodes); + nodeHeight.resize(_numberOfNodes); + visited.resize(_numberOfNodes); + adjList.resize(_numberOfNodes); + binaryLiftDp = std::vector< std::vector >(_numberOfNodes, std::vector(_maxLog)); + /**Construction of the Adjacency List to increase + *The efficiency of the tree traversal to O(V + E). + */ + for(auto edge : edges){ + adjList[edge.first].push_back(edge.second); + adjList[edge.second].push_back(edge.first); + } + //Initialize the Dynamic programming Vector. + initDP(); +} -void dfs(int currentNode, int currentParent) +/** +*DFS is used to find the parent and the height of each node +*allowing the use of Binary Lifting. +*/ +void LCA::dfs(int currentNode, int currentParent) { visited[currentNode] = true; parent[currentNode] = currentParent; @@ -25,40 +45,58 @@ void dfs(int currentNode, int currentParent) } } -int getMaxLog(){ +/** +*Used to Calculate the Log to the base of two +*for the number of the nodes to create the sparse table +*used in binary Lifting. +*/ +int LCA::getMaxLog(){ int curValue = 1; int curLog = 1; - while(curValue < numberOfNodes) curValue *= 2, curLog++; + while(curValue < _numberOfNodes) curValue *= 2, curLog++; return curLog; } -void initializeDP() +void LCA::initDP() { - nodeHeight[-1] = -1; - maxLog = getMaxLog(); dfs(0, -1); - for(int i = 0; i < numberOfNodes; i++) binaryLiftDp[i][0] = parent[i]; - for(int i = 1; i <= maxLog; i++) + for(int i = 0; i < _numberOfNodes; i++) binaryLiftDp[i][0] = parent[i]; + for(int i = 1; i <= _maxLog; i++) { - for(int j = 0; j < numberOfNodes; j++) + for(int j = 0; j < _numberOfNodes; j++) { - if(binaryLiftDp[j][i - 1] + 1) + /** + * Since the ith parent of the current node is equal to + * the ith / 2 parent to the ith /2 parent of the current node + * That's why the Recurrence relation is described as follow + */ + if(binaryLiftDp[j][i - 1] != -1) binaryLiftDp[j][i] = binaryLiftDp[binaryLiftDp[j][i - 1]][i - 1]; else binaryLiftDp[j][i] = -1; } } } -int LCA(int a, int b) +int LCA::lcaQuery(int a, int b) { + /** + * First Both nodes must have same height + * So we will rise the node with the deeper height up in + * the tree to where they're equal. + */ if(nodeHeight[a] < nodeHeight[b]) std::swap(a,b); - for(int i = maxLog; i >= 0; i--) + for(int i = _maxLog; i >= 0; i--) { if(binaryLiftDp[a][i] + 1 && nodeHeight[binaryLiftDp[a][i]] >= nodeHeight[b]) a = binaryLiftDp[a][i]; } - if(!(a - b)) return a; - for(int i = maxLog; i >= 0; i--) + /** + * If the node Lower is the LCA then return it. + * Else keep moving both nodes up as much as they aren't the same + * until it's only 1 node left which is the direct parent of both of them + */ + if(a == b) return a; + for(int i = _maxLog; i >= 0; i--) { if(binaryLiftDp[a][i] + 1 && binaryLiftDp[a][i] - binaryLiftDp[b][i]) a = binaryLiftDp[a][i], b = binaryLiftDp[b][i]; @@ -66,38 +104,14 @@ int LCA(int a, int b) return parent[a]; } -void buildTree() -{ - printf("Enter number of nodes of the tree: "); - scanf("%d", &numberOfNodes); - adjList.resize(numberOfNodes, std::vector ()); - for(int i = 0; i < numberOfNodes - 1; i++) - { - int firstNode, secondNode; - printf("Enter the two nodes to be connected: "); - scanf("%d %d", &firstNode, &secondNode); - adjList[firstNode].push_back(secondNode); - adjList[secondNode].push_back(firstNode); - } -} - -void answerQueries() -{ - int queryCount; - printf("Enter the number of queries: "); - scanf("%d", &queryCount); - for(int i = 0; i < queryCount; i++) - { - int firstNode, secondNode; - printf("Enter the two nodes : "); - scanf("%d %d", &firstNode, &secondNode); - printf("%d\n", LCA(firstNode, secondNode)); - } -} - -int main() -{ - buildTree(); - initializeDP(); - answerQueries(); +int main(){ + std::vector< std::pair > edges; + edges.push_back({0,1}); + edges.push_back({1,2}); + edges.push_back({2,3}); + edges.push_back({1,4}); + LCA* l = new LCA(v); + std::cout << l->lcaQuery(0,1) << endl; + std::cout << l->lcaQuery(3,4) << endl; + std::cout << l->lcaQuery(3,2) << endl; } From 82dd1bb637986c3ed6394e8da09b7ddd5592600a Mon Sep 17 00:00:00 2001 From: xtaci Date: Tue, 21 Feb 2017 10:00:11 +0800 Subject: [PATCH 47/53] Delete .goutputstream-UUQE8X --- include/.goutputstream-UUQE8X | 61 ----------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 include/.goutputstream-UUQE8X diff --git a/include/.goutputstream-UUQE8X b/include/.goutputstream-UUQE8X deleted file mode 100644 index e92d1ab5..00000000 --- a/include/.goutputstream-UUQE8X +++ /dev/null @@ -1,61 +0,0 @@ -/******************************************************************************* - * Fenwick Tree - * - * Data structure providing prefix sums and modify the table in O(log n) - n is the size o the table. - * - * In this algorithm we use two functions: - * - RSQ - This function calculates the range sum query in O(log n) - * - Update - This function adjusts the values in the given range in O(log n) - * - * https://en.wikipedia.org/wiki/Fenwick_tree - * - * @author Gabriel Duarte (gabriellagoa10@yahoo.com.br) - * @github Gabriel123Duarte - * - ******************************************************************************/ - -#ifndef __FENWICK_H__ -#define __FENWICK_H__ - -#include - -#define LSONE(x) (x & (-x)) - -class Fenwick -{ - private: - // Vector representing the table - std::vector fen; - public: - Fenwick() {} - - // We don't use the index 0, because it is the base case - Fenwick(int n) - { - fen.assign(n + 1, 0); - } - - // Calculate the - int rsq(int a) - { - int ans = 0; - for(; a; a -= LSONE(a)) - ans += fen[a]; - return ans; - } - - // RSQ a..b - inline int rsq(int a, int b) - { - return rsq(b) - (a == 1 ? 0 : rsq(a - 1)); - } - - // Update the value of the k-th element by x - void update(int k, int x) - { - for(; k < (int)fen.size(); k += LSONE(k)) - fen[k] += x; - } -}; - -#endif From 1a94def5c1dc01b051df1aa72315cd85757d3ca7 Mon Sep 17 00:00:00 2001 From: Mohamed Ayman Date: Wed, 22 Feb 2017 00:20:42 +0200 Subject: [PATCH 48/53] add LCA entry to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dac3795d..4bd9675a 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ |Disjoint-Set|https://github.com/xtaci/algorithms/blob/master/include/disjoint-set.h| |8-Queue Problem|https://github.com/xtaci/algorithms/blob/master/include/8queen.h| |Palindrome|https://github.com/xtaci/algorithms/blob/master/include/palindrome.h| +|LCA using Binary Lifting|https://github.com/xtaci/algorithms/blob/master/include/LCA.h| ####贡献者 ( Contributors ) : Samana: for heavy work of MSVC compatability From fb003ae73232d5ff619e80fed430a3384fcffca2 Mon Sep 17 00:00:00 2001 From: yrong Date: Sun, 16 Apr 2017 11:33:07 +0800 Subject: [PATCH 49/53] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4bd9675a..58cd686e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [1]: https://travis-ci.org/xtaci/algorithms.svg?branch=master [2]: https://travis-ci.org/xtaci/algorithms -####目标 ( goal ) : +#### 目标 ( goal ) : 1. 经典的算法实现 (classical algorithms implementations) @@ -12,7 +12,7 @@ 3. 正确,易于使用和改造, 一个头文件一个算法,并附带一个demo. (correct! and ease of use, one .header file per algorithm) -####约定 ( Convention ): +#### 约定 ( Convention ): 1. 一个算法用一个.h文件表示放到include下. ( one .header file per algorithm. ) 2. 算法演示的demo程序放到src下. ( one demo per algorithm. ) @@ -24,7 +24,7 @@ eg: ![demograph](demo_graph.png) -####已实现 ( Implemented ): +#### 已实现 ( Implemented ): | Name | File | |------|------| @@ -90,7 +90,7 @@ |Palindrome|https://github.com/xtaci/algorithms/blob/master/include/palindrome.h| |LCA using Binary Lifting|https://github.com/xtaci/algorithms/blob/master/include/LCA.h| -####贡献者 ( Contributors ) : +#### 贡献者 ( Contributors ) : Samana: for heavy work of MSVC compatability wycg1984: for K-Means xmuliang: for HeapSort, Kruskal MST @@ -99,6 +99,6 @@ UsingtcNower: Suffix Array afernandez90: AVL trees -####支持此项目 ( Donations ) : +#### 支持此项目 ( Donations ) : ![donate](donate_alg.png) 欢迎使用支付宝扫描上面的二维码,对该项目进行捐赠。捐赠款项将用于持续优化补全及完善。 From a4333de509ae5f04ddf83b7abc5dfc285021dc24 Mon Sep 17 00:00:00 2001 From: xtaci Date: Sat, 3 Jun 2017 21:00:38 +0800 Subject: [PATCH 50/53] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 58cd686e..b7fcffcb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ -###Algorithms & Data Structures in C++ +### Algorithms & Data Structures in C++ + [![Build Status][1]][2] + [1]: https://travis-ci.org/xtaci/algorithms.svg?branch=master [2]: https://travis-ci.org/xtaci/algorithms From 21aed1508df1460182d5be231e771a9f99d5428c Mon Sep 17 00:00:00 2001 From: godbod Date: Sun, 18 Feb 2018 22:41:11 +0100 Subject: [PATCH 51/53] This pull request solve the cleaning of *.o file issue --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ee02999c..82f536a5 100644 --- a/Makefile +++ b/Makefile @@ -263,5 +263,5 @@ suffix_array_demo: $(SRCDIR)/suffix_array_demo.cpp $(CPP) $(C11FLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBS) clean: - rm -rf $(PROGRAMS) *.dSYM + rm -rf $(PROGRAMS) *.dSYM *.o From a159c2fe5605c4b9dd2038ac3e30321139f4a780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=B7=83=E0=B7=92=E0=B6=AD=E0=B7=94=E0=B6=B8=E0=B7=8A?= <56906402+SHandapangoda@users.noreply.github.com> Date: Mon, 17 May 2021 17:01:12 +0530 Subject: [PATCH 52/53] Add files via upload --- src/searchPatterns.cpp | 112 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/searchPatterns.cpp diff --git a/src/searchPatterns.cpp b/src/searchPatterns.cpp new file mode 100644 index 00000000..75a728e0 --- /dev/null +++ b/src/searchPatterns.cpp @@ -0,0 +1,112 @@ +// +// Created by Sithum on 2021-01-01. +// + +#include "clientClass.h" + +#include + + + +class patternSearchNaive { + + char* pattern; + char* text; + // naive bias pattern search +public: void search(char* pattern, char* text) { + int txt = strlen(text); + int pat = strlen(pattern); + + for (int i = 0; i <= txt - pat; i++) { + // for pattern matching + for (int j = 0; j < pat; j++) { + if (text[i + j] != pattern[j]) { + break; + } + // if pattern matches + if (j = pat) { + std::cout << "Pattern found" << i << std::endl; + } + } + } + +} + +}; + +//for Knuth morris pratt algorithm +class patternSearchKMP { + + //occurance of text[],pattern[] +public: + void search(char* pattern, char* text) { + + int txt = strlen(text); + int pat = strlen(pattern); + + // to hold longest prefix suffix + // value for pattern + int lps[1]; + + //preprocess the pattern(calculate lps[] array) + LPS(pattern, pat, lps); + + int index = 0; + int j_index = 0; + + while (index < txt) { + if (pattern[j_index] == text[index]) { + j_index++; // index for pattern + index++; // index for text + } + + if (j_index == pat) { + std::cout << "Index found " << index - j_index << std::endl; + j_index = lps[j_index - 1]; + } + + //mismatch after pattern match + else if (index < pat && pattern[j_index] != text[index]) { + if (j_index != 0) { + j_index = lps[j_index - 1]; + } + else { + + index = index++; + } + } + } + + } + +public: void LPS(char* pattern, int pat, int* lps) { + + int lenght = 0; + + lps[0] = 0; + + // the loop calculates lps[index] for index = 1 to pat-1 + int index = 1; + + while (index < pat) { + if (pattern[index] == pattern[lenght]) { + + lenght++; + lps[index] = lenght; + index++; + } + else { + if (lenght != 0) { + lenght = lps[lenght - 1]; + } + else { + lps[index] = 0; + index++; + + } + } + } + + +} +}; \ No newline at end of file From db678f9ba75bd54ffa66ba2598044c6d1bcd42f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=B7=83=E0=B7=92=E0=B6=AD=E0=B7=94=E0=B6=B8=E0=B7=8A=20?= =?UTF-8?q?=E0=B7=84=E0=B6=AF=E0=B6=B4=E0=B7=8F=E0=B6=B1=E0=B7=8A=E0=B6=9C?= =?UTF-8?q?=E0=B7=9C=E0=B6=A9?= Date: Sun, 18 Jul 2021 08:57:23 +0530 Subject: [PATCH 53/53] Add files via upload --- src/quicksort.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/quicksort.cpp diff --git a/src/quicksort.cpp b/src/quicksort.cpp new file mode 100644 index 00000000..a41cb257 --- /dev/null +++ b/src/quicksort.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +using namespace std; +void swap(int* a, int* b){ + int store = *a; + *a = *b; + *b = store; +} + +int part(int array[], int high, int low){ + int pin = array[high]; + int index = (low - 1); + + for(int i = low; i <= high; i++){ + if(array[i] <= pin){ + index++; + swap(&array[index], &array[high]); + } + } + swap(&array[index + 1], &array[high]); + return (index + 1); +} + +void quicksort(int array[], int high, int low){ + if(low < high){ + int pi = part(array,high,low); + + quicksort(array, low, pi - 1 ); + quicksort(array, pi + 1, high); + } +} + +void printArray(int array[], int size){ + int index; + for (index =0; index < size; index++){ + cout << "" << array[index] << endl; + cout << endl; + } +} + +int main(){ + int array[] = {25,17,3,1,22}; + int n = sizeof(array) / sizeof(array[0]); + quicksort(array, n-1, 0); + printArray(array, n); + +} \ No newline at end of file