美文网首页
算法导论笔记

算法导论笔记

作者: 太捉急 | 来源:发表于2019-05-31 22:21 被阅读0次

1.1 简单介绍何为算法,它能解决什么样的问题,介绍NP完全问题。

1.2 比较算法复杂度

\log(n) < \sqrt(n) < n < n \log(n) < n^2 < n^3 < 2^n < n! \quad \text{when} \quad n \rightarrow \infty

2.1 Insert Sort

O(n^2)

def insert_sort(A, reverse=False):
    for j in range(1, len(A)):
        key = A[j]
        i = j - 1


        # Insert A[j] to sorted sequence of A[:j]
        if reverse:
            while i >= 0 and A[i] < key:
                A[i+1] = A[i]
                i -= 1
        else:
            while i >= 0 and A[i] > key:
                A[i+1] = A[i]
                i -= 1
        A[i+1] = key

    return A

2.3 Merge Sort

O(n\log n)

def merge(A, p, q, r):
    L = A[p:q] + [float('inf')]
    R = A[q:r] + [float('inf')]

    i, j = 0, 0
    for k in range(p, r):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1


def merge_sort(A, p, r):
    if p < r:
        q = int((p + r) / 2)
        if q == p:
            merge(A, p, p+1, r+1)
        else:
            merge_sort(A, p, q)
            merge_sort(A, q, r)
            merge(A, p, q, r)
    return A

3. Algorithm Complexity Notations:

image.png

We say an algorithm's complexity is O(n^2) means:
There exists c > 0, n_0 > 0 such that running time of the algorithm is below c n^2 when n > n_0

4.1 Find maximum subarray

An array has n(n+1) subarrays, find the maximum sum of all the subarrays.

def find_maximum_cross_subarray(A, low, mid, high):
    left_sum = - float('inf')
    sum_ = 0
    for i in range(mid, low-1, -1):
        sum_ += A[i]
        if sum_ > left_sum:
            left_sum = sum_
            max_left = i
    
    right_sum = - float('inf')
    sum_ = 0
    for j in range(mid+1, high+1):
        sum_ += A[j]
        if sum_ > right_sum:
            right_sum = sum_
            max_right = j
    
    return max_left, max_right, left_sum + right_sum


def find_maximum_subarray(A, low, high):
    if high == low:
        return low, high, A[low]
    
    else:
        mid = (low + high) // 2
        left_low, left_high, left_sum = find_maximum_subarray(A, low, mid)
        right_low, right_high, right_sum = find_maximum_subarray(A, mid+1, high)
        cross_low, cross_high, cross_sum = find_maximum_cross_subarray(A, low, mid, high)
    
    if left_sum >= right_sum and left_sum >= cross_sum:
        return left_low, left_high, left_sum
    elif right_sum >= left_sum and right_sum >= cross_sum:
        return right_low, right_high, right_sum
    else:
        return cross_low, cross_high, cross_sum

6 Heapsort

Heap (tree) has following properties:

  • Parent of node i is node [i / 2]
  • Left child of node i is node 2i
  • Right child of node i is node 2i + 1

Max-heap is such that the value of node i is smaller than its parent node's value

def max_heapify(A, i, till):
    l = 2*i
    r = 2*i + 1

    if l < till and A[l] < A[i]:
        largest = l
    else:
        largest = i
    
    if r < till and A[r] < A[largest]:
        largest = r
    
    if largest != i:
        A[i], A[largest] = A[largest], A[i]
        max_heapify(A, largest, till)

    return A



def heap_sort(A):
    for i in range(len(A) // 2, -1, -1):
        max_heapify(A, i, len(A))
    print(A)
    for i in range(len(A)-1, 0, -1):
        A[0], A[i] = A[i], A[0]
        A = max_heapify(A, 0, i)
    return A

7 Quicksort

image.png
def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i+1], A[r] = A[r], A[i+1]
    return i+1

def quick_sort(A, p, r):
    if p < r:
        q = partition(A, p, r)
        print(p, q, r, A)
        quick_sort(A, p, q-1)
        quick_sort(A, q+1, r)

10 Data Structure

  • Queue: FIFO
  • Stack: FILO
  • LinkedList
    image.png

11 Hash Table

image.png

12 Binary Tree

Let x be a node in a binary search tree. If y is a node in the left subtree of x, then y.key <= x.key. If y is a node in the right subtree of y.key >= x.key.


class Node:
    def __init__(self, left=None, right=None, parent=None, key=None):
        self.left = left
        self.right = right
        self.parent = parent
        self.key = key

def in_order_tree_walk(x:Node):
    if x != None:
        in_order_tree_walk(x.left)
        print(x.key)
        in_order_tree_walk(x.right)

def tree_search(x, k):
    if x == None or k == x.key:
        return x
    if k < x.key:
        return tree_search(x.left, k)
    if k > x.key:
        return tree_search(x.right, k)

def iterative_tree_search(x, k):
    while x is not None and k != x.key:
        if k < x.key:
            x = x.left
        else:
            x = x.right
    return x

def tree_min(x):
    while x.left is not None:
        x = x.left
    return x

def tree_max(x):
    while x.right is not None:
        x = x.right
    return x

def tree_succ(x):
    if x.right is not None:
        return tree_min(x.right)
    
    y = x.parent
    while y is not None and x == y.right:
        x = y
        y = y.parent
    return y

def tree_prec(x):
    if x.left is not None:
        return tree_max(x.left)
    
    y = x.parent
    while y is not None and x == y.left:
        x = y
        y = y.parent
    return y

15 Dynamic Programming

相关文章

  • 算法导论笔记

    1.1 简单介绍何为算法,它能解决什么样的问题,介绍NP完全问题。 1.2 比较算法复杂度 2.1 Insert ...

  • 算法导论笔记

    贪心算法 贪心算法:每一步在当时看起来是最佳的选择,总是做出局部最优的选择 贪心算法并不保证得到最优解,但对于很多...

  • 算法导论笔记

    读算法导论 记录一下读算法导论的过程 1.算法 如果问我什么是算法(思考中) 利用数据结构,考虑时间以及空间效率,...

  • 2018-11-07

    算法运用(读《智能科学技术导论》笔记) 学计算机玩的就是算法,算法之于程序员就如同菜谱之于厨师。人类通过编制算法,...

  • 算法导论----学习笔记

    渐进符号 1、Θ记号 Θ(g(n)) = { f(n) : 若存在正常数c1,c2和n0,使对所有n>=n0时有...

  • 《算法导论》笔记(一)

    第一章 练习1.1 Ans: 给学生成绩进行排名需要用到排序;TBD Ans: 工作量;完成度;…… Ans: 栈...

  • 《算法导论》笔记(二)

    循环不变式 1.初始化2.保持3.终止(与数学归纳法类似) 练习2.1 Ans: ① j = 2,{31,41,5...

  • 数据结构与算法参考书籍

    数据结构与算法分析 算法 算法导论 java编程思想

  • 好文章索引

    算法 《算法导论》快速指南:我是如何10天入门算法导论的。 - 渗透之美 - 知乎专栏 推荐内容索引 - 老赵点滴...

  • 算法导论贪心算法笔记

    贪心算法也是用来解决最优解问题 它在每一步都做出当时看起来最佳的选择,未必有动态规划严谨,但是在某些问题中,确实可...

网友评论

      本文标题:算法导论笔记

      本文链接:https://www.haomeiwen.com/subject/eaagtctx.html