AOP-Chap20-Introduction To Algorithms and Data Structures

  • data structure—how we store the data makes access to its elements more or less efficient depending on the operations required
  • interface—what a data structure can do— from the implementation—how it does it—in the data structures that we design

1 Big-Oh Notation

  • Big-Oh notation, which considers the number of steps our algorithm must execute as a function of its input size
  • asymptotic渐进 behavior—what happens for large input sizes only—of that function and ignores constant factors
  • f(x) is O(g(x)) --> f最终会被g超过

1.1 Definition

请添加图片描述
请添加图片描述
请添加图片描述

1.2 Practical Big Ohs for Algorithms

请添加图片描述
请添加图片描述

  • O(1) --> an algorithm that can give an answer in the same number of steps no matter what size of input it needs to operate on
    请添加图片描述
  • log-star time— those with runtime that is 请添加图片描述请添加图片描述

请添加图片描述
practical purpose:请添加图片描述
请添加图片描述

  • “next best” runtime that comes up often in practice is logarithmic time --> arise whenever our algorithm can split its input in half at each step 比如二叉树
    请添加图片描述
  • linear time algorithms 要遍历每个元素
  • linearithmic time algorithms typically arise from doing one or more operations
    that take O(lg(N)) time to each item in the data set --> best possible runtime for a general sorting algorithm
  • O(N2) arise frequently when the algorithm must examine all pairings of the input data (of which there are O(N2)) or perform some O(N) operation for each item of the input --> 很多排序算法
  • Exponential time algorithm非常棘手,因为N只要add 1变化就翻倍
  • O(N!)更差

1.3 Typical 典型, Worst 最差, or Amortized 平摊

  • amortized behavior—if we do the operation M times, what is the total runtime of all M of those operations, divided by M

1.4 Space

  • input有N个,即O(N),不一定要存O(N),可能是O(1), O(N2),因此有空间复杂度

1.5 Limitations

  • constant factors do matter --> 不同大小的N下,一次不一定比二次好

2 Abstract Data Types

  • abstraction—the separation of interface from implementation
  • 在数据结构下,abstraction arises from separating the abstract data type (ADT) from its concrete implementation
  • An abstract data type defines the interface—and only the interface—for a data structure.
  • ADT specifies what the data structure does but says nothing about how it accomplishes those tasks.

3 Queues 队列

  • queue is a first-in first-out (FIFO) sequence of items 先进先出
  • enqueue inserts an item into the queue, and dequeue retrieves the next item from the queue (removing it in the process)
    请添加图片描述
  • place items into one end of the queue (i.e., the tail) and remove items from the other end (i.e., the head)

3.1 ADT Definition

  • generic Queue class (a queue that can hold any type of data)
template<typename T>
class Queue {
public:
	void enqueue(const T & item);
	T dequeue(); //might choose to return void instead T & peek();
	const T & peek() const;
	int numberOfItems() const;
};	
  • ADT defines an interface but says nothing about the implementation—those details would be private to the class and are not needed to use the class
  • C++’s STL (Standard Template Library)有std::queue class, which implements a generic queue 但method name不同,在STL,enqueue是push,dequeue是pop,这样就可以保证和其他ADT拥有同名interface,便于模版化
  • queue ADT和C++ STL的另一个区别是:STL implementation of the queue has its pop method return void (it only removes the element, it does not return it)
  • http://www.cplusplus.com/reference/queue/queue/

3.2 An Array or Vector Implementation

  • The operation of an array/vector-based queue, including resizing to add more elements
    在这里插入图片描述
    虽然A被dequeue,但仍在array里,只是head指到后面
    在这里插入图片描述
    注意tail指向mod the array size
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • pop删除 from a queue in O(1) time. When we enqueue, our worst case is O(N), as we may have to resize the queue (copying all the elements). However, as long as we double the capacity each time that we need to resize, we can achieve amortized O(1) behavior.

3.3 Deques

4 Stacks

  • A stack is a last-in first-out (LIFO) sequence of items
  • primary operations on a stack are push (which places an item onto the stack) and pop (which obtains and removes an item from the stack)
    请添加图片描述

