├── docs ├── GraphTheory │ ├── ShortestPath │ │ └── res │ │ │ ├── BellmanFord.xml │ │ │ └── KnowledgePoint1.png │ ├── BinaryMatch │ │ ├── Color │ │ │ └── README.md │ │ ├── Coloring │ │ │ └── README.md │ │ ├── Hoproft-Karp │ │ │ └── README.md │ │ ├── KuhnMunkres │ │ │ └── README.md │ │ ├── HopcroftKarp │ │ │ └── README.md │ │ ├── Kuhn-Munkres │ │ │ └── README.md │ │ ├── MatchToMaxFlow │ │ │ └── README.md │ │ ├── MinimumJoinPathCovering │ │ │ └── README.md │ │ ├── MinimumJointPathCovering │ │ │ └── README.md │ │ ├── MinimumDisjointPathCovering │ │ │ └── README.md │ │ ├── WeightedCoveringAndIndependentSet │ │ │ └── README.md │ │ ├── Introduction-Domination-Independent-Covering-Clique │ │ │ └── README.md │ │ ├── Introduction-Domination_Independent_Covering_Clique │ │ │ └── README.md │ │ ├── res │ │ │ └── KnowledgePoint1.png │ │ └── Hungarian │ │ │ └── README.md │ ├── res │ │ └── GraphTheory.png │ ├── NetworkFlow │ │ └── res │ │ │ ├── Dinic1.png │ │ │ ├── EdmondsKarp1.png │ │ │ ├── EdmondsKarp2.png │ │ │ ├── EdmondsKarp3.png │ │ │ ├── EdmondsKarp4.png │ │ │ ├── KnowledgePoint1.png │ │ │ ├── MinimumCostFlow1.png │ │ │ └── MinimumCostFlow2.png │ ├── MinSpanningTree │ │ ├── res │ │ │ ├── Prim1.png │ │ │ └── Kruskal1.png │ │ ├── README.md │ │ ├── SecondMinSpanningTree │ │ │ └── README.md │ │ └── OptimalRatioSpanningTree │ │ │ └── README.md │ ├── Traverse │ │ └── res │ │ │ ├── EulerCycle1.png │ │ │ ├── EulerCycle2.png │ │ │ ├── EulerCycle3.png │ │ │ ├── EulerCycle4.png │ │ │ ├── KnowledgePoint1.png │ │ │ ├── KnowledgePoint2.png │ │ │ ├── KnowledgePoint3.png │ │ │ ├── DepthFirstSearch1.png │ │ │ ├── TopologicalSort.vsdx │ │ │ ├── TopologicalSort1.png │ │ │ ├── TopologicalSort2.png │ │ │ ├── TopologicalSort3.png │ │ │ ├── TopologicalSort4.png │ │ │ └── BreadthFirstSearch1.png │ └── StronglyConnectedComponents │ │ └── res │ │ ├── Kosaraju1.png │ │ ├── Kosaraju2.png │ │ └── Kosaraju3.png ├── LinearAlgebra │ ├── Matrix │ │ ├── LUP │ │ │ └── README.md │ │ ├── InverseMatrix │ │ │ └── README.md │ │ └── README.md │ ├── LinearProgramming │ │ ├── Simplex │ │ │ └── README.md │ │ └── Dinkelback │ │ │ └── README.md │ ├── res │ │ ├── Matrix.png │ │ └── Matrix.xml │ └── README.md ├── res │ ├── alipay.jpg │ ├── wxpay.jpg │ └── keyboard.jpg ├── Sort │ ├── res │ │ ├── Sort.png │ │ ├── BubbleSort1.png │ │ ├── BubbleSort2.png │ │ ├── BubbleSort3.png │ │ ├── BubbleSort4.png │ │ ├── BubbleSort5.png │ │ ├── BubbleSort6.png │ │ ├── BubbleSort7.png │ │ ├── BubbleSort8.png │ │ ├── BubbleSort9.png │ │ ├── InsertSort1.png │ │ ├── InsertSort2.png │ │ ├── InsertSort3.png │ │ ├── InsertSort4.png │ │ ├── InsertSort5.png │ │ ├── MergeSort1.png │ │ ├── MergeSort2.png │ │ ├── QuickSort1.png │ │ ├── QuickSort2.png │ │ ├── QuickSort3.png │ │ ├── QuickSort4.png │ │ └── Sort.xml │ └── README.md ├── Search │ ├── res │ │ ├── Search.png │ │ ├── AStarSearch1.png │ │ ├── AStarSearch2.png │ │ ├── BinarySearch1.png │ │ ├── BinarySearch2.png │ │ ├── BinarySearch3.png │ │ ├── DancingLink1.png │ │ ├── DancingLink2.png │ │ ├── DancingLink3.png │ │ ├── DancingLink4.png │ │ ├── DancingLink5.png │ │ ├── DancingLink6.png │ │ ├── DancingLink7.png │ │ ├── DancingLink8.png │ │ ├── KnowledgePoint1.png │ │ ├── KnowledgePoint2.png │ │ ├── KnowledgePoint3.png │ │ ├── BreadthFirstSearch1.png │ │ ├── BreadthFirstSearch2.png │ │ ├── BreadthFirstSearch3.png │ │ ├── BreadthFirstSearch4.png │ │ ├── BreadthFirstSearch11.png │ │ ├── BreadthFirstSearch12.png │ │ ├── BreadthFirstSearch20.png │ │ ├── BreadthFirstSearch21.png │ │ ├── BidirectionalBreadthSearch1.png │ │ ├── BidirectionalBreadthSearch2.png │ │ ├── BidirectionalBreadthSearch3.png │ │ ├── BidirectionalBreadthSearch7.png │ │ ├── BidirectionalBreadthSearch8.png │ │ └── Search.xml │ ├── AdditionMultiplicationPrinciple │ │ └── README.md │ └── README.md ├── GameTheory │ ├── res │ │ ├── GameTheory.png │ │ ├── WythoffGame1.png │ │ ├── WythoffGame2.png │ │ ├── WythoffGame3.png │ │ └── GameTheory.xml │ └── README.md ├── DataStructure │ ├── res │ │ ├── AVLTree1.png │ │ ├── AVLTree10.png │ │ ├── AVLTree11.png │ │ ├── AVLTree12.png │ │ ├── AVLTree13.png │ │ ├── AVLTree2.png │ │ ├── AVLTree3.png │ │ ├── AVLTree4.png │ │ ├── AVLTree5.png │ │ ├── AVLTree6.png │ │ ├── AVLTree7.png │ │ ├── AVLTree8.png │ │ ├── AVLTree9.png │ │ ├── SkipList1.png │ │ ├── SkipList2.png │ │ ├── SkipList3.png │ │ ├── DisjointSet1.png │ │ ├── DisjointSet2.png │ │ ├── DisjointSet3.png │ │ ├── DisjointSet4.png │ │ ├── DisjointSet5.png │ │ ├── DisjointSet6.png │ │ ├── DisjointSet7.png │ │ ├── DisjointSet8.png │ │ ├── DisjointSet9.png │ │ ├── FenwickTree1.png │ │ ├── FenwickTree2.png │ │ ├── FenwickTree4.png │ │ ├── LeftistTree1.png │ │ ├── LeftistTree2.png │ │ ├── LeftistTree3.png │ │ ├── LeftistTree4.png │ │ ├── LeftistTree5.png │ │ ├── LeftistTree6.png │ │ ├── LeftistTree7.png │ │ ├── LeftistTree8.png │ │ ├── LeftistTree9.png │ │ ├── PrefixTree1.png │ │ ├── SegmentTree1.png │ │ ├── DataStructure.png │ │ ├── LeftistTree10.png │ │ ├── LeftistTree11.png │ │ ├── LeftistTree12.png │ │ ├── RedBlackTree.docx │ │ ├── RedBlackTree.vsdx │ │ ├── RedBlackTree1.png │ │ ├── RedBlackTree2.png │ │ ├── RedBlackTree3.png │ │ ├── RedBlackTree4.png │ │ ├── RedBlackTree5.png │ │ ├── RedBlackTree6.png │ │ ├── BinarySearchTree1.png │ │ ├── BinarySearchTree2.png │ │ ├── BinarySearchTree3.png │ │ ├── BinarySearchTree4.png │ │ ├── BinarySearchTree5.png │ │ ├── BinarySearchTree6.png │ │ ├── BinarySearchTree7.png │ │ ├── BinarySearchTree8.png │ │ ├── BinarySearchTree9.png │ │ ├── BinarySearchTree10.png │ │ └── BinarySearchTree11.png │ ├── README.md │ ├── BPlusTree │ │ └── README.md │ ├── BMinusTree │ │ └── README.md │ └── PrefixTree │ │ └── README.md ├── NumberTheory │ ├── res │ │ ├── NumberTheory.png │ │ └── NumberTheory.xml │ └── README.md ├── PatternMatch │ ├── res │ │ ├── AhoCorasick1.png │ │ ├── AhoCorasick10.png │ │ ├── AhoCorasick2.png │ │ ├── AhoCorasick3.png │ │ ├── AhoCorasick4.png │ │ ├── AhoCorasick5.png │ │ ├── AhoCorasick6.png │ │ ├── AhoCorasick7.png │ │ ├── AhoCorasick8.png │ │ ├── AhoCorasick9.png │ │ ├── PatternMatch.png │ │ ├── SimpleMatch1.png │ │ ├── SimpleMatch2.png │ │ ├── SimpleMatch3.png │ │ ├── KnuthMorrisPratt1.png │ │ └── PatternMatch.xml │ ├── README.md │ └── BoyerMoore │ │ └── README.md ├── AnalyticGeometry │ ├── Polygon │ │ └── res │ │ │ ├── Cross1.png │ │ │ ├── Cross2.png │ │ │ ├── Cross3.png │ │ │ ├── Sweeping1.png │ │ │ ├── Sweeping2.png │ │ │ ├── NearestNeighbor1.png │ │ │ ├── NearestNeighbor2.png │ │ │ ├── ConvexPolygonArea1.png │ │ │ ├── ConvexPolygonArea2.png │ │ │ ├── SegmentIntersection1.png │ │ │ ├── SegmentIntersection2.png │ │ │ └── SegmentIntersection3.png │ ├── res │ │ └── AnalyticGeometry.png │ ├── ConvexHull │ │ ├── res │ │ │ ├── ConvexHull1.png │ │ │ ├── GrahamScan1.png │ │ │ ├── GrahamScan2.png │ │ │ ├── GrahamScan3.png │ │ │ └── GrahamScan4.png │ │ ├── README.md │ │ └── QuickHull │ │ │ └── README.md │ └── README.md ├── DynamicProgramming │ ├── RegionalDP │ │ └── res │ │ │ ├── UniquePath1.png │ │ │ ├── MinMergeCost1.png │ │ │ ├── TrianglePath1.png │ │ │ ├── TrianglePath2.png │ │ │ └── TrianglePath.xml │ └── TreeDP │ │ └── res │ │ ├── MaxBinaryTree1.png │ │ ├── MaxBinaryTree2.png │ │ ├── MaxBinaryTree3.png │ │ ├── MaxBinaryTreeRadiusSum1.png │ │ ├── MaxBinaryTreeRadiusSum2.png │ │ ├── MaxBinaryTreeRadiusSum3.png │ │ └── MaxBinaryTreeRadiusSum4.png ├── BasicKnowledge │ ├── README.md │ └── Recursion │ │ └── README.md ├── CombinatorialMathematics │ ├── res │ │ ├── CombinationMathematics.png │ │ └── CombinationMathematics.xml │ └── Permutation │ │ └── README.md ├── _config.yml └── Preface │ └── README.md ├── src ├── CombinatorialMathematics │ ├── PermutationGroup.hpp │ ├── CatalanTest.cpp │ ├── PermutationGroup.cpp │ ├── FullPermutation.h │ ├── Combination.h │ ├── print_status.cpp │ ├── Catalan.h │ ├── com_local.h │ ├── 2_full_permutation.cpp │ ├── FullPermutation.cpp │ ├── CMakeLists.txt │ ├── FullPermutationTest.cpp │ ├── Catalan.cpp │ └── CombinationTest.cpp ├── test.bat ├── Sort │ ├── Util.h │ ├── BubbleSort.h │ ├── InsertSort.h │ ├── MergeSort.h │ ├── QuickSort.h │ ├── BubbleSort.cpp │ ├── Util.cpp │ ├── QuickSortTest.cpp │ ├── BubbleSortTest.cpp │ ├── InsertSortTest.cpp │ ├── MergeSortTest.cpp │ ├── CMakeLists.txt │ ├── InsertSort.cpp │ ├── QuickSort.cpp │ └── MergeSort.cpp ├── test.sh ├── GraphTheory │ ├── ShortestPath │ │ ├── DijkstraTest.cpp │ │ ├── BellmanFordTest.cpp │ │ ├── FloydWarshallTest.cpp │ │ ├── DifferenceConstraintsTest.cpp │ │ ├── FloydWarshall.h │ │ ├── Dijkstra.h │ │ ├── BellmanFord.h │ │ ├── DifferenceConstraints.h │ │ ├── FloydWarshall.cpp │ │ ├── Dijkstra.cpp │ │ └── DifferenceConstraints.cpp │ ├── StronglyConnectedComponents │ │ ├── TwoSatisfiabilityTest.cpp │ │ ├── Kosaraju.h │ │ ├── TwoSatisfiability.h │ │ ├── TwoSatisfiability.cpp │ │ ├── Tarjan.h │ │ ├── KosarajuTest.cpp │ │ ├── TarjanTest.cpp │ │ └── Kosaraju.cpp │ ├── Traverse │ │ ├── DepthFirstSearch.h │ │ ├── BreadthFirstSearch.h │ │ ├── EulerCycle.h │ │ ├── TopologicalSort.h │ │ ├── BreadthFirstSearch.cpp │ │ └── DepthFirstSearch.cpp │ ├── MinSpanningTree │ │ ├── Kruskal.h │ │ ├── Prim.h │ │ ├── 4_degree_bounded_spanning_tree.cpp │ │ └── Prim.cpp │ ├── NetworkFlow │ │ ├── 6_highest_label_preflow_push.cpp │ │ ├── 10_multi_source_sink_max_flow.cpp │ │ └── 3_dinic.cpp │ ├── BinaryMatch │ │ ├── 3_match2maxflow.cpp │ │ ├── 6_weighted_covering_independent_set.cpp │ │ └── 8_minimum_joint_path_coverage.cpp │ ├── Util.h │ └── Util.cpp ├── NumberTheory │ ├── ModularExponentiation.h │ ├── ExtendedEuclid.h │ ├── Util.h │ ├── Euclid.h │ ├── ChineseRemainerTheorem.h │ ├── PrimeSieve.h │ ├── Euclid.cpp │ ├── EuclidTest.cpp │ ├── ModularExponentiation.cpp │ ├── ExtendedEuclidTest.cpp │ ├── CMakeLists.txt │ ├── ChineseRemainerTheoremTest.cpp │ ├── ChineseRemainerTheorem.cpp │ ├── Util.cpp │ ├── ModularExponentiationTest.cpp │ └── ExtendedEuclid.cpp ├── AnalyticGeometry │ ├── Polygon │ │ ├── Cross.h │ │ ├── Cross.cpp │ │ ├── SegmentIntersection.h │ │ ├── Sweeping.h │ │ ├── ConvexPolygonAreaTest.cpp │ │ ├── ConvexPolygonArea.h │ │ ├── ConvexPolygonGravityCenter.h │ │ ├── ConvexPolygonArea.cpp │ │ ├── CMakeLists.txt │ │ ├── SegmentIntersectionTest.cpp │ │ ├── CrossTest.cpp │ │ └── ConvexPolygonGravityCenter.cpp │ ├── CMakeLists.txt │ └── ConvexHull │ │ ├── CMakeLists.txt │ │ └── GrahamScan.h ├── DynamicProgramming │ ├── BagDP │ │ ├── CompleteBag.h │ │ ├── ZeroOneBag.h │ │ ├── TwoDimensionBag.h │ │ ├── CompleteBag.cpp │ │ ├── ZeroOneBag.cpp │ │ └── TwoDimensionBag.cpp │ ├── LinearDP │ │ ├── BidirectionalSubsequence.h │ │ ├── LongestIncreasingSubsequence.h │ │ ├── MaximumContinuousSubsequenceSum.h │ │ ├── LongestCommonSubsequence.h │ │ ├── LongestPalindromicSubsequence.h │ │ ├── MaximumContinuousSubsequenceSum.cpp │ │ ├── LongestIncreasingSubsequence.cpp │ │ ├── LongestCommonSubsequence.cpp │ │ ├── MaximumContinuousSubsequenceSumTest.cpp │ │ ├── LongestPalindromicSubsequenceTest.cpp │ │ └── LongestPalindromicSubsequence.cpp │ ├── RegionalDP │ │ ├── UniquePath.h │ │ ├── MinimumMergeCost.h │ │ ├── TrianglePath.h │ │ ├── UniquePath.cpp │ │ ├── UniquePathTest.cpp │ │ ├── TrianglePathTest.cpp │ │ └── MinimumMergeCost.cpp │ ├── TreeDP │ │ ├── MaximumMultipleTreeTest.cpp │ │ ├── CMakeLists.txt │ │ ├── 3_multi2binary.h │ │ ├── MaximumBinaryTree.h │ │ ├── MaximumMultipleTree.h │ │ ├── MaximumBinaryTreeRadiusSum.h │ │ └── MaximumBinaryTreeTest.cpp │ ├── Util.h │ └── Util.cpp ├── Search │ ├── BruteForce.h │ ├── DancingLink.h │ ├── BinarySearch.h │ ├── Recursion.h │ ├── BreadthFirstSearch.h │ ├── BidirectionalBreadthSearch.h │ ├── AStarSearch.h │ ├── BinarySearch.cpp │ ├── Recursion.cpp │ ├── BruteForce.cpp │ ├── BinarySearchTest.cpp │ ├── BreadthFirstSearchTest.cpp │ ├── CMakeLists.txt │ ├── BidirectionalBreadthSearchTest.cpp │ ├── Util.h │ ├── RecursionTest.cpp │ └── BruteForceTest.cpp ├── GameTheory │ ├── BashGame.cpp │ ├── BashGame.h │ ├── WythoffGame.h │ ├── NimGame.h │ ├── NimGame.cpp │ ├── CMakeLists.txt │ ├── WythoffGame.cpp │ ├── NimGameTest.cpp │ ├── BashGameTest.cpp │ └── WythoffGameTest.cpp ├── PatternMatch │ ├── Util.h │ ├── RabinKarp.h │ ├── SimpleMatch.h │ ├── KnuthMorrisPratt.h │ ├── Util.cpp │ ├── SimpleMatch.cpp │ ├── CMakeLists.txt │ ├── AhoCorasickAutomata.h │ ├── KnuthMorrisPrattTest.cpp │ ├── SimpleMatchTest.cpp │ ├── RabinKarpTest.cpp │ └── KnuthMorrisPratt.cpp ├── DataStructure │ ├── FenwickTree.h │ ├── DisjointSet.h │ ├── SegmentTree.h │ ├── RedBlackTree.h │ ├── PrefixTree.h │ ├── DisjointSet.cpp │ ├── SkipList.h │ ├── AvlTree.h │ ├── FenwickTree.cpp │ ├── SegmentTreeTest.cpp │ ├── LeftistTree.h │ ├── FenwickTreeTest.cpp │ ├── CMakeLists.txt │ └── AvlTreeTest.cpp ├── CMakeLists.txt └── LinearAlgebra │ └── Matrix │ └── 4_inverse_matrix.cpp ├── leetcode ├── leetcode-46.cpp ├── leetcode-40.hpp ├── leetcode-120.cpp └── leetcode-5.cpp ├── .gitignore ├── book.json ├── README.md └── LICENSE /docs/GraphTheory/ShortestPath/res/BellmanFord.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/PermutationGroup.hpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/Color/README.md: -------------------------------------------------------------------------------- 1 | # Coloring 染色问题 2 | -------------------------------------------------------------------------------- /docs/LinearAlgebra/Matrix/LUP/README.md: -------------------------------------------------------------------------------- 1 | # LUP 2 | # LUP分解 3 | 4 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/Coloring/README.md: -------------------------------------------------------------------------------- 1 | # Coloring 染色问题 2 | 3 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/CatalanTest.cpp: -------------------------------------------------------------------------------- 1 | int main() { return 0; } 2 | -------------------------------------------------------------------------------- /src/test.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | mkdir .bin 4 | cd .bin 5 | cmake .. 6 | -------------------------------------------------------------------------------- /docs/LinearAlgebra/LinearProgramming/Simplex/README.md: -------------------------------------------------------------------------------- 1 | # Simplex 2 | # 单纯形算法 3 | -------------------------------------------------------------------------------- /docs/LinearAlgebra/Matrix/InverseMatrix/README.md: -------------------------------------------------------------------------------- 1 | # InverseMatrix 2 | # 矩阵求逆 3 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/Hoproft-Karp/README.md: -------------------------------------------------------------------------------- 1 | # Hopcroft-Karp Hopcroft-Karp算法 2 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/KuhnMunkres/README.md: -------------------------------------------------------------------------------- 1 | # KuhnMunkres Kuhn-Munkres算法 2 | 3 | -------------------------------------------------------------------------------- /src/Sort/Util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void AssertAscend(const int *s, int n); 4 | 5 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/HopcroftKarp/README.md: -------------------------------------------------------------------------------- 1 | # HopcroftKarp Hopcroft-Karp算法 2 | 3 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/Kuhn-Munkres/README.md: -------------------------------------------------------------------------------- 1 | # Kuhn-Munkres 2 | # Kuhn-Munkres算法 3 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/MatchToMaxFlow/README.md: -------------------------------------------------------------------------------- 1 | # MatchToMaxflow 2 | # 二分匹配转化为最大流 3 | -------------------------------------------------------------------------------- /docs/LinearAlgebra/LinearProgramming/Dinkelback/README.md: -------------------------------------------------------------------------------- 1 | # Dinkelback 2 | # Dinkelback算法 3 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/PermutationGroup.cpp: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | return 0; 4 | } 5 | -------------------------------------------------------------------------------- /src/test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | mkdir -p .bin 4 | cd .bin 5 | cmake .. 6 | make -j 7 | -------------------------------------------------------------------------------- /docs/res/alipay.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/res/alipay.jpg -------------------------------------------------------------------------------- /docs/res/wxpay.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/res/wxpay.jpg -------------------------------------------------------------------------------- /docs/res/keyboard.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/res/keyboard.jpg -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/MinimumJoinPathCovering/README.md: -------------------------------------------------------------------------------- 1 | # MinimumJointPathCovering 2 | # 最小可相交路径覆盖 3 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/MinimumJointPathCovering/README.md: -------------------------------------------------------------------------------- 1 | # MinimumJointPathCovering 最小可相交路径覆盖 2 | 3 | -------------------------------------------------------------------------------- /docs/Sort/res/Sort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/Sort.png -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/MinimumDisjointPathCovering/README.md: -------------------------------------------------------------------------------- 1 | # MinimumDisjointPathCovering 2 | # 最小不相交路径覆盖 3 | -------------------------------------------------------------------------------- /docs/Search/res/Search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/Search.png -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/DijkstraTest.cpp: -------------------------------------------------------------------------------- 1 | #include "Dijkstra.h" 2 | 3 | int main(void) { return 0; } 4 | 5 | -------------------------------------------------------------------------------- /docs/Sort/res/BubbleSort1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/BubbleSort1.png -------------------------------------------------------------------------------- /docs/Sort/res/BubbleSort2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/BubbleSort2.png -------------------------------------------------------------------------------- /docs/Sort/res/BubbleSort3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/BubbleSort3.png -------------------------------------------------------------------------------- /docs/Sort/res/BubbleSort4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/BubbleSort4.png -------------------------------------------------------------------------------- /docs/Sort/res/BubbleSort5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/BubbleSort5.png -------------------------------------------------------------------------------- /docs/Sort/res/BubbleSort6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/BubbleSort6.png -------------------------------------------------------------------------------- /docs/Sort/res/BubbleSort7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/BubbleSort7.png -------------------------------------------------------------------------------- /docs/Sort/res/BubbleSort8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/BubbleSort8.png -------------------------------------------------------------------------------- /docs/Sort/res/BubbleSort9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/BubbleSort9.png -------------------------------------------------------------------------------- /docs/Sort/res/InsertSort1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/InsertSort1.png -------------------------------------------------------------------------------- /docs/Sort/res/InsertSort2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/InsertSort2.png -------------------------------------------------------------------------------- /docs/Sort/res/InsertSort3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/InsertSort3.png -------------------------------------------------------------------------------- /docs/Sort/res/InsertSort4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/InsertSort4.png -------------------------------------------------------------------------------- /docs/Sort/res/InsertSort5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/InsertSort5.png -------------------------------------------------------------------------------- /docs/Sort/res/MergeSort1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/MergeSort1.png -------------------------------------------------------------------------------- /docs/Sort/res/MergeSort2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/MergeSort2.png -------------------------------------------------------------------------------- /docs/Sort/res/QuickSort1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/QuickSort1.png -------------------------------------------------------------------------------- /docs/Sort/res/QuickSort2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/QuickSort2.png -------------------------------------------------------------------------------- /docs/Sort/res/QuickSort3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/QuickSort3.png -------------------------------------------------------------------------------- /docs/Sort/res/QuickSort4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Sort/res/QuickSort4.png -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/BellmanFordTest.cpp: -------------------------------------------------------------------------------- 1 | #include "BellmanFord.h" 2 | 3 | int main(void) { return 0; } 4 | 5 | -------------------------------------------------------------------------------- /src/NumberTheory/ModularExponentiation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | int ModularExponentiation(int b, int e, int m); 4 | 5 | -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/FloydWarshallTest.cpp: -------------------------------------------------------------------------------- 1 | #include "FloydWarshall.h" 2 | 3 | int main(void) { return 0; } 4 | 5 | -------------------------------------------------------------------------------- /docs/GameTheory/res/GameTheory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GameTheory/res/GameTheory.png -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/WeightedCoveringAndIndependentSet/README.md: -------------------------------------------------------------------------------- 1 | # WeightedCoveringAndIndependentSet 2 | # 最小点权覆盖和最大点权独立集 3 | -------------------------------------------------------------------------------- /docs/LinearAlgebra/res/Matrix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/LinearAlgebra/res/Matrix.png -------------------------------------------------------------------------------- /docs/Search/res/AStarSearch1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/AStarSearch1.png -------------------------------------------------------------------------------- /docs/Search/res/AStarSearch2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/AStarSearch2.png -------------------------------------------------------------------------------- /docs/Search/res/BinarySearch1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BinarySearch1.png -------------------------------------------------------------------------------- /docs/Search/res/BinarySearch2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BinarySearch2.png -------------------------------------------------------------------------------- /docs/Search/res/BinarySearch3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BinarySearch3.png -------------------------------------------------------------------------------- /docs/Search/res/DancingLink1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/DancingLink1.png -------------------------------------------------------------------------------- /docs/Search/res/DancingLink2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/DancingLink2.png -------------------------------------------------------------------------------- /docs/Search/res/DancingLink3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/DancingLink3.png -------------------------------------------------------------------------------- /docs/Search/res/DancingLink4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/DancingLink4.png -------------------------------------------------------------------------------- /docs/Search/res/DancingLink5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/DancingLink5.png -------------------------------------------------------------------------------- /docs/Search/res/DancingLink6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/DancingLink6.png -------------------------------------------------------------------------------- /docs/Search/res/DancingLink7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/DancingLink7.png -------------------------------------------------------------------------------- /docs/Search/res/DancingLink8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/DancingLink8.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree1.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree10.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree11.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree12.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree13.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree2.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree3.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree4.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree5.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree6.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree7.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree8.png -------------------------------------------------------------------------------- /docs/DataStructure/res/AVLTree9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/AVLTree9.png -------------------------------------------------------------------------------- /docs/DataStructure/res/SkipList1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/SkipList1.png -------------------------------------------------------------------------------- /docs/DataStructure/res/SkipList2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/SkipList2.png -------------------------------------------------------------------------------- /docs/DataStructure/res/SkipList3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/SkipList3.png -------------------------------------------------------------------------------- /docs/GameTheory/res/WythoffGame1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GameTheory/res/WythoffGame1.png -------------------------------------------------------------------------------- /docs/GameTheory/res/WythoffGame2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GameTheory/res/WythoffGame2.png -------------------------------------------------------------------------------- /docs/GameTheory/res/WythoffGame3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GameTheory/res/WythoffGame3.png -------------------------------------------------------------------------------- /docs/GraphTheory/res/GraphTheory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/res/GraphTheory.png -------------------------------------------------------------------------------- /docs/Search/res/KnowledgePoint1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/KnowledgePoint1.png -------------------------------------------------------------------------------- /docs/Search/res/KnowledgePoint2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/KnowledgePoint2.png -------------------------------------------------------------------------------- /docs/Search/res/KnowledgePoint3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/KnowledgePoint3.png -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/Cross.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Util.h" 3 | 4 | double Cross(const Vec &v1, const Vec &v2); 5 | 6 | -------------------------------------------------------------------------------- /src/DynamicProgramming/BagDP/CompleteBag.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | int CompleteBag(const int *v, const int *w, int n, int weight); 4 | 5 | -------------------------------------------------------------------------------- /src/DynamicProgramming/BagDP/ZeroOneBag.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | int ZeroOneBag(const int *v, const int *w, int n, int weight); 4 | 5 | -------------------------------------------------------------------------------- /src/Search/BruteForce.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | std::vector> BruteForce(int *s, int n, int m); 5 | -------------------------------------------------------------------------------- /docs/DataStructure/res/DisjointSet1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/DisjointSet1.png -------------------------------------------------------------------------------- /docs/DataStructure/res/DisjointSet2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/DisjointSet2.png -------------------------------------------------------------------------------- /docs/DataStructure/res/DisjointSet3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/DisjointSet3.png -------------------------------------------------------------------------------- /docs/DataStructure/res/DisjointSet4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/DisjointSet4.png -------------------------------------------------------------------------------- /docs/DataStructure/res/DisjointSet5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/DisjointSet5.png -------------------------------------------------------------------------------- /docs/DataStructure/res/DisjointSet6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/DisjointSet6.png -------------------------------------------------------------------------------- /docs/DataStructure/res/DisjointSet7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/DisjointSet7.png -------------------------------------------------------------------------------- /docs/DataStructure/res/DisjointSet8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/DisjointSet8.png -------------------------------------------------------------------------------- /docs/DataStructure/res/DisjointSet9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/DisjointSet9.png -------------------------------------------------------------------------------- /docs/DataStructure/res/FenwickTree1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/FenwickTree1.png -------------------------------------------------------------------------------- /docs/DataStructure/res/FenwickTree2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/FenwickTree2.png -------------------------------------------------------------------------------- /docs/DataStructure/res/FenwickTree4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/FenwickTree4.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree1.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree2.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree3.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree4.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree5.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree6.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree7.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree8.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree9.png -------------------------------------------------------------------------------- /docs/DataStructure/res/PrefixTree1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/PrefixTree1.png -------------------------------------------------------------------------------- /docs/DataStructure/res/SegmentTree1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/SegmentTree1.png -------------------------------------------------------------------------------- /docs/NumberTheory/res/NumberTheory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/NumberTheory/res/NumberTheory.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/AhoCorasick1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/AhoCorasick1.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/AhoCorasick10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/AhoCorasick10.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/AhoCorasick2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/AhoCorasick2.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/AhoCorasick3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/AhoCorasick3.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/AhoCorasick4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/AhoCorasick4.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/AhoCorasick5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/AhoCorasick5.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/AhoCorasick6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/AhoCorasick6.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/AhoCorasick7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/AhoCorasick7.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/AhoCorasick8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/AhoCorasick8.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/AhoCorasick9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/AhoCorasick9.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/PatternMatch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/PatternMatch.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/SimpleMatch1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/SimpleMatch1.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/SimpleMatch2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/SimpleMatch2.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/SimpleMatch3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/SimpleMatch3.png -------------------------------------------------------------------------------- /docs/Search/res/BreadthFirstSearch1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BreadthFirstSearch1.png -------------------------------------------------------------------------------- /docs/Search/res/BreadthFirstSearch2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BreadthFirstSearch2.png -------------------------------------------------------------------------------- /docs/Search/res/BreadthFirstSearch3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BreadthFirstSearch3.png -------------------------------------------------------------------------------- /docs/Search/res/BreadthFirstSearch4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BreadthFirstSearch4.png -------------------------------------------------------------------------------- /src/AnalyticGeometry/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | add_subdirectory(Polygon) 4 | add_subdirectory(ConvexHull) 5 | -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/BidirectionalSubsequence.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | int BidirectionalSubsequence(const int *s, int n); 4 | 5 | -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/DifferenceConstraintsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "DifferenceConstraints.h" 2 | 3 | int main(void) { return 0; } 4 | 5 | -------------------------------------------------------------------------------- /src/NumberTheory/ExtendedEuclid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | std::tuple ExtendedEuclid(int a, int b); 5 | 6 | -------------------------------------------------------------------------------- /src/NumberTheory/Util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void AssertGcd(int a, int b, int gcd); 4 | 5 | void AssertLcm(int a, int b, int lcm); 6 | 7 | -------------------------------------------------------------------------------- /docs/DataStructure/res/DataStructure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/DataStructure.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree10.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree11.png -------------------------------------------------------------------------------- /docs/DataStructure/res/LeftistTree12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/LeftistTree12.png -------------------------------------------------------------------------------- /docs/DataStructure/res/RedBlackTree.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/RedBlackTree.docx -------------------------------------------------------------------------------- /docs/DataStructure/res/RedBlackTree.vsdx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/RedBlackTree.vsdx -------------------------------------------------------------------------------- /docs/DataStructure/res/RedBlackTree1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/RedBlackTree1.png -------------------------------------------------------------------------------- /docs/DataStructure/res/RedBlackTree2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/RedBlackTree2.png -------------------------------------------------------------------------------- /docs/DataStructure/res/RedBlackTree3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/RedBlackTree3.png -------------------------------------------------------------------------------- /docs/DataStructure/res/RedBlackTree4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/RedBlackTree4.png -------------------------------------------------------------------------------- /docs/DataStructure/res/RedBlackTree5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/RedBlackTree5.png -------------------------------------------------------------------------------- /docs/DataStructure/res/RedBlackTree6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/RedBlackTree6.png -------------------------------------------------------------------------------- /docs/Search/res/BreadthFirstSearch11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BreadthFirstSearch11.png -------------------------------------------------------------------------------- /docs/Search/res/BreadthFirstSearch12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BreadthFirstSearch12.png -------------------------------------------------------------------------------- /docs/Search/res/BreadthFirstSearch20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BreadthFirstSearch20.png -------------------------------------------------------------------------------- /docs/Search/res/BreadthFirstSearch21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BreadthFirstSearch21.png -------------------------------------------------------------------------------- /src/GraphTheory/StronglyConnectedComponents/TwoSatisfiabilityTest.cpp: -------------------------------------------------------------------------------- 1 | #include "TwoSatisfiability.h" 2 | 3 | int main(void) { return 0; } 4 | 5 | -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/Cross1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/Cross1.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/Cross2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/Cross2.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/Cross3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/Cross3.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree1.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree2.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree3.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree4.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree5.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree6.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree7.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree8.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree9.png -------------------------------------------------------------------------------- /docs/GraphTheory/NetworkFlow/res/Dinic1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/NetworkFlow/res/Dinic1.png -------------------------------------------------------------------------------- /docs/PatternMatch/res/KnuthMorrisPratt1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/PatternMatch/res/KnuthMorrisPratt1.png -------------------------------------------------------------------------------- /src/GameTheory/BashGame.cpp: -------------------------------------------------------------------------------- 1 | #include "BashGame.h" 2 | 3 | bool BashGame(int n, int m) { 4 | int s = n % (m + 1); 5 | return s != 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /docs/AnalyticGeometry/res/AnalyticGeometry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/res/AnalyticGeometry.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree10.png -------------------------------------------------------------------------------- /docs/DataStructure/res/BinarySearchTree11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DataStructure/res/BinarySearchTree11.png -------------------------------------------------------------------------------- /docs/GraphTheory/MinSpanningTree/res/Prim1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/MinSpanningTree/res/Prim1.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/EulerCycle1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/EulerCycle1.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/EulerCycle2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/EulerCycle2.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/EulerCycle3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/EulerCycle3.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/EulerCycle4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/EulerCycle4.png -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/Cross.cpp: -------------------------------------------------------------------------------- 1 | #include "Cross.h" 2 | 3 | double Cross(const Vec &v1, const Vec &v2) { return v1.x * v2.y - v2.x * v1.y; } 4 | 5 | -------------------------------------------------------------------------------- /src/GameTheory/BashGame.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | /** 5 | * @return true若赢 false若输 6 | */ 7 | bool BashGame(int n, int m); 8 | 9 | -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/Sweeping1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/Sweeping1.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/Sweeping2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/Sweeping2.png -------------------------------------------------------------------------------- /docs/GraphTheory/MinSpanningTree/res/Kruskal1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/MinSpanningTree/res/Kruskal1.png -------------------------------------------------------------------------------- /docs/GraphTheory/NetworkFlow/res/EdmondsKarp1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/NetworkFlow/res/EdmondsKarp1.png -------------------------------------------------------------------------------- /docs/GraphTheory/NetworkFlow/res/EdmondsKarp2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/NetworkFlow/res/EdmondsKarp2.png -------------------------------------------------------------------------------- /docs/GraphTheory/NetworkFlow/res/EdmondsKarp3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/NetworkFlow/res/EdmondsKarp3.png -------------------------------------------------------------------------------- /docs/GraphTheory/NetworkFlow/res/EdmondsKarp4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/NetworkFlow/res/EdmondsKarp4.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/KnowledgePoint1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/KnowledgePoint1.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/KnowledgePoint2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/KnowledgePoint2.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/KnowledgePoint3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/KnowledgePoint3.png -------------------------------------------------------------------------------- /docs/Search/res/BidirectionalBreadthSearch1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BidirectionalBreadthSearch1.png -------------------------------------------------------------------------------- /docs/Search/res/BidirectionalBreadthSearch2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BidirectionalBreadthSearch2.png -------------------------------------------------------------------------------- /docs/Search/res/BidirectionalBreadthSearch3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BidirectionalBreadthSearch3.png -------------------------------------------------------------------------------- /docs/Search/res/BidirectionalBreadthSearch7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BidirectionalBreadthSearch7.png -------------------------------------------------------------------------------- /docs/Search/res/BidirectionalBreadthSearch8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/Search/res/BidirectionalBreadthSearch8.png -------------------------------------------------------------------------------- /src/GameTheory/WythoffGame.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | /** 5 | * @return true若赢 false若输 6 | */ 7 | bool WythoffGame(int p, int k); 8 | 9 | -------------------------------------------------------------------------------- /src/NumberTheory/Euclid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | int GreatestCommonDivisor(int a, int b); 5 | int LeastCommonMultiple(int a, int b); 6 | 7 | -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/DepthFirstSearch1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/DepthFirstSearch1.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/TopologicalSort.vsdx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/TopologicalSort.vsdx -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/TopologicalSort1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/TopologicalSort1.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/TopologicalSort2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/TopologicalSort2.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/TopologicalSort3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/TopologicalSort3.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/TopologicalSort4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/TopologicalSort4.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/ConvexHull/res/ConvexHull1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/ConvexHull/res/ConvexHull1.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/ConvexHull/res/GrahamScan1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/ConvexHull/res/GrahamScan1.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/ConvexHull/res/GrahamScan2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/ConvexHull/res/GrahamScan2.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/ConvexHull/res/GrahamScan3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/ConvexHull/res/GrahamScan3.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/ConvexHull/res/GrahamScan4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/ConvexHull/res/GrahamScan4.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/NearestNeighbor1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/NearestNeighbor1.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/NearestNeighbor2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/NearestNeighbor2.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/RegionalDP/res/UniquePath1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/RegionalDP/res/UniquePath1.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/TreeDP/res/MaxBinaryTree1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/TreeDP/res/MaxBinaryTree1.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/TreeDP/res/MaxBinaryTree2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/TreeDP/res/MaxBinaryTree2.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/TreeDP/res/MaxBinaryTree3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/TreeDP/res/MaxBinaryTree3.png -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/Introduction-Domination-Independent-Covering-Clique/README.md: -------------------------------------------------------------------------------- 1 | # Introduction - Domination,Independent,Covering,Clique 2 | # 介绍支配集、独立集、覆盖集和团 3 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/Introduction-Domination_Independent_Covering_Clique/README.md: -------------------------------------------------------------------------------- 1 | # Introduction-Domination,Independent,Covering,Clique 介绍支配集、独立集、覆盖集和团 2 | 3 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/res/KnowledgePoint1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/BinaryMatch/res/KnowledgePoint1.png -------------------------------------------------------------------------------- /docs/GraphTheory/NetworkFlow/res/KnowledgePoint1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/NetworkFlow/res/KnowledgePoint1.png -------------------------------------------------------------------------------- /docs/GraphTheory/NetworkFlow/res/MinimumCostFlow1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/NetworkFlow/res/MinimumCostFlow1.png -------------------------------------------------------------------------------- /docs/GraphTheory/NetworkFlow/res/MinimumCostFlow2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/NetworkFlow/res/MinimumCostFlow2.png -------------------------------------------------------------------------------- /docs/GraphTheory/ShortestPath/res/KnowledgePoint1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/ShortestPath/res/KnowledgePoint1.png -------------------------------------------------------------------------------- /docs/GraphTheory/Traverse/res/BreadthFirstSearch1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/Traverse/res/BreadthFirstSearch1.png -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/LongestIncreasingSubsequence.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // s的范围是[1,n] 4 | 5 | int LongestIncreasingSubsequence(const int *s, int n); 6 | 7 | -------------------------------------------------------------------------------- /src/NumberTheory/ChineseRemainerTheorem.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 1024 4 | #endif 5 | 6 | int ChineseRemainerTheorem(int *a, int *m, int n); 7 | 8 | -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/ConvexPolygonArea1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/ConvexPolygonArea1.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/ConvexPolygonArea2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/ConvexPolygonArea2.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/RegionalDP/res/MinMergeCost1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/RegionalDP/res/MinMergeCost1.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/RegionalDP/res/TrianglePath1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/RegionalDP/res/TrianglePath1.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/RegionalDP/res/TrianglePath2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/RegionalDP/res/TrianglePath2.png -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/MaximumContinuousSubsequenceSum.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // s范围是[1,n] 4 | 5 | int MaximumContinuousSubsequenceSum(const int *s, int n); 6 | 7 | -------------------------------------------------------------------------------- /src/DynamicProgramming/RegionalDP/UniquePath.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef MAX 4 | #define MAX 128 5 | #endif 6 | 7 | int UniquePath(int n, int m); 8 | 9 | -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/SegmentIntersection1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/SegmentIntersection1.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/SegmentIntersection2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/SegmentIntersection2.png -------------------------------------------------------------------------------- /docs/AnalyticGeometry/Polygon/res/SegmentIntersection3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/AnalyticGeometry/Polygon/res/SegmentIntersection3.png -------------------------------------------------------------------------------- /docs/BasicKnowledge/README.md: -------------------------------------------------------------------------------- 1 | # Chapter-1 Basic Knowledge 2 | # 第1章 基础知识 3 | 4 | -------- 5 | 6 | 1. [TimeComplexity 时间复杂度](TimeComplexity/) 7 | 2. [Recursion 递归式](Recursion/) 8 | -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/LongestCommonSubsequence.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/src/DynamicProgramming/LinearDP/LongestCommonSubsequence.h -------------------------------------------------------------------------------- /src/PatternMatch/Util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | bool StringEq(const std::string &s1, int p1, const std::string &s2, int p2, 5 | int len); 6 | 7 | -------------------------------------------------------------------------------- /docs/CombinatorialMathematics/res/CombinationMathematics.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/CombinatorialMathematics/res/CombinationMathematics.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/TreeDP/res/MaxBinaryTreeRadiusSum1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/TreeDP/res/MaxBinaryTreeRadiusSum1.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/TreeDP/res/MaxBinaryTreeRadiusSum2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/TreeDP/res/MaxBinaryTreeRadiusSum2.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/TreeDP/res/MaxBinaryTreeRadiusSum3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/TreeDP/res/MaxBinaryTreeRadiusSum3.png -------------------------------------------------------------------------------- /docs/DynamicProgramming/TreeDP/res/MaxBinaryTreeRadiusSum4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/DynamicProgramming/TreeDP/res/MaxBinaryTreeRadiusSum4.png -------------------------------------------------------------------------------- /docs/GraphTheory/StronglyConnectedComponents/res/Kosaraju1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/StronglyConnectedComponents/res/Kosaraju1.png -------------------------------------------------------------------------------- /docs/GraphTheory/StronglyConnectedComponents/res/Kosaraju2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/StronglyConnectedComponents/res/Kosaraju2.png -------------------------------------------------------------------------------- /docs/GraphTheory/StronglyConnectedComponents/res/Kosaraju3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linrongbin16/way-to-algorithm/HEAD/docs/GraphTheory/StronglyConnectedComponents/res/Kosaraju3.png -------------------------------------------------------------------------------- /src/DynamicProgramming/RegionalDP/MinimumMergeCost.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef INF 4 | #define INF INT_MAX 5 | #endif 6 | 7 | int MinimumMergeCost(int *s, int n); 8 | 9 | -------------------------------------------------------------------------------- /src/DynamicProgramming/BagDP/TwoDimensionBag.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | int TwoDimensionBag(const int *v, const int *w1, const int *w2, int n, 4 | int weight1, int weight2); 5 | 6 | -------------------------------------------------------------------------------- /src/GraphTheory/Traverse/DepthFirstSearch.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef MAX 4 | #define MAX 64 5 | #endif 6 | 7 | std::vector DepthFirstSearch(int g[MAX][MAX], int n); 8 | 9 | -------------------------------------------------------------------------------- /src/GraphTheory/Traverse/BreadthFirstSearch.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef MAX 4 | #define MAX 64 5 | #endif 6 | 7 | std::vector BreadthFirstSearch(int g[MAX][MAX], int n); 8 | 9 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/FullPermutation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef MAX 4 | #define MAX 1024 5 | #endif 6 | 7 | std::vector> FullPermutation(int s[MAX], int n); 8 | 9 | -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/LongestPalindromicSubsequence.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | // s的范围是[1,n] 5 | 6 | std::pair LongestPalindromicSubsequence(const int *s, int n); 7 | 8 | -------------------------------------------------------------------------------- /src/DynamicProgramming/TreeDP/MaximumMultipleTreeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "MaximumMultipleTree.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(void) { return 0; } 7 | 8 | -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate 2 | layout: post 3 | markdown: kramdown 4 | mathjax: true 5 | comments: true 6 | title: "Way-to-Algorithm" 7 | date: 2018-09-01 12:00:00 8 | categories: github jekyll 9 | -------------------------------------------------------------------------------- /src/GameTheory/NimGame.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef MAX 4 | #define MAX 128 5 | #endif 6 | 7 | /** 8 | * @return true若赢 false若输 9 | */ 10 | bool NimGame(int *s, int n); 11 | 12 | -------------------------------------------------------------------------------- /src/GraphTheory/StronglyConnectedComponents/Kosaraju.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef MAX 4 | #define MAX 64 5 | #endif 6 | 7 | std::vector> Kosaraju(int g[MAX][MAX], int n); 8 | 9 | -------------------------------------------------------------------------------- /src/GraphTheory/MinSpanningTree/Kruskal.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Util.h" 3 | #include 4 | #ifndef MAX 5 | #define MAX 64 6 | #endif 7 | 8 | std::vector Kruskal(int g[MAX][MAX], int n); 9 | 10 | -------------------------------------------------------------------------------- /src/GraphTheory/StronglyConnectedComponents/TwoSatisfiability.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef MAX 4 | #define MAX 64 5 | #endif 6 | 7 | std::vector> TwoSatisfiability(int g[MAX][MAX], int n); 8 | 9 | -------------------------------------------------------------------------------- /src/PatternMatch/RabinKarp.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 1024 4 | #endif 5 | #include 6 | #include 7 | 8 | std::vector RabinKarp(const std::string &text, const std::string &pattern); 9 | 10 | -------------------------------------------------------------------------------- /src/DynamicProgramming/RegionalDP/TrianglePath.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef MAX 4 | #define MAX 1024 5 | #endif 6 | #ifndef INF 7 | #define INF INT_MAX 8 | #endif 9 | 10 | int TrianglePath(int *s, int k); 11 | 12 | -------------------------------------------------------------------------------- /src/GameTheory/NimGame.cpp: -------------------------------------------------------------------------------- 1 | #include "NimGame.h" 2 | #include 3 | 4 | bool NimGame(int *s, int n) { 5 | int sum = 0; 6 | for (int i = 0; i < n; i++) { 7 | sum = sum ^ s[i]; 8 | } 9 | return sum != 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/FloydWarshall.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #ifndef MAX 5 | #define MAX 64 6 | #endif 7 | 8 | std::vector> FloydWarshall(int g[MAX][MAX], int n); 9 | 10 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/ConvexHull/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | set(SRC_FILE 4 | ../Util.cpp 5 | GrahamScan.cpp 6 | ) 7 | 8 | #add_executable(AnalyticGeometry-GrahamScan.exe GrahamScanTest.cpp ${SRC_FILE}) 9 | -------------------------------------------------------------------------------- /docs/GameTheory/README.md: -------------------------------------------------------------------------------- 1 | # Chapter-12 Game Theory 2 | # 第12章 博弈论 3 | 4 | ![GameTheory.png](res/GameTheory.png) 5 | 6 | -------- 7 | 8 | 1. [BashGame 巴什博弈](BashGame/) 9 | 2. [WythoffGame 威佐夫博弈](WythoffGame/) 10 | 3. [NimGame 尼姆博弈](NimGame/) 11 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/Combination.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #ifndef MAX 5 | #define MAX 1024 6 | #endif 7 | 8 | std::vector> Combination(int s[MAX], int n, int m); 9 | 10 | -------------------------------------------------------------------------------- /src/GraphTheory/StronglyConnectedComponents/TwoSatisfiability.cpp: -------------------------------------------------------------------------------- 1 | #include "TwoSatisfiability.h" 2 | 3 | std::vector> TwoSatisfiability(int g[MAX][MAX], int n) { 4 | std::vector> result; 5 | return result; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /src/Sort/BubbleSort.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /** 4 | * @brief BubbleSort 冒泡排序 5 | * @param s[MAX] 无序序列 6 | * @param beg 序列s的起始下标 7 | * @param end 序列s是左闭右开区间[beg, end) 8 | */ 9 | void BubbleSort(int *s, int beg, int end); 10 | 11 | -------------------------------------------------------------------------------- /src/Sort/InsertSort.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /** 4 | * @brief InsertSort 插入排序 5 | * @param s[MAX] 无序序列 6 | * @param beg 序列s的起始下标 7 | * @param end 序列s为左闭右开区间[beg, end) 8 | */ 9 | void InsertSort(int *s, int beg, int end); 10 | 11 | -------------------------------------------------------------------------------- /src/Sort/MergeSort.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /** 4 | * @brief MergeSort 归并排序 5 | * @param s[MAX] 无序序列 6 | * @param beg 序列s的起始下标 7 | * @param end 序列s为左闭右开区间[beg, end) 8 | */ 9 | void MergeSort(int *s, int beg, int end); 10 | 11 | -------------------------------------------------------------------------------- /src/Sort/QuickSort.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /** 4 | * @brief QuickSort 5 | * @param s[MAX] 无序序列 6 | * @param beg 序列s的起始下标 7 | * @param end 序列s的末尾下标加1,即左闭右开区间[beg, end) 8 | */ 9 | void QuickSort(int *s, int beg, int end); 10 | 11 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/SegmentIntersection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Util.h" 3 | 4 | /** 5 | * @param s1, s2 两线段 6 | * @return false 若两线段不相交 true 若两线段相交 7 | */ 8 | bool SegmentIntersection(const Segment &s1, const Segment &s2); 9 | 10 | -------------------------------------------------------------------------------- /src/PatternMatch/SimpleMatch.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #ifndef MAX 5 | #define MAX 1024 6 | #endif 7 | 8 | std::vector SimpleMatch(const std::string &text, 9 | const std::string &pattern); 10 | -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/Dijkstra.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #ifndef MAX 5 | #define MAX 64 6 | #endif 7 | #ifndef INF 8 | #define INF INT_MAX 9 | #endif 10 | 11 | std::vector Dijkstra(int g[MAX][MAX], int n, int beg); 12 | 13 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/Sweeping.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Util.h" 3 | #ifndef MAX 4 | #define MAX 1024 5 | #endif 6 | 7 | /** 8 | * @param s 一组线段 9 | * @param n 线段数量 10 | * @return true存在相交 false不存在 11 | */ 12 | bool Sweeping(Segment *s, int n); 13 | 14 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/print_status.cpp: -------------------------------------------------------------------------------- 1 | #include "general_head.h" 2 | #include "com_local.h" 3 | 4 | void print_status(int *s, int beg, int end) 5 | {//[beg, end)是左闭右开区间,序列s下标从beg到end-1 6 | for(int i = beg; i < end; ++ i) 7 | cout << s[i] << ' '; 8 | cout << endl; 9 | } 10 | -------------------------------------------------------------------------------- /src/GraphTheory/MinSpanningTree/Prim.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Util.h" 3 | #include 4 | #include 5 | #ifndef MAX 6 | #define MAX 64 7 | #endif 8 | #ifndef INF 9 | #define INF INT_MAX 10 | #endif 11 | 12 | std::vector Prim(int g[MAX][MAX], int n); 13 | 14 | -------------------------------------------------------------------------------- /src/PatternMatch/KnuthMorrisPratt.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 1024 4 | #endif 5 | #include 6 | #include 7 | 8 | std::vector KnuthMorrisPratt(const std::string &text, 9 | const std::string &pattern); 10 | 11 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/ConvexHull/GrahamScan.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Polygon/Cross.h" 3 | #include "../Util.h" 4 | #include 5 | #include 6 | #ifndef INF 7 | #define INF INT_MAX 8 | #endif 9 | 10 | void GrahamScan(std::vector s, std::stack &stk); 11 | 12 | -------------------------------------------------------------------------------- /src/GraphTheory/StronglyConnectedComponents/Tarjan.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #ifndef MAX 5 | #define MAX 64 6 | #endif 7 | #ifndef INF 8 | #define INF INT_MAX 9 | #endif 10 | 11 | std::vector> Tarjan(int g[MAX][MAX], int n); 12 | 13 | -------------------------------------------------------------------------------- /src/Sort/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include "BubbleSort.h" 2 | #include 3 | 4 | void BubbleSort(int *s, int beg, int end) { 5 | for (int i = end - 1; i >= beg; i--) 6 | for (int j = beg; j < i; j++) 7 | if (s[j] > s[j + 1]) 8 | std::swap(s[j], s[j + 1]); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /src/DynamicProgramming/Util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | int *ArrayNew(int n); 4 | 5 | void ArrayFree(int *f); 6 | 7 | int **Array2DNew(int n, int m); 8 | 9 | void Array2DFree(int **f, int n); 10 | 11 | int ***Array3DNew(int n, int m, int t); 12 | 13 | void Array3DFree(int ***f, int n, int m); 14 | 15 | -------------------------------------------------------------------------------- /src/GraphTheory/Traverse/EulerCycle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #ifndef MAX 5 | #define MAX 64 6 | #endif 7 | 8 | // 无向图 9 | // bool表示该无向图是否存在欧拉回路 10 | // vector表示该无向图的欧拉回路的节点顺序 11 | std::pair> EulerCycle(int g[MAX][MAX], int n); 12 | 13 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/Catalan.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "../Calculation/LargeNumber.hpp" 8 | #ifndef MAX 9 | #define MAX 1024 10 | #endif 11 | 12 | Number* Catalan(const Number& a); 13 | -------------------------------------------------------------------------------- /src/NumberTheory/PrimeSieve.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | struct BitVec { 5 | int n; 6 | unsigned char **bits; 7 | }; 8 | 9 | BitVec *SimpleSieve(int n); 10 | 11 | BitVec *EratosthenesSieve(int n); 12 | 13 | bool IsPrime(const BitVec *p, int x); 14 | 15 | void BitVecFree(BitVec *p); 16 | 17 | -------------------------------------------------------------------------------- /src/DynamicProgramming/TreeDP/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | project(WayToAlgorithm CXX) 4 | 5 | # add_executable(BubbleSort.exe BubbleSort.cpp) 6 | # add_executable(InsertSort.exe InsertSort.cpp) 7 | # add_executable(MergeSort.exe MergeSort.cpp) 8 | # add_executable(QuickSort.exe QuickSort.cpp) 9 | -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/BellmanFord.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #ifndef MAX 6 | #define MAX 64 7 | #endif 8 | #ifndef INF 9 | #define INF INT_MAX 10 | #endif 11 | 12 | std::pair> BellmanFord(int g[MAX][MAX], int n, int beg); 13 | 14 | -------------------------------------------------------------------------------- /src/GraphTheory/Traverse/TopologicalSort.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 64 4 | #endif 5 | #include 6 | 7 | /** 8 | * @brief 拓扑排序 9 | * @param g 有向图 10 | * @param n 图中节点数量 下标为[0, n-1] 11 | * @return 拓扑排序结果 12 | */ 13 | std::vector TopologicalSort(int g[MAX][MAX], int n); 14 | 15 | -------------------------------------------------------------------------------- /src/DynamicProgramming/TreeDP/3_multi2binary.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | //多叉树转二叉树动规 5 | //multi to binary 6 | 7 | //仍然是同一个多叉树求最大权值的问题 8 | 9 | //将多叉树动规问题转化为二叉树动规来求解: 10 | //将多叉树转化为一个二叉树森林 11 | //对于原本多叉树中的每个节点 12 | //其左孩子是当前节点的所有孩子中的第一个孩子节点指针 13 | //其右孩子是当前节点的所有兄弟 14 | //对这样的森林中的每个二叉树递归调用二叉树动规的方法 15 | //就可以求出多叉树的最大权值 16 | -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/DifferenceConstraints.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #ifndef MAX 5 | #define MAX 64 6 | #endif 7 | 8 | std::pair> DifferenceConstraints(int a[MAX][MAX], int n, 9 | int m, int *b); 10 | 11 | -------------------------------------------------------------------------------- /src/DataStructure/FenwickTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 1024 4 | #endif 5 | 6 | struct FwTree { 7 | int bits[MAX]; 8 | }; 9 | 10 | FwTree *FenwickTreeNew(); 11 | void FenwickTreeFree(FwTree *t); 12 | void FenwickTreeAdd(FwTree *t, int i, int value); 13 | int FenwickTreeSum(FwTree *t, int i, int j); 14 | 15 | -------------------------------------------------------------------------------- /src/NumberTheory/Euclid.cpp: -------------------------------------------------------------------------------- 1 | #include "Euclid.h" 2 | 3 | int GreatestCommonDivisor(int a, int b) { 4 | if (b == 0) { 5 | return a; 6 | } 7 | return GreatestCommonDivisor(b, a % b); 8 | } 9 | 10 | int LeastCommonMultiple(int a, int b) { 11 | int gcd = GreatestCommonDivisor(a, b); 12 | return gcd * (a / gcd) * (b / gcd); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /src/Sort/Util.cpp: -------------------------------------------------------------------------------- 1 | #include "Util.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void AssertAscend(const int *s, int n) { 7 | bool asc = true; 8 | for (int i = 0; i < n - 1; i++) { 9 | if (s[i] > s[i + 1]) { 10 | asc = false; 11 | break; 12 | } 13 | } 14 | assert(asc); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/ConvexPolygonAreaTest.cpp: -------------------------------------------------------------------------------- 1 | #include "ConvexPolygonArea.h" 2 | #include "../Util.h" 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define TEST_MAX 8 8 | 9 | struct Test { 10 | Node convex[TEST_MAX]; 11 | int n; 12 | double result; 13 | } tests[] = { 14 | {{}}, 15 | }; 16 | 17 | int main() { return 0; } 18 | 19 | -------------------------------------------------------------------------------- /docs/LinearAlgebra/Matrix/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Section-1 Matrix 矩阵 4 | 5 | -------- 6 | 7 | 1. [Strassen Strassen算法](Strassen/) 8 | 2. [GaussElimination 高斯消元法](GaussElimination/) 9 | 3. [LUP LUP分解](LUP/) 10 | 4. [InverseMatrix 矩阵求逆](InverseMatrix/) 11 | -------------------------------------------------------------------------------- /src/GameTheory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | set(SRC_FILE 4 | BashGame.cpp 5 | WythoffGame.cpp 6 | NimGame.cpp 7 | ) 8 | 9 | add_executable(GameTheory-BashGame.exe BashGameTest.cpp ${SRC_FILE}) 10 | add_executable(GameTheory-WythoffGame.exe WythoffGameTest.cpp ${SRC_FILE}) 11 | add_executable(GameTheory-NimGame.exe NimGameTest.cpp ${SRC_FILE}) 12 | -------------------------------------------------------------------------------- /src/PatternMatch/Util.cpp: -------------------------------------------------------------------------------- 1 | #include "Util.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | bool StringEq(const std::string &s1, int p1, const std::string &s2, int p2, 8 | int len) { 9 | for (int i = 0; i < len; i++) { 10 | if (s1[p1 + i] != s2[p2 + i]) 11 | return false; 12 | } 13 | return true; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /src/Search/DancingLink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #ifndef MAX 5 | #define MAX 128 6 | #endif 7 | 8 | /** 9 | * @param n 集合s的元素[1,n] 10 | * @param m 子集集合t的数量[s1,s2,...,sm] 11 | * @param s s[i][j] = 1表示子集s[i]包含元素j 12 | * @return 返回的数组ret[i] = 1表示精确覆盖中包括子集s[i] 13 | */ 14 | std::pair> DancingLink(int n, int m, int s[MAX][MAX]); 15 | 16 | -------------------------------------------------------------------------------- /src/DynamicProgramming/TreeDP/MaximumBinaryTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 1024 4 | #endif 5 | 6 | struct BiNode { 7 | int index; 8 | int value; 9 | BiNode *left; 10 | BiNode *right; 11 | }; 12 | 13 | BiNode *MaximumBinaryTreeNew(int *value, int *left, int *right, int n); 14 | 15 | void MaximumBinaryTreeFree(BiNode *t); 16 | 17 | int MaximumBinaryTree(BiNode *t, int n, int m); 18 | 19 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/ConvexPolygonArea.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Util.h" 3 | 4 | /** 5 | * @param a 三角形端点数组 6 | * @param n 三角形端点数量 7 | * @return 三角形面积 8 | */ 9 | double TriangleArea(const Node &a, const Node &b, const Node &c); 10 | 11 | /** 12 | * @param a 凸多边形端点数组 13 | * @param n 凸多边形端点数量 14 | * @return 凸多边形面积 15 | */ 16 | double ConvexPolygonArea(Node *a, int n); 17 | 18 | -------------------------------------------------------------------------------- /src/Sort/QuickSortTest.cpp: -------------------------------------------------------------------------------- 1 | #include "QuickSort.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define MAX 1024 8 | 9 | int main(void) { 10 | int s[MAX]; 11 | for (int i = 1; i < MAX; i++) { 12 | for (int j = 0; j < i; j++) { 13 | s[j] = rand() % MAX; 14 | } 15 | QuickSort(s, 0, i); 16 | AssertAscend(s, i); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /src/Sort/BubbleSortTest.cpp: -------------------------------------------------------------------------------- 1 | #include "BubbleSort.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define MAX 1024 8 | 9 | int main(void) { 10 | int s[MAX]; 11 | for (int i = 1; i < MAX; i++) { 12 | for (int j = 0; j < i; j++) { 13 | s[j] = rand() % MAX; 14 | } 15 | BubbleSort(s, 0, i); 16 | AssertAscend(s, i); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /src/Sort/InsertSortTest.cpp: -------------------------------------------------------------------------------- 1 | #include "InsertSort.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define MAX 1024 8 | 9 | int main(void) { 10 | int s[MAX]; 11 | for (int i = 1; i < MAX; i++) { 12 | for (int j = 0; j < i; j++) { 13 | s[j] = rand() % MAX; 14 | } 15 | InsertSort(s, 0, i); 16 | AssertAscend(s, i); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /src/GraphTheory/NetworkFlow/6_highest_label_preflow_push.cpp: -------------------------------------------------------------------------------- 1 | //最高标号预留与推进算法 2 | //highest_label_preflow_push.cpp 3 | 4 | //最高标号预留与推进算法属于预留与推进类算法,用该算法求解最大流问题 5 | 6 | //使用了距离标号算法中的距离标号d(预留与推进类算法中称之为高度函数h) 7 | //维护一个以距离函数d为优先级的队列 8 | //优先处理d值较高的节点,对该节点进行排除操作(见重标记与前移算法) 9 | //直到该队列为空 10 | //它与重标记与前移算法的唯一的在说法上的区别就是它们的队列优先级不一样 11 | //但实际上该算法就是重标记与前移算法,该算法可以说没什么意义 12 | 13 | //int highest_label_preflow_push(graph_list residue, int beg, int end) 14 | -------------------------------------------------------------------------------- /docs/BasicKnowledge/Recursion/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Recursion - 递归式 4 | 5 | -------- 6 | 7 | #### 简介 8 | 9 | -------- 10 | 11 | #### Introduction to Algorithms 12 | 13 | * [I.Foundations - 2.Getting Started - 2.3.Designing algorithms](https://www.google.com/search?q=Introduction+to+Algorithms+3rd+Edition+pdf) 14 | -------------------------------------------------------------------------------- /src/DataStructure/DisjointSet.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 1024 4 | #endif 5 | 6 | struct DjSet { 7 | int father[MAX]; 8 | }; 9 | 10 | // create DisjointSet 11 | DjSet *DisjointSetNew(); 12 | 13 | // free DisjointSet 14 | void DisjointSetFree(DjSet *s); 15 | 16 | // union DisjointSet 17 | void DisjointSetUnion(DjSet *s, int i, int j); 18 | 19 | // query DisjointSet 20 | bool DisjointSetQuery(DjSet *s, int i, int j); 21 | 22 | -------------------------------------------------------------------------------- /src/Search/BinarySearch.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef MAX 4 | #define MAX 1024 5 | #endif 6 | 7 | /** 8 | * 在升序序列s中找出x的位置(下标) 9 | * @params s 升序序列 10 | * @params beg 序列s的起始下标 11 | * @params end 序列s的末尾下标加1,即左闭右开区间[beg, end) 12 | * @params index 返回找到的x下标 13 | * @return 找到x返回true 否则返回false 14 | */ 15 | std::pair BinarySearch(int *s, int beg, int end, int x); 16 | 17 | -------------------------------------------------------------------------------- /src/Search/Recursion.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifndef MAX 4 | #define MAX 1024 5 | #endif 6 | 7 | /** 8 | * @brief Recursion 递归 9 | * @param s 所有成员的选择 s[i]表示成员i的选择 10 | * @param n 成员数量 11 | * @param m 每个成员的选择数量 12 | * @param prev 上一个选择过的成员下标 13 | * @param result 所有选择的集合 14 | */ 15 | void Recursion(int *s, int n, int m, int prev, 16 | std::vector> &result); 17 | 18 | -------------------------------------------------------------------------------- /src/GameTheory/WythoffGame.cpp: -------------------------------------------------------------------------------- 1 | #include "WythoffGame.h" 2 | #include 3 | 4 | double phi = ((double)1.0 + (double)sqrt(5.0f)) / (double)2.0; 5 | 6 | bool WythoffGame(int p, int k) { 7 | int xa = (int)((double)k / phi); 8 | int xb = (int)((double)k * phi) + 1; 9 | int yc = (int)((double)p * phi) + 1; 10 | int yd = (int)((double)p / phi); 11 | 12 | // 边缘点必输 13 | // 否则必赢 14 | return !(k == yc || k == yd || p == xa || p == xb); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /src/Search/BreadthFirstSearch.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Util.h" 3 | #include 4 | #ifndef MAX 5 | #define MAX 128 6 | #endif 7 | 8 | /** 9 | * BreadthFirstSearch 广度优先搜索 10 | * @param n 行 11 | * @param m 列 12 | * @param beg 起点座标 13 | * @param end 终点座标 14 | * @return 从beg到end的搜索路径 15 | */ 16 | std::vector BreadthFirstSearch(int n, int m, const BfsNode &beg, 17 | const BfsNode &end); 18 | 19 | -------------------------------------------------------------------------------- /src/Sort/MergeSortTest.cpp: -------------------------------------------------------------------------------- 1 | #include "MergeSort.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define MAX 1024 8 | 9 | int main(void) { 10 | int s[MAX]; 11 | for (int i = 1; i < MAX; i++) { 12 | std::cout << i << std::endl; 13 | for (int j = 0; j < i; j++) { 14 | s[j] = rand() % MAX; 15 | } 16 | MergeSort(s, 0, i); 17 | AssertAscend(s, i); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /src/NumberTheory/EuclidTest.cpp: -------------------------------------------------------------------------------- 1 | #include "Euclid.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | 6 | #define MAX 4096 7 | 8 | int main(void) { 9 | for (int i = 0; i < MAX; i++) { 10 | int a = abs(rand() % MAX); 11 | int b = abs(rand() % MAX); 12 | int gcd = GreatestCommonDivisor(a, b); 13 | int lcm = LeastCommonMultiple(a, b); 14 | AssertGcd(a, b, gcd); 15 | AssertLcm(a, b, lcm); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /src/Sort/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | set(SRC_FILE 4 | BubbleSort.cpp 5 | InsertSort.cpp 6 | MergeSort.cpp 7 | QuickSort.cpp 8 | Util.cpp) 9 | 10 | add_executable(Sort-BubbleSort.exe BubbleSortTest.cpp ${SRC_FILE}) 11 | add_executable(Sort-InsertSort.exe InsertSortTest.cpp ${SRC_FILE}) 12 | add_executable(Sort-MergeSort.exe MergeSortTest.cpp ${SRC_FILE}) 13 | add_executable(Sort-QuickSort.exe QuickSortTest.cpp ${SRC_FILE}) 14 | -------------------------------------------------------------------------------- /src/DynamicProgramming/TreeDP/MaximumMultipleTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 1024 4 | #endif 5 | #ifndef CHILD_MAX 6 | #define CHILD_MAX 64 7 | #endif 8 | 9 | struct MtNode { 10 | int index; 11 | int value; 12 | MtNode *child[CHILD_MAX]; 13 | int child_cnt; 14 | }; 15 | 16 | MtNode *MaximumMultipleTreeNew(int *value, int *father, int n); 17 | void MaximumMultipleTreeFree(MtNode *t); 18 | 19 | int MaximumMultipleTree(MtNode *t, int n, int m); 20 | 21 | -------------------------------------------------------------------------------- /src/GraphTheory/BinaryMatch/3_match2maxflow.cpp: -------------------------------------------------------------------------------- 1 | //二分匹配转最大流 2 | //match2maxflow.cpp 3 | 4 | //将二分最大匹配问题转化为最大流问题求解 5 | 6 | //本文是算法导论中关于求解二分最大匹配的方法 7 | //将二分图B=构造为对应的所有边的权值都为1的流网络 8 | //该流网络的最大流正好等于原二分图B的最大匹配,则将二分匹配问题转化为了最大流问题 9 | // 10 | //向二分图B中添加源点beg和汇点end 11 | //从源点beg向X集合中每个节点添加一条权值为1的边,从源点beg指向X集合中节点 12 | //从Y集合中每个节点向汇点end添加一条权值为1的边,从Y集合中节点指向汇点end 13 | //原图B中的无向边都改为从X集合中节点指向Y集合中节点的有向边,权值为1 14 | //该流网络的最大流即为原二分图B的最大匹配数 15 | 16 | //int match2maxflow(bipartite& b, int *ymatch) 17 | -------------------------------------------------------------------------------- /docs/Sort/README.md: -------------------------------------------------------------------------------- 1 | # Chapter-2 Sort 2 | # 第2章 排序 3 | 4 | ![Sort.png](res/Sort.png) 5 | 6 | -------- 7 | 8 | 1. [InsertSort 插入排序](InsertSort/) 9 | 2. [BubbleSort 冒泡排序](BubbleSort/) 10 | 3. [QuickSort 快速排序](QuickSort/) 11 | 4. [MergeSort 归并排序](MergeSort/) 12 | 13 | -------- 14 | 15 | #### 公共代码 16 | 17 | [Util.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/Sort/Util.h) 18 | 19 | [Util.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/Sort/Util.cpp) 20 | -------------------------------------------------------------------------------- /src/NumberTheory/ModularExponentiation.cpp: -------------------------------------------------------------------------------- 1 | #include "ModularExponentiation.h" 2 | 3 | #define INT_BIT 30 4 | 5 | int ModularExponentiation(int b, int e, int m) { 6 | int c = 1; 7 | for (int i = 0; i < INT_BIT && ((1 << i) < e); i++) { 8 | int exp_val = 1 << i; 9 | if (e & exp_val) { 10 | int temp = 1; 11 | for (int j = 0; j < exp_val; j++) { 12 | temp = (temp * b) % m; 13 | } 14 | c = (c * temp) % m; 15 | } 16 | } 17 | return c; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/ConvexPolygonGravityCenter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Util.h" 3 | #ifndef MAX 4 | #define MAX 128 5 | #endif 6 | 7 | /** 8 | * @param a 三角形端点数组 9 | * @param n 三角形端点数量 10 | * @return 三角形重心坐标点 11 | */ 12 | Node TriangleGravityCenter(const Node &a, const Node &b, const Node &c); 13 | 14 | /** 15 | * @param a 凸多边形端点数组 16 | * @param n 凸多边形端点数量 17 | * @return 凸多边形重心坐标点 18 | */ 19 | Node ConvexPolygonGravityCenter(Node *a, int n); 20 | 21 | -------------------------------------------------------------------------------- /src/GraphTheory/NetworkFlow/10_multi_source_sink_max_flow.cpp: -------------------------------------------------------------------------------- 1 | //多源点多汇点最大流 2 | //multi_source_sink_maxflow.cpp 3 | 4 | //求拥有多个源点和汇点流网络的最大流 5 | 6 | //多源点多汇点最大流问题可以很轻松的转化为已知的单源点汇点最大流问题 7 | //对原流网络R进行如下构造得到新的单源点汇点流网络R': 8 | //添加超级源点s_beg和超级汇点s_end 9 | //从s_beg向原R中每个源点添加一条容量为极大值INF的管道,原R中源点现成为普通节点 10 | //从原R中每个汇点向s_end添加一条容量为极大值INF的管道,原R中汇点现成为普通节点 11 | //对新R'应用距离标号算法得到的最大流即为原多源点多汇点流网络R的最大流 12 | 13 | //int multi_source_sink_maxflow(graph_matrix residue, 14 | // int *beg, int bn, int *end, int en); 15 | -------------------------------------------------------------------------------- /src/Search/BidirectionalBreadthSearch.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Util.h" 3 | #include 4 | #ifndef MAX 5 | #define MAX 128 6 | #endif 7 | 8 | /** 9 | * BidirectionalBreadthSearch 双向广度搜索 10 | * @param n 行 11 | * @param m 列 12 | * @param beg 起点座标 13 | * @param end 终点座标 14 | * @return 从起点到终点的座标路径 15 | */ 16 | std::deque BidirectionalBreadthSearch(int n, int m, const BfsNode &beg, 17 | const BfsNode &end); 18 | 19 | -------------------------------------------------------------------------------- /src/DynamicProgramming/TreeDP/MaximumBinaryTreeRadiusSum.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 1024 4 | #endif 5 | 6 | struct BrsNode { 7 | int index; 8 | int value; 9 | BrsNode *left; 10 | BrsNode *right; 11 | BrsNode *father; 12 | }; 13 | 14 | BrsNode *MaximumBinaryTreeRadiusSumNew(int *value, int *left, int *right, 15 | int n); 16 | 17 | void MaximumBinaryTreeRadiusSumFree(BrsNode *t); 18 | 19 | int MaximumBinaryTreeRadiusSum(BrsNode *t); 20 | 21 | -------------------------------------------------------------------------------- /src/DataStructure/SegmentTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 1024 4 | #endif 5 | 6 | struct StNode { 7 | int left[MAX]; 8 | int right[MAX]; 9 | int sum[MAX]; 10 | }; 11 | 12 | // create s[beg, end) 13 | StNode *SegmentTreeNew(int s[MAX], int beg, int end); 14 | 15 | // free s[beg, end) 16 | void SegmentTreeFree(StNode *t); 17 | 18 | // modify s[i] += value 19 | void SegmentTreeAdd(StNode *t, int i, int value); 20 | 21 | // sum s[beg, end) 22 | int SegmentTreeSum(StNode *t, int beg, int end); 23 | 24 | -------------------------------------------------------------------------------- /src/NumberTheory/ExtendedEuclidTest.cpp: -------------------------------------------------------------------------------- 1 | #include "ExtendedEuclid.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define MAX 4096 8 | 9 | int main(void) { 10 | for (int i = 0; i < MAX; i++) { 11 | int a = abs(rand() % MAX); 12 | int b = abs(rand() % MAX); 13 | tuple result = ExtendedEuclid(a, b); 14 | AssertGcd(a, b, get<0>(result)); 15 | assert(a * get<1>(result) + b * get<2>(result) == get<0>(result)); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/MaximumContinuousSubsequenceSum.cpp: -------------------------------------------------------------------------------- 1 | #include "MaximumContinuousSubsequenceSum.h" 2 | #include "../Util.h" 3 | #include 4 | #include 5 | 6 | int MaximumContinuousSubsequenceSum(const int *s, int n) { 7 | int *f = ArrayNew(n + 1); 8 | 9 | for (int i = 0; i <= n; i++) { 10 | f[i] = 0; 11 | } 12 | 13 | for (int i = 1; i <= n; i++) { 14 | f[i] = std::max(f[i - 1] + s[i], s[i]); 15 | } 16 | 17 | int result = f[n]; 18 | ArrayFree(f); 19 | return result; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /src/PatternMatch/SimpleMatch.cpp: -------------------------------------------------------------------------------- 1 | #include "SimpleMatch.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | std::vector SimpleMatch(const std::string &text, 9 | const std::string &pattern) { 10 | std::vector match; 11 | int i, j; 12 | 13 | for (i = 0; i < text.length(); i++) { 14 | if (StringEq(text, i, pattern, 0, pattern.length())) { 15 | match.push_back(i); 16 | } 17 | } 18 | return match; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /src/Search/AStarSearch.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Util.h" 3 | #include 4 | #include 5 | #ifndef MAX 6 | #define MAX 128 7 | #endif 8 | #ifndef INF 9 | #define INF INT_MAX 10 | #endif 11 | 12 | /** 13 | * AStarSearch A*搜索 14 | * @param n 行 15 | * @param m 列 16 | * @param beg 起点座标 17 | * @param end 终点座标 18 | * @return 从起点到终点的座标路径 19 | */ 20 | std::vector AStarSearch(int block[MAX][MAX], int n, int m, 21 | const BfsNode &beg, const BfsNode &end); 22 | 23 | -------------------------------------------------------------------------------- /src/Search/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include "BinarySearch.h" 2 | #include 3 | 4 | std::pair BinarySearch(int *s, int beg, int end, int x) { 5 | int low = beg; 6 | int high = end - 1; 7 | int mid; 8 | while (low <= high) { 9 | mid = (low + high) / 2; 10 | if (s[mid] == x) { 11 | return std::make_pair(true, mid); 12 | } else if (s[mid] > x) { 13 | high = mid - 1; 14 | } else if (s[mid] < x) { 15 | low = mid + 1; 16 | } 17 | } 18 | return std::make_pair(false, -1); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /docs/AnalyticGeometry/ConvexHull/README.md: -------------------------------------------------------------------------------- 1 | # Section-2 Convex Hull 2 | # 第2节 凸包 3 | 4 | 1. [GrahamScan Graham扫描算法](GrahamScan/) 5 | 2. [QuickHull 快速凸包算法](QuickHull/) 6 | 3. [RotatingCalipers 旋转卡壳](RotatingCalipers/) 7 | 8 | -------- 9 | 10 | #### 凸包 11 | 12 | 凸包(Convex Hull)是二维平面上一组点集$$ Q $$的最小凸多边形$$ P $$,满足$$ Q $$中的任意点要么在$$ P $$内部,要么在$$ P $$的边界上。形象地说,可以想象$$ Q $$中的每个点都是钉在木板上的一个铁钉,凸包$$ P $$是一根拉紧的橡皮筋,包住所有铁钉后构成的形状。如图所示: 13 | 14 | ![ConvexHull1.png](res/ConvexHull1.png) 15 | 16 | 上图中$$ [p_0, p_1, p_2, p_3, p_4] $$是点集$$ Q $$的凸包,记作$$ CH(Q) $$。 17 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | project(way-to-algorithm CXX) 4 | set(CMAKE_CXX_STANDARD 11) 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 6 | 7 | add_subdirectory(Sort) 8 | add_subdirectory(Search) 9 | add_subdirectory(DataStructure) 10 | add_subdirectory(DynamicProgramming) 11 | add_subdirectory(GameTheory) 12 | add_subdirectory(GraphTheory) 13 | add_subdirectory(NumberTheory) 14 | add_subdirectory(CombinatorialMathematics) 15 | add_subdirectory(AnalyticGeometry) 16 | add_subdirectory(PatternMatch) 17 | -------------------------------------------------------------------------------- /docs/LinearAlgebra/README.md: -------------------------------------------------------------------------------- 1 | # Chapter-9 Linear Algebra 2 | # 第9章 线性代数 3 | 4 | ![Matrix.png](res/Matrix.png) 5 | 6 | -------- 7 | 8 | 1. [Matrix 矩阵](Matrix/) 9 | 1. [Determinant 行列式](Matrix/Determinant/) 10 | 1. [Strassen Strassen算法](Matrix/Strassen/) 11 | 2. [GaussElimination 高斯消元法](Matrix/GaussElimination/) 12 | 3. [LUP LUP分解](Matrix/LUP/) 13 | 4. [InverseMatrix 矩阵求逆](Matrix/InverseMatrix/) 14 | 2. LinearProgramming 线性规划 15 | 1. [Simplex 单纯形算法](LinearProgramming/Simplex/) 16 | 2. [Dinkelback Dinkelback算法](LinearProgramming/Dinkelback/) 17 | -------------------------------------------------------------------------------- /src/NumberTheory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | set(SRC_FILE 4 | PrimeSieve.cpp 5 | Euclid.cpp 6 | ExtendedEuclid.cpp 7 | ChineseRemainerTheorem.cpp 8 | Util.cpp 9 | ) 10 | 11 | add_executable(NumberTheory-PrimeSieve.exe PrimeSieveTest.cpp ${SRC_FILE}) 12 | add_executable(NumberTheory-Euclid.exe EuclidTest.cpp ${SRC_FILE}) 13 | add_executable(NumberTheory-ExtendedEuclid.exe ExtendedEuclidTest.cpp ${SRC_FILE}) 14 | add_executable(NumberTheory-ChineseRemainerTheorem.exe ChineseRemainerTheoremTest.cpp ${SRC_FILE}) 15 | -------------------------------------------------------------------------------- /docs/GraphTheory/MinSpanningTree/README.md: -------------------------------------------------------------------------------- 1 | # Section-2 Min Spanning Tree 2 | # 第2节 最小生成树 3 | 4 | -------- 5 | 6 | 1. [Kruskal - Kruskal算法](Kruskal/) 7 | 2. [Prim - Prim算法](Prim/) 8 | 9 | -------- 10 | 11 | #### 最小生成树(Min Spanning Tree) 12 | 13 | 图$$ G $$中所有边都拥有一个正整数权值。最小生成树是图$$ G $$中连接所有顶点,边的权值之和最小的非环路径。 14 | 15 | -------- 16 | 17 | #### Introduction To Algorithms 18 | 19 | * [VI.Graph Algorithms](https://www.google.com/search?q=Introduction+to+Algorithms+3rd+Edition+pdf) 20 | 21 | #### 图论术语 22 | 23 | * https://en.wikipedia.org/wiki/Glossary_of_graph_theory_terms 24 | -------------------------------------------------------------------------------- /src/PatternMatch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | set(SRC_FILE 4 | SimpleMatch.cpp 5 | KnuthMorrisPratt.cpp 6 | AhoCorasickAutomata.cpp 7 | RabinKarp.cpp 8 | Util.cpp 9 | ) 10 | 11 | add_executable(PatternMatch-SimpleMatch.exe SimpleMatchTest.cpp ${SRC_FILE}) 12 | add_executable(PatternMatch-AhoCorasickAutomata.exe AhoCorasickAutomataTest.cpp ${SRC_FILE}) 13 | add_executable(PatternMatch-KnuthMorrisPratt.exe KnuthMorrisPrattTest.cpp ${SRC_FILE}) 14 | add_executable(PatternMatch-RabinKarp.exe RabinKarpTest.cpp ${SRC_FILE}) 15 | -------------------------------------------------------------------------------- /src/Sort/InsertSort.cpp: -------------------------------------------------------------------------------- 1 | #include "InsertSort.h" 2 | 3 | void InsertSort(int *s, int beg, int end) { 4 | for (int j = beg + 1; j < end; j++) { 5 | int i; 6 | int p = s[j]; 7 | // s[0]到s[j]找到i,满足s[i-1]<=p<=s[i] 8 | //注意i=beg时不存在s[i-1]的情况 9 | for (i = beg; i < j; i++) { 10 | if (i == beg && p <= s[i]) 11 | break; 12 | else if (i > beg && s[i - 1] <= p && p <= s[i]) 13 | break; 14 | } 15 | // i即为p将被插入的位置 16 | //将[i,j-1]之间的元素向右移动一位 17 | for (int k = j; k > i; k--) 18 | s[k] = s[k - 1]; 19 | s[i] = p; 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /src/GameTheory/NimGameTest.cpp: -------------------------------------------------------------------------------- 1 | #include "NimGame.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define TEST_MAX 128 7 | 8 | struct Test { 9 | int s[TEST_MAX]; 10 | int n; 11 | bool result; 12 | } tests[] = {{{1}, 1, true}, {{1, 1}, 2, false}, {{1, 1, 1}, 3, true}, 13 | {{1, 2}, 2, true}, {{3, 4, 5}, 3, true}, {{7, 2, 4, 1}, 4, false}}; 14 | 15 | int main() { 16 | for (int i = 0; i < sizeof(tests) / sizeof(Test); i++) { 17 | Test &t = tests[i]; 18 | assert(NimGame(t.s, t.n) == t.result); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /src/GraphTheory/NetworkFlow/3_dinic.cpp: -------------------------------------------------------------------------------- 1 | //Dinic算法 2 | //dinic.cpp 3 | 4 | //Dinic算法属于增广路径类算法,用该算法求解最大流问题 5 | 6 | //使用距离标号d,d值相等的节点属于流网络中的同一层,从而对流网络分层 7 | //重复以下步骤: 8 | //借助距离标号d 9 | //Dinic算法在流网络的相邻层之间进行dfs搜索增广路径,而同一层节点不必搜索 10 | //即从源点开始,对节点u不考虑和它d值相同的节点,只考虑比它d值小1的节点 11 | //在拓扑顺序上看,d值小1的节点是拓扑上相邻的节点 12 | //即满足d[i] = d[j] + 1,其中i是增广路径上一条边的父节点,j是该边的子节点 13 | //通过设置距离标号加速dfs搜索增广路径的速度 14 | //可以看出来,流网络分层和容许边的本质是一样的 15 | //使用得到的增广路径具有副作用:添加反向边 16 | //可以用bfs从汇点反向搜索到源点(遍历的顺序即为节点相反的拓扑顺序) 17 | //得到残留网络的新距离标号d 18 | //这样一直到无法继续找出新的增广路径时即可得到最大流 19 | 20 | //int dinic(graph_matrix residue, int beg, int end) 21 | -------------------------------------------------------------------------------- /src/GameTheory/BashGameTest.cpp: -------------------------------------------------------------------------------- 1 | #include "BashGame.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Test { 7 | int n; 8 | int m; 9 | bool result; 10 | } tests[] = { 11 | {50, 4, false}, 12 | {50, 5, true}, 13 | {80, 6, true}, 14 | {70, 9, false}, 15 | {60, 5, false}, 16 | }; 17 | 18 | int main() { 19 | for (int i = 0; i < sizeof(tests) / sizeof(Test); i++) { 20 | Test &t = tests[i]; 21 | cout << i << " " << BashGame(t.n, t.m) << endl; 22 | assert(BashGame(t.n, t.m) == t.result); 23 | } 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /src/NumberTheory/ChineseRemainerTheoremTest.cpp: -------------------------------------------------------------------------------- 1 | #include "ChineseRemainerTheorem.h" 2 | #include 3 | #define MAX 1024 4 | 5 | struct Test { 6 | int a[MAX]; 7 | int m[MAX]; 8 | int n; 9 | int result; 10 | } test_cases[] = { 11 | { 12 | {1, 2, 3, 4}, 13 | {0, 1, 2, 3}, 14 | 4, 15 | 120, 16 | }, 17 | }; 18 | 19 | int main(void) { 20 | for (int i = 0; i < sizeof(test_cases) / sizeof(Test); i++) { 21 | Test &t = test_cases[i]; 22 | int x = ChineseRemainerTheorem(t.a, t.m, t.n); 23 | assert(x == t.result); 24 | } 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /docs/DataStructure/README.md: -------------------------------------------------------------------------------- 1 | # Chapter-4 Data Structure 2 | # 第4章 数据结构 3 | 4 | ![DataStructure.png](res/DataStructure.png) 5 | 6 | -------- 7 | 8 | 1. [DisjointSet 并查集](DisjointSet/) 9 | 2. [PrefixTree(TrieTree) 前缀树](PrefixTree/) 10 | 3. [LeftistTree(LeftistHeap) 左偏树(左偏堆)](LeftistTree/) 11 | 4. [SegmentTree 线段树](SegmentTree/) 12 | 5. [FenwickTree(BinaryIndexedTree) 树状数组](FenwickTree/) 13 | 6. [BinarySearchTree 二叉查找树](BinarySearchTree/) 14 | 7. [AVLTree AVL平衡树](AVLTree/) 15 | 8. [RedBlackTree 红黑树](RedBlackTree/) 16 | 9. [SkipList 跳跃表](SkipList/) 17 | 10. [BPlusTree B+树](BPlusTree/) 18 | 11. [BMinusTree B-树](BMinusTree/) 19 | -------------------------------------------------------------------------------- /docs/PatternMatch/README.md: -------------------------------------------------------------------------------- 1 | # Chapter-11 Pattern Match 2 | # 第11章 文本匹配 3 | 4 | ![PatternMatch.png](res/PatternMatch.png) 5 | 6 | -------- 7 | 8 | * [SimpleMatch 简单匹配](SimpleMatch/) 9 | * [AhoCorasickAutomata AC自动机](AhoCorasickAutomata/) 10 | * [KnuthMorrisPratt KMP匹配算法](KnuthMorrisPratt/) 11 | * [RabinKarp RabinKarp算法](RabinKarp/) 12 | * [BoyerMoore BoyerMoore算法](BoyerMoore/) 13 | 14 | -------- 15 | 16 | #### 公共代码 17 | 18 | [Util.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/PatternMatch/Util.h) 19 | 20 | [Util.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/PatternMatch/Util.cpp) 21 | -------------------------------------------------------------------------------- /src/DynamicProgramming/RegionalDP/UniquePath.cpp: -------------------------------------------------------------------------------- 1 | #include "UniquePath.h" 2 | #include "../Util.h" 3 | 4 | int UniquePath(int n, int m) { 5 | int **f = Array2DNew(n + 1, m + 1); 6 | for (int i = 0; i <= n; i++) { 7 | for (int j = 0; j <= m; j++) { 8 | f[i][j] = 0; 9 | } 10 | } 11 | 12 | for (int i = 1; i <= n; i++) { 13 | for (int j = 1; j <= m; j++) { 14 | if (i == 1 && j == 1) { 15 | f[1][1] = 1; 16 | } else { 17 | f[i][j] = f[i - 1][j] + f[i][j - 1]; 18 | } 19 | } 20 | } 21 | 22 | int result = f[n][m]; 23 | Array2DFree(f, n + 1); 24 | return result; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /src/Search/Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include "Recursion.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | #include 6 | 7 | void Recursion(int *s, int n, int m, int prev, 8 | std::vector> &result) { 9 | // 递归终止条件 10 | if (prev == n) { 11 | std::vector vec; 12 | std::transform(s, s + n, std::back_inserter(vec), [](const int &v) { return v; }); 13 | result.push_back(vec); 14 | return; 15 | } 16 | 17 | // 遍历当前成员s[prev] 18 | // 递归进入下一个成员 19 | for (int i = 0; i < m; i++) { 20 | s[prev] = i; 21 | Recursion(s, n, m, prev + 1, result); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /src/NumberTheory/ChineseRemainerTheorem.cpp: -------------------------------------------------------------------------------- 1 | #include "ChineseRemainerTheorem.h" 2 | #include "Euclid.h" 3 | #include 4 | 5 | int ChineseRemainerTheorem(int *a, int *m, int n) { 6 | int M = 1; 7 | for (int i = 0; i < n; i++) { 8 | M *= a[i]; 9 | } 10 | 11 | int Mi[MAX]; 12 | int t[MAX]; 13 | std::memset(Mi, 0, sizeof(int) * MAX); 14 | std::memset(t, 0, sizeof(int) * MAX); 15 | for (int i = 0; i < n; i++) { 16 | Mi[i] = M / m[i]; 17 | t[i] = GreatestCommonDivisor(Mi[i], m[i]); 18 | } 19 | 20 | int x = 0; 21 | for (int i = 0; i < n; i++) { 22 | x += a[i] * t[i] * Mi[i]; 23 | } 24 | 25 | return x; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/ConvexPolygonArea.cpp: -------------------------------------------------------------------------------- 1 | #include "ConvexPolygonArea.h" 2 | #include "Cross.h" 3 | #include 4 | 5 | //三角形面积 6 | double TriangleArea(const Node &a, const Node &b, const Node &c) { 7 | Vec v1 = Vec(a, b); 8 | Vec v2 = Vec(a, c); 9 | return std::abs(Cross(v1, v2) / 2.0f); 10 | } 11 | 12 | //凸多边形面积 13 | double ConvexPolygonArea(Node *a, int n) { 14 | double area = 0.0f; 15 | Node p = a[0]; 16 | for (int i = 1; i < n; i++) { 17 | double tmp_area = 0.0f; 18 | if (i != n - 1) { 19 | tmp_area = TriangleArea(p, a[i], a[i + 1]); 20 | } 21 | area += tmp_area; 22 | } 23 | 24 | return area; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /leetcode/leetcode-46.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | void FullPermutationImpl(vector &nums, int prev, vector> & result) 3 | { 4 | if (prev == 0) { 5 | result.push_back(nums); 6 | return; 7 | } 8 | for (int i = prev; i >= 0; i--) { 9 | swap(nums[i], nums[prev]); 10 | FullPermutationImpl(nums, prev-1, result); 11 | swap(nums[i], nums[prev]); 12 | } 13 | } 14 | public: 15 | vector> permute(vector& nums) { 16 | vector> ret; 17 | FullPermutationImpl(nums, nums.size()-1, ret); 18 | return ret; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /src/GraphTheory/StronglyConnectedComponents/KosarajuTest.cpp: -------------------------------------------------------------------------------- 1 | #include "Kosaraju.h" 2 | #include "../Util.h" 3 | #include 4 | using namespace std; 5 | 6 | struct Test { 7 | int g[MAX][MAX]; 8 | int n; 9 | } test_cases[] = { 10 | { 11 | { 12 | {0}, 13 | }, 14 | 1, 15 | }, 16 | }; 17 | 18 | static void AssertStrongCompoennts(const vector> &components) {} 19 | 20 | int main(void) { 21 | for (int i = 0; i < sizeof(test_cases) / sizeof(Test); i++) { 22 | Test &t = test_cases[i]; 23 | vector> result = Kosaraju(t.g, t.n); 24 | AssertStrongCompoennts(result); 25 | } 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /src/DynamicProgramming/RegionalDP/UniquePathTest.cpp: -------------------------------------------------------------------------------- 1 | #include "UniquePath.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct Test { 9 | int n; 10 | int m; 11 | int result; 12 | } test_cases[] = { 13 | {3, 4, 10}, {1, 5, 1}, {9, 1, 1}, {2, 8, 8}, {7, 6, 462}, 14 | {4, 7, 84}, {5, 9, 495}, {8, 7, 1716}, {9, 6, 1287}, {7, 8, 1716}, 15 | }; 16 | 17 | int main(void) { 18 | for (int i = 0; i < sizeof(test_cases) / sizeof(Test); i++) { 19 | Test &t = test_cases[i]; 20 | int r = UniquePath(t.n, t.m); 21 | assert(r == t.result); 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /src/GameTheory/WythoffGameTest.cpp: -------------------------------------------------------------------------------- 1 | #include "WythoffGame.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Test { 7 | int p; 8 | int k; 9 | bool result; 10 | } tests[] = {{1, 2, false}, {2, 1, false}, {1, 1, true}, {3, 5, false}, 11 | {5, 3, false}, {4, 7, false}, {7, 4, false}, {4, 4, true}, 12 | {3, 2, true}, {2, 3, true}, {3, 4, true}, {4, 3, true}, 13 | {6, 4, true}, {6, 1, true}, {4, 9, true}}; 14 | 15 | int main() { 16 | for (int i = 0; i < sizeof(tests) / sizeof(Test); i++) { 17 | Test &t = tests[i]; 18 | assert(WythoffGame(t.p, t.k) == t.result); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /src/NumberTheory/Util.cpp: -------------------------------------------------------------------------------- 1 | #include "Util.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void AssertGcd(int a, int b, int gcd) { 7 | assert(a > 0); 8 | assert(b > 0); 9 | assert(gcd > 0); 10 | assert(a % gcd == 0); 11 | assert(b % gcd == 0); 12 | for (int i = gcd + 1; i < a && i < b; i++) { 13 | assert(!(a % i == 0 && b % i == 0)); 14 | } 15 | } 16 | 17 | void AssertLcm(int a, int b, int lcm) { 18 | assert(a > 0); 19 | assert(b > 0); 20 | assert(lcm > 0); 21 | assert(lcm % a == 0); 22 | assert(lcm % b == 0); 23 | for (int i = std::max(a, b) + 1; i < lcm; i++) { 24 | assert(!(i % a == 0 && i % b == 0)); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /docs/NumberTheory/README.md: -------------------------------------------------------------------------------- 1 | # Chapter-8 Number Theory 2 | # 第8章 数论 3 | 4 | ![NumberTheory.png](res/NumberTheory.png) 5 | 6 | -------- 7 | 8 | 1. [PrimeSieve 素数筛法](PrimeSieve/) 9 | 2. [GreatestCommonDivisor 最大公约数](GreatestCommonDivisor/) 10 | 3. [Euclid 欧几里得算法](Euclid/) 11 | 4. [ExtendedEuclid 扩展欧几里得算法](ExtendedEuclid/) 12 | 5. [ChineseRemainerTheorem 中国剩余定理](ChineseRemainerTheorem/) 13 | 6. [ModularExponentiation 模幂运算](ModularExponentiation/) 14 | 15 | -------- 16 | 17 | #### 公共代码 18 | 19 | [Util.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/NumberTheory/Util.h) 20 | 21 | [Util.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/NumberTheory/Util.cpp) 22 | -------------------------------------------------------------------------------- /src/DataStructure/RedBlackTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | struct RbNode { 7 | char color; 8 | int value; 9 | RbNode *left; 10 | RbNode *right; 11 | RbNode *father; 12 | }; 13 | 14 | struct RbTree { 15 | RbNode *root; 16 | RbNode NIL; 17 | }; 18 | 19 | #define is_nil(t, e) ((e) == &((t)->NIL)) 20 | #define not_nil(t, e) (!is_nil(t, e)) 21 | #define set_nil(t, e) ((e) = &((t)->NIL)) 22 | 23 | RbTree *RedBlackTreeNew(); 24 | void RedBlackTreeFree(RbTree *t); 25 | void RedBlackTreeInsert(RbTree *t, int value); 26 | RbNode *RedBlackTreeFind(RbTree *t, int value); 27 | void RedBlackTreeErase(RbTree *t, int value); 28 | 29 | -------------------------------------------------------------------------------- /docs/DataStructure/BPlusTree/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # B+ Tree - B+树 4 | 5 | -------- 6 | 7 | #### 特性 8 | 9 | #### 插入 10 | 11 | -------- 12 | 13 | #### B+ Tree 14 | 15 | -------- 16 | 17 | #### 源码 18 | 19 | [BPlusTree.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/DataStructure/BPlusTree.h) 20 | 21 | [BPlusTree.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/DataStructure/BPlusTree.cpp) 22 | 23 | #### 测试 24 | 25 | [BPlusTreeTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/DataStructure/BPlusTreeTest.cpp) 26 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/com_local.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | *局部文件,存放函数的声明 4 | */ 5 | 6 | #ifndef combination_head_h 7 | #define combination_head_h 8 | 9 | #include "general_head.h" 10 | 11 | void full_permutation(int* s, int n, int& cnt, int prev = 0); // 1 12 | void combination1(int* s, int n, int m, int prev = 0); // 2 13 | void combination2(int* s, int n, int m, int prev = 0); // 3 14 | void combination3(int*, int, int); // 4 15 | void permutation_group(int*, int, int*, int, int*); // 5 16 | void print_status(int*, int, int); 17 | void calculus_number_diverse(int argc, char** argv); 18 | void catalan(int** a, int* b); 19 | 20 | #endif // combination_head_h 21 | -------------------------------------------------------------------------------- /docs/DataStructure/BMinusTree/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # B- Tree - B-树 4 | 5 | -------- 6 | 7 | #### 特性 8 | 9 | #### 插入 10 | 11 | -------- 12 | 13 | #### B- Tree 14 | 15 | -------- 16 | 17 | #### 源码 18 | 19 | [BMinusTree.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/DataStructure/BMinusTree.h) 20 | 21 | [BMinusTree.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/DataStructure/BMinusTree.cpp) 22 | 23 | #### 测试 24 | 25 | [BMinusTreeTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/DataStructure/BMinusTreeTest.cpp) 26 | -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/LongestIncreasingSubsequence.cpp: -------------------------------------------------------------------------------- 1 | #include "LongestIncreasingSubsequence.h" 2 | #include "../Util.h" 3 | #include 4 | 5 | int LongestIncreasingSubsequence(const int *s, int n) { 6 | int *f = ArrayNew(n + 1); 7 | 8 | for (int i = 0; i <= n; i++) { 9 | f[i] = 0; 10 | } 11 | 12 | for (int i = 1; i <= n; i++) { 13 | int maxlen = 0; 14 | for (int k = 1; k < i; k++) { 15 | if (s[i] > s[k]) 16 | maxlen = std::max(maxlen, f[k]); 17 | } 18 | f[i] = maxlen + 1; 19 | } 20 | 21 | int result = 0; 22 | for (int i = 1; i <= n; i++) { 23 | result = std::max(result, f[i]); 24 | } 25 | 26 | ArrayFree(f); 27 | return result; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /src/DataStructure/PrefixTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 128 4 | #endif 5 | #ifndef CHILD_MAX 6 | #define CHILD_MAX 26 7 | #endif 8 | #include 9 | #include 10 | 11 | struct PtNode { 12 | char letter; 13 | const char *word; 14 | PtNode *child[CHILD_MAX]; 15 | }; 16 | 17 | // create prefix tree 18 | PtNode *PrefixTreeNew(); 19 | 20 | // free prefix tree 21 | void PrefixTreeFree(PtNode *t); 22 | 23 | // insert word to prefix tree 24 | void PrefixTreeInsert(PtNode *t, const char *word); 25 | 26 | // find word from prefix tree 27 | int PrefixTreeFind(PtNode *t, const char *word); 28 | 29 | // erase word from prefix tree 30 | void PrefixTreeErase(PtNode *t, const char *word); 31 | 32 | -------------------------------------------------------------------------------- /src/NumberTheory/ModularExponentiationTest.cpp: -------------------------------------------------------------------------------- 1 | #include "ModularExponentiation.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | #define MAX 4096 10 | 11 | int SimpleModularExp(int b, int e, int m) { 12 | int c = 1; 13 | for (int i = 0; i < e; i++) { 14 | c = (c * b) % m; 15 | } 16 | return c; 17 | } 18 | 19 | int main(void) { 20 | for (int i = 0; i < MAX; i++) { 21 | int b = abs(rand()); 22 | int e = abs(rand()); 23 | int m = abs(rand()); 24 | int c = ModularExponentiation(b, e, m); 25 | assert(c == SimpleModularExp(b, e, m)); 26 | } 27 | std::cout << std::endl; 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | set(SRC_FILE 4 | Cross.cpp 5 | SegmentIntersection.cpp 6 | Sweeping.cpp 7 | ConvexPolygonArea.cpp 8 | ConvexPolygonGravityCenter.cpp 9 | ../Util.cpp 10 | ) 11 | 12 | add_executable(AnalyticGeometry-Cross.exe CrossTest.cpp ${SRC_FILE}) 13 | add_executable(AnalyticGeometry-SegmentIntersection.exe SegmentIntersectionTest.cpp ${SRC_FILE}) 14 | add_executable(AnalyticGeometry-Sweeping.exe SweepingTest.cpp ${SRC_FILE}) 15 | add_executable(AnalyticGeometry-ConvexPolygonArea.exe ConvexPolygonAreaTest.cpp ${SRC_FILE}) 16 | add_executable(AnalyticGeometry-ConvexPolygonGravityCenter.exe ConvexPolygonGravityCenterTest.cpp ${SRC_FILE}) 17 | -------------------------------------------------------------------------------- /src/Search/BruteForce.cpp: -------------------------------------------------------------------------------- 1 | #include "BruteForce.h" 2 | #include "Util.h" 3 | 4 | std::vector> BruteForce(int *s, int n, int m) { 5 | std::vector> result; 6 | for (int i_0 = 0; i_0 < m; i_0++) 7 | for (int i_1 = 0; i_1 < m; i_1++) 8 | for (int i_2 = 0; i_2 < m; i_2++) /* ... */ 9 | for (int i_n_1 = 0; i_n_1 < m; i_n_1++) { 10 | s[0] = i_0; 11 | s[1] = i_1; 12 | s[2] = i_2; 13 | /* ... */ 14 | s[n - 1] = i_n_1; 15 | 16 | std::vector vec; 17 | std::transform(s, s + n, std::back_inserter(vec), [](const int &v) { return v; }); 18 | result.push_back(vec); 19 | } 20 | return result; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /src/GraphTheory/Traverse/BreadthFirstSearch.cpp: -------------------------------------------------------------------------------- 1 | #include "BreadthFirstSearch.h" 2 | #include 3 | #include 4 | #include 5 | 6 | std::vector BreadthFirstSearch(int g[MAX][MAX], int n) { 7 | std::vector result; 8 | std::queue q; 9 | int visited[MAX]; 10 | std::memset(visited, 0, sizeof(visited)); 11 | 12 | q.push(0); 13 | visited[0] = 1; 14 | 15 | while (!q.empty()) { 16 | int e = q.front(); 17 | q.pop(); 18 | result.push_back(e); 19 | 20 | // adjacent node to e 21 | for (int i = 0; i < n; i++) { 22 | if (i != e && g[e][i] && !visited[i]) { 23 | q.push(i); 24 | visited[i] = 1; 25 | } 26 | } 27 | } 28 | return result; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/DataStructure/DisjointSet.cpp: -------------------------------------------------------------------------------- 1 | #include "DisjointSet.h" 2 | #include 3 | 4 | static int FindFather(DjSet *s, int i) { 5 | if (s->father[i] != i) 6 | s->father[i] = FindFather(s, s->father[i]); 7 | return s->father[i]; 8 | } 9 | 10 | DjSet *DisjointSetNew() { 11 | DjSet *s = new DjSet(); 12 | for (int i = 0; i < MAX; i++) 13 | s->father[i] = i; 14 | return s; 15 | } 16 | 17 | void DisjointSetFree(DjSet *s) { delete s; } 18 | 19 | void DisjointSetUnion(DjSet *s, int i, int j) { 20 | int i_father = FindFather(s, i); 21 | int j_father = FindFather(s, j); 22 | s->father[j] = i_father; 23 | } 24 | 25 | bool DisjointSetQuery(DjSet *s, int i, int j) { 26 | return FindFather(s, i) == FindFather(s, j); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /src/GraphTheory/Util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | struct Vertex { 5 | int index; 6 | int value; 7 | 8 | Vertex(); 9 | Vertex(int index, int value); 10 | Vertex(const Vertex &other); 11 | Vertex &operator=(const Vertex &other); 12 | 13 | friend bool operator!=(const Vertex &a, const Vertex &b); 14 | friend bool operator==(const Vertex &a, const Vertex &b); 15 | }; 16 | 17 | struct Edge { 18 | int u; 19 | int v; 20 | int value; 21 | 22 | Edge(); 23 | Edge(int u, int v); 24 | Edge(int u, int v, int value); 25 | Edge(const Edge &e); 26 | Edge &operator=(const Edge &e); 27 | 28 | friend bool operator!=(const Edge &a, const Edge &b); 29 | friend bool operator==(const Edge &a, const Edge &b); 30 | }; 31 | 32 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/2_full_permutation.cpp: -------------------------------------------------------------------------------- 1 | //全排列 2 | //full_permutaion.cpp 3 | 4 | //求序列s的所有成员的组合 5 | //当序列s有三个元素,s = {1, 2, 3}时,它的所有组合方式是 6 | //{1, 2, 3} {1, 3, 2} {2, 1, 3} {2, 3, 1} {3, 1, 2} {3, 2, 1} 7 | 8 | //全排列的方法非常像深度优先搜索,同样通过递归技术实现 9 | 10 | #include "general_head.h" 11 | #include "com_local.h" 12 | //brute_force.cpp 13 | extern void print_status(int *s, int beg, int end); 14 | 15 | void full_permutation(int *s, int n, int &cnt, int prev ) 16 | {//数组s有n个成员,从0到n-1,对他们进行全排列,cnt初始应该为0 17 | if(prev == n){ 18 | //将每种排列加入答案集 19 | print_status(s, 0, n); 20 | ++ cnt; 21 | return; 22 | } 23 | for(int i = prev; i < n; ++ i){ 24 | swap(s[prev], s[i]); 25 | full_permutation(s, n, cnt, prev + 1); 26 | swap(s[prev], s[i]); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/LongestCommonSubsequence.cpp: -------------------------------------------------------------------------------- 1 | #include "LongestCommonSubsequence.h" 2 | #include "../Util.h" 3 | #include 4 | #include 5 | 6 | int LongestCommonSubsequence(const int *a, const int *b, int n) { 7 | int **f = Array2DNew(n + 1, n + 1); 8 | 9 | // 初始化 10 | for (int i = 0; i <= n; i++) { 11 | for (int j = 0; j <= n; j++) { 12 | f[i][j] = 0; 13 | } 14 | } 15 | 16 | for (int i = 1; i <= n; i++) { 17 | for (int j = 1; j <= n; j++) { 18 | if (a[i] == b[j]) 19 | f[i][j] = f[i - 1][j - 1] + 1; 20 | else 21 | f[i][j] = std::max(f[i - 1][j], f[i][j - 1]); 22 | } 23 | } 24 | 25 | int result = f[n][n]; 26 | Array2DFree(f, n + 1); 27 | return result; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /src/GraphTheory/StronglyConnectedComponents/TarjanTest.cpp: -------------------------------------------------------------------------------- 1 | #include "Tarjan.h" 2 | #include "../Util.h" 3 | #include 4 | using namespace std; 5 | 6 | struct Test { 7 | int g[MAX][MAX]; 8 | int n; 9 | } test_cases[] = { 10 | { 11 | { 12 | {0}, 13 | }, 14 | 1, 15 | }, 16 | { 17 | { 18 | {0}, 19 | }, 20 | 1, 21 | }, 22 | }; 23 | 24 | static void AssertStrongCompoennts(const vector> &components) {} 25 | 26 | int main(void) { 27 | for (int i = 0; i < sizeof(test_cases) / sizeof(Test); i++) { 28 | Test &t = test_cases[i]; 29 | vector> result = Tarjan(t.g, t.n); 30 | AssertStrongCompoennts(result); 31 | } 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/SegmentIntersectionTest.cpp: -------------------------------------------------------------------------------- 1 | #include "SegmentIntersection.h" 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | Segment s1(Node(0.0f, 0.0f), Node(1.0f, 0.0f)); 7 | Segment s2(Node(0.0f, 0.0f), Node(0.0f, 1.0f)); 8 | Segment s3(Node(0.0f, 0.0f), Node(1.0f, 1.0f)); 9 | Segment s4(Node(0.0f, 0.0f), Node(2.0f, 0.5f)); 10 | Segment s5(Node(1.0f, 0.0f), Node(1.0f, 1.5f)); 11 | Segment s6(Node(0.0f, 2.0f), Node(1.0f, 0.5f)); 12 | 13 | assert(SegmentIntersection(s1, s2)); 14 | assert(SegmentIntersection(s3, s4)); 15 | assert(SegmentIntersection(s4, s5)); 16 | assert(!SegmentIntersection(s2, s5)); 17 | assert(!SegmentIntersection(s1, s6)); 18 | assert(!SegmentIntersection(s2, s6)); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/Search/BinarySearchTest.cpp: -------------------------------------------------------------------------------- 1 | #include "BinarySearch.h" 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int s[MAX]; 9 | for (int i = 0; i < MAX; i++) { 10 | s[i] = i; 11 | } 12 | for (int i = 1; i < MAX; i++) { 13 | for (int j = -MAX; j < 0; j++) { 14 | pair r = BinarySearch(s, 0, i, j); 15 | assert(!r.first); 16 | } 17 | for (int j = 0; j < i; j++) { 18 | pair r = BinarySearch(s, 0, i, j); 19 | assert(r.first); 20 | assert(r.second == j); 21 | } 22 | for (int j = i; j < MAX; j++) { 23 | pair r = BinarySearch(s, 0, i, j); 24 | assert(!r.first); 25 | } 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /src/DataStructure/SkipList.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | struct SkNode { 5 | int value; 6 | 7 | // forward[0] is lowest link, forward[max_level-1] is highest link 8 | // pointer to next node 9 | SkNode **forward; 10 | }; 11 | 12 | struct SkList { 13 | int size; 14 | int level; 15 | SkNode *head; 16 | }; 17 | 18 | // create skip list 19 | SkList *SkipListNew(int node_count); 20 | 21 | // delete skip list 22 | void SkipListFree(SkList *l); 23 | 24 | // skip list size 25 | int SkipListSize(SkList *l); 26 | 27 | // insert value to skip list 28 | void SkipListInsert(SkList *l, int value); 29 | 30 | // find value in skip list 31 | SkNode *SkipListFind(SkList *l, int value); 32 | 33 | // erase value from skip list 34 | void SkipListErase(SkList *l, int value); 35 | 36 | -------------------------------------------------------------------------------- /src/LinearAlgebra/Matrix/4_inverse_matrix.cpp: -------------------------------------------------------------------------------- 1 | //逆矩阵 2 | //inverse_matrix.cpp 3 | 4 | //求n阶可逆矩阵的逆矩阵 5 | 6 | //设A是一个n阶满秩矩阵,则它是可逆的 7 | //事实上,秩为n的满秩方程组中左边的n阶矩阵A满足Ax = b,且x = Bb,其中B是A的逆矩阵 8 | //定理: 9 | //n阶矩阵的乘法和求n阶矩阵的逆矩阵具有相同的时间复杂度 10 | // 11 | //设A是一个n阶对称正定矩阵,根据对称正定矩阵的性质可以使用递归的方法求出A的逆矩阵 12 | //设A = | B CT |,其中CT是子矩阵C的转置矩阵 13 | // | C D | 14 | //引入矩阵S = D - CB'CT,其中B'是矩阵B的逆矩阵 15 | //经过进一步转化可以用B和S矩阵的逆矩阵B'和S'来表示A的逆矩阵A' 16 | //则通过求n/2阶矩阵B和S的逆矩阵B'和S'就可以得到矩阵A的逆矩阵A' 17 | //从而可以用分而治之的策略来求解对称正定矩阵A的逆矩阵 18 | // 19 | //当A是普通的可逆矩阵时,则先求AT*A的逆矩阵,其中AT是A的转置矩阵 20 | //由于AT*A必然是一个对称正定矩阵,因此可以通过上述的分支法策略求得AT*A的逆矩阵 21 | //再经过一次矩阵转化即可得到原矩阵A的逆矩阵 22 | // 23 | //上面的描述确实比较晦涩,逆矩阵求解过程比较麻烦所以我只做了最简单的描述 24 | //更加深入的内容还有正定矩阵相关的算法 25 | //这些内容请参考算法导论第28章28.4矩阵求逆和28.5对称正定矩阵与最小二乘逼近 26 | 27 | //void inverse_matrix(matrix e, matrix& inverse) 28 | -------------------------------------------------------------------------------- /src/DynamicProgramming/BagDP/CompleteBag.cpp: -------------------------------------------------------------------------------- 1 | #include "CompleteBag.h" 2 | #include "../Util.h" 3 | #include 4 | #include 5 | 6 | int CompleteBag(const int *v, const int *w, int n, int weight) { 7 | 8 | // 初始化 9 | 10 | int **f = Array2DNew(n + 1, weight + 1); 11 | for (int i = 0; i <= n; i++) 12 | for (int j = 0; j <= weight; j++) 13 | f[i][j] = 0; 14 | 15 | for (int i = 1; i <= n; i++) { 16 | for (int j = 0; j <= weight; j++) { 17 | 18 | // 珠宝i最多可以装count个 19 | 20 | int count = j / w[i]; 21 | 22 | for (int k = 0; k <= count; k++) { 23 | f[i][j] = std::max(f[i - 1][j], f[i - 1][j - k * w[i]] + k * v[i]); 24 | } 25 | } 26 | } 27 | 28 | int result = f[n][weight]; 29 | Array2DFree(f, n + 1); 30 | return result; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /src/DynamicProgramming/BagDP/ZeroOneBag.cpp: -------------------------------------------------------------------------------- 1 | #include "ZeroOneBag.h" 2 | #include "../Util.h" 3 | #include 4 | #include 5 | 6 | int ZeroOneBag(const int *v, const int *w, int n, int weight) { 7 | //初始化 8 | 9 | int **f = Array2DNew(n + 1, weight + 1); 10 | for (int i = 0; i <= n; i++) 11 | for (int j = 0; j <= weight; j++) 12 | f[i][j] = 0; 13 | 14 | //第i个物品 15 | 16 | for (int i = 1; i <= n; i++) { 17 | 18 | //重量不超过j 19 | 20 | for (int j = 0; j <= weight; j++) { 21 | 22 | if (j >= w[i]) { 23 | f[i][j] = std::max(f[i - 1][j], f[i - 1][j - w[i]] + v[i]); 24 | } else { 25 | f[i][j] = f[i - 1][j]; 26 | } 27 | } 28 | } 29 | 30 | int result = f[n][weight]; 31 | Array2DFree(f, n + 1); 32 | return result; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /src/GraphTheory/Traverse/DepthFirstSearch.cpp: -------------------------------------------------------------------------------- 1 | #include "DepthFirstSearch.h" 2 | #include 3 | #include 4 | 5 | static void Dfs(int g[MAX][MAX], int n, int beg, int *visited, 6 | std::vector &result) { 7 | visited[beg] = 1; 8 | result.push_back(beg); 9 | for (int i = 0; i < n; i++) { 10 | if (i != beg && g[beg][i] && !visited[i]) { 11 | Dfs(g, n, i, visited, result); 12 | } 13 | } 14 | } 15 | 16 | std::vector DepthFirstSearch(int g[MAX][MAX], int n) { 17 | int visited[MAX]; 18 | std::vector result; 19 | 20 | std::memset(visited, 0, sizeof(visited)); 21 | 22 | // all node [0,n-1] 23 | for (int i = 0; i < n; i++) 24 | if (!visited[i]) { 25 | Dfs(g, n, i, visited, result); 26 | } 27 | 28 | return result; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/FloydWarshall.cpp: -------------------------------------------------------------------------------- 1 | #include "FloydWarshall.h" 2 | #include 3 | 4 | std::vector> FloydWarshall(int g[MAX][MAX], int n) { 5 | int distance[MAX][MAX]; 6 | for (int i = 0; i < n; i++) { 7 | for (int j = 0; j < n; j++) { 8 | distance[i][j] = g[i][j]; 9 | } 10 | } 11 | 12 | for (int k = 0; k < n; k++) 13 | for (int i = 0; i < n; i++) 14 | for (int j = 0; j < n; j++) 15 | distance[i][j] = 16 | std::min(distance[i][j], distance[i][k] + distance[k][j]); 17 | 18 | std::vector> result; 19 | for (int i = 0; i < n; i++) { 20 | std::vector tmp; 21 | for (int j = 0; j < n; j++) { 22 | tmp.push_back(distance[i][j]); 23 | } 24 | result.push_back(tmp); 25 | } 26 | return result; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/CrossTest.cpp: -------------------------------------------------------------------------------- 1 | #include "Cross.h" 2 | #include "../Util.h" 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct Test { 8 | Vec v1; 9 | Vec v2; 10 | double result; 11 | } tests[] = { 12 | { 13 | {1.0f, 1.0f}, 14 | {0.0f, 2.0f}, 15 | 2.0f, 16 | }, 17 | { 18 | {-1.0f, 0.0f}, 19 | {0.5f, -1.3f}, 20 | 1.3f, 21 | }, 22 | { 23 | {1.4f, -0.8f}, 24 | {0.5f, 1.1f}, 25 | 1.94f, 26 | }, 27 | { 28 | {1.8f, -8.0f}, 29 | {0.3f, 2.0f}, 30 | 6.0f, 31 | }, 32 | }; 33 | 34 | int main() { 35 | for (int i = 0; i < sizeof(tests) / sizeof(Test); i++) { 36 | Test &t = tests[i]; 37 | assert(FloatEq(Cross(t.v1, t.v2), t.result)); 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /docs/Search/AdditionMultiplicationPrinciple/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Addition Multiplication Principle - 加法乘法原理 4 | 5 | -------- 6 | 7 | #### 加法原理 8 | 9 | 完成一件事情有$$ n $$类方法,每类方法有若干子方法,完成这件事需要且只需要$$ n $$类方法中的一类方法中的一个子方法。 10 | 11 | 第$$ 1 $$类方法有$$ m_1 $$种子方法,第$$ 2 $$类方法有$$ m_2 $$种子方法,$$ \dots $$,第$$ n $$类方法有$$ m_n $$种子方法。则完成这件事共有 12 | 13 | $$ 14 | m_1 + m_2 + \cdots + m_n = \sum_{i=1}^{n} m_i 15 | $$ 16 | 17 | 种方法。 18 | 19 | #### 乘法原理 20 | 21 | 完成一件事情需要$$ n $$个步骤,每个步骤有若干子方法,完成这件事情需要$$ n $$个步骤都完成,每个步骤需要且只需要选择一种方法。 22 | 23 | 第$$ 1 $$步有$$ m_1 $$种子方法,第$$ 2 $$步有$$ m_2 $$种子方法,$$ \dots $$,第$$ n $$步有$$ m_n $$种子方法。则完成这件事共有 24 | 25 | $$ 26 | m_1 \times m_2 \times \cdots \times m_n = \prod_{i=1}^{n} m_i 27 | $$ 28 | 29 | 种方法。 30 | -------------------------------------------------------------------------------- /src/Search/BreadthFirstSearchTest.cpp: -------------------------------------------------------------------------------- 1 | #include "BreadthFirstSearch.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define TEST_M_MAX 64 8 | #define TEST_N_MAX 64 9 | 10 | int main() { 11 | for (int i = 1; i < TEST_N_MAX; i++) 12 | for (int j = 1; j < TEST_M_MAX; j++) { 13 | BfsNode beg(0, 0); 14 | BfsNode end(i - 1, j - 1); 15 | vector path = BreadthFirstSearch(i, j, beg, end); 16 | //保证路径长度为 j-1+i-1+1 17 | assert(path.size() == (i - 1 + j - 1 + 1)); 18 | //保证起点和终点位置 19 | assert(path[0] == end); 20 | assert(path[path.size() - 1] == beg); 21 | for (int k = 0; k < path.size() - 1; k++) { 22 | //保证路径中相邻两点在二维方格中也是相邻点 23 | AssertAdjacent(path[k], path[k + 1]); 24 | } 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /src/Search/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | set(SRC_FILE 4 | BinarySearch.cpp 5 | BruteForce.cpp 6 | Recursion.cpp 7 | BreadthFirstSearch.cpp 8 | BidirectionalBreadthSearch.cpp 9 | AStarSearch.cpp 10 | DancingLink.cpp 11 | Util.cpp) 12 | 13 | add_executable(Search-BinarySearch.exe BinarySearchTest.cpp ${SRC_FILE}) 14 | add_executable(Search-BruteForce.exe BruteForceTest.cpp ${SRC_FILE}) 15 | add_executable(Search-Recursion.exe RecursionTest.cpp ${SRC_FILE}) 16 | add_executable(Search-BreadthFirstSearch.exe BreadthFirstSearchTest.cpp ${SRC_FILE}) 17 | add_executable(Search-BidirectionalBreadthSearch.exe BidirectionalBreadthSearchTest.cpp ${SRC_FILE}) 18 | add_executable(Search-AStarSearch.exe AStarSearchTest.cpp ${SRC_FILE}) 19 | add_executable(Search-DancingLink.exe DancingLinkTest.cpp ${SRC_FILE}) 20 | -------------------------------------------------------------------------------- /src/Sort/QuickSort.cpp: -------------------------------------------------------------------------------- 1 | #include "QuickSort.h" 2 | 3 | /** 4 | * @brief Partion 5 | * 以s[low]为中间值,将所有小于等于s[low]的放在左边, 6 | * 大于等于[low]的放在右边 7 | * 8 | * @param s[MAX] 无序序列 9 | * @param low 序列s的起始下标 10 | * @param high 序列s的末尾下标,即左闭右闭区间[low, high] 11 | * @return 最终s[low]所在下标 12 | */ 13 | static int Partion(int *s, int low, int high) { 14 | int p = s[low]; 15 | while (low < high) { 16 | while (low < high && s[high] >= p) 17 | high--; 18 | s[low] = s[high]; 19 | while (low < high && s[low] <= p) 20 | low++; 21 | s[high] = s[low]; 22 | } 23 | s[low] = p; 24 | return low; 25 | } 26 | 27 | void QuickSort(int *s, int beg, int end) { 28 | if (beg < end - 1) { 29 | int mid = Partion(s, beg, end - 1); 30 | QuickSort(s, beg, mid); 31 | QuickSort(s, mid + 1, end); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /docs/PatternMatch/BoyerMoore/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Boyer Moore - Boyer-Moore算法 4 | 5 | -------- 6 | 7 | #### 问题 8 | 9 | 在文本$$ text $$中查找字符串$$ pattern $$出现的所有位置($$ text $$长度为$$ n $$,$$ pattern $$长度为$$ m $$,$$ n, m $$都是正整数且$$ n \gt m $$)。 10 | 11 | #### 解法 12 | 13 | Boyer-Moore算法与KMP算法类似,当匹配失败时跳转到某个位置继续匹配。 14 | 15 | -------- 16 | 17 | #### 源码 18 | 19 | [BoyerMoore.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/PatternMatch/BoyerMoore.h) 20 | 21 | [BoyerMoore.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/PatternMatch/BoyerMoore.cpp) 22 | 23 | #### 测试 24 | 25 | [BoyerMooreTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/PatternMatch/BoyerMooreTest.cpp) 26 | -------------------------------------------------------------------------------- /src/DataStructure/AvlTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | struct AvlNode { 4 | int value; 5 | int height; 6 | AvlNode *left; 7 | AvlNode *right; 8 | 9 | AvlNode(); 10 | AvlNode(int v, int h, AvlNode *l, AvlNode *r); 11 | }; 12 | 13 | extern AvlNode AVLNIL; 14 | 15 | struct AvlTree { 16 | AvlNode *root; 17 | }; 18 | 19 | #define is_nil(e) ((e) == &AVLNIL) 20 | #define not_nil(e) ((e) != &AVLNIL) 21 | #define set_nil(e) ((e) = &AVLNIL) 22 | 23 | // create AvlTree 24 | AvlTree *AvlTreeNew(); 25 | 26 | // free AVL Tree 27 | void AvlTreeFree(AvlTree *t); 28 | 29 | // insert into AvlTree 30 | void AvlTreeInsert(AvlTree *t, int value); 31 | 32 | // find value node 33 | AvlNode *AvlTreeFind(AvlTree *t, int value); 34 | 35 | // erase AvlTree 36 | void AvlTreeErase(AvlTree *t, int value); 37 | 38 | // AvlTree height 39 | int AvlTreeHeight(AvlTree *t); 40 | 41 | -------------------------------------------------------------------------------- /src/Sort/MergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include "MergeSort.h" 2 | #include 3 | 4 | static void Merge(int *s, int beg, int mid, int end) { 5 | int *t = new int[end + 1]; 6 | int i = beg, j = mid, k = beg; 7 | 8 | while (i < mid || j < end) { 9 | if (i < mid && j < end) { 10 | if (s[i] < s[j]) { 11 | t[k++] = s[i++]; 12 | } else { 13 | t[k++] = s[j++]; 14 | } 15 | } else if (i < mid) { 16 | t[k++] = s[i++]; 17 | } else if (j < end) { 18 | t[k++] = s[j++]; 19 | } 20 | } 21 | 22 | std::memcpy(s + beg, t + beg, (end - beg) * sizeof(int)); 23 | delete[] t; 24 | } 25 | 26 | void MergeSort(int *s, int beg, int end) { 27 | if (end >= beg + 2) { 28 | int mid = (beg + end) / 2; 29 | MergeSort(s, beg, mid); 30 | MergeSort(s, mid, end); 31 | Merge(s, beg, mid, end); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | *.out 3 | *.o 4 | *.slo 5 | *.lo 6 | *.a 7 | *.so 8 | *.gch 9 | *.exe 10 | *~ 11 | *.tlog 12 | *.lastbuildstate 13 | *.vs 14 | *.db 15 | *.pdb 16 | *.ilk 17 | *.idb 18 | *.obj 19 | *.pro.user 20 | *.dylib 21 | *.dll 22 | *.swp 23 | *.lai 24 | *.la 25 | *.lib 26 | *.app 27 | *.xcuserdata 28 | *.stamp 29 | *.depend 30 | *.cbp 31 | *.enc 32 | *.pdf 33 | *.epub 34 | *.mobi 35 | 36 | .idea 37 | *.idea 38 | *.filters 39 | *.vcxproj 40 | *.sln 41 | CMakeFiles 42 | *.cmake 43 | cmake-build-debug 44 | cmake_install.cmake 45 | makefile 46 | Makefile 47 | cmake-build-debug 48 | CMakeCache.txt 49 | .DS_Store 50 | _DS_Store 51 | Thumbs.db 52 | .temp 53 | .tags 54 | tags 55 | tags.lock 56 | tags.temp 57 | 58 | build 59 | bin 60 | .bin 61 | etc 62 | node_modules 63 | _book 64 | package.json 65 | package-lock.json 66 | -------------------------------------------------------------------------------- /docs/GraphTheory/MinSpanningTree/SecondMinSpanningTree/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Second Min Spanning Tree - 次小生成树 4 | 5 | -------- 6 | 7 | #### 问题 8 | 9 | 求无向图$$ UG $$的次小生成树(即权值第二小的生成树)。 10 | 11 | #### 解法 12 | 13 | -------- 14 | 15 | #### 源码 16 | 17 | [SecondMinSpanningTree.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/GraphTheory/MinimumSpanningTree/SecondMinSpanningTree.h) 18 | 19 | [SecondMinSpanningTree.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/GraphTheory/MinimumSpanningTree/SecondMinSpanningTree.cpp) 20 | 21 | #### 测试 22 | 23 | [SecondMinSpanningTreeTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/GraphTheory/MinimumSpanningTree/SecondMinSpanningTreeTest.cpp) 24 | 25 | -------------------------------------------------------------------------------- /src/Search/BidirectionalBreadthSearchTest.cpp: -------------------------------------------------------------------------------- 1 | #include "BidirectionalBreadthSearch.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define TEST_M_MAX 16 8 | #define TEST_N_MAX 16 9 | 10 | int main() { 11 | for (int i = 2; i < TEST_N_MAX; i++) 12 | for (int j = 2; j < TEST_M_MAX; j++) { 13 | BfsNode beg_pos(0, 0); 14 | BfsNode end_pos(i - 1, j - 1); 15 | deque path = BidirectionalBreadthSearch(i, j, beg_pos, end_pos); 16 | //保证路径长度为 j-1+i-1+1 17 | assert(path.size() == (i - 1 + j - 1 + 1)); 18 | //保证起点和终点位置 19 | assert(path.front() == beg_pos); 20 | assert(path.back() == end_pos); 21 | for (int k = 0; k < path.size() - 1; k++) { 22 | //保证路径中相邻两点在二维方格中也是相邻点 23 | AssertAdjacent(path[k], path[k + 1]); 24 | } 25 | } 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /leetcode/leetcode-40.hpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void backtrack(vector> &list, vector &tempList, const vector &nums, int remain, int start) { 4 | if (remain < 0) 5 | return; 6 | if (remain == 0) { 7 | list.push_back(tempList); 8 | return; 9 | } 10 | for (int i = start; i < nums.size(); i++) { 11 | tempList.push_back(nums[i]); 12 | backtrack(list, tempList, nums, remain - nums[i], i); 13 | tempList.erase(tempList.end()-1); 14 | } 15 | } 16 | vector> combinationSum(vector& candidates, int target) { 17 | vector> list; 18 | std::sort(candidates.begin(), candidates.end()); 19 | vector tempList; 20 | backtrack(list, tempList, candidates, target, 0); 21 | return list; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /src/PatternMatch/AhoCorasickAutomata.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #ifndef MAX 6 | #define MAX 128 7 | #endif 8 | #ifndef CHILD_MAX 9 | #define CHILD_MAX 26 10 | #endif 11 | 12 | struct AcNode { 13 | int code; 14 | // a-z 15 | char ch; 16 | bool is_leaf; 17 | AcNode *father; 18 | AcNode *child[CHILD_MAX]; 19 | AcNode *fail; 20 | AcNode *output; 21 | 22 | AcNode(int tag); 23 | AcNode(const AcNode &other); 24 | AcNode &operator=(const AcNode &other); 25 | }; 26 | 27 | // create AC automation 28 | AcNode *AhoCorasickAutomataNew(const std::vector &pattern); 29 | 30 | // free AC automation 31 | void AhoCorasickAutomataFree(AcNode *root); 32 | 33 | // match text with AC automation 34 | std::unordered_map> 35 | AhoCorasickAutomataMatch(AcNode *root, const std::string &text); 36 | 37 | -------------------------------------------------------------------------------- /src/DataStructure/FenwickTree.cpp: -------------------------------------------------------------------------------- 1 | #include "FenwickTree.h" 2 | #include 3 | #include 4 | 5 | static int LowBit(int x) { return x & (-x); } 6 | 7 | FwTree *FenwickTreeNew() { 8 | FwTree *t = new FwTree(); 9 | memset(t->bits, 0, MAX * sizeof(int)); 10 | return t; 11 | } 12 | 13 | void FenwickTreeFree(FwTree *t) { delete t; } 14 | 15 | void FenwickTreeAdd(FwTree *t, int i, int value) { 16 | assert(i > 0); 17 | for (int j = i; j < MAX; j += LowBit(j)) { 18 | t->bits[j] += value; 19 | } 20 | } 21 | 22 | static int PrefixSum(FwTree *t, int n) { 23 | int sum = 0; 24 | for (int j = n; j > 0; j -= LowBit(j)) { 25 | sum += t->bits[j]; 26 | } 27 | return sum; 28 | } 29 | 30 | int FenwickTreeSum(FwTree *t, int i, int j) { 31 | assert(i > 0); 32 | assert(j > i); 33 | int p = PrefixSum(t, i - 1); 34 | int q = PrefixSum(t, j - 1); 35 | return q - p; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /src/DataStructure/SegmentTreeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "SegmentTree.h" 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define TEST_MAX 128 8 | 9 | static int Sum(const int *s, int beg, int end) { 10 | int sum = 0; 11 | for (int i = beg; i < end; i++) { 12 | sum += s[i]; 13 | } 14 | return sum; 15 | } 16 | 17 | int main() { 18 | int s[TEST_MAX]; 19 | for (int i = 0; i < TEST_MAX; i++) { 20 | s[i] = i; 21 | } 22 | for (int i = 1; i < TEST_MAX; i++) { 23 | StNode *t = SegmentTreeNew(s, 0, i); 24 | for (int j = 0; j < i; j++) { 25 | int r = rand() % TEST_MAX; 26 | SegmentTreeAdd(t, j, r); 27 | s[j] += r; 28 | for (int p = 0; p <= i; p++) { 29 | assert(Sum(s, 0, p) == SegmentTreeSum(t, 0, p)); 30 | } // for 31 | } // for 32 | SegmentTreeFree(t); 33 | } // for 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /src/DataStructure/LeftistTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MAX 3 | #define MAX 64 4 | #endif 5 | 6 | struct LeftistTree; 7 | 8 | struct LtNode { 9 | //节点下标 10 | int index; 11 | int distance; 12 | LtNode *left; 13 | LtNode *right; 14 | LeftistTree *tree; 15 | }; 16 | 17 | struct LeftistTree { 18 | LtNode *root; 19 | int size; 20 | int (*cmp)(LtNode *, LtNode *); 21 | }; 22 | 23 | // create leftist tree 24 | LeftistTree *LeftistTreeNew(int (*Compare)(LtNode *a, LtNode *b)); 25 | 26 | // free leftist tree 27 | void LeftistTreeFree(LeftistTree *t); 28 | 29 | // merge two leftist tree 30 | LeftistTree *LeftistTreeMerge(LeftistTree *a, LeftistTree *b); 31 | 32 | // get top of leftist tree 33 | int LeftistTreeTop(LeftistTree *t); 34 | 35 | // push value to leftist tree 36 | int LeftistTreePush(LeftistTree *t, int index); 37 | 38 | // pop value from leftist tree 39 | int LeftistTreePop(LeftistTree *t); 40 | 41 | -------------------------------------------------------------------------------- /src/DynamicProgramming/RegionalDP/TrianglePathTest.cpp: -------------------------------------------------------------------------------- 1 | #include "TrianglePath.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct Test { 9 | int s[MAX]; 10 | int k; 11 | int result; 12 | } test_cases[] = { 13 | { 14 | {INF, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 15 | 10, 16 | 14, 17 | }, 18 | { 19 | {INF, 1, 10, 2, 9, 3, 8, 4, 7, 5, 6}, 20 | 10, 21 | 11, 22 | }, 23 | { 24 | {INF, 1, 10, 2, 9, 3, 8, 4, 7, 5, 6, 12, 7, 31, 30, 9}, 25 | 15, 26 | 20, 27 | }, 28 | }; 29 | 30 | int main(void) { 31 | for (int i = 0; i < sizeof(test_cases) / sizeof(Test); i++) { 32 | Test &t = test_cases[i]; 33 | int r = TrianglePath(t.s, t.k); 34 | cout << "i:" << i << ", r:" << r << endl; 35 | assert(r == t.result); 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /src/GraphTheory/BinaryMatch/6_weighted_covering_independent_set.cpp: -------------------------------------------------------------------------------- 1 | //最小点权覆盖和最大点权独立集 2 | //weighted_covering_independent_set.cpp 3 | 4 | //二分图中每个节点都拥有一个正权值,为了方便权值取正整数,求给定二分图中 5 | //权值最小的最小点覆盖的权值之和,和权值最大的点独立集的权值之和 6 | 7 | //1)最小点权覆盖 8 | //当二分图中每个节点的权值就是它的邻节点的数量时 9 | //最小点权覆盖问题退化为求二分图的最小点覆盖 10 | //而最小点权覆盖问题转化为最大流问题进行求解 11 | //将原二分图B构造为对应的流网络G,则对应流网络G的最大流即为原二分图B的最小点权覆盖 12 | //在二分图B=上添加源点beg和汇点end 13 | //设二分图中每条边e,一个端点i属于X集合,另一端点j属于Y集合 14 | //将每条边e改为一条从节点i指向节点j的有向边,权值为INF 15 | //从源点beg到X集合中每个节点i添加一条有向边,权值为节点i的权值 16 | //从Y集合中每个节点j到汇点end添加一条有向边,权值为节点j的权值 17 | //即得到对应的流网络 18 | //定理: 19 | //原二分图B的最小点权覆盖的权值之和 = 对应流网络的最小割 = 对应流网络的最大流 20 | //则可通过求对流流网络的最大流来解决原二分图的最小点权覆盖的权值之和 21 | //2)最大点权独立集 22 | //定理: 23 | //最大独立数 + 最小覆盖数 = 节点总数 24 | //应用于带权值的二分图时也成立: 25 | //最大点权独立集的权值和 + 最小点权覆盖的权值和 = 节点权值总和 26 | //则最大点权独立集的权值和 = 节点权值总和 - 最小点权覆盖的权值和 27 | //即最大点权独立集的权值和可以转化为最小点权覆盖的权值和,进一步转化为最大流问题 28 | 29 | //int weight_covering_independent_set(bipartite b) 30 | -------------------------------------------------------------------------------- /src/NumberTheory/ExtendedEuclid.cpp: -------------------------------------------------------------------------------- 1 | #include "ExtendedEuclid.h" 2 | 3 | std::tuple ExtendedEuclid(int a, int b) { 4 | // step_{k} 5 | // a_{k} = a 6 | // b_{k} = b 7 | 8 | if (b == 0) { 9 | 10 | // b_{n+1} = 0 11 | // gcd_{n+1} = a_{n+1} 12 | // x_{n+1} = 1 13 | // y_{n+1} = 0 14 | 15 | return std::make_tuple(a, 1, 0); 16 | } 17 | 18 | // step_{k+1} 19 | // a_{k+1} = b_{k} 20 | // b_{k+1} = a_{k} % b_{k} 21 | 22 | std::tuple temp = ExtendedEuclid(b, a % b); 23 | 24 | // temp_{0} = gcd(a_{k+1}, b_{k+1}); 25 | // temp_{1} = x_{k+1} 26 | // temp_{2} = y_{k+1} 27 | 28 | // x_{k} = y_{k+1} 29 | // y_{k} = x_{k+1} - (a_{k} / b_{k}) * y_{k+1} 30 | 31 | int gcd = std::get<0>(temp); 32 | int x = std::get<2>(temp); 33 | int y = std::get<1>(temp) - (a / b) * std::get<2>(temp); 34 | 35 | // recursion return 36 | return std::make_tuple(gcd, x, y); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/DataStructure/FenwickTreeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "FenwickTree.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #define TEST_MAX 128 9 | 10 | static int Sum(const int *s, int beg, int end) { 11 | int sum = 0; 12 | for (int i = beg; i < end; i++) { 13 | sum += s[i]; 14 | } 15 | return sum; 16 | } 17 | 18 | int main() { 19 | for (int i = 2; i < TEST_MAX; i++) { 20 | int s[TEST_MAX]; 21 | memset(s, 0, sizeof(int) * TEST_MAX); 22 | FwTree *t = FenwickTreeNew(); 23 | for (int j = 1; j < i; j++) { 24 | int r = rand() % TEST_MAX; 25 | FenwickTreeAdd(t, j, r); 26 | s[j] += r; 27 | for (int p = 2; p <= i; p++) { 28 | int r1 = Sum(s, 1, p); 29 | int r2 = FenwickTreeSum(t, 1, p); 30 | assert(r1 == r2); 31 | } // for 32 | } // for 33 | FenwickTreeFree(t); 34 | } // for 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/FullPermutation.cpp: -------------------------------------------------------------------------------- 1 | #include "FullPermutation.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static void FullPermutationImpl(std::vector &A, int n, int prev, 7 | std::vector> &result) { 8 | if (prev == 0) { 9 | result.push_back(A); 10 | return; 11 | } 12 | 13 | // 依次将A[prev]与它前面的prev-1个元素进行交换 14 | // i从prev开始 是为了产生一个初始化排列 该排列与A传入时完全一样 15 | for (int i = prev; i >= 0; i--) { 16 | std::swap(A[i], A[prev]); 17 | FullPermutationImpl(A, n, prev - 1, result); 18 | std::swap(A[i], A[prev]); 19 | } 20 | } 21 | 22 | std::vector> FullPermutation(int s[MAX], int n) { 23 | // 初始化 A=[s1, s2, s3, ..., sn] 24 | std::vector A; 25 | for (int i = 0; i < n; i++) 26 | A.push_back(s[i]); 27 | std::vector> perm; 28 | FullPermutationImpl(A, n, n - 1, perm); 29 | 30 | return perm; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /src/PatternMatch/KnuthMorrisPrattTest.cpp: -------------------------------------------------------------------------------- 1 | #include "KnuthMorrisPratt.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Test { 7 | string text; 8 | string pattern; 9 | vector pos; 10 | } tests[] = { 11 | {"asdfasdfasdfasdf", "asdf", {0, 4, 8, 12}}, 12 | {"helloworldgoodbyeworldthisisagoodbookformanycomputersciencestudentihopeyo" 13 | "uenjoy", 14 | "oo", 15 | {11, 30, 34}}, 16 | {"sting", "in", {2}}, 17 | {"abedget", "be", {1}}, 18 | {"aaaaaaaa", "aa", {0, 1, 2, 3, 4, 5, 6}}, 19 | {"shherishers", "her", {2, 7}}, 20 | {"helloworldgoodbyeworldthisisagoodbookformanycomputersciencestudentihop" 21 | "eyouenjoy", 22 | "i", 23 | {24, 26, 54, 66}}, 24 | }; 25 | 26 | int main() { 27 | for (int i = 0; i < sizeof(tests) / sizeof(Test); i++) { 28 | Test &t = tests[i]; 29 | assert(KnuthMorrisPratt(t.text, t.pattern) == t.pos); 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /docs/GraphTheory/BinaryMatch/Hungarian/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Hungarian - 匈牙利算法 4 | 5 | -------- 6 | 7 | #### 问题 8 | 9 | 用匈牙利算法求二分图的两个顶点子集。 10 | 11 | #### 解法 12 | 13 | 14 | -------- 15 | 16 | #### Introduction To Algorithms 17 | 18 | * [VI.Graph Algorithms - 26.Maximum Flow - 26.2.The-Ford-Fulkerson method](https://www.google.com/search?q=Introduction+to+Algorithms+3rd+Edition+pdf) 19 | 20 | -------- 21 | 22 | #### 源码 23 | 24 | [Hungarian.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/GraphTheory/NetworkFlow/Hungarian.h) 25 | 26 | [Hungarian.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/GraphTheory/NetworkFlow/Hungarian.cpp) 27 | 28 | #### 测试 29 | 30 | [HungarianTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/GraphTheory/NetworkFlow/HungarianTest.cpp) 31 | -------------------------------------------------------------------------------- /src/Search/Util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | void AssertEq(int *s1, int n1, int *s2, int n2); 6 | void AssertUnique(const std::vector> &v); 7 | 8 | struct BfsNode { 9 | int row; 10 | int col; 11 | 12 | BfsNode(); 13 | BfsNode(int row, int col); 14 | friend bool operator==(const BfsNode &a, const BfsNode &b); 15 | friend bool operator!=(const BfsNode &a, const BfsNode &b); 16 | }; 17 | 18 | namespace std { 19 | 20 | template <> struct hash { 21 | std::size_t operator()(const BfsNode &e) const { 22 | using std::hash; 23 | using std::size_t; 24 | 25 | return hash()(e.row) ^ hash()(e.col); 26 | } 27 | }; 28 | 29 | } // namespace std 30 | 31 | extern int row_dir[4]; 32 | extern int col_dir[4]; 33 | 34 | #ifndef in_range 35 | #define in_range(pos, range) ((pos) >= 0 && (pos) < range) 36 | #endif 37 | 38 | void AssertAdjacent(const BfsNode &a, const BfsNode &b); 39 | 40 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | #add_executable(CombinatorialMathematics-Catalan.exe Catalan.cpp Catalan.hpp) 4 | #add_executable(CombinatorialMathematics-Combination.exe Combination.cpp Combination.hpp) 5 | #add_executable(CombinatorialMathematics-DuplicableCombination.exe DuplicableCombination.cpp DuplicableCombination.hpp) 6 | #add_executable(CombinatorialMathematics-FullPermutation.exe FullPermutation.cpp FullPermutation.hpp) 7 | #add_executable(CombinatorialMathematics-UniqueFullPermutation.exe UniqueFullPermutation.cpp UniqueFullPermutation.hpp) 8 | #add_executable(CombinatorialMathematics-Permutation.exe Permutation.cpp Permutation.hpp) 9 | #add_executable(CombinatorialMathematics-PermutationGroup.exe PermutationGroup.cpp PermutationGroup.hpp) 10 | #add_executable(CombinatorialMathematics-Subset.exe Subset.cpp Subset.hpp) 11 | #add_executable(CombinatorialMathematics-UniqueSubset.exe UniqueSubset.cpp UniqueSubset.hpp) 12 | -------------------------------------------------------------------------------- /src/PatternMatch/SimpleMatchTest.cpp: -------------------------------------------------------------------------------- 1 | #include "SimpleMatch.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define TEST_MAX 1024 7 | 8 | struct Test { 9 | string text; 10 | string pattern; 11 | vector pos; 12 | } tests[] = { 13 | {"asdfasdfasdfasdf", "asdf", {0, 4, 8, 12}}, 14 | {"sting", "in", {2}}, 15 | {"abedget", "bed", {1}}, 16 | {"aaaaaaaa", "aaa", {0, 1, 2, 3, 4, 5}}, 17 | {"shherishers", "he", {2, 7}}, 18 | {"helloworldgoodbyeworldthisisagoodbookformanycomputersciencestudentihop" 19 | "eyouenjoy", 20 | "oo", 21 | {11, 30, 34}}, 22 | {"helloworldgoodbyeworldthisisagoodbookformanycomputersciencestudentihop" 23 | "eyouenjoy", 24 | "world", 25 | {5, 17}}, 26 | }; 27 | 28 | int main() { 29 | for (int i = 0; i < sizeof(tests) / sizeof(Test); i++) { 30 | Test &t = tests[i]; 31 | assert(SimpleMatch(t.text, t.pattern) == t.pos); 32 | } 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /src/PatternMatch/RabinKarpTest.cpp: -------------------------------------------------------------------------------- 1 | #include "RabinKarp.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Test { 7 | string text; 8 | string pattern; 9 | vector pos; 10 | } tests[] = { 11 | {"asdfasdfasdfasdf", "asdf", {0, 4, 8, 12}}, 12 | {"helloworldgoodbyeworldthisisagoodbookformanycomputersciencestudentihopeyo" 13 | "uenjoy", 14 | "oo", 15 | {11, 30, 34}}, 16 | {"sting", "in", {2}}, 17 | {"abedget", "be", {1}}, 18 | {"aaaaaaaa", "aa", {0, 1, 2, 3, 4, 5, 6}}, 19 | {"shherishers", "her", {2, 7}}, 20 | {"helloworldgoodbyeworldthisisagoodbookformanycomputersciencestudentihop" 21 | "eyouenjoy", 22 | "i", 23 | {24, 26, 54, 66}}, 24 | }; 25 | 26 | int main() { 27 | for (int i = 0; i < sizeof(tests) / sizeof(Test); i++) { 28 | Test &t = tests[i]; 29 | cout << "i: " << i << endl; 30 | assert(RabinKarp(t.text, t.pattern) == t.pos); 31 | } 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /docs/Preface/README.md: -------------------------------------------------------------------------------- 1 | # Preface 2 | # 前言 3 | 4 | -------- 5 | 6 | 在大学期间刷算法题的时候,遇到很多题目要么太简单,要么怎么也想不出办法。 7 | 这时因为没有真正理解算法,只会套用各种模型,题目稍微改变就搞不定了。 8 | 我当时收集了各种关于算法的文档、资料和源码,整理、分类,还写了很多自己的理解,放在github项目中,就是本书的原型。 9 | 后来又尝试加入了数学公式、插图和C++源码,把这些东西通过gitbook的形式展示出来。就成了目前的样子。 10 | 11 | 将算法和数据结构图形化、公式化可以更准确、更清晰的描述问题、解决方案和算法的时间/空间复杂度。 12 | 真正把算法背后的数学模型讲明白,让读者理解。 13 | 除此以外,本书对种类繁多的算法进行了细致的分类,每一章专门讲一类问题,其中每个小节专门讲解该类问题的一个分支或变种。 14 | 从目录就可以看出各个算法之间的联系和变化。通读整章之后更能够加深学习。 15 | 16 | 本书的所有算法都有C++实现和测试,配套辅助的运行脚本,可以在主流的Windows、Linux、MacOS系统上运行。 17 | 一些章节还会介绍相对小众的专业知识、参考资料和leetcode题目,多维度的提供优质资料。 18 | 19 | 由于个人经验有限,难免存在一些疏漏和错误的地方,还请指正。 20 | 读者的支持是这个项目更新的动力,欢迎大家增加更多的内容,和对已有的文档、代码进行修正。 21 | 22 | 特别感谢以下同学参与项目: 23 | 24 | * [NEWPLAN](https://github.com/NEWPLAN) 25 | 26 | 如果你想参与,编写文档时尽量与本书风格保持一致,推荐使用我的开发工具: 27 | 28 | * [lin-vim](https://github.com/linrongbin16/lin-vim) 29 | * [boostscript](https://github.com/linrongbin16/boostscript) 30 | 31 | -------- 32 | 33 | 西安交通大学计算机系
34 | 林荣彬
35 | 2014年2月16日
36 | -------------------------------------------------------------------------------- /docs/Search/res/Search.xml: -------------------------------------------------------------------------------- 1 | zZZNc5swEIZ/DdcOICPLx4Ym7aWdzvjQ9iiLBTQRiBFyMP31FSA+ZHCSTtOZcLH21Wp39ewa20NxcfmsaJV/lQkIL/STi4c+eWEY7EJsPjqlHZQ9IoOQKZ5Yp1k48t9gRd+qZ55A7ThqKYXmlSsyWZbAtKNRpWTjuqVSuFkrmsFKODIq1uoPnuh8UEm4n/UvwLN8zBzgw7BzouwxU/Jc2nxeiNL+GbYLOsayF61zmshmIaF7D8VKSj2siksMomM7YhvOPdzYnepWUOpXHYhs4U9UnGGsua9MtyON/j7QnfA9dNfkXMOxoqzbbUz/jZbrQhgrMMtUlvqBFlx0rY/lWXFQJtw3aOym7XZAOpsLEUshVZ9oRIXuaq3kIyx2/P4xO7ZaUBouN+8cTCTNhIIsQKvWuNgDkW/h2+EM99Zu5laHO6vlizZPIrXjlU2xZ8RmYSlvE4/eEe+EAknZFm/MCJzSifcK7kYLbvLGxOW9i9a88QZu/Aa0yTuinRIGbJP2iUQdlDeh/Zrp/l+0Axy8zBvK5GP3ijYWE7SuOXMRm6ur9qdtR2/86owP0cQHktXbewVMU5WBXnzl1sAWQKINIKOmQFDNn9yMW5Rshu+Sm1rmfuCrfhwObojaTBEDe2r5qr4KhNELgYYrrwIZ2LRduFWdQ327YBxs57lZ15U/Iv6z/hH6N/8d+kt/N75ZDETmiZ5mYmvIjTn/HA/u838edP8H -------------------------------------------------------------------------------- /src/DynamicProgramming/Util.cpp: -------------------------------------------------------------------------------- 1 | #include "Util.h" 2 | #include 3 | 4 | int *ArrayNew(int n) { 5 | int *f = new int[n]; 6 | return f; 7 | } 8 | 9 | void ArrayFree(int *f) { delete[] f; } 10 | 11 | int **Array2DNew(int n, int m) { 12 | int **f = new int *[n]; 13 | for (int i = 0; i < n; i++) { 14 | f[i] = new int[m]; 15 | } 16 | return f; 17 | } 18 | 19 | void Array2DFree(int **f, int n) { 20 | for (int i = 0; i < n; i++) { 21 | delete[] f[i]; 22 | } 23 | delete[] f; 24 | } 25 | 26 | int ***Array3DNew(int n, int m, int t) { 27 | int ***f = new int **[n]; 28 | for (int i = 0; i < n; i++) { 29 | f[i] = new int *[m]; 30 | for (int j = 0; j < m; j++) { 31 | f[i][j] = new int[t]; 32 | } 33 | } 34 | return f; 35 | } 36 | 37 | void Array3DFree(int ***f, int n, int m) { 38 | for (int i = 0; i < n; i++) { 39 | for (int j = 0; j < m; j++) { 40 | delete[] f[i][j]; 41 | } 42 | delete[] f[i]; 43 | } 44 | delete[] f; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /book.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "算法之路", 3 | "description": "算法教程与源码", 4 | "language": "zh-hans", 5 | "author": "林荣彬", 6 | "plugins": [ 7 | "mathjax@https://github.com/OrgVue/gitbook-plugin-mathjax.git#speech-fix", 8 | "splitter", 9 | "-search", 10 | "-lunr", 11 | "livereload", 12 | "autocover" 13 | ], 14 | "pluginsConfig": { 15 | "search": { 16 | "maxIndexSize": 100000 17 | }, 18 | "include-codeblock": { 19 | "template": "ace", 20 | "theme": "monokai" 21 | }, 22 | "autocover": { 23 | "font": { 24 | "size": null, 25 | "family": "Impact", 26 | "color": "#FFF" 27 | }, 28 | "size": { 29 | "w": 1800, 30 | "h": 2360 31 | }, 32 | "background": { 33 | "color": "#09F" 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /docs/CombinatorialMathematics/res/CombinationMathematics.xml: -------------------------------------------------------------------------------- 1 | 7ZnLkqMgFIafxn2E6JhtO33ZTG+y6DWNR6UaxULSmnn6wYjxQqZqFhM6XcZN5Achh49zKfVwXLTPklT5L5EA99AmaT3800PI36JQ/3TKsVeiYNcLmWSJGTQKe/YbjLgx6oElUM8GKiG4YtVcpKIsgaqZRqQUzXxYKvh81YpkYAl7SritvrFE5cYK9GPUX4Bl+bCyHxr73gn9yKQ4lGY9D+H0dPXdBRnmMobWOUlEM5Hwo4djKYTq74o2Bt7t7bBt/XNPf+k9/28JpfqXB1D/wCfhh7nptToOm9HkTMG+IrRrNxq4hx9yVXDd8vUtqaseQcpa0NM+1EqKD4gFF/I0AX6Pgm2w0T2pKNUTKRjvjkUsDpKB1Gu9QmM6zUnwo25ezrJSN6g2RQ/TAxjnk1nTiAKlWrdtNjZ8glTQTiSzB88gClDyqIcMvTvDw5xXFJl2M9IPjZRPwA8aMectO8887rm+Mdt+GQG2EKA1Ijjv5Rcg2LpAsDld/x1BH12u4gU4cIcgtBDgNSLA+Ou8YEi+a08Gy0jk0g1832JwBT+4fQZLP3DKwEk6uH0Gy3SwRQ4ZBHc/uBSLnDKwc/IV8sH3y8lOGezuddHFWLRzxwDZddEq84EVi1wysOuiVdamVixyycCui1aZk5exKAgdMrDrolX6wTIWOWVg10WrfF20jEVOGUQu/OD2GSxjUYgdMnBSm36/WOSSAb7Xphdj0RUZ6Ob4fe7UN/kIih//AA== -------------------------------------------------------------------------------- /docs/DataStructure/PrefixTree/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Prefix Tree(Trie Tree) - 前缀树 4 | 5 | -------- 6 | 7 | #### 前缀树 8 | 9 | 前缀树是一种按照前缀快速查找的数据结构,典型的应用场景是在一个英文单词集合中快速查询某个单词。 10 | 11 | 为了方便,本问题只考虑字典中的$$ a - z $$这$$ 26 $$个小写字母。前缀树的树节点包含$$ 26 $$个孩子节点,跟节点不包含任何字符,从根节点开始向下查找就可以得到完整的单词。一个包含$$ boy $$、$$ dog $$、$$ bible $$、$$ bill $$的前缀树如图: 12 | 13 | ![PrefixTree1.png](../res/PrefixTree1.png) 14 | 15 | 每次查找单词,从根节点开始向下匹配即可。在前缀树中查找一个长度为$$ n $$的单词的时间复杂度为$$ O(n) $$。 16 | 17 | -------- 18 | 19 | #### 源码 20 | 21 | [PrefixTree.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/DataStructure/PrefixTree.h) 22 | 23 | [PrefixTree.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/DataStructure/PrefixTree.cpp) 24 | 25 | #### 测试 26 | 27 | [PrefixTreeTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/DataStructure/PrefixTreeTest.cpp) 28 | -------------------------------------------------------------------------------- /docs/Sort/res/Sort.xml: -------------------------------------------------------------------------------- 1 | 1VdNj5swEP01XCuCAyHHNpvdHtqqUg5tjw4M4K5hkHFC0l9fA+MASRq1UlQ2XPC8GX/Mmzd8OGyVH14UL7PPGIN0PDc+OOzJ8bzZ3AvMrUGOHbJgYQekSsQU1AMb8QsIdAndiRiqUaBGlFqUYzDCooBIjzCuFNbjsATleNeSp3ABbCIuL9FvItZZh4beosc/gkgzu/MsWHaeLY9eU4W7gvZzPJa0V+fOuV2LEq0yHmM9gNjaYSuFqLtRfliBbLi1tHXznv/gPZ1bQaH/ZoI9d6WPNneIDRVkotIZplhwue7RD21+0KzgGivTuTTDmRmaTdXxe4O/8635g3w/QesjVZrvNBqoX/0TYklxCRb6medCNtJZ4U4JUOaAX6AmJ60xC419mS4xUJmJESXkkYC4SoGiSJ5NqoNpRNELYA7m5CZAgeRa7Meq4CSu9BRHU98rxY+DgBJFoavByl8bwARQn5xUQF3C/LNincXP3ZvxZtCdwFqDVHqoFcB1MRBRey53YOV7Jo5x6etMaNiUvGW6No+CsRz+qZSJkHKFElW7EYs5hElk8EorfIWBJ4hC2Ca3ir8HpeFws7DWuxwz6i3IrvuuDwjKBg1vsWtSGJTjJtvzKdm+A3eePx13/oNzx2bTcRe8oS5Pwgiiq12+Df25796J7cV0bC8eXKlzNh134aNzN+HbZfmWurz7Ar7S5W57NR77HXynlvcnfDnZZ/ujcH8HuoP/9z4zZv971H3k9v+gbP0b -------------------------------------------------------------------------------- /src/DynamicProgramming/BagDP/TwoDimensionBag.cpp: -------------------------------------------------------------------------------- 1 | #include "TwoDimensionBag.h" 2 | #include "../Util.h" 3 | #include 4 | #include 5 | 6 | int TwoDimensionBag(const int *v, const int *w1, const int *w2, int n, 7 | int weight1, int weight2) { 8 | int ***f = Array3DNew(n + 1, weight1 + 1, weight2 + 1); 9 | for (int i = 0; i <= n; i++) 10 | for (int j = 0; j <= weight1; j++) 11 | for (int k = 0; k <= weight2; k++) 12 | f[i][j][k] = 0; 13 | 14 | for (int i = 1; i <= n; i++) { 15 | for (int j = 0; j <= weight1; j++) { 16 | for (int k = 0; k <= weight2; k++) { 17 | 18 | if (j >= w1[i] && k >= w2[i]) { 19 | f[i][j][k] = 20 | std::max(f[i - 1][j][k], f[i - 1][j - w1[i]][k - w2[i]] + v[i]); 21 | } else { 22 | f[i][j][k] = f[i - 1][j][k]; 23 | } 24 | } 25 | } 26 | } 27 | 28 | int result = f[n][weight1][weight2]; 29 | Array3DFree(f, n + 1, weight1 + 1); 30 | return result; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /src/GraphTheory/MinSpanningTree/4_degree_bounded_spanning_tree.cpp: -------------------------------------------------------------------------------- 1 | //度限制生成树 2 | //degree_bounded_spanning_tree.cpp 3 | 4 | //在构造最小生成树时,图中一个指定的节点有度的限制 5 | //指定某节点v0,它在生成树中的度数为k,它在生成树中必须有k条边,即degree(v0) = k 6 | //这样被指定的度限制节点只有一个,当为多个时,本问题成为一个NP完全问题 7 | 8 | //无解情况: 9 | //在无向简单图G中,当将指定节点v0及其边去掉,剩下的图被分成m个图,即m个连通分量 10 | //当k < m时,肯定不存在k度限制最小生成树,即无解情况 11 | // 12 | //有解情况: 13 | //在图G中,先将v0点及其边排除在外,得到剩余图的m个连通分量 14 | //构造每个连通分量的最小生成树,记录每个连通分量的生成树的权值和weight 15 | //然后从v0向每个连通分量添加一条边(选择权值最小的边),共m条边(m <= k) 16 | //这样就得到了m度限制生成树 17 | // 18 | //1)原始思路: 19 | //若m < k,则还需要向v0添加 k-m 条边,对v0的剩余边按权值从小到大进行如下操作: 20 | //对v0剩余的每条边e,添加入一个连通分量的生成树中 21 | //此边一会让该生成树形成环,找出环中权值最大的边并删除 22 | //可以得到一个新的生成树权值和weight,若新替换的边使生成树权值减少,则枚举下一条边 23 | //若这条新替换的边不使生成树权值和减少,则跳过这条边 24 | //这样添加 k-m 条边,则v0度数为k,即可得到v0节点的k度限制生成树 25 | //缺点是每考虑一条边,就要枚举这条边的环上的所有边,找出权值最大的边,时间复杂度较高 26 | // 27 | //2)优化方法:用动态规划方法求环中权值最大的边 28 | //和次小生成树中的方法一样用bfs和动态规划的方法构造一个max_edge表 29 | //得到图中任意两点之间,在最小生成树的路径上的权值最大的边 30 | 31 | //void degree_bounded_spanning_tree(edge_list& e, int n, int beg, int k) 32 | -------------------------------------------------------------------------------- /docs/AnalyticGeometry/README.md: -------------------------------------------------------------------------------- 1 | # Chapter-10 Analytic Geometry 2 | # 第10章 解析几何 3 | 4 | ![AnalyticGeometry.png](res/AnalyticGeometry.png) 5 | 6 | -------- 7 | 8 | 1. Section-1 Polygon 第1节 多边形 9 | 1. [Cross 向量叉积](Polygon/Cross/) 10 | 2. [SegmentIntersection 线段相交](Polygon/SegmentIntersection/) 11 | 3. [Sweeping 扫除算法](Polygon/Sweeping/) 12 | 4. [ConvexPolygonArea 凸多边形面积](Polygon/ConvexPolygonArea/) 13 | 5. [ConvexPolygonGravityCenter 凸多边形重心](Polygon/ConvexPolygonGravityCenter/) 14 | 6. [NearestNeighbor 最近点对](Polygon/NearestNeighbor/) 15 | 2. [Section-2 ConvexHull 第2节 凸包](ConvexHull/) 16 | 2. [GrahamScan Graham扫描算法](ConvexHull/GrahamScan/) 17 | 3. [QuickHull 快速凸包算法](ConvexHull/QuickHull/) 18 | 4. [RotatingCalipers 旋转卡壳](Polygon/RotatingCalipers/) 19 | 20 | -------- 21 | 22 | #### 公共代码 23 | 24 | [Util.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/AnalyticGeometry/Util.h) 25 | 26 | [Util.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/AnalyticGeometry/Util.cpp) 27 | -------------------------------------------------------------------------------- /src/DataStructure/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | set(SRC_FILE 4 | AvlTree.cpp 5 | DisjointSet.cpp 6 | FenwickTree.cpp 7 | LeftistTree.cpp 8 | PrefixTree.cpp 9 | RedBlackTree.cpp 10 | SegmentTree.cpp 11 | BinarySearchTree.cpp 12 | SkipList.cpp) 13 | 14 | add_executable(DataStructure-DisjointSet.exe DisjointSetTest.cpp ${SRC_FILE}) 15 | add_executable(DataStructure-SegmentTree.exe SegmentTreeTest.cpp ${SRC_FILE}) 16 | add_executable(DataStructure-LeftistTree.exe LeftistTreeTest.cpp ${SRC_FILE}) 17 | add_executable(DataStructure-PrefixTree.exe PrefixTreeTest.cpp ${SRC_FILE}) 18 | add_executable(DataStructure-FenwickTree.exe FenwickTreeTest.cpp ${SRC_FILE}) 19 | add_executable(DataStructure-BinarySearchTree.exe BinarySearchTreeTest.cpp ${SRC_FILE}) 20 | add_executable(DataStructure-AvlTree.exe AvlTreeTest.cpp ${SRC_FILE}) 21 | add_executable(DataStructure-RedBlackTree.exe RedBlackTreeTest.cpp ${SRC_FILE}) 22 | add_executable(DataStructure-SkipList.exe SkipListTest.cpp ${SRC_FILE}) 23 | -------------------------------------------------------------------------------- /src/DynamicProgramming/TreeDP/MaximumBinaryTreeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "MaximumBinaryTree.h" 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct Test { 8 | int value[MAX]; 9 | int left[MAX]; 10 | int right[MAX]; 11 | int n; 12 | int m; 13 | int result; 14 | } testcases[] = { 15 | { 16 | {0, 1, 2, 3}, 17 | {0, 2, 0, 0}, 18 | {0, 3, 0, 0}, 19 | 3, 20 | 2, 21 | 4, 22 | }, 23 | { 24 | {0, 10, 9, 4, 1, 13, 2, 10}, 25 | {0, 2, 4, 6, 0, 0, 0, 0}, 26 | {0, 3, 5, 7, 0, 0, 0, 0}, 27 | 7, 28 | 4, 29 | 36, 30 | }, 31 | }; 32 | 33 | int main(void) { 34 | for (int i = 0; i < sizeof(testcases) / sizeof(Test); i++) { 35 | Test &t = testcases[i]; 36 | BiNode *tree = MaximumBinaryTreeNew(t.value, t.left, t.right, t.n); 37 | int r = MaximumBinaryTree(tree, t.n, t.m); 38 | MaximumBinaryTreeFree(tree); 39 | assert(r == t.result); 40 | } 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /docs/CombinatorialMathematics/Permutation/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Permutation - 排列 4 | 5 | -------- 6 | 7 | #### 问题 8 | 9 | 从包含$$ n $$个不同元素的集合$$ s = [ x_0, x_1, x_2, \dots , x_{n-1} ]$$中任意取$$ m $$个元素($$ m \leq n $$),组成的所有排列。其中任意两元素互不相同。 10 | 11 | #### 解法 12 | 13 | 在Full Permutation和Combination的基础上,从拥有$$ n $$个元素的$$ s $$中选取$$ m $$个元素,可得到$$ \frac{n!}{m! \cdot (n-m)!} $$个组合。遍历所有组合进行全排列,即为所求。 14 | 15 | 该算法的时间复杂度为$$ O(P_m^n) = O( \frac {n!} {(n-m)!} ) $$。 16 | 17 | -------- 18 | 19 | #### 源码 20 | 21 | [Permutation.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/CombinatorialMathematics/Permutation.h) 22 | 23 | [Permutation.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/CombinatorialMathematics/Permutation.cpp) 24 | 25 | #### 测试 26 | 27 | [PermutationTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/CombinatorialMathematics/PermutationTest.cpp) 28 | -------------------------------------------------------------------------------- /docs/AnalyticGeometry/ConvexHull/QuickHull/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Quick Hull - 快速凸包算法 4 | 5 | -------- 6 | 7 | #### 问题 8 | 9 | 用Quick Hull算法求拥有$$ n $$个点的点集$$ Q $$的凸包$$ CH(Q) $$,任意两点的坐标不同。 10 | 11 | #### 解法 12 | 13 | 该算法的时间复杂度为$$ O(n \cdot log_2 n) $$。 14 | 15 | -------- 16 | 17 | #### Introduction to Algorithms 18 | 19 | * [VII.Selected Topics - 33.Computational Geometry - 33.3.Finding the convex hull](https://www.google.com/search?q=Introduction+to+Algorithms+3rd+Edition+pdf) 20 | 21 | -------- 22 | 23 | #### 源码 24 | 25 | [QuickHull.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/AnalyticGeometry/ConvexHull/QuickHull.h) 26 | 27 | [QuickHull.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/AnalyticGeometry/ConvexHull/QuickHull.cpp) 28 | 29 | #### 测试 30 | 31 | [QuickHullTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/AnalyticGeometry/ConvexHull/QuickHullTest.cpp) 32 | -------------------------------------------------------------------------------- /docs/NumberTheory/res/NumberTheory.xml: -------------------------------------------------------------------------------- 1 | 5ZnNjpswFIWfhmUrbBN+tqGT6abdZNG1B0ywxuDIOJOkT18nmAC6HalVGScFFhE+xnb8+YqjIzySVqdnRfflN5kz4WE/P3nki4dxnBDzexHOrYBjFLXKTvG81VAvbPlPZkXfqgees2b0oJZSaL4fi5msa5bpkUaVksfxY4UU41X3dMeAsM2ogOoPnuvS7gtHvf6V8V3ZrYzCpO15odnrTslDbdfzMCmuV9td0W4uu9GmpLk8DiTy5JFUSanbu+qUMnFh22Frx23e6b39b8Vq/UcDYn/Vjnmj4jDefaPPHY9jyTXb7ml2aR/NmXtkXepKmBYyt7TZt6dQ8BMzM68LLkQqhVTX4WRzvS66rLU9bRTb9oZWXFwKJZUHxZkyK39nBska7sX+sTemNDsNJLu3ZyYrptXZPGJ78cpytoX4Cfu+VY79uYZWKgdH2mnUVtLuNndP09xYoO/CjQBcPDXcIs5Ylv0t3EYr+coG07zEq8DQmgQ7Ce6MPQbYyQKwB+TO2BOAPZjNq2SF7wu3s8YB3NUCajpEd8aOAPZwNjUN7BElyClcDOBGC6hpYI+usROAPZ5NTQMTdA03AHCT2cAFJuga7m/Cij8busDrXNMNId3Js+ADvo+hDcbYKXeYEjtnnEFVQ7dzTBeGQbTINOiaO0yDaMZx0DHdbrEh3cnz4AM5oWO6MPWhOce+iDilC2MfWmbuc8wd5j405+DnmC4Mfmjy5PeAVQ2d0DF3mAlvX9f+/6qGTuiYLsyEn6/XpIRrWbN/K2o7w4e4Yxh8GHHT7D/2XvsGX9TJ0y8= -------------------------------------------------------------------------------- /src/Search/RecursionTest.cpp: -------------------------------------------------------------------------------- 1 | #include "Recursion.h" 2 | #include "Util.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | #define TEST_N_MAX 6 12 | #define TEST_M_MAX 6 13 | 14 | void AssertCombination(int n, int m, const vector> &result) { 15 | assert(result.size() == (int)std::pow(m, n)); 16 | AssertUnique(result); 17 | for (int i = 0; i < result.size(); i++) { 18 | const vector &vec = result[i]; 19 | assert(vec.size() == n); 20 | for (int j = 0; j < vec.size(); j++) { 21 | assert(vec[j] >= 0); 22 | assert(vec[j] < m); 23 | } 24 | } 25 | } 26 | 27 | int main() { 28 | int s[MAX]; 29 | 30 | for (int i = 1; i < TEST_N_MAX; i++) 31 | for (int j = 1; j < TEST_M_MAX; j++) { 32 | std::memset(s, 0, sizeof(int) * MAX); 33 | vector> result; 34 | Recursion(s, i, j, 0, result); 35 | AssertCombination(i, j, result); 36 | } 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/MaximumContinuousSubsequenceSumTest.cpp: -------------------------------------------------------------------------------- 1 | #include "MaximumContinuousSubsequenceSum.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define TEST_MAX 1024 7 | 8 | struct Test { 9 | int s[TEST_MAX]; 10 | int n; 11 | int result; 12 | } test_cases[] = { 13 | { 14 | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 15 | 10, 16 | 55, 17 | }, 18 | { 19 | {0, 1, -2, 3, -4, -5, -6, 7, 18, -9, 10}, 20 | 10, 21 | 26, 22 | }, 23 | { 24 | {0, 7, -2, 23, -4, -15, -6, 7, 38, -19, 20}, 25 | 10, 26 | 49, 27 | }, 28 | { 29 | {0, 12, 142, -125, 14, 15, 45, 42, 61, 41, 30 | 0, 42, 42, -205, 14, 215, 45, -42, 1, 31}, 31 | 19, 32 | 390, 33 | }, 34 | }; 35 | 36 | int main(void) { 37 | for (int i = 0; i < sizeof(test_cases) / sizeof(Test); i++) { 38 | Test &t = test_cases[i]; 39 | int r = MaximumContinuousSubsequenceSum(t.s, t.n); 40 | assert(r == t.result); 41 | } 42 | 43 | return 0; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /docs/Search/README.md: -------------------------------------------------------------------------------- 1 | # Chapter-3 Search 2 | # 第3章 搜索 3 | 4 | ![Search.png](res/Search.png) 5 | 6 | -------- 7 | 8 | 1. [BinarySearch 二分查找法(折半查找法)](BinarySearch/) 9 | 2. [AdditionMultiplicationPrinciple 加法乘法原理](AdditionMultiplicationPrinciple/) 10 | 3. [BruteForce 暴力枚举](BruteForce/) 11 | 4. [Recursion 递归](Recursion/) 12 | 5. [BreadthFirstSearch 广度优先搜索](BreadthFirstSearch/) 13 | 6. [BidirectionalBreadthSearch 双向广度搜索](BidirectionalBreadthSearch/) 14 | 7. [AStarSearch A\*搜索](AStarSearch/) 15 | 8. [DancingLink 舞蹈链](DancingLink/) 16 | 17 | -------- 18 | 19 | #### 平面搜索中的矩阵 20 | 21 | ![KnowledgePoint1.png](res/KnowledgePoint1.png) 22 | 23 | 上图是$$ 3 $$行$$ 5 $$列的矩阵,用$$ 3 \times 5 $$的二位数组表示,$$ matrix[row, col] $$表示第$$ row $$行、第$$ col $$列的元素。例如 24 | 25 | ![KnowledgePoint2.png](res/KnowledgePoint2.png) 26 | 27 | ![KnowledgePoint3.png](res/KnowledgePoint3.png) 28 | 29 | -------- 30 | 31 | #### 公共代码 32 | 33 | [Util.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/Search/Util.h) 34 | 35 | [Util.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/Search/Util.cpp) 36 | -------------------------------------------------------------------------------- /src/Search/BruteForceTest.cpp: -------------------------------------------------------------------------------- 1 | #include "BruteForce.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define TEST_N_MAX 4 7 | #define TEST_M_MAX 16 8 | 9 | int main() { 10 | int s[TEST_N_MAX]; 11 | for (int i = 1; i < TEST_M_MAX; i++) { 12 | vector> result = BruteForce(s, TEST_N_MAX, i); 13 | assert(result.size() == pow((double)i, (double)TEST_N_MAX)); 14 | vector s0; 15 | int sum0; 16 | s0 = result[0]; 17 | sum0 = 0; 18 | for (int k = 0; k < TEST_N_MAX; k++) { 19 | sum0 = sum0 * 10 + s0[k]; 20 | } 21 | /* 第一个排列组合必然是[0, 0, 0, 0] */ 22 | assert(sum0 == 0); 23 | for (int j = 0; j < result.size() - 1; j++) { 24 | vector s1, s2; 25 | int sum1 = 0, sum2 = 0; 26 | s1 = result[j]; 27 | s2 = result[j + 1]; 28 | /* 排列组合的所有情况相当于依次递增的TEST_N_MAX位i进制正整数 */ 29 | for (int k = 0; k < TEST_N_MAX; k++) { 30 | sum1 = sum1 * i + s1[k]; 31 | sum2 = sum2 * i + s2[k]; 32 | } 33 | assert(sum1 == sum2 - 1); 34 | } 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /docs/GameTheory/res/GameTheory.xml: -------------------------------------------------------------------------------- 1 | 5Zddb5swFIZ/DbcTYAjkck3b7WbTpFysu3TNAawaHBmnkP36GWyHr0SNoohWXW6CX38dP699MA7aFM03gXf5D54Ac3w3aRx07/i+F/gr9dcqB61EKNZCJmhiGvXClv4FI7pG3dMEqlFDyTmTdDcWCS9LIHKkYSF4PW6WcjaedYczmAlbgtlc/U0TmWs19qNe/w40y+3M3mqta54xeckE35dmPsdHaffT1QW2Y5mFVjlOeD2Q0IODNoJzqZ+KZgOsZWux6X6PZ2qPcQso5SUdIhPHK2Z7sCF3gcmDhaF6KO6qcFfnVMJ2h0lbUyvrlZbLgqmSpx5xtdNmpLQBNcFdShnbcMZFNxBKYwKEKL2Sgr/AoOY5DoPQVTUmGhASmrNL8o6g1AYEXoAUB9XEdlibNZm9F6xMue6dtFI+MNFq2Oyd7Dhyz089GIRncHpL4tQb6wROt/vdCGcwxhmGC+L0Px3OeLI5/QVpordpdqmrpXPvvk005aV8xAVl7WI2fC8oCDXcT6hNpUnsXjynnWCI05O5YEVieE5vQxtF74g7+O9w+5PUGy6JO/xAuJfJJSiepubgS7gc8OjTXR18dwwULXl1iC/AWSZf29utKhGGq4qSMUVoqHwaPP9pd7raEqpUqnCezMbvCn2dngaS2ZV4gk2Fok4BgfHNUWKRgRy+YeZ4B/jCE/isJoBhSV/HYZxiamb4xakKsD8O/sS9YGKLDt/0Gl6KpwNN3hrHtGYH0mueDdRZfFz2Za6vb+e6cdr6PnTdG7ruXe95cMLz6D09Px49mwLdaz2fXsymZ/pqz1Wx/57TzfuPZvTwDw== -------------------------------------------------------------------------------- /src/CombinatorialMathematics/FullPermutationTest.cpp: -------------------------------------------------------------------------------- 1 | #include "FullPermutation.hpp" 2 | #include 3 | #include 4 | // #include 5 | 6 | #define TEST_MAX 8 7 | 8 | int FullPermutationCount(int n) { 9 | int count = 1; 10 | for (int i = 1; i <= n; i++) { 11 | count *= i; 12 | } 13 | return count; 14 | } 15 | 16 | int main() { 17 | int s[MAX]; 18 | for (int i = 0; i < MAX; i++) { 19 | s[i] = i; 20 | } 21 | 22 | for (int n = 1; n < TEST_MAX; n++) { 23 | unordered_set ss; 24 | for (int i = 0; i < n; i++) { 25 | ss.insert(i); 26 | } 27 | 28 | vector> result = FullPermutation(s, n); 29 | // cout << "n: " << n << ", result.size: " << result.size() << ", count: 30 | // " << FullPermutationCount(n) << endl; 31 | assert(result.size() == FullPermutationCount(n)); 32 | for (int i = 0; i < result.size(); i++) { 33 | unordered_set rs; 34 | for (int j = 0; j < result[i].size(); j++) 35 | rs.insert(result[i][j]); 36 | assert(rs == ss); 37 | } 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Way to Algorithm - 算法之路

