Introduction to Data Structures and Algorithms

For Java programmers, algorithms and data structures usually do much with this thing work. When reading in a company internship over there with my direct supervisor told me that the algorithm is not important, use very little. Probably it is the truth, and now has graduated two years, the content of algorithms and data structures involved was very small indeed. But I think data structures and algorithms are the basic skills of a programmer, the programmer is able to distinguish between a level of knowledge, so I want to systematically organize under common data structures and algorithms.

1. What is a data structure

Study data structure is a data logical structure, storage structure and discipline related operations (add, delete, change, and search) a.
The data structure may be divided into storage structure and logical structure, there are four basic types of logical structure: a set of linear structure, a tree structure, like the structure of FIG.

  • Collection structure: In addition to the same belonging to one type, but no other relations.
  • Linear structure: one relationship exists between the elements. Common types are: ** arrays, linked lists, queues, stacks, *. Between them differ in operation. For example: the list may be inserted at any position or deleting elements, and the queue at the tail element is inserted, deleted queue head element, the stack can only be inserted in the top of the stack, the delete operation.
  • Tree Structure: many relationship between the elements. Common types are: tree (there are many exceptions: binary tree, balanced binary trees, search trees, etc.)
  • Graph structure: many relationship exists, the precursor graph structure of nodes and each node of the plurality of nodes may be any number between subsequent elements.

Storing data in the form of a structure represented in a computer , there are the following four types of storage structure

  • Sequential storage structure: sequentially storing the data structures stored in consecutive addresses in the memory cell.
  • Link storage structure: linked storage structure to store data in an arbitrary memory cell, find the data elements associated by storing the addresses.
  • Index storage structure; (convenient query)
  • Hash storage structure; (convenient query)

2. What is the algorithm

Program may be understood as data structures and algorithms, and ( program + data structures = Algorithm ). Algorithm should have five basic features: input, output, finite, certainty and feasibility.

  • 输入:一个算法具有零个或者多个输出。以刻画运算对象的初始情况,所谓0个输入是指算法本身定出了初始条件。后面一句话翻译过来就是,如果一个算法本身给出了初始条件,那么可以没有输出;
  • 输出:算法至少有一个输出。也就是说,算法一定要有输出。输出的形式可以是打印,也可以使返回一个值或者多个值等。也可以是显示某些提示。
  • 有穷性:算法的执行步骤是有限的,算法的执行时间也是有限的。
  • 确定性:算法的每个步骤都有确定的含义,不会出现二义性。
  • 可行性:算法是可用的,也就是能够解决当前问题。

在开始之前先普及下算法复杂度的相关概念。计算机能够根据我们给出的算法完成实现相应的功能,但是要想编写出能高效运行的程序,我们就需要考虑到算法的效率。算法的效率主要由以下两个复杂度来评估:

  • 时间复杂度:评估执行程序所需的时间。可以估算出程序对处理器的使用程度。
  • 空间复杂度:评估执行程序所需的存储空间。可以估算出程序对计算机内存的使用程度。
  • 正确性:算法对于合法数据能够得到满足要求的结果,能够处理非法输入,并得到合理的结果。
  • 可读性:算法要便于阅读、理解和交流
  • 健壮性:算法不应该得到莫名其妙的结果
  • 性价比:利用最少的资源得到满足要求的结果

设计算法时,一般是要先考虑系统环境,然后权衡时间复杂度和空间复杂度,选取一个平衡点。不过,时间复杂度要比空间复杂度更容易产生问题,因此算法研究的主要也是时间复杂度,不特别说明的情况下,复杂度就是指时间复杂度。

时间复杂度
一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道。但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间少就可以了。并且一个算法花费的时间与算法中语句的执行次数成正比例,哪个算法中语句执行次数多,它花费时间就多。一个算法中的语句执行次数称为语句频度或时间频度。记为T(n)。

前面提到的时间频度T(n)中,n称为问题的规模,当n不断变化时,时间频度T(n)也会不断变化。但有时我们想知道它变化时呈现什么规律,为此我们引入时间复杂度的概念。一般情况下,算法中基本操作重复执行的次数是问题规模n的某个函数,用T(n)表示,若有某个辅助函数f(n),使得当n趋近于无穷大时,T(n)/f(n)的极限值为不等于零的常数,则称f(n)是T(n)的同数量级函数,记作T(n)=O(f(n)),它称为算法的渐进时间复杂度,简称时间复杂度。更详细介绍参考博客

O(1) < O(logn) < O(n) < O(nlogn) < O(n²) < O(n³) < O(2ⁿ) < O(n!)

空间复杂度
算法在执行过程中除了输入输出参数占用的空间之外,还需要多少辅助空间。这些辅助空间称为算法的空间复杂度。

算法空间复杂度是算法运行后对空间需求量的定性描述。通常使用S(n)表示算法的空间复杂度。使用时间复杂度的推导方法推导空间复杂度。当算法所需的内存空间大小为常数时,算法的空间复杂度为S(1)。通常情况下,算法的时间复杂度更受关注。可以通过增加额外空间降低时间复杂度。算法是解决具体问题的步骤,数据结构是算法解决问题的载体。

常见的算法

  • 图搜索 (广度优先、深度优先)深度优先特别重要
  • 排序
  • 动态规划
  • 匹配算法和网络流算法
  • 正则表达式和字符串匹配
  • 三路划分-快速排序
  • 合并排序(更具扩展性,复杂度类似快速排序)
  • DF/BF 搜索 (要知道使用场景)
  • Prim / Kruskal (最小生成树)
  • Dijkstra (最短路径算法)
  • 选择算法

3. 一句话总结

算法是解决具体问题的步骤,数据结构是算法解决问题的载体。

4. 参考

Guess you like

Origin www.cnblogs.com/54chensongxia/p/11448695.html