Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 2.1 Stacks and Queues Ordered list (or linear list) –a = (a 1, a 2, …, a n ) where a i 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)

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

4 2.1 Stacks and Queues Representation of stack –Using one-dimensional array –Class definition of stack (Program 2.1) #include template 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()) {

5 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; } };

6 Representation of stack –Using linked list (Figure 2.2) –Linked representation of stack (Program 2.2) 2.1 Stacks and Queues stack data link EDCBA 0 #define NULL 0 #include class Stack { private: struct node { Type data; struct node *link; }; struct node *top;

7 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; } };

8 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; }

9 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; }

10 2.1 Stacks and Queues Representation of queue –Using one-dimensional array (Figure 2.3)

11 2.1 Stacks and Queues Representation of queue –A class definition of queue (Programs 2.3) #define NULL 0 #include 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(); };

12 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 n  0 disjoint sets T 1, …,T n, where each of these sets is a tree. The sets T 1, …,T n 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

13 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 L K H J I M level 1 2 3 4 A0 BF0CG0DIJ0 EKL0 HM0

14 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) C B A G D FE H I B A C D E level 1 2 3 4 5

15 2.2.1 Binary Trees Lemma 2.1 –Maximum number of nodes on level i of a binary tree = 2 i-1 –Maximum number of nodes in a binary tree of depth k = 2 k -1, k>0 Sequential representation of binary tree (Figure 2.9) Lemma 2.2 –parent(i) is at floor(i/2) if i  1 –lchild(i) is at 2i if 2i  n –rchild(i) is at 2i+1 if 2i+1  n tree A B- C- - -D - GH I DE F AB C 0 0 0 E (1) (2) (3) (4) (5) (6) (7) (8) (9) (16)

16 2.2.1 Binary Trees Linked representation of binary tree (Figure 2.10) –Three fields (lchild, data, rchild) A0 B0 C0 D0 E00 tree H00I0 0 E00F00 G00 D B C A (a)(b)

17 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)

18 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

19 2.3.1 Binary Search Trees Example binary trees (Figure 2.11) –Which are the binary search trees ? (a) (b) (c) 25 15 20 12 40 5 30 2 70 60 80 65 10 22

20 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);

21 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. };

22 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 data) return (Search(t->lchild,x)); else return (Search(t->rchild, x)); }

23 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 data) t = t->lchild; else t = t->rchild; } if (found) return t; else return NULL; }

24 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) 40 5 30 2 40 5 30 280 40 5 30 802 35

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

26 2.3.1 Binary Search Trees Delete operation –Deleting example (Figure 2.13) –Program for deletion Write for yourself ! 40 5 30 280 (a) 40 5 5 280 (b) 40 2 5 80 (c)

27 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

28 2.3.1 Binary Search Trees Summary of dictionary implementations (Table 2.1)

29 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)

30 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; }

31 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. }; 90 45 80 3540 70 50 70 45 80 3540 90 50 80 45 90 3540 70 50

32 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; }

33 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; }

34 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; }

35 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; }

36 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) 40 80 40 80 40 35 (b) (e) (c) 80 40 35 (d) 90 80 35 40 90 80 35 40 45

37 (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)

38 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?)

39 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); }

40 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) 118 119 100 112171 132 151 119 100 112171 132 118 151 171 100 112119 132 118 151 119 100 112100 132 118 (b) (c) (d)

41 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); }

42 2.5 Graphs Introduction –Konigsberg bridge problem (Figure 2.24) –Applications: analysis of electric circuits, finding shortest routes, project planning, identification of chemical compounds, ….

43 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 3 2 3 2 1 54 7 6 1 3 2 (c) G 1 (c) G 2 (c) G 3

44 2.5.2 Graph Representation Adjacency matrix (Figure 2.30)

45 Adjacency list (Figure 2.31) 2.5.2 Graph Representation 42 30 34 10 24 10 12 30 [1] [2] [3] [4] 2 31 [1] [2] [3] 0 0 0 head nodes vertex link (a) G 1 (b) G 3

46 32 41 14 23 [1] [2] [3] [4] [5] [6] [7] [8] 6 7 80 0 head nodes (c) G 4 6 75 0 0 0 0 0 0

47 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; } }


Download ppt "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."

Similar presentations


Ads by Google