数据结构与算法CS600A学习笔记

搭配视频食用更佳,请认准原作者及其github,笔记中链接有误,请见谅。

Array,Vector,List

Array 数组

char a[5] = {'h', 'e', 'l', 'l', 'o'}; // Array: Fixed size, contiguous memory.
// random access
char x = a[1]; // x is 'e'
a[0] = 'b';
  • A size-n array can be created in this way:
    char a[n];
  • When writing the code, n must be known.
  • What if n is unknown until program is running?
// Dynamic Allocation of Arrays
char* a = NULL;
int n; // array size
cin >> n; // read in the size, e.g., get n = 5
a = new char[n];
// store something in the array
a[0] = 'h';
a[1] = 'e';
a[2] = 'l';
a[3] = 'l';
a[4] = 'o';
// When done, free memory.
// Otherwise, memory leak can happen.
delete[] a;
a = NULL;

Properties of Array

  1. The size is fixed. (New elements cannot be appended.)
  2. Random access using a[i] has O ( 1 ) O(1) O(1) time complexity.
  3. Removing an element in the middle has O ( n ) O(n) O(n) time complexity. (Require moving the remaining items leftward.)

Vector 向量

  1. Vector is almost the same as array.
  2. The main difference is that vector’s capacity can automatically grow.
  3. New elements can be appended using push_back() in O ( 1 ) O(1) O(1) time (on average).
  4. The last element can be removed using pop_back() in O ( 1 ) O(1) O(1) time.
vector<char> v = {'h', 'e', 'l', 'l', 'o'}; // Vector: dynamic size, contiguous memory.
// random access
char x = v[1]; // x is 'e'
// assignment
v[0] = 'b';
// Insert
// insert a new element to the end
v.push_back('s');
// Delete
// delete the element in the end
v.pop_back();
// delete an element in the middle
v.erase(v.begin() + 1); // O(n) time complexity! Slow!

Vector capacity can grow

vector<char> v(100);
cout << v.size(); // print "100"
cout << v.capacity(); // print "100"
v.push_back('x');
cout << v.size(); // print "101"
cout << v.capacity(); // print "200"
/*
* capacity of vector = 100
* size of vector = 100
* v.push_back('x');
* capacity of vector = 200
* size of vector = 101
* What happens when size is going to exceed capacity?
* Create a new array of capacity 200.
* Copy the 100 elements from the old array to the new.
* Put the new element in the 101-st position.
* Free the old array from memory.
*/

List 链表

// Doubly Linked List
list<char> l = {'h', 'e', 'l', 'l', 'o'}; // List: dynamic size, not contiguous memory.
cout << l[2]; // does not work
l[0] = 'a'; // does not work
cout << l.front(); // print 'h'
cout << l.back(); // print 'o'
// Iterator
list<char>::iterator iter = l.begin();
cout << *iter; // print 'h'
iter++;
cout << *iter; // print 'e'
*iter = 'a'; // change 'e' to 'a'
// Insertion
l.push_back('s'); // Only O(1) time
l.push_front('a') // Only O(1) time
// Delete Node
l.pop_back(); // Only O(1) time
l.pop_front(); // Only O(1) time

Properties

Array Vector List
Size fixed can increase and decrease can increase ans decrease
Memory contiguous contiguous not contiguous
Rand Access O ( 1 ) O(1) O(1) O ( 1 ) O(1) O(1) -
push_back() - O ( 1 ) O(1) O(1) (average) O ( 1 ) O(1) O(1)
pop_back() - O ( 1 ) O(1) O(1) O ( 1 ) O(1) O(1)
insert() - O ( n ) O(n) O(n) (average) O ( 1 ) O(1) O(1)
erase() - O ( n ) O(n) O(n) (average) O ( 1 ) O(1) O(1)

Which shall we use?

  • Array: Fixed size throughout.
  • Vector:
    • Random access(i.e., read or write the i-th element) is fast.
    • Insertion and deletion at the end are fast.
    • Insertion ans deletion in the front and middle are slow.
  • List:
    • Sequentially visiting elements is fast; random access is not allowed.
    • Frequent insertion and deletion at any position are OK.

二分查找 Binary Search

Problem of Search

  • Inputs: (i) an array whose elements are in the ascending order and (ii) a key.
  • Goal: Search for the key in the array. If found, return its index; if not found, return -1.
