[Notes] algorithm illustration

EDITORIAL 0

The concept of this book for a more detailed explanation of the algorithm, is deeply impressed with the figures graphically compare the difference between an array and a linked list, but the principle of the concept of the algorithm description of the content is not particularly large, only play a role in the Enlightenment, the proposed as a leisure books or enhance self-confidence purposes, about three hours when I read it.

The following is a brief author's note for the book, a strong personal style, be careful eating.

In view of copyright reasons, such as the need to review the original book resources, code, see GitHub (not entirely, because it is simply too lazy to write, there is a need I will add, comments welcome).

1 Introduction to Algorithms

1.1 Introduction

Algorithm i.e. a set of instructions to complete the task, any code segment can be considered algorithms. But talk about a part of the book describes the algorithm either faster or to solve interesting problems, or both.

1.2 Binary Search

Guess the least number of words, for example, from 1 in order to start up the suspect called simple lookup

Binary search that is high school science dichotomy, slightly

Running time: For a list of n elements with a binary search can take up to log2n step, and simply need to find the most n steps.

Note: Only when the list is ordered when the binary search was useful.

1.3 Big O notation

Big O notation that means how fast algorithm. It is defined as follows, assuming list contains n elements. Find a simple need to check each element, it is necessary to perform n operations. O notation large, the running time is O (n). For the above example, and the binary search is O (log n)
Note that, Big O notation refers to the running time in the worst case, and is not in seconds.

Common running time Big O

THE ( l The g n ) O(log n) , also called logarithmic time, such algorithms includebinary search.
O ( n ) O (n) , also known as linear time, such algorithms include simple lookup.
O ( n l o g n ) O(n * log n) , such as Chapter 4 of the quick sort described - one speed fast sort algorithm.
O ( n 2 ) O (n ^ 2) , such an algorithm such as Chapter 2 describes the selection sort - An slower sorting algorithm.
O ( n ! ) O (n!) , This algorithm includes solutions to the traveling salesman problem will next be described - a very slow algorithm.

Traveling Salesman Problem

Slightly, can Google

2 Select Sort

2.1 Memory Works

When writing or storing data, the storage space required to provide a requesting computer, the computer will address a memory, while storing a plurality of data, there are two basic ways: list array and

2.2 arrays and linked lists

For the array must be a contiguous memory addresses, and lists can be anywhere.

The disadvantage is that if you want to add an array of new elements may be difficult to transfer enough space, even if the reserved space may also be a waste of memory, while the corresponding list an advantage in terms of insertion elements, intervening when necessary elements, linked lists better, but also remove elements

But the list only access the middle or last element, you must first access the first element, and has been pushed to the middle or the last element, the efficiency is relatively low, but knowing the address of each array element can easily access

The position of elements in the array called index (index)

2.3 Selection Sort

Given a number of columns by descending order, you can traverse the list to find the largest number, and add it to the new list, and empathy to find the second largest number, and so on, so the time required for the O ( n 2 ) O (n ^ 2) This isselection sort

3 recursive

3.2 baseline conditions and recursive condition

Note that recursive function involving the preparation of the array, the array baseline conditions usually is empty or contains only one element. When in trouble, please check the baseline condition is not the case.

3.3 Stack

There are basic call stack and recursive call stack

4 Quick Sort

Divide and conquer (divide and conquer, D & C) is a well-known recursive problem-solving methods, such as quick sort

4.1 Divide and conquer

Private thought similar to the idea of ​​dynamic programming, the "biggest box this small piece of land, and it is applicable to the whole field of the biggest squares" in this case divided the land.

4.2 Quick Sort

Specific principles can Baidu, its running time is in the average case O ( n l o g n ) O(n * log n) , in the worst case O ( n 2 ) O (n ^ 2) . There is also a merge sort algorithm (merge sort), its running time is always O ( n l o g n ) O(n * log n)

The average case and worst case of quick discharge, see the original book 4.3.2

5 hash table

That hash table (hash), hash table implementation is in python dictionary

