Contents of Chapter 2 Sections –2.1 Stacks and Queues –2.2 Trees –2.3 Dictionaries –2.4...

Post on 06-Jan-2018

221 views 3 download

description

2.1 Stacks and Queues Example (Figure 2.1) EDCBAEDCBA top A B C D E front rear queuestack

Transcript of Contents of Chapter 2 Sections –2.1 Stacks and Queues –2.2 Trees –2.3 Dictionaries –2.4...

전북대학교

전자정보공학부

Contents of Chapter 2• Sections

– 2.1 Stacks and Queues– 2.2 Trees– 2.3 Dictionaries– 2.4 Priority queues– 2.5 Sets and disjoint set union– 2.6 Graphs– 2.7 References and readings

• One of the basic techniques for improving algorithms– By structuring the data in such a way that the

resulting operations can be efficiently carried out

전북대학교

전자정보공학부

2.1 Stacks and Queues • Ordered list (or linear list)

– a = (a1, a2, …, an) where ai is atom (or element)• Stack

– Ordered list in which all insertions and deletions made at top

– LIFO (Last In First Out)• Queue

– Ordered list in which insertions made at rear and deletions at front

– FIFO (First In First Out)

전북대학교

전자정보공학부

2.1 Stacks and Queues • Example (Figure 2.1)

EDCBA

top

A B C D E

front rear

queuestack

전북대학교

전자정보공학부

2.1 Stacks and Queues • Representation of stack

– Using one-dimensional array– Class definition of stack (Program 2.1)

#include<iostream.h>

template<class Type> class Stack { private: int top, MaxSize; Type *stack;

public: Stack(int MSize): MaxSize(MSize) { stack = new Type[MaxSize]; top=-1;} ~Stack() { delete [] stack;} inline bool Add(const Type item) // Push an element onto the stack; return // true if successful else return false. { if (StackFull()) {

전북대학교

전자정보공학부

cout << "Stack is full"; return false; } else { stack[++top] = item; return true; } } inline bool Delete(Type& item) // Pop the top element from the stack; return // true if successful else return false. { if (StackEmpty()) { cout << "Stack is empty"; return false; } else { item = stack[top--]; return true; } } inline bool StackEmpty() // Is the stack empty? { if (top < 0) return true; else return false; } inline bool StackFull() // Is the stack full? { if (top >= (MaxSize-1)) return true; else return false; } };

전북대학교

전자정보공학부

• Representation of stack– Using linked list (Figure 2.2)

– Linked representation of stack (Program 2.2)

2.1 Stacks and Queues

stack

data linkE D C B A 0

#define NULL 0 #include<iostream.h>