left = 0.
right = arr.size() - 1.
mid = |(left + right) / 2|.
If arr[mid] == key ==> return mid.
If arr[mid] > key ==> right <- mid - 1.
If arr[mid] < key ==> left = mid + 1.

Time Complexity

  • Each iteration reduces the size of array by half.
  • Let n be the size of the array.
  • The total number of iterations is at most l o g 2 n log_2n log2n.
  • Per-iteration time complexity: O ( 1 ) O(1) O(1).
  • Overall time complexity: O ( l o g n ) O(logn) O(logn).
int search(int arr[], int left, int right, int key) {
	while (left <= right) {
		int mid = (left + right) / 2;
		if (key == arr[mid]) return mid; // key is found
		if (key > arr[mid]) left = mid + 1; // search in the right half
		else right = mid - 1; // search in the left half
	}
	return -1; // key is not found
}

How to support both search and insertion?

  • Vector
    • The asscending order must be kept; otherwise, search would take O ( n ) O(n) O(n) time.
    • Inserting an item into the middle has O ( n ) O(n) O(n) time complexity (on average).
  • List
    • Can we perform binary search in the list?
    • No. Given left and right, we cannot get mid efficiently.
Search Insertion
Vector O ( l o g n ) O(logn) O(logn) O ( n ) O(n) O(n)
List O ( n ) O(n) O(n) O ( 1 ) O(1) O(1)
Skip List O ( l o g n ) O(logn) O(logn) O ( l o g n ) O(logn) O(logn)

跳跃列表 Skip List

Why skip list?

  • Linked list does not aupport binary search.
  • Skip list allows fast search and fast insertion.
  • Search: O ( l o g n ) O(logn) O(logn) time complexity on average.
  • Insertion: O ( l o g n ) O(logn) O(logn) time complexity on average.

Outline

  1. Buliding a skip list. (创建)
  2. Search. (查找)
  3. Insertion. (插入)

Building a skip list

  • Initially, a linked list contains n numbers in ascending order.
  • Add sentinel in the front.
  • Build the L1 linked list.
  • Bulid the L2 linked list.
  • Bulid the L3 linked list.
  • The number of layers is up to the user; we use a total of 4 layers.
  • The number of layers should be l o g n log n logn.

Search

Insertion

  • First, search key = 9 and record the path.
  • Second, create a node whose height is random, e.g., height = 2.
  • Third, link the new node to the skip list.

矩阵加法、乘法、时间复杂度 Vector and Matrix Basics

Additions

Vector Addition

  • Given n × 1 n \times 1 n×1 vectors: a ∈ R n a \in \mathrm{R}^n aRn and b ∈ R n b \in \mathrm{R}^n bRn
  • Vector addition: c = a + b ∈ R n c = a + b \in \mathrm{R}^n c=a+bRn
  • Pseudo Code
  • Initialization: c ⇐ [ 0 , 0 , ⋅ ⋅ ⋅ , 0 ] c \Leftarrow [0,0,···,0] c[0,0,⋅⋅⋅,0].
  • For i = 1 to n:
    c i ⇐ a i + b i c_i \Leftarrow a _i + b_i ciai+bi
  • Time complexity: O ( n ) O(n) O(n)

Matrix Addition

  • Given m × n m \times n m×n matrices: A ∈ R m × n A \in \mathrm{R}^{m \times n} ARm×n and B ∈ R m × n B \in \mathrm{R}^{m \times n} BRm×n.
  • Matrix addition: C = A + B ∈ R m × n \mathrm{C} = \mathrm{A} + \mathrm{B} \in \mathrm{R}^{m \times n} C=A+BRm×n.
  • Pseudo Code
  • Initialization: C ⇐ m × n \mathrm{C} \Leftarrow m \times n Cm×n all-zero matrix.
  • For i = 1 to m:
    • For j = 1 to n:
      c i j ⇐ a i j + b i j c_{ij} \Leftarrow a_{ij} + b_{ij} cijaij+bij.
  • Time complexity: O ( m n ) O(mn) O(mn).

Multiplications

Vector Inner Product

  • Given vectors: a ∈ R n a \in \mathrm{R}^n aRn and b ∈ R n b \in \mathrm{R}^n bRn.
  • Vector inner product: c = a T b = a 1 b 1 + ⋅ ⋅ ⋅ + a n b n c = a^Tb = a_1b_1 + ··· + a_nb_n c=aTb=a1b
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值