6 BFS

BFS is a graph algorithms (breadth-first search, BFS), be able to find the shortest distance between two things, which means there are many, are as follows:

  • Write Checkers AI, calculate the minimum number of steps you can take to win

  • Write the spelling checker, calculate how many places can at least edit the misspelled words into the correct word, such as the

    READED changed READER need to edit a place

  • Find the nearest doctor relationship based on your network of relationships

Figure 6.1-6.2

FIGS of nodes and edges, a node may be directly connected to many nodes are called neighbors.

FIG sub undirected graph with a directed graph, directed and undirected graph annular FIG equivalent, as follows:

Here Insert Picture Description

6.3 BFS

For example, to find your nearest fruit to friends, search for all your friends to form a relationship once list, and then search your friends' friends list form second degree relations, and so on, until you find the fruit merchant or traverse the entire map so far, this is the breadth First Search.

Its running time is O ( V + E ) O (V + E) , V is the number of graphs or nodes, E is the number of edges

7 Dijkstra algorithm

BFS can only find the number of stages or the total number of edges minimum path length is not the same for each side of FIG follows:

Here Insert Picture Description

Side length of 4 minutes, 5 minutes, etc., at this time the need to use Dijkstra algorithm. Such a side or sides associated with different numbers, these numbers is called the weighting with the weighting graph of FIG weight, otherwise called the non-weighted graph. Also note that the Dijkstra algorithm is only applicable to a directed acyclic graph (directed acyclic graph, DAG), and a positive weight is required. If you include negative weights, use  Bellman Ford algorithm.

Dijkstra algorithm principle can Google.

However, if the algorithm will be able to traverse all the nodes that end? Not yet understand

8 greedy algorithm

Not so much an algorithm, as it is a strategy, that each step select local optimal solution is to finally get global optimal solution. But not all effective in any case, but easy to implement!

Mentioned above are not greedy quick sort algorithm, and breadth-first search algorithm and Dijkstra are.

NP-complete problems

Such as the traveling salesman problem, see the original book Detailed

How to determine whether the NP-complete

If it is determined a problem is NP-complete, will not have to find the exact solution, but the use of approximate solution.

There is no way to determine whether a system is NP-complete, but there are some clues to follow:

  • When there are fewer elements running speed of the algorithm is very fast, but with the increase in number of elements, the speed will become very slow.
  • Issues involving "all combinations" are generally NP-complete problem.
  • The problem can not be divided into small problems, we must consider all possible cases. This may be NP-complete.
  • If the issue is the sequence (such as urban sequence Traveling Salesman Problem) and difficult to solve, it might be NP-complete.
  • If the problem relates to the collection (such as radio stations collection) and difficult to solve, it might be NP-complete.
  • If the problem can be converted to a set covering problem or traveling salesman problem, then it certainly is NP-complete.

9 Dynamic Programming

Dynamic programming is about thinking the problem down into sub-problems to solve, specifically difficult to understand, can deepen the impression step by step through an example.

See examples of the original book

Note but only if each sub-problems are discrete, that is not dependent on other sub-problems, dynamic programming was effective.

10 K nearest neighbor algorithm

A little before the machine learning combat there

This chapter also has machine learning profiles, lists the OCR, spam filters and predict the stock market three examples

11 Then how do

10 include algorithms that are not described, i.e.

  • tree
  • Inverted index
  • Fourier transform
  • Parallel Algorithms
  • The MapReduce, a special parallel algorithms, i.e. distributed algorithm
  • Bloom filter and HyperLogLog
  • SHA algorithm is mainly used for encryption, SHA-0, SHA-1, SHA-2, and SHA-3, the former two have been found to be defective, the safest currently bcrypt
  • Locality sensitive hashing algorithm, i.e. Simhash
  • Diffie-Hellman key exchange, which has a replacement RSA
  • Linear Programming
Published 95 original articles · won praise 30 · views 40000 +

Guess you like

Origin blog.csdn.net/JohnJim0/article/details/104874706