2 | 3 |

4 | keyboard 5 |

6 | 7 |

Algorithm Tutorial and Source Code - 算法教程与源码

8 | 9 | -------- 10 | 11 | #### Introduction - 简介 12 | 13 | 本书围绕大学生计算机算法,借鉴了一些经典书籍和资料。通过公式、插图来描述算法,配合源码、测试、OJ题目,清晰易懂。 14 | 由于自己经验有限,难免存在疏漏和错误,还请指正。读者的支持是本项目更新的动力,欢迎大家给星、分享以及参与编写。 15 | 16 | #### Read Address - 阅读地址 17 | 18 | * github pages: [Way-to-Algorithm](https://linrongbin16.github.io/Way-to-Algorithm/) 19 | * gitbook: [Way-to-Algorithm](https://linrongbin16.gitbook.io/gitbook-way-to-algorithm/) 20 | * legacy-gitbook: [Way-to-Algorithm](https://linrongbin16.gitbooks.io/way-to-algorithm/content/) 21 | 22 | #### Reference - 参考 23 | 24 | * [Introduction to Algorithm](https://www.google.com/search?q=Introduction+to+Algorithms+3rd+Edition+pdf) 25 | * [Wikipedia](https://www.wikipedia.org/) 26 | * [背包问题九讲](https://www.kancloud.cn/kancloud/pack/70124) 27 | * [编程之法:面试和算法心得](https://github.com/julycoding/The-Art-Of-Programming-By-July) 28 | -------------------------------------------------------------------------------- /docs/GraphTheory/MinSpanningTree/OptimalRatioSpanningTree/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Optimal Ratio Spanning Tree - 最优比率生成树 4 | 5 | -------- 6 | 7 | #### 问题 8 | 9 | 无向图$$ UG $$的每个边$$ e_i $$都有两个值,距离$$ dist_i $$和开销$$ cost_i $$。设该图生成树的点集为$$ V_{tree} $$,边集为$$ E_{tree} $$,生成树的比率为所有边的开销和与距离和之比: 10 | 11 | $$ 12 | ratio = \frac{ \sum_{i=0}{n-1} cost_{i} }{ \sum_{i=0}{n-1} dist_{i} } 13 | $$ 14 | 15 | 求无向图$$ UG $$的最优/最小比率生成树。 16 | 17 | #### 解法 18 | 19 | -------- 20 | 21 | #### 源码 22 | 23 | [OptimalRatioSpanningTree.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/GraphTheory/MinimumSpanningTree/OptimalRatioSpanningTree.h) 24 | 25 | [OptimalRatioSpanningTree.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/GraphTheory/MinimumSpanningTree/OptimalRatioSpanningTree.cpp) 26 | 27 | #### 测试 28 | 29 | [OptimalRatioSpanningTreeTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/GraphTheory/MinimumSpanningTree/OptimalRatioSpanningTreeTest.cpp) 30 | -------------------------------------------------------------------------------- /src/AnalyticGeometry/Polygon/ConvexPolygonGravityCenter.cpp: -------------------------------------------------------------------------------- 1 | #include "ConvexPolygonGravityCenter.h" 2 | #include "../Util.h" 3 | #include "ConvexPolygonArea.h" 4 | #include 5 | 6 | Node TriangleGravityCenter(const Node &a, const Node &b, const Node &c) { 7 | double x = (a.x + b.x + c.x) / (double)3.0f; 8 | double y = (a.y + b.y + c.y) / (double)3.0f; 9 | return Node(x, y); 10 | } 11 | 12 | Node ConvexPolygonGravityCenter(Node *a, int n) { 13 | Node p(0, 0); 14 | 15 | //划分的三角形重心 16 | Node center[MAX]; 17 | //划分的三角形面积 18 | double area[MAX]; 19 | 20 | for (int i = 0; i < n; i++) { 21 | if (i != n - 1) { 22 | center[i] = TriangleGravityCenter(p, a[i], a[i + 1]); 23 | area[i] = TriangleArea(p, a[i], a[i + 1]); 24 | } 25 | } 26 | 27 | double sum_area = 0.0f; 28 | Node sum_center(0.0f, 0.0f); 29 | for (int i = 0; i < n; i++) 30 | sum_area += area[i]; 31 | for (int i = 0; i < n; i++) { 32 | sum_center.x += center[i].x * area[i]; 33 | sum_center.y += center[i].y * area[i]; 34 | } 35 | return Node(sum_center.x / sum_area, sum_center.y / sum_area); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /src/GraphTheory/BinaryMatch/8_minimum_joint_path_coverage.cpp: -------------------------------------------------------------------------------- 1 | //最小可相交路径覆盖 2 | //minimum_joint_path_covering.cpp 3 | 4 | //求简单有向图的最小可相交路径覆盖 5 | 6 | //最小可相交路径覆盖问题可以转化为最小不相交路径覆盖 7 | //原始的最小路径覆盖是不可相交的,只需要将图中所有相互能到达的节点都连接起来 8 | //即向任意两存在路径的节点直接添加一条有向边 9 | //用Floyd算法求出任意节点间距离 10 | //对于任意节点i和j,若i到j的距离不是INF,即i到j存在一条路经 11 | //则在图G中添加一条从i指向j的有向边,构造出新的简单有向图G' 12 | //求G'的最小不相交路径覆盖即为原图G的最小可相交路径覆盖 13 | // 14 | //本文引用了“PKU 2594 Treasure Exploration 可相交的最小路径覆盖:传递闭包Floyd+最小路径覆盖”,作者“爱尔兰KEN” 15 | 16 | #include "general_head.h" 17 | #include "graph.h" 18 | //floyd.cpp 19 | extern void floyd(graph_matrix& g); 20 | //minimum_disjoint_path_covering.cpp 21 | extern int minimum_disjoint_path_covering(graph_matrix g); 22 | 23 | int minimum_joint_path_covering(graph_matrix g) 24 | {//简单有向图G有g_cnt个节点,下标从0到g_cnt-1 25 | //若从节点i指向节点j存在有向边则g_m[i][j]为1 26 | //同一节点g_m[i][i]为0,不相连节点g_m[i][j]为INF 27 | //返回最小可相交路径覆盖数 28 | graph_matrix f(g); 29 | floyd(f); 30 | for(int i = 0; i < f.g_cnt; ++ i) 31 | for(int j = 0; j < f.g_cnt; ++ j) 32 | if(i != j and f.g_m[i][j] != INF) 33 | g.g_m[i][j] = 1; 34 | return(minimum_disjoint_path_covering(g)); 35 | } 36 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/Catalan.cpp: -------------------------------------------------------------------------------- 1 | #include "Catalan.h" 2 | #include 3 | #include 4 | #include "../Calculation/LargeNumber.hpp" 5 | 6 | Number* Catalan(const Number& a) { 7 | int i, j, len, carry, temp; 8 | a[1][0] = b[1] = 1; 9 | len = 1; 10 | for (i = 2; i <= 100; i++) { 11 | //乘法 12 | for (j = 0; j < len; j++) 13 | a[i][j] = a[i - 1][j] * (4 * (i - 1) + 2); 14 | carry = 0; 15 | //处理相乘结果 16 | for (j = 0; j < len; j++) { 17 | temp = a[i][j] + carry; 18 | a[i][j] = temp % 10; 19 | carry = temp / 10; 20 | } 21 | //进位处理 22 | while (carry) { 23 | a[i][len++] = carry % 10; 24 | carry /= 10; 25 | } 26 | carry = 0; 27 | //除法 28 | for (j = len - 1; j >= 0; j--) { 29 | temp = carry * 10 + a[i][j]; 30 | a[i][j] = temp / (i + 1); 31 | carry = temp % (i + 1); 32 | } 33 | //高位零处理 34 | while (!a[i][len - 1]) 35 | len--; 36 | b[i] = len; 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/LongestPalindromicSubsequenceTest.cpp: -------------------------------------------------------------------------------- 1 | #include "LongestPalindromicSubsequence.h" 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define TEST_MAX 1024 8 | 9 | struct Test { 10 | int a[TEST_MAX]; 11 | int n; 12 | pair result; 13 | } test_cases[] = { 14 | {{0, 1, 2, 3, 4, 5, 4, 3, 2, 9, 10}, 10, {2, 8}}, // 0 15 | {{0, 1, 3, 5, 15, 9, 11, 9, 15, 9}, 9, {4, 8}}, // 1 16 | {{0, 1, 2, 3, 3, 2, 10, 9, 9, 10, 2}, 10, {5, 10}}, // 2 17 | {{0, 42, 42, 5, 14, 14, 5, -42, 14, 31, 18 | 0, 12, 142, -5, 61, 42, 45, 42, 61, 41}, 19 | 19, 20 | {14, 18}}, 21 | {{0, 12, 142, -5, 14, 15, 45, 42, 61, 41, 22 | 0, 42, 42, -5, 14, -5, 45, -42, 1, 31}, 23 | 19, 24 | {13, 15}}, 25 | {{0, 1, 2, 1, 2, 3}, 5, {1, 3}}, 26 | }; 27 | 28 | int main(void) { 29 | for (int i = 0; i < sizeof(test_cases) / sizeof(Test); i++) { 30 | Test &t = test_cases[i]; 31 | pair r = LongestPalindromicSubsequence(t.a, t.n); 32 | assert(r == t.result); 33 | } 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2014- linrongbin16@gmail.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/PatternMatch/KnuthMorrisPratt.cpp: -------------------------------------------------------------------------------- 1 | #include "KnuthMorrisPratt.h" 2 | #include 3 | #include 4 | 5 | static void BuildPartialMatchTable(const std::string &pattern, int *pmt) { 6 | pmt[0] = -1; 7 | for (int i = 1; i < pattern.length(); i++) { 8 | if (i > 0 && pattern[pmt[i - 1] + 1] == pattern[i]) { 9 | pmt[i] = pmt[i - 1] + 1; 10 | } else { 11 | pmt[i] = -1; 12 | } 13 | } 14 | } 15 | 16 | std::vector KnuthMorrisPratt(const std::string &text, 17 | const std::string &pattern) { 18 | std::vector match; 19 | int pmt[MAX]; 20 | BuildPartialMatchTable(pattern, pmt); 21 | 22 | int i = 0; 23 | int j = 0; 24 | while (i < text.length()) { 25 | if (j == 0 && text[i] != pattern[j]) { 26 | i++; 27 | } else if (j < pattern.length() && text[i] == pattern[j]) { 28 | i++; 29 | j++; 30 | } else { 31 | // j >= pattern.length() 时 32 | // text[i] != pattern[j] 时 33 | // 沿着后缀指针跳转 34 | j = pmt[j - 1] + 1; 35 | } 36 | if (j == pattern.length()) { 37 | match.push_back(i - j); 38 | } 39 | } 40 | 41 | return match; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /docs/LinearAlgebra/res/Matrix.xml: -------------------------------------------------------------------------------- 1 | 7ZpNk5owGMc/DdcOJIB4rOxue2kvHnrOQoTMBsLEWLSfvgkEFAizdrQwjjijkifv/1+e5BG0YJgdv3FUpD9YjKkF7PhowRcLgMDx5KcynGqDG6xqQ8JJXJucs2FL/mBttLX1QGK87xQUjFFBiq4xYnmOI9GxIc5Z2S22Y7Tba4ESPDBsI0SH1l8kFqmeFlid7d8xSdKmZ8df1znvKPpIODvkuj8LwF31qrMz1LSlJ7pPUczKCxN8tWDIGRP1VXYMMVXSNrLV9d5Gcttxc5yLayq4GtRvRA+4GXI1MHFqxKimg1UF24KbMiUCbwsUqdxS0pe2VGRUphx5qZvDXODj6JicdqZyAWGWYcFPskhTYa3F0YsHBDpdnlFAX9vSCwytEWn8Sdv2WQJ5oVUwKwKGgngbOeDQsbyXgTafqIH2Rb08d+SoFNzsBWcfOGSU8aoBaFcvmbNjuXhDGaFq1iE7cIK57OsnLnWm9hInUO1SkuQyEUlJZTFZgFB60apecw2OgfYGQjfhMNG4Bww4BgM8KQzgzQfDNcEAi2e0MKA7HQx/zDPgk8KAznye0XQ9dI1ln5reNRxnjMbiGzPQMB4bcDk2WhquPSENQ7xf01h2qhloGE9xuOxUs9AAxnNDxVTZk9JwZ/zpZ45wwUJjllMcGiNcuNCYZaeCgYlG/tgx1V3vGXpT3qRaj9F44JjqJhr9mGpKGq5xp8ofO6a6iUY/ppqUBhij8cDnxk00+ufGpDSGMdUXC/hUdrF5l/L4iboym3qkpACiC6fLImc57gmpTQOxlZwkQvSrzshIHKtujI+aug+j/onyPTY2vwfPGcJzDfDAPeCtTPBs/V7wVCex18HTPhC8wLP6X3iGMZnBkeQ3ypRQ+fu+qNL2daWurbisA8U9+NxN77QOZPL8tL7Ku/hHBHz9Cw== -------------------------------------------------------------------------------- /leetcode/leetcode-120.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAX 1001 5 | 6 | class Solution { 7 | bool f[MAX][MAX]; 8 | 9 | public: 10 | string longestPalindrome(string s) { 11 | for (int i = 0; i < s.length(); i++) { 12 | for (int j = 0; j < s.length(); j++) { 13 | f[i][j] = (i == j); 14 | } 15 | } 16 | 17 | for (int k = 0; k < s.length(); k++) { 18 | for (int i = k - 1, j = k + 1; i >= 0 && j < s.length(); i--, j++) { 19 | f[i][j] = f[i + 1][j - 1] && s[i] == s[j]; 20 | } 21 | for (int i = k, j = k + 1; i >= 0 && j < s.length(); i--, j++) { 22 | f[i][j] = 23 | (i + 1 == j) ? (s[i] == s[j]) : (f[i + 1][j - 1] && s[i] == s[j]); 24 | } 25 | } 26 | 27 | int maxlen = 0; 28 | int beg = 0, end = 0; 29 | for (int i = 0; i < s.length(); i++) { 30 | for (int j = i; j < s.length(); j++) { 31 | if (f[i][j]) { 32 | if (maxlen < j - i + 1) { 33 | maxlen = j - i + 1; 34 | beg = i; 35 | end = j; 36 | } 37 | } 38 | } 39 | } 40 | 41 | return s.substr(beg, end - beg + 1); 42 | } 43 | }; 44 | 45 | -------------------------------------------------------------------------------- /leetcode/leetcode-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAX 1001 5 | 6 | class Solution { 7 | bool f[MAX][MAX]; 8 | 9 | public: 10 | string longestPalindrome(string s) { 11 | for (int i = 0; i < s.length(); i++) { 12 | for (int j = 0; j < s.length(); j++) { 13 | f[i][j] = (i == j); 14 | } 15 | } 16 | 17 | for (int k = 0; k < s.length(); k++) { 18 | for (int i = k - 1, j = k + 1; i >= 0 && j < s.length(); i--, j++) { 19 | f[i][j] = f[i + 1][j - 1] && s[i] == s[j]; 20 | } 21 | for (int i = k, j = k + 1; i >= 0 && j < s.length(); i--, j++) { 22 | f[i][j] = 23 | (i + 1 == j) ? (s[i] == s[j]) : (f[i + 1][j - 1] && s[i] == s[j]); 24 | } 25 | } 26 | 27 | int maxlen = 0; 28 | int beg = 0, end = 0; 29 | for (int i = 0; i < s.length(); i++) { 30 | for (int j = i; j < s.length(); j++) { 31 | if (f[i][j]) { 32 | if (maxlen < j - i + 1) { 33 | maxlen = j - i + 1; 34 | beg = i; 35 | end = j; 36 | } 37 | } 38 | } 39 | } 40 | 41 | return s.substr(beg, end - beg + 1); 42 | } 43 | }; 44 | 45 | -------------------------------------------------------------------------------- /docs/PatternMatch/res/PatternMatch.xml: -------------------------------------------------------------------------------- 1 | 7VdLj9sgEP4tPfga2SbPY5Juuqq01Uo5bPdI7IlNF0OKcR799R0MfifSHrqRKq0PNvMBM8x8H4R4ZJ2dvyl6SJ9kDNwL/fjska9eGE5mPr4NcLFAuCAWSBSLLRQ0wJb9AQe6eUnBYsg7A7WUXLNDF4ykEBDpDkaVkqfusL3k3agHmsAA2EaUD9EXFuvUovNw1uCPwJK0ihxMF7ZnR6O3RMlCuHheSPblY7szWvlyieYpjeWpBZEHj6yVlNq2svMauCltVTY7b3Ojt163AqHfM2Hu1nGkvHC5PzK0v8tUfMHvUgG+C1NDBaYphVk15keR2g22ny4uE32pqqfhjMFXqc44AgE2c63kG6wllwoRIQWOXO0Z5z2IcpYINDnsjYcjKM2QlaWDMxbHJsjqlDIN2wONTMQTShCxsupg8vKNcyn0hmaMGwWuZaEYKFzlDzi5Tie6YI62qwFGg/PNQgY1Pah6kBlohan71QTiKlkpvqrsqdEPmTosbWmnUgJ1kk1q1w1t2HDM3WAxGLC4U1KnZcos90yMKdcu83Lf2LIj/LswWsMSEN+flUJtoGlivpHMmEgqD7gW68R2mpWj2DFPo44Dp6Lcb+pTFFdEQe4qinAoCpaYQ5nhoalG2HqhQlD8/pLM0Fbk5Zb+JG5A3HhxT+LIgDjcbTQzZRG7/GB33UdC1Y7/35Vg7HV91JHNhuDzQQoh91TI+IpCemSBiJfmJoRWxGmes6jkiyo9hFuM/pP6Yg3V5adhbDTGO5MDXs20kV/bz6AYpg7KUQtnpjuT0H5t9fXHxzRPS10ENaUQD+51PUKxRphUBN2thmVJQLd/TIfEt4idXOG1whRwqtmxu4xrZLsIz3j26pauJj1dzXp6sct3s9o3u56j0O856gvP5jxwVGqvTvuaHNFsLqh2ePMngDz8BQ== -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/Dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include "Dijkstra.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static int MinDistance(int *distance, int *visit, int n) { 7 | int min_dist = INF, min_index = 0; 8 | for (int i = 0; i < n; i++) 9 | if (min_dist > distance[i] && !visit[i]) { 10 | min_dist = distance[i]; 11 | min_index = i; 12 | } 13 | return min_index; 14 | } 15 | 16 | std::vector Dijkstra(int g[MAX][MAX], int n, int beg) { 17 | int visit[MAX]; 18 | int distance[MAX]; 19 | std::memset(visit, 0, MAX * sizeof(int)); 20 | std::memset(distance, 0, MAX * sizeof(int)); 21 | 22 | for (int i = 0; i < n; i++) { 23 | distance[i] = (i == beg) ? 0 : INF; 24 | } 25 | 26 | for (int i = 0; i < n; i++) { 27 | int k = MinDistance(distance, visit, n); 28 | visit[k] = 1; 29 | for (int j = 0; j < n; j++) { 30 | if (distance[k] + g[k][j] < distance[j]) { 31 | distance[j] = distance[k] + g[k][j]; 32 | } 33 | } 34 | } 35 | 36 | std::vector result; 37 | std::transform(distance, distance + n, std::back_inserter(result), [](const int &v) { return v; }); 38 | return result; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /src/DynamicProgramming/LinearDP/LongestPalindromicSubsequence.cpp: -------------------------------------------------------------------------------- 1 | #include "LongestPalindromicSubsequence.h" 2 | #include "../Util.h" 3 | #include 4 | #include 5 | 6 | std::pair LongestPalindromicSubsequence(const int *s, int n) { 7 | int **f = Array2DNew(n + 1, n + 1); 8 | 9 | for (int i = 1; i <= n; i++) { 10 | for (int j = 1; j <= n; j++) { 11 | f[i][j] = (i == j); 12 | } 13 | } 14 | 15 | for (int k = 1; k <= n; k++) { 16 | for (int i = k - 1, j = k + 1; i >= 1 && j <= n; i--, j++) { 17 | f[i][j] = f[i + 1][j - 1] && s[i] == s[j]; 18 | } 19 | for (int i = k, j = k + 1; i >= 1 && j <= n; i--, j++) { 20 | f[i][j] = 21 | (i + 1 == j) ? (s[i] == s[j]) : (f[i + 1][j - 1] && s[i] == s[j]); 22 | } 23 | } 24 | 25 | int maxlen = 0; 26 | int beg = 0, end = 0; 27 | for (int i = 1; i <= n; i++) { 28 | for (int j = i; j <= n; j++) { 29 | if (f[i][j]) { 30 | if (maxlen < j - i + 1) { 31 | maxlen = j - i + 1; 32 | beg = i; 33 | end = j; 34 | } 35 | } 36 | } 37 | } 38 | 39 | Array2DFree(f, n + 1); 40 | return std::make_pair(beg, end); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/DynamicProgramming/RegionalDP/MinimumMergeCost.cpp: -------------------------------------------------------------------------------- 1 | #include "MinimumMergeCost.h" 2 | #include "../Util.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // 防止int溢出 10 | 11 | static int Add(int a = 0, int b = 0, int c = 0) { 12 | if (a == INF || b == INF || c == INF) 13 | return INF; 14 | return a + b + c; 15 | } 16 | 17 | int MinimumMergeCost(int *s, int n) { 18 | int **f = Array2DNew(n + 1, n + 1); 19 | int **sum = Array2DNew(n + 1, n + 1); 20 | 21 | for (int i = 1; i <= n; i++) 22 | for (int j = 1; j <= n; j++) { 23 | f[i][j] = (i == j) ? 0 : INF; 24 | sum[i][j] = 0; 25 | } 26 | 27 | for (int i = 1; i <= n; i++) 28 | for (int j = i; j <= n; j++) 29 | for (int k = i; k <= j; k++) 30 | sum[i][j] += s[k]; 31 | 32 | for (int i = 1; i <= n; i++) 33 | for (int j = i + 1; j <= n; j++) 34 | for (int k = i; k < j; k++) { 35 | f[i][j] = std::min( 36 | f[i][j], Add(Add(f[i][k], f[k + 1][j]), sum[i][k], sum[k + 1][j])); 37 | } 38 | 39 | int result = f[1][n]; 40 | Array2DFree(f, n + 1); 41 | Array2DFree(sum, n + 1); 42 | return result; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /src/GraphTheory/ShortestPath/DifferenceConstraints.cpp: -------------------------------------------------------------------------------- 1 | #include "DifferenceConstraints.h" 2 | #include "BellmanFord.h" 3 | #include 4 | 5 | static void CreateDirectedGraph(int a[MAX][MAX], int n, int m, int *b, 6 | int g[MAX][MAX], int &gn) { 7 | std::memset(g, 0, MAX * MAX * sizeof(int)); 8 | 9 | for (int i = 0; i <= n; i++) { 10 | for (int j = 0; j <= n; j++) { 11 | g[i][j] = (i == j) ? 0 : INF; 12 | } // for 13 | } // for 14 | 15 | for (int i = 0; i < n; i++) { 16 | g[0][i + 1] = 0; 17 | } 18 | 19 | for (int i = 0; i < m; i++) { 20 | int u, v; 21 | for (int j = 0; j < n; ++j) { 22 | if (a[i][j] == 1) { 23 | u = j; 24 | } else if (a[i][j] == -1) { 25 | v = j; 26 | } 27 | } // for 28 | g[u + 1][v + 1] = b[i]; 29 | } // for 30 | 31 | gn = n + 1; 32 | } 33 | 34 | std::pair> DifferenceConstraints(int a[MAX][MAX], int n, 35 | int m, int *b) { 36 | int g[MAX][MAX]; 37 | int gn; 38 | CreateDirectedGraph(a, n, m, b, g, gn); 39 | std::pair> result = BellmanFord(g, gn, 0); 40 | return result; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/GraphTheory/StronglyConnectedComponents/Kosaraju.cpp: -------------------------------------------------------------------------------- 1 | #include "Kosaraju.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static void Dfs(int g[MAX][MAX], int n, int beg, int *visit, 7 | std::vector &q) { 8 | if (visit[beg]) { 9 | return; 10 | } 11 | 12 | for (int i = 0; i < n; i++) { 13 | if (g[beg][i] > 0 && !visit[i]) { 14 | visit[i] = 1; 15 | q.push_back(i); 16 | Dfs(g, n, i, visit, q); 17 | } 18 | } 19 | } 20 | 21 | std::vector> Kosaraju(int g[MAX][MAX], int n) { 22 | std::vector> result; 23 | std::vector q; 24 | int visit[MAX]; 25 | std::memset(visit, 0, MAX * sizeof(int)); 26 | 27 | for (int i = 0; i < n; i++) { 28 | Dfs(g, n, i, visit, q); 29 | } 30 | 31 | int father[MAX]; 32 | for (int i = 0; i < n; i++) { 33 | father[i] = i; 34 | } 35 | std::reverse(q.begin(), q.end()); 36 | 37 | for (int i = 0; i < n; i++) { 38 | int v = q[i]; 39 | std::vector strong_component; 40 | std::memset(visit, 0, MAX * sizeof(int)); 41 | Dfs(g, n, v, visit, strong_component); 42 | result.push_back(strong_component); 43 | } 44 | 45 | return result; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /src/GraphTheory/Util.cpp: -------------------------------------------------------------------------------- 1 | #include "Util.h" 2 | 3 | Vertex::Vertex() {} 4 | 5 | Vertex::Vertex(int index, int value) : index(index), value(value) {} 6 | 7 | Vertex::Vertex(const Vertex &other) : index(other.index), value(other.value) {} 8 | 9 | Vertex &Vertex::operator=(const Vertex &other) { 10 | if (&other == this) { 11 | return *this; 12 | } 13 | index = other.index; 14 | value = other.value; 15 | return *this; 16 | } 17 | 18 | bool operator!=(const Vertex &a, const Vertex &b) { return !(a == b); } 19 | 20 | bool operator==(const Vertex &a, const Vertex &b) { 21 | return a.index == b.index && a.value == b.value; 22 | } 23 | 24 | Edge::Edge() {} 25 | 26 | Edge::Edge(int u, int v) : u(u), v(v) {} 27 | 28 | Edge::Edge(int u, int v, int value) : u(u), v(v), value(value) {} 29 | 30 | Edge::Edge(const Edge &e) : u(e.u), v(e.v), value(e.value) {} 31 | 32 | Edge &Edge::operator=(const Edge &e) { 33 | if (&e == this) { 34 | return *this; 35 | } 36 | u = e.u; 37 | v = e.v; 38 | value = e.value; 39 | return *this; 40 | } 41 | 42 | bool operator!=(const Edge &a, const Edge &b) { return !(a == b); } 43 | 44 | bool operator==(const Edge &a, const Edge &b) { 45 | return a.u == b.u && a.v == b.v; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /src/GraphTheory/MinSpanningTree/Prim.cpp: -------------------------------------------------------------------------------- 1 | #include "Prim.h" 2 | #include 3 | #include 4 | 5 | static Edge MinAdjacentEdge(int g[MAX][MAX], int n, 6 | const std::unordered_set &vtree) { 7 | int min_value = INF; 8 | int a_vertex = -1; 9 | int b_vertex = -1; 10 | for (auto i = vtree.begin(); i != vtree.end(); i++) { 11 | int v = *i; 12 | for (int j = 0; j < n; j++) { 13 | if (g[v][j] > 0 && vtree.find(j) == vtree.end() && g[v][j] < min_value) { 14 | min_value = g[v][j]; 15 | a_vertex = v; 16 | b_vertex = j; 17 | } 18 | } 19 | } 20 | assert(min_value != INF); 21 | assert(a_vertex != -1); 22 | assert(b_vertex != -1); 23 | return Edge(a_vertex, b_vertex, min_value); 24 | } 25 | 26 | std::vector Prim(int g[MAX][MAX], int n) { 27 | std::unordered_set vtree = {0}; 28 | std::vector etree; 29 | while (vtree.size() != n) { 30 | Edge e = MinAdjacentEdge(g, n, vtree); 31 | if (vtree.find(e.u) == vtree.end()) { 32 | vtree.insert(e.u); 33 | } 34 | if (vtree.find(e.v) == vtree.end()) { 35 | vtree.insert(e.v); 36 | } 37 | etree.push_back(e); 38 | } 39 | return etree; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /src/CombinatorialMathematics/CombinationTest.cpp: -------------------------------------------------------------------------------- 1 | #include "Combination.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define TEST_MAX 10 7 | 8 | int CombinationCount(int n, int m) { 9 | int count = 1; 10 | for (int i = 1; i <= n; i++) { 11 | count *= i; 12 | } 13 | for (int i = 1; i <= m; i++) { 14 | count /= i; 15 | } 16 | for (int i = 1; i <= n - m; i++) { 17 | count /= i; 18 | } 19 | 20 | return count; 21 | } 22 | 23 | void AssertDifference(const vector> &vs) { 24 | for (int i = 0; i < vs.size(); i++) 25 | for (int j = i + 1; j < vs.size(); j++) { 26 | assert(vs[i] != vs[j]); 27 | } 28 | } 29 | 30 | int main() { 31 | int s[MAX]; 32 | for (int i = 0; i < MAX; i++) { 33 | s[i] = i; 34 | } 35 | 36 | for (int n = 1; n < TEST_MAX; n++) { 37 | for (int m = 1; m <= n; m++) { 38 | vector> result = Combination(s, n, m); 39 | int count = CombinationCount(n, m); 40 | assert(result.size() == count); 41 | AssertDifference(result); 42 | for (int i = 0; i < result.size(); i++) { 43 | assert(result[i].size() == m); 44 | } 45 | } 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/DataStructure/AvlTreeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "AvlTree.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | #define TEST_MAX 8192 10 | 11 | int main() { 12 | int start = time(NULL); 13 | for (int i = 1; i < TEST_MAX; i++) { 14 | vector val; 15 | for (int j = 0; j < i; j++) { 16 | val.push_back(j); 17 | } 18 | 19 | random_shuffle(val.begin(), val.end()); 20 | AvlTree *t = AvlTreeNew(); 21 | for (int j = 0; j < i; j++) { 22 | assert(is_nil(AvlTreeFind(t, val[j]))); 23 | AvlTreeInsert(t, val[j]); 24 | AvlNode *e = AvlTreeFind(t, val[j]); 25 | assert(not_nil(e)); 26 | assert(e->value == val[j]); 27 | } // for 28 | random_shuffle(val.begin(), val.end()); 29 | for (int j = 0; j < i; j++) { 30 | AvlNode *e = AvlTreeFind(t, val[j]); 31 | assert(not_nil(e)); 32 | assert(e->value == val[j]); 33 | AvlTreeErase(t, val[j]); 34 | assert(is_nil(AvlTreeFind(t, val[j]))); 35 | } // for 36 | AvlTreeFree(t); 37 | } 38 | int end = time(NULL); 39 | cout << "start: " << start << " end: " << end << " use: " << (end - start) << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /docs/DynamicProgramming/RegionalDP/res/TrianglePath.xml: -------------------------------------------------------------------------------- 1 | 7ZpNk6MgEIZ/TY4zJeAHHjeZmd3L7iWHPbNKlBqUFCGbZH/9YsQkhKHKqUrpHMgl8raI8rTY3bpAq+b4XZJt/VOUlC9gVB4X6GUBIcAg0n+dcjJKlmS9UklWGu0qrNk/akTTsdqzku6sHZUQXLGtLRaibWmhLI1IKQ72bhvB7VG3pKKOsC4Id9XfrFR1r2KYXfUflFX1MDJI897yhxTvlRT71oy3gGhz/vXmhgzHMhe6q0kpDjcSel2glRRC9VvNcUV5N7nDtPX93jzWy3lL2qoxHWAewb7PX8L39tXv1GmYD91JT71uLA81U3S9JUVnOWj8WqtVw3UL6E2y2/Y8NuxI9RjLnZLina4EF/J8KBSdf9pSSVIyfZo3NjNVaLkRrXojDeOdA63EXjIq9Rn9ogdjNC4DsGl/NADhrGq1VuhBdHe0NBdKpaJH73yBCwXt3lQ0VMmT3mXoMHio8WyITftwdZPUSPWNhwwaMY5ZXY58haM3DB8vK+SwgoGVjxW2UaF4UlSxgwoFVN7bKp6VVeKwigMrH6vERhVHk6JKHVRJQDX2aTUxq8xhlQZWXlbZrKywwyoLrHys4B2qdFJUuYMKB1Qjg8BpUQ3L7w2qPKAaGwROzAo4rAZ8AdZHS+CssNyihZsI64tTNhSbQSvaDuSGcX4nORPWTRUrCP9mDA0rS+7zgnP5p2P+Ej2K4AOAPYG7pxaCLrH4A2LwMcTcfDiULrywchvVEwLPAOdpkoAMxgBhlE55s4X0+BPoUhsdxM8YRCiOMcwSnII0npKcmy2HDMwff8CvhM5NnkPo6EWHbHL4ntyU4NxMOrxPGfugmxWcm1eHir1/sUy+DjnoptmhKOySG17Gx3OicrPsULwaG0vOS85NuUFYH0cHk/Oyc78bCMGkHx3+QuiQm8K54WSodNmVrrvY5AmCZ5Tl6MLQBfigwpduXr/dOttuvpBDr/8B --------------------------------------------------------------------------------