4.1 Uses of Stacks

  • call stack is the stack of frames the computer maintains
  • Using a stack to keep track of which HTML tags are open
    在这里插入图片描述
    在这里插入图片描述

4.2 ADT Definition

  • genetic Stack class
template<typename T>
class Stack {
public:
	void push(const T & item);
	T pop(); //might choose to return void instead
	T & peek();
	const T & peek() const;
	int numberOfItems() const;
};	

4.3 An Array or Vector Implementation

  • Using a vector gives amortized O(1) push and pop behavior for a stack, as those are the runtimes of the underlying vector operations
  • However, any particular push operation might take O(N) time if the vector must be resized (recall that the vector doubles its capacity whenever it must resize so that the cost of copying all of the elements is amortized over many additional operations)

5 Sets 集合

  • set is simply a collection of elements,元素不能重复
  • 功能:adding items, testing if an item is in the set, checking if the set is empty, taking the union of two sets, intersecting two sets and iterating through all elements
  • multiset (also called a bag), which allows the same element to appear multiple times
  • 注意集合的无限情况,有限集合的补集是无限的,无限无法在有限内存表示

5.1 ADT Definition

template<typename T>
class Set {
public:
	void add(const T & item);
	bool contains(const T & item) const;
	bool numItems() const;
	void remove(const T & item);
	Set<T> intersect(const Set<T> & s) const; 
	Set<T> unionSets(const Set<T> & s) const;
};	

6 Maps 映射

  • A map tracks a mapping from keys to values
  • To look up an item in a map, one provides the desired key, and the map returns the corresponding value (or some indication that the key is invalid if it is not currently in the map)

6.1 ADT Definition

template<typename K, typename V>
class Map {
public:
	void add(const K & key, const V & value); 
	const V& lookup(const K & key) const;
	V & lookup(const K & key);
	bool numItems() const;
	void remove(const K & key) ;
};	

6.2 An Array or Vector Implementation

  • 给Map一个key,查找对应的value,没解决找不到的问题
  • c++的STL采用了2种方法是提供两种不同的方法来查找元素
    1. 重载[]操作符以接受一个键并返回对相应值的reference。如果键已经不在映射中,那么[]操作符将添加键,并将默认构造值(即V())添加到映射并返回对新构造值的引用。注意,没有const重载,因为如果没有找到键,操作符将修改映射。
    2. 使用find,它返回一个迭代器(有const和非const重载)。然后,应该将结果与映射的末端进行比较,看看在对迭代器解引用之前是否没有找到键
  • 可以考虑的第三种方法是如果找不到键,则抛出异常

7 ADTs and Abstract Classes

template<typename T> class AbstractSet {
protected:
	class iterator_impl {
	public:
		virtual iterator_impl * copy() const = 0;
		virtual T & dereference() = 0;
		virtual void increment() = 0;
		virtual bool equals(const iterator_impl * rhs) const = 0; 
		virtual ~iterator_impl() {}
	};
public:
	class iterator { 
		iterator_impl * impl;
	public:
		iterator(iterator_impl * i): impl(i) { }
		iterator(const iterator & rhs): impl(rhs.impl->copy()) {} 
		~iterator() {delete impl;}
		iterator & operator=(const iterator & rhs) {
			if (this != &rhs) {
				iterator_impl * temp = rhs.impl->copy(); 
				delete impl;
				impl = temp;
			}
			return *this; 
		}
		T & operator*() {
			return impl->dereference();
		}
		iterator & operator++() {
   			impl->increment();
			return *this; 
		}
		bool operator!=(const iterator & rhs) const { 
			return !impl->equals(rhs.impl);
		}
		bool operator==(const iterator & rhs) const {
			return impl->equals(rhs.impl); 
		}
	};
	virtual iterator begin() = 0; 
	virtual iterator end() = 0; 
	//everything else omitted	
};		
  • ADT的接口中有一个具体的迭代器类,但是它有一个指向抽象实现的指针。然后,ADT的每个子类将implement iterator_impl的一个具体子类,以及implement begin和end以返回具有指向适当指针的迭代器
  • 这种方法对性能有一些影响:主要来自分配/复制/删除对象,以及动态分派
  • STL类没有一个抽象类来定义纯接口
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值