1 advanced data structures. 2 topics data structures algorithm design & analysis queue tree...

33
1 Advanced Data Structures

Upload: hilary-stokes

Post on 15-Jan-2016

239 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

1

Advanced Data Structures

Page 2: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

2

Topics Data structures Algorithm design & analysis Queue Tree Graph

Page 3: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

3

What is a “data structure”?

Page 4: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

4

Why study data structures? Example problem

Given: a set of N numbers Goal: search for number k

Solution Store numbers in an array of size N Linearly scan array until k is found or array is

exhausted Number of checks

Best case: 1 Worst case: N Average case: N/2

7 16 10 4383

Page 5: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

5

Why study data structures? Solution #2

Store numbers in a binary search tree Search tree until find k Number of checks

Best case: 1 Worst case: log2N

Average case: (log2N)/2

7

3 10

1 6 8 43

Page 6: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

6

Analysis Does it matter?

N vs. log2N

N vs. Log N

0

20

40

60

80

100

120

1 2 3 4 5 6 7 8 9 10

N

linear (N)

Logarithmic (log N)

Page 7: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

7

Analysis Does it matter? Assume

N = 1,000,000,000 1 billion (Walmart transactions in 100 days)

1 GHz processor = 109 cycles per second 1 cycle per transaction

O(N) algorithm 1 billion transactions = > 1 billion clock cycles

O(lg N) algorithm 1 billion transactions => 30 clock cycles

Page 8: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

8

Example 2 Scheduling job in a printer

Write a code to manage the printer queue Functions to support

Insert, delete Special accommodations needed for:

Priority Dynamic update Scheduling challenges

Page 9: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

9

Example 3 Exploring the Facebook connection network

Write a code to tell who is connected to who (directly or indirectly) through your Facebook profile

6-degrees of separation

Page 10: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

10

Example 4 Pattern matching

Write a code to do Google search on your web database

Page 11: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

11

Summary Keep the data organized

Choice of data structures matters

Appropriate data structures ease design & improve performance

Challenge Design appropriate data structure & associated algorithms

for a problem Analyze to show improved performance

Page 12: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Queue ADT Like a stack, a queue is also a list.

However, with a queue, insertion is done at one end, while deletion is performed at the other end.

Accessing the elements of queues follows a First In, First Out (FIFO) order. Like customers standing in a check-out line in

a store, the first customer in is the first customer served.

12

Page 13: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

:Basic operations

enqueue: insert an element at the rear of the list

dequeue: delete the element at the front of the list

13

Page 14: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Implementation of Queue Just as stacks can be implemented as

arrays or linked lists, so with queues.

Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks

14

Page 15: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Trees Linear access time of linked lists is prohibitive

Does there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)?

Trees Basic concepts Tree traversal Binary tree Binary search tree and its operations

15

Page 16: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

TreesA tree T is a collection of nodes

T can be empty (recursive definition) If not empty, a tree T

consists of a (distinguished) node r (the root), and zero or more nonempty subtrees T1,

T2, ...., Tk

16

Page 17: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Continue..

17

Page 18: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Tree Traversal Used to print out the data in a tree in a certain order Pre-order traversal (Root, left, right)

Print the data at the root Recursively print out all data in the leftmost sub

tree Recursively print out all data in the rightmost sub

tree

18

Page 19: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Continue…

19

Page 20: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Continue…. Inorder traversal (left, root, right)

Recursively print out all data in the leftmost sub tree

Print the data at the root Recursively print out all data in the rightmost sub

tree

20

Page 21: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Continue…

21

Page 22: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Continue.. Post-order traversal (left, right, root)

Recursively print out all data in the leftmost sub tree

Recursively print out all data in the rightmost sub tree

Print the data at the root

22

Page 23: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Continue..

23

Page 24: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Binary Trees A tree in which no node can have more than two

children

24

Page 25: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Continue.. The depth of an “average” binary tree is considerably

smaller than N, even though in the worst case, the depth can be as large as N – 1.

25

Page 26: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Binary Search Trees (BST) A data structure for efficient searching, inser-tion and

deletion Binary search tree property

For every node X All the keys in its left

subtree are smaller than the key value in X

All the keys in its right subtree are larger than the key value in X

26

Page 27: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Continue..

27

Page 28: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Grapho A Simple graph G = (V, E) consists of a nonempty set V

of vertices and a possibly empty set E of edges, each edge being a set of two vertices from V.

The number of vertices and edges are denoted by |V| and |E|, respectively.

28

Page 29: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Graph Representation There are variety of ways to represent a graph. Adjacency lists. An adjacency matrix of graph G = (V, E) is a binary

|V| x |V| matrix such that each entry of this matrix An incident matrix of graph G = (V, E) is a binary |

V| x |E| matrix such that each entry of this matrix

29

Page 30: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Continue..

30

Page 31: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Continue..

31

Adjacency matrix Incident matrix

Page 32: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

Graph Traversal As in trees, traversing a graph consists of visiting

each vertex only one time. The simple traversal algorithm used for trees can not be applied here because graph may include cycles (result in to infinite loop) or, isolated vertices (left some nodes).

The most popular algorithms for traversing in graphs are:

1. Depth First Search

2. Breadth First search

32

Page 33: 1 Advanced Data Structures. 2 Topics Data structures Algorithm design & analysis Queue Tree Graph

33