C + + algorithm--Summary of common algorithms

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_61bebe480100v7c7.html

The basic C + + algorithm is divided into three categories: sorting algorithm, tree algorithm, graph algorithm

There are three kinds of algorithm ideas: recursion, divide and conquer, dynamic programming and greedy algorithm. This article will briefly introduce the above three types of algorithms, introduction of the introduction of algorithm ideas. First, sorting algorithm 1, the basic O (n^2) sorting algorithm: (The time complexity analysis of the basic sorting algorithm mainly consider the comparison times, the number of data exchange) bubble sort: for arrays, local ordering, the need to exchange data. O (1) Additional space selection sort: generally for arrays, local sorting, need to exchange data. O (1) Extra space Insert sort: can be a local sort for an array, you need to move large pieces of data at this time, but the number of comparisons is O (N*logn). If it is for a linked list, the number of comparisons is O (n^2), but there is no need to exchange data. Note: The general sort is the local sort for the array, the array can be accessed randomly compared with the linked list, the space is more efficient (the list needs to hold the pointer), and the linked list generally has better performance for insert and delete operations. 2. O (N*LOGN) algorithm Quick sort: for the local ordering of the array, the time complexity of the average O (N*LOGN), the worst O (n^2). Spatial complexity O (1) Merge sort: can be for arrays or linked lists. The time complexity for an array is O (N*logn), and the spatial complexity is O (N) 3. Heap Sort 1991 Computer Pioneer Award winner, professor of Computer science Department, Stanford University, Robert Floyd Robert W.. Floyd) and Williams (J. Williams co-invented the well-known heap sorting algorithm (heap sort) heap in 1964 as a very efficient data structure that can be used to represent heaps in arrays. Heap ordering is a local sort of array, with TIME complexity O (N*LOGN), and Space complexity O (1) for descending permutations with the smallest heap, the heap is first established and then the first element (the heap header) is exchanged with the latter element of the group. The last element of the permutation is found, then the first N-1 element is the smallest heap, and so on, which completes the sort.  Note: Heap build, heap build time complexity is O (N*LOGN), there can be many ways to build heap 4. The value range of radix sort data is not large, using bitmap 5. Sort merge Sort, multi-merge sort (use heap to implement multi-merge sort) when using heap for multi-routing, each element in the heap is appended with the last field, and the element from that heap can also be used for the TOPK algorithm, TOPK maximum can use the minimum heap of k elements to maintain K maximum elements, Scan the original array once. Tree algorithm tree is generally implemented with a linked list, usually using tree data structure to achieve fast data insertion, deletion, search. 1. Balance binary search Tree rb-tree, AVL, treap, Stretch tree (no additional information required) 2. B-trees are used to establish the index of a file system or database. The B-tree is designed to reduce the number of IO accesses. B-Tree is also a balance tree 3. Two-item tree, two-item heap, faipot-odd heap three, graph algorithm 1. The representation of the graph, the adjacency table representation of the non-direction graph, and the matrix representation 2. Breadth-first search, depth-first search breadth-first search (BFS): Find from a single source point to all points Shortest Path, applicable to the direction graph, the non-direction diagram. The algorithm has an O (v+e), note that the algorithm corresponding to the edge of the graph has no weight. The algorithm uses the queue to achieve breadth first. The algorithm uses three auxiliary variables, a color, a paraent, and an distance array. After the search, the parent constitutes a breadth-first tree   depth-first search (DFS): You can start at random from a node, traversing all the nodes of the tree, so that it is used for the forward graph, the non-direction graph. Search constitutes a forest. The algorithm is implemented by recursion, and then it is possible to search only some nodes of the whole connected tree after recursion, so we need to select a new node from the new DFS, the whole search result constitutes the search forest. The algorithm uses three helper variables: color,v,f. V indicates the time that the node was searched for, and F indicates the time at which the node's neighboring nodes were scanned for completion.   Note Dfs can complete topological sorting. The descending of the F-occurrence time is the topological order, which can be obtained in the process of depth-first searching. DFS algorithms can also be used to discover strongly connected branches.  3. Minimum spanning tree   for undirected connected graphs, the common algorithm has the Kruskal algorithm and the prim algorithm  kruskal algorithm to arrange all the edges in a non-descending order, all vertices are initialized to disjoint sets, each take an edge, if the edge belongs to a different collection, then the edge joins the solution set, Also updates do not want to cross the collection. Note: There is a special data structure that you do not want to make a collection. The  prim algorithm is similar to the Dijkstra algorithm, where each join has the shortest edge between the cover set A and the set B not covered.    4. Shortest path problem with shortest path   right value. Can be a forward graph, which can be a graph without direction. The weight value can be negative.   Variants: The shortest path between points and points, the shortest path fixed to the end, the shortest path between a bit, the longest path (using the Bellman-ford algorithm for negative weights)  bellman-ford algorithm wakes V-1 the relaxation traverse, each traversal, in a certain order , then relax all the edges in turn. At the end, a check is performed on all edges, and if there is an edge that does not meet the relaxation condition, returns false, indicating that there is a ring with negative weights in the graph (which can be used to detect the loop in the directed graph), and the algorithm complexity O (VE)  dijkstra algorithm can only be applied to the edge of the positive value. Algorithm complexity O (v^2)   5. A dynamic programming algorithm is obtained on the shortest path matrix between all points. The Floyd-warshall algorithm, D_ij (k), indicates that the middle node contains only the shortest path of {1, 2 、... k} from Vertex I to vertex J. D_ij (n) is the request. The Floyd-warshall algorithm can be used to find out whether any two verticesUp to   

C + + algorithm--Summary of common algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.