class Stack { private: struct node { Type data; struct node *link; }; struct node *top;

전북대학교

전자정보공학부

2.1 Stacks and Queues public: Stack() { top = NULL; } ~Stack() { struct node *temp; while (top) { temp=top; top=top->link; delete temp; } } bool Add(const Type item); bool Delete(Type& item); inline bool StackEmpty() // Is the stack empty? { if (top) return false; else return true; } };

전북대학교

전자정보공학부

2.1 Stacks and Queues • Representation of stack

– Add programs (Programs on page 69)

bool Stack::Add(const Type item){ struct node *temp = new node; if (temp) { temp->data = item; temp->link = top;

top = temp; return true; } else { cout << “Out of space!” << end; return false; }

전북대학교

전자정보공학부

2.1 Stacks and Queues • Representation of stack

– Delete programs (Programs on page 69)

bool Stack::Delete(Type& item){ if (StackEmpty()) { cout << “Stack is empty” << endl; return false; } else { struct node *temp; item=top->data; temp = top; top = top->link; delete temp; return true; } }

전북대학교

전자정보공학부

2.1 Stacks and Queues • Representation of queue

– Using one-dimensional array (Figure 2.3)

전북대학교

전자정보공학부

2.1 Stacks and Queues • Representation of queue

– A class definition of queue (Programs 2.3) #define NULL 0 #include<iostream.h> class Queue { private: Type* q; int front, rear, MaxSize; public: Queue(int MSize): MaxSize(MSize) { q = new Type[MaxSize]; rear=front=0; } ~Queue() { delete [] q; } bool AddQ(Type item); bool DeleteQ(Type& item); bool QFull(); bool QEmpty(); };

전북대학교

전자정보공학부

2.2 Trees • Definition 2.1 [Tree]

– A tree is a finite set of one or more nodes such that there is a special designated node called root and the remaining nodes are partitioned into n0 disjoint sets T1,…,Tn, where each of these sets is a tree. The sets T1,…,Tn are called the subtrees of the root.

• Terminologies– Degree of a node and degree of a tree– Leaf node (or terminal node) and nonterminal

nodes– Children, parents, siblings, ancestors– Level of a node and height (or depth) of a tree– Forest

전북대학교

전자정보공학부

2.2 Trees • Sample tree (Figure 2.5)

• List representation of the tree of Figure 2.5 (Figure 2.6)– A node has three fields (tag, data, link)

A

C

G

DB

E F

LK

H JI

M

level1

2

3

4

A 0

B F 0 C G 0 D I J 0

E K L 0 H M 0

전북대학교

전자정보공학부

2.2.1 Binary Trees • Definition 2.2

– A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left and right subtrees.

• Two sample binary trees– Skewed tree (Figure 2.7(a)) and complete binary

tree (Figure 2.7(b))

(a)

(b)

CB

A

GD FE

H I

B

A

C

D

E

level1

2

3

4

5

전북대학교

전자정보공학부

2.2.1 Binary Trees • Lemma 2.1

– Maximum number of nodes on level i of a binary tree = 2i-1

– Maximum number of nodes in a binary tree of depth k = 2k-1, k>0

• Sequential representation of binary tree (Figure 2.9)

• Lemma 2.2– parent(i) is at floor(i/2) if i1– lchild(i) is at 2i if 2in– rchild(i) is at 2i+1 if 2i+1n

tree

tree

A B - C - - - D -

G H ID E FA B C

0 0 0 E

0 0 0(1) (2) (3) (4) (5) (6) (7) (8) (9) (16)

전북대학교

전자정보공학부

2.2.1 Binary Trees • Linked representation of binary tree (Figure

2.10)– Three fields (lchild, data, rchild)

A 0

B 0

C 0

D 0

E 00

tree

H 00 I 00

E 00 F 00 G 00D

B C

A

tree

(a)

(b)

전북대학교

전자정보공학부

2.3 Dictionaries • Dictionary

– An abstract data type that supports the operations insert, delete, and search

– Numerous applications– Essential to support the operations as efficiently

as possible– Comparison methods (eg, binary search tree) and

direct access methods (eg, hashing)

전북대학교

전자정보공학부

2.3.1 Binary Search Trees• Definition 2.3 [Binary search tree]

– A binary search tree is a binary tree. It may be empty. If it is not empty, then it satisfies the following properties:• Every element has a key and no two elements

have the same key (distinctness)• The keys (if any) in the left subtree are smaller

than the key in the root• The keys (if any) in the right subtree are larger

than the key in the root• The left and right subtrees are also binary

search trees• Three operations (insert, delete, and search)

– Search by key or by rank

전북대학교

전자정보공학부

2.3.1 Binary Search Trees• Example binary trees (Figure 2.11)

– Which are the binary search trees ?

(a)

(b)

(c)

2515

20

12

405

30

2

70

60

806510 22

전북대학교

전자정보공학부

2.3.1 Binary Search Trees• Class definition for binary search trees

(Program 2.6)class TreeNode{ friend class BSTree; private: TreeNode *lchild, *rchild; Type data; };

class BSTree { private: TreeNode* tree; TreeNode* Search(TreeNode* t, Type x);

전북대학교

전자정보공학부

public: BSTree() {tree=NULL;} ~BSTree() {delete tree;} TreeNode* Search(Type x); // Recursively search for x. TreeNode* ISearch(Type x); // Iteratively search for x. void Insert(Type x); // Insert x. void Delete(Type x); // Delete x. };

전북대학교

전자정보공학부

2.3.1 Binary Search Trees• Search operation

– Recursive search (Program 2.7)

#define NULL 0

TreeNode* BSTree::Search(Type x) { return Search(tree, x); }

TreeNode* BSTree::Search(TreeNode* t, Type x) { if (t == NULL) return 0; else if (x == t->data) return t; else if (x<t->data) return (Search(t->lchild,x)); else return (Search(t->rchild, x)); }

전북대학교

전자정보공학부

2.3.1 Binary Search Trees• Search operation

– Iterative search (Program 2.8) #define NULL 0

TreeNode* BSTree::ISearch(Type x) { bool found = false; TreeNode* t = tree; while ((t) && (!found)) { if (x == t->data) found = true; else if (x<t->data) t = t->lchild; else t = t->rchild; } if (found) return t; else return NULL; }

전북대학교

전자정보공학부

2.3.1 Binary Search Trees• Insert operation

– Inserting example (Figure 2.12)

– Program (Program 2.10)

#define NULL 0

void BSTree::Insert(Type x) // Insert x into the binary search tree. { bool notfound=true; TreeNode *p = tree, *q;

(a)

(b)

(c)

405

30

2

405

30

2 80

405

30

802 35

전북대학교

전자정보공학부

// Search for x. q is parent of p. while ((p) && (notfound)) { q = p; // Save p. if (x == (p->data)) notfound = false; else if (x < (p->data)) p = (p->lchild); else p = (p->rchild); }

// Perform insertion. if (notfound) { p = new TreeNode; p->lchild = NULL; p->rchild = NULL; p->data = x; if (tree) { if (x < (q->data)) q->lchild = p; else q->rchild = p; } else tree = p; } }

전북대학교

전자정보공학부

2.3.1 Binary Search Trees• Delete operation

– Deleting example (Figure 2.13)

– Program for deletion• Write for yourself !

405

30

2 80

(a)

405

5

2 80

(b)

402

5

80

(c)

전북대학교

전자정보공학부

2.3.1 Binary Search Trees• Height of a binary search tree

– Worst case• Height of a BST with n elements can become as

large as n• The case when inserting the keys [1,2,3,4,…,n]

in this order– Average case

• When the insertions and deletions are made at random

• O(log n)• Balanced search trees

– Search trees with a worst-case height of O(log n)– AVL tree, 2-3 tree, Red-Black tree, and B-tree

전북대학교

전자정보공학부

2.3.1 Binary Search Trees• Summary of dictionary implementations

(Table 2.1)

전북대학교

전자정보공학부

2.4 Priority Queues• Priority queue

– Any data structures supporting the operations of search min (or max), insert, and delete min (or max)

– Examples• Example 2.2: selling the services of a machine• Example 2.3: simulating a large factory

• Representation of priority queue– The simplest way using an unordered linear list

• Insert: (1)• Delete: (n)

– Use of ordered linear list• Insert: (n)• Delete: (1)

– Use of max heap• Insert: O(log n)• Delete: O(log n)

전북대학교

전자정보공학부

2.4.1 Heaps• Definition 2.4 [Heap]

– A max (min) heap is a complete binary tree with the property that the value at each node is at least as large as (as small as) the values at its children (if they exist). Call this property the heap property.

• The root has the largest element.• Class definition of a max heap (Program

2.11) class Heap { private: Type *array; int MaxSize, Nel; // Max. size of the heap, no. of elements void Adjust(Type a[], int i, int n); public: Heap(int MSize): MaxSize(MSize) { array = new Type[MaxSize+1]; Nel=0; }

전북대학교

전자정보공학부

2.4.1 Heaps

• Example of insert (Figure 2.14)

~Heap() { delete []array; } bool Insert(Type item); // Insert item. bool DelMax(Type& item); // Delete the maximum. };

9045

80

3540 7050

7045

80

3540 9050

8045

90

3540 7050

전북대학교

전자정보공학부

2.4.1 Heaps• Program for insert (Program 2.12)

• Analysis– Best case: (1)– Worst case: (log n)

bool Heap::Insert(Type item) { // Inserts item. int i = ++Nel; if (i==MaxSize) { cout << "heap size exceeded" << endl; return false; } while ((i>1) && (array[i/2]<item)) { array[i] = array[i/2]; i /= 2; } array[i] = item; return true; }

전북대학교

전자정보공학부

2.4.1 Heaps• Program for delete (Program 2.13)

void Heap::Adjust(Type a[], int i, int n) // The complete binary trees with roots 2*i and 2*i+1 are // combined with node i to form a heap rooted at i. No // node has an address greater than n or less than 1. { int j = 2*i, item = a[i]; while (j <= n) { if ((j<n) && (a[j]<a[j+1])) j++; // Compare left and right child // and let j be the larger child. if (item >= a[j]) break; // A position for item is found. a[j/2] = a[j]; j *= 2; } a[j/2] = item; }

전북대학교

전자정보공학부

2.4.1 Heaps

• Analysis– Best case: (1)– Worst case: (log n)

bool Heap::DelMax(Type& item) { if (!Nel) { cout << "heap is empty" << endl; return false; } item=array[1]; array[1]=array[Nel--]; Adjust(array, 1, Nel); return true; }

전북대학교

전자정보공학부

2.4.1 Heaps• Heap sorting algorithm (Program on page 93)

– To sort n elements, it suffices to make n insertions followed by n deletions from a heap.

• Analysis– Since insertion and deletion take O(log n) time each in

the worst case, the heap sorting algorithm has a time complexity of O(n log n)

void Sort(Type a[], int n)//Sort the elements a[1:n].{ Heap heap(SIZE); for (int i=1; i<=n; i++) heap.Insert(a[i]); Type x; for (i=1; i<=n; i++) { heap.DelMax(x); a[n-i+1] = x; }}

전북대학교

전자정보공학부

2.4.1 Heaps• Insert n elements using the program Sort

– Example• Forming a heap from {40, 80, 35, 90, 45, 50,

70} (Figure 2.15)

(a)

4040

80

80

40

80

40 35(b)

(e)

(c)

80

40 35

(d)

90

90

80 35

40

90

80 35

40 45

전북대학교

전자정보공학부

(g)

90

80

40 45

35

50

90

80

40 45

50

35 70

90

80

40 45

70

35 50

90

80

40 45

50

35(f)

전북대학교

전자정보공학부

2.4.1 Heaps• Insert n elements using the program Sort

– Worst-case analysis• Worst-case happens when the list is in

ascending order• O(n log n)

– Average case• O(n)• On the average, each new value only rises a

constant number of levels (why?)

전북대학교

전자정보공학부

2.4.1 Heaps• Can we insert n elements faster?

– Yes!– Using programs Heapify (Programs 2.14)

void Heapify(Type a[], int n) // Readjust the elements in a[1:n] to form a heap. { for (int i=n/2; i; i--) AdjustH(a, i, n); }

전북대학교

전자정보공학부

• Can we insert n elements faster? (Continued)– Example of Heapify (Figure 2.16)

– Worst-case analysis (Formula 2.1)• O(n)

– However, requiring that all the elements be available before heap creation begins

2.4.1 Heaps

(a)

118119100

112171 132151

151119100

112171 132118

151171100

112119 132118

151119100

112100 132118

(b)

(c)

(d)

전북대학교

전자정보공학부

2.4.2 Heapsort• Program Heapsort (Program 2.15)

– Worst-case analysis• O(n log n)

– Storage requirement• A few extra variables

void HeapSort(Type a[], int n) // a[1:n] contains n elements to be sorted. HeapSort // rearranges them in-place into nondecreasing order. { Heapify(a, n); // Transform the array into a heap. // Interchange the new maximum with the element // at the end of the array. for (int i=n; i>=2; i--) { Type t = a[i]; a[i] = a[1]; a[1]=t; AdjustH(a, 1, i-1); } }

전북대학교

전자정보공학부

2.5 Graphs • Introduction

– Konigsberg bridge problem (Figure 2.24)

– Applications: analysis of electric circuits, finding shortest routes, project planning, identification of chemical compounds, ….

전북대학교

전자정보공학부

2.5.2 Definitions • A graph G

– V and E– Terminologies

• Vertices, edges, directed graph, undirected graph, complete graph, adjacent and incident, subgraph, path, length, simple path, cycle, connected, connected component, strongly connected, strongly connected component, in-degree and out-degree

– Examples (Figure 2.25)1

4

32 32

1

54 76

1

3

2

(c) G1 (c) G2(c) G3

전북대학교

전자정보공학부

2.5.2 Graph Representation • Adjacency matrix (Figure 2.30)

전북대학교

전자정보공학부

• Adjacency list (Figure 2.31)

2.5.2 Graph Representation

4 2 3 03 4 1 02 4 1 01 2 3 0

[1][2][3][4]

23 1

[1][2][3]

00

0

head nodes

head nodes

vertex link

(a) G1

(b) G3

전북대학교

전자정보공학부

3 24 11 42 3

[1][2][3][4][5][6][7][8]

67

8 0

0

head nodes

(c) G4

67 5 0

0000

0

전북대학교

전자정보공학부

2.5.2 Graph Representation • Class definition of adjacency list (Program

on page 120)Class Graph{ private: int n; //Number of vertices struct node { int vertex; struct node* link; }; struct node* headnodes[n+1]; public: Graph() { for (int i=1; i<=n; i++) headnodes[i] = NULL; } }