Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE233 Course Review CSE, POSTECH. What we studied in the 1 st half Week 1 : Overview of C++, Program performance Week 2 : Array-based and linked representations.

Similar presentations


Presentation on theme: "CSE233 Course Review CSE, POSTECH. What we studied in the 1 st half Week 1 : Overview of C++, Program performance Week 2 : Array-based and linked representations."— Presentation transcript:

1 CSE233 Course Review CSE, POSTECH

2 What we studied in the 1 st half Week 1 : Overview of C++, Program performance Week 2 : Array-based and linked representations of Lists Week 3 : Arrays and Matrices Week 4 : Performance Measurement Week 5 : Stacks Week 6 : Queues Week 7 : Skip lists and Hashing Week 8 : Review and Midterm Exam

3 What we studied in the 2 nd half Week 9 : Binary and other trees Week 10 : Priority queues, Heaps, Leftist trees Week 11 : Tournament trees and Bin packing Week 12 : Binary search trees Week 13 : AVL trees Week 14 : Graphs Week 15 : Graph Search Methods Week 16 : Review and Final exam

4 Trees

5 Tree Terminology A tree t is a finite nonempty set of elements The element at the top is called the root The remaining elements, if any, are partitioned into trees, which are called the subtrees of t. Elements next in the hierarchy are the children of the root. Elements next in the hierarchy are the grandchildren of the root, and so on. Elements at the lowest level of the hierarchy are the leaves.

6 Leaves, Parent, Grandparent, Siblings, Ancestors, Descendents Leaves = {Mike,AI,Sue,Chris} Parent(Mary) = Joe Grandparent(Sue) = Mary Siblings(Mary) = {Ann,John} Ancestors(Mike) = {Ann,Joe} Descendents(Mary)={Mark,Sue}

7 Levels and Height Root is at level 1 and its children are at level 2. Height = depth = number of levels level 1level 2level 3level 4

8 Node Degree Node degree is the number of children it has

9 Tree Degree Tree degree is the maximum of node degrees tree degree = 3

10 Binary Trees

11 Binary Tree A finite (possibly empty) collection of elements A nonempty binary tree has a root element and the remaining elements (if any) are partitioned into two binary trees They are called the left and right subtrees of the binary tree

12 Difference Between a Tree & a Binary Tree A binary tree may be empty; a tree cannot be empty. No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. The subtrees of a binary tree are ordered; those of a tree are not ordered. a bc a cb - different when viewed as a binary tree - same when viewed as a tree

13 Binary Tree for Mathematical Expressions Figure 11.5 Expression Trees

14 Full Binary Tree A full binary tree of height h has exactly 2 h -1 nodes. Numbering the nodes in a full binary tree –Number the nodes 1 through 2 h -1 –Number by levels from top to bottom –Within a level, number from left to right

15 Complete Binary Tree with N Nodes Start with a full binary tree that has at least n nodes Number the nodes as described earlier. The binary tree defined by the nodes numbered 1 through n is the n-node complete binary tree. A full binary tree is a special case of a complete binary tree

16 Example of Complete Binary Tree Complete binary tree with 10 nodes.

17 Incomplete Binary Trees Complete binary tree with some missing elements Fig. 11.8 Incomplete binary trees

18 Binary Tree Representation Array representation Linked representation

19 Array Representation of Binary Tree The binary tree is represented in an array by storing each element at the array position corresponding to the number assigned to it.

20 Right-Skewed Binary Tree An n node binary tree needs an array whose length is between n+1 and 2 n. Right-skewed binary tree wastes the most space What about left-skewed binary tree?

21 Linked Representation of Binary Tree The most popular way to present a binary tree Each element is represented by a node that has two link fields (leftChild and rightChild) and an element field

22 Priority Queues

23 A priority queue is a collection of zero or more elements  each element has a priority or value Unlike the FIFO queues, the order of deletion from a priority queue (e.g., who gets served next) is determined by the element priority Elements are deleted by increasing or decreasing order of priority rather than by the order in which they arrived in the queue

24 Priority Queues Operations performed on priority queues 1) Find an element, 2) insert a new element, 3) delete an element, etc. In a Min priority queue, find/delete operation finds/deletes the element with minimum priority In a Max priority queue, find/delete operation finds/deletes the element with maximum priority Two or more elements can have the same priority

25 Implementation of Priority Queues Implemented using heaps and leftist trees Heap is a complete binary tree that is efficiently stored using the array-based representation Leftist tree is a linked data structure suitable for the implementation of a priority queue

26 Min (Max) Trees

27 Max (Min) Tree A max tree (min tree) is a tree in which the value in each node is greater (less) than or equal to those in its children (if any) – Nodes of a max or min tree may have more than two children (i.e., may not be binary tree)

28 Max Tree Example

29 Min Tree Example

30 Heaps

31 Heaps - Definitions A max heap (min heap) is a max (min) tree that is also a complete binary tree

32 Max Heap with 9 Nodes

33 Min Heap with 9 Nodes

34 Array Representation of Heap A heap is efficiently represented as an array.

35 9 8 6 7 726 5120 New element is 20 Are we finished? Insertion into a Max Heap

36 9 8 6 7 26 517 20 Exchange the positions with 7 Are we finished? Insertion into a Max Heap

37 9 6 7 26 517 8 20 Insertion into a Max Heap Exchange the positions with 8 Are we finished?

38 6 7 26 517 8 9 20 Insertion into a Max Heap Exchange the positions with 9 Are we finished?

39 Deletion from a Max Heap Max element is in the root What happens when we delete an element? 20 6 7 26 517 15 8 9

40 Deletion from a Max Heap After the max element is removed. Are we finished? 6 7 26 517 15 8 9

41 Deletion from a Max Heap Heap with 10 nodes. Reinsert 8 into the heap. 6 7 26 517 15 8 9

42 Deletion from a Max Heap Reinsert 8 into the heap. Are we finished? 6 7 26 517 15 9 8

43 Deletion from a Max Heap Exchange the position with 15 Are we finished? 6 7 26 517 9 15 8

44 Deletion from a Max Heap 6 7 26 517 8 15 9 Exchange the position with 9 Are we finished?

45 Max Heap Initialization Heap initialization means to construct a heap by adjusting the tree if necessary Example: input array = [-,1,2,3,4,5,6,7,8,9,10,11]

46 Max Heap Initialization - Start at rightmost array position that has a child.

47 Max Heap Initialization

48

49

50

51 Are we finished? Done!

52 Exercise 12.7 Do Exercise 12.7 – theHeap = [-, 10, 2, 7, 6, 5, 9, 12, 35, 22, 15, 1, 3, 4] 12.7 (a) – complete binary tree

53 Exercise 12.7 (b) 12.7 (b) – The heapified tree

54 Exercise 12.7 (c) 12.7 (c) – The heap after 15 is inserted is:

55 Exercise 12.7 (c) 12.7 (c) – The heap after 20 is inserted is:

56 Exercise 12.7 (c) 12.7 (c) – The heap after 45 is inserted is:

57 Exercise 12.7 (d) 12.7 (d) – The heap after the first remove max operation is:

58 Exercise 12.7 (d) 12.7 (d) – The heap after the second remove max operation is:

59 Exercise 12.7 (d) 12.7 (d) – The heap after the third remove max operation is:

60 Leftist Trees

61 Leftist tree is a tree which tends to lean to the left Leftist tree structures are useful for applications – to meld (i.e., combine) pairs of priority queues – using multiple queues of varying size External node – a special node that replaces each empty subtree Internal node – a node with non-empty subtrees Extended binary tree – a binary tree with external nodes added

62 Height-Biased Leftist Tree (HBLT) Let s(x) be the length (height) of a shortest path from node x to an external node in its subtree If x is an external node, s(x) = 0 If x is an internal node, s(x) = min {s(L), s(R)} + 1, where L and R are left and right children of x A binary tree is a height-biased leftist tree (HBLT) iff at every internal node, the s value of the left child is greater than or equal to the s value of the right child A max HBLT is an HBLT that is also a max tree A min HBLT is an HBLT that is also a min tree

63 Weight-Biased Leftist Tree (WBLT) Let the weight, w(x), of node x to be the number of internal nodes in the subtree with root x If x is an external node, w(x) = 0 If x is an internal node, its weight is one more than the sum of the weights of its children A binary tree is a weight-biased leftist tree (WBLT) iff at every internal node, the w value of the left child is greater than or equal to the w value of the right child A max (min) WBLT is a max (min) tree that is also a WBLT

64 Extended Binary Tree Figure 12.6 s and w values

65 Melding max HBLTs Figure 12.7 Melding (combining) max HBLTs

66 Applications of Heaps Sort (heap sort) Machine scheduling Huffman codes

67 Heap Sort use element key as priority Algorithm put elements to be sorted into a priority queue (i.e., initialize a heap) extract (delete) elements from the priority queue – if a min priority queue is used, elements are extracted in non-decreasing order of priority – if a max priority queue is used, elements are extracted in non-increasing order of priority

68 Machine Scheduling Problem m identical machines n jobs/tasks to be performed The machine scheduling problem is to assign jobs to machines so that the time at which the last job completes is minimum

69 The class of problems for which no one has developed a polynomial time algorithm. No algorithm whose complexity is O(n k m l ) is known for any NP-hard problem (for any constants k and l) NP stands for Nondeterministic Polynomial NP-hard problems are often solved by heuristics (or approximation algorithms), which do not guarantee optimal solutions Longest Processing Time (LPT) rule is a good heuristic for minimum finish time scheduling. NP-hard Problems

70 Huffman Codes For text compression, the LZW method relies on the recurrence of substrings in a text Huffman codes is another text compression method, which relies on the relative frequency (i.e., the number of occurrences of a symbol) with which different symbols appear in a text Uses extended binary trees Variable-length codes that satisfy the property, where no code is a prefix of another Huffman tree is a binary tree with minimum weighted external path length for a given set of frequencies (weights)

71 Tournament Trees

72 Like the heap, a tournament tree is a complete binary tree that is most efficiently stored using array-based binary tree Used to obtain efficient implementations of two approximation algorithms for the bin packing problem (another NP-hard problem) Types of tournament trees: winner & loser trees The tournament is played in the sudden-death mode Tournament trees are also called selection trees

73 Winner Trees A winner tree for n players is a complete binary tree with n external nodes and n-1 internal nodes. Each internal node records the winner of the match. To determine the winner of a match, we assume that each player has a value In a min (max) winner tree, the player with the smaller (larger) value wins

74 Loser Trees A loser tree for n players is also a complete binary tree with n external nodes and n-1 internal nodes. Each internal node records the loser of the match. The overall winner is recorded in tree[0] Figure 13.5 Eight-player min loser trees

75 Bin Packing Problems

76 Bin Packing Problem We have bins that have a capacity binCapacity and n objects that need to be packed into these bins Object i requires objSize[i], where 0 < objSize[i]  binCapacity, units of capacity Feasible packing - an assignment of objects to bins so that no bin’s capacity is exceeded Optimal packing - a feasible packing that uses the fewest number of bins Goal: pack objects with the minimum number of bins The bin packing problem is an NP-hard problem

77 Truck Loading Problem Have parcels to pack into trucks Each parcel has a weight Each truck has a load limit Goal: Minimize the number of trucks needed Equivalent to the bin packing problem

78 Bin Packing Approximation Algorithms First Fit (FF) First Fit Decreasing (FFD) Best Fit (BF) Best Fit Decreasing (BFD)

79 First Fit (FF) Bin Packing Bins are arranged in left to right order. Objects are packed one at a time in a given order. Current object is packed into the leftmost bin into which it fits. If there is no bin into which current object fits, start a new bin.

80 Best Fit (BF) Bin Packing Let bin[j].unusedCapacity denote the capacity available in bin j Initially, the available capacity is binCapacity for all bins Object i is packed into the bin with the least unusedCapacity that is at least objSize[i] If there is no bin into which current object fits, start a new bin.

81 First Fit Decreasing (FFD) Bin Packing This method is the same as FF except that the objects reordered in a decreasing size so that objSize[i]  objSize[i+1], 1  i < n

82 Best Fit Decreasing (BFD) Bin Packing This method is the same as BF except that the objects are reordered as for FFD

83 Binary Search Trees

84 Binary Search Tree A binary tree that may be empty. A nonempty binary search tree satisfies the following properties: 1. Each node has a key (or value), and no two nodes have the same key (i.e., all keys are distinct). 2. For every node x, all keys in the left subtree of x are smaller than that in x. 3. For every node x, all keys in the right subtree of x are larger than that in x. 4. The left and right subtrees of the root are also binary search trees

85 Examples of Binary Trees Which of the above trees are binary search trees?  (b) and (c) Why isn’t (a) a binary search tree?  It violates the property #3 Figure 14.1 Binary Trees

86 Indexed Binary Search Trees Binary search tree. Each node has an additional field ‘LeftSize’. Support search and delete operations by rank as well as all the binary search tree operations. LeftSize – the number of elements in its left subtree – the rank of an element with respect to the elements in its subtree (e.g., the fourth element in sorted order)

87 Indexed Binary Search Tree Example LeftSize values are in red. 20 4010 6 28 1530 2535 7 18 7 4 1 0 0 1 0 3 1 000

88 The Operation Ascend() How can we output all elements in ascending order of keys? Do an inorder traversal (left, root, right). What would be the output? 2, 6, 8, 10, 15, 20, 25, 30, 40 20 4010 6 28 1530 25

89 The Operation Search(key, e) Search begins at the root If the root is NULL, the search tree is empty and the search fails. If key is less than the root, then left subtree is searched If key is greater than the root, then right subtree is searched If key equals the root, then the search terminates successfully The time complexity for search is O(height) See Program 11.4 for the search operation code

90 The Operation Insert(key, e) To insert a new element e into a binary search tree, we must first verify that its key does not already exist by performing a search in the tree If the search is successful, we do not insert If the search is unsuccessful, then the element is inserted at the point the search terminated – Why insert it at that point? The time complexity for insert is O(height) See Figure 14.3 for examples See Program 14.5 for the insert operation code

91 Insert Example We wish to insert an element with the key 35. Where should it be inserted? 20 4010 6 28 1530 25 35

92 Insert Example Insert an element with the key 7. 20 4010 6 28 1530 2535 7

93 Insert Example Insert an element with the key 18. 20 4010 6 28 1530 2535 7 18

94 The Operation Delete(key, e) For deletion, there are three cases for the element to be deleted: 1. Element is in a leaf. 2. Element is in a degree 1 node (i.e., has exactly one nonempty subtree). 3. Element is in a degree 2 node (i.e., has exactly two nonempty subtrees).

95 Case 1: Delete from a Leaf For case 1, we can simply discard the leaf node. Example, delete a leaf element. key=7 20 4010 6 28 1530 2535 7 18

96 Case 2: Delete from a Degree 1 Node Example: Delete key=40 20 4010 6 28 1530 2518

97 Case 3: Delete from a Degree 2 Node 20 4010 6 28 1530 2535 7 18 Example: Delete key=10

98 Replace with the largest key in the left subtree (or the smallest in the right subtree) Which node is the largest key in the left subtree? 20 4010 6 28 1530 2535 7 18 Case 3: Delete from a Degree 2 Node

99 20 408 6 28 1530 2535 7 18 The largest key must be in a leaf or degree 1 node. Case 3: Delete from a Degree 2 Node

100 AVL Trees

101 Balanced Binary Search Trees Trees with a worst-case height of O(log n) are called balanced trees An example of a balanced tree is AVL (Adelson- Velsky and Landis) tree

102 AVL Tree Definition Binary tree. If T is a nonempty binary tree with T L and T R as its left and right subtrees, then T is an AVL tree iff 1. T L and T R are AVL trees, and 2. |h L – h R |  1 where h L and h R are the heights of T L and T R, respectively

103 AVL Search Trees An AVL search tree is a binary search tree that is also an AVL tree

104 Indexed AVL Search Trees An indexed AVL search tree is an indexed binary search tree that is also an AVL tree

105 Balance Factor To facilitate insertion and deletion, a balance factor (bf) is associated with each node The balance factor bf(x) of a node x is defined as height(x  leftChild) – height(x  rightChild) Balance factor of each node in an AVL tree must be –1, 0, or 1

106 AVL Tree with Balance Factors Is this an AVL tree? What is the balance factor for each node in this AVL tree? Is this an AVL search tree? 1 0 00 0 1 1 0 0 0 10 40 3045 2035 25 60 7 38 15

107 Inserting into an AVL Search Trees If we use the strategy of Program 14.5 to insert an element into an AVL search tree, the result may not be an AVL tree That is, the tree may become unbalanced If the tree becomes unbalanced, we must adjust the tree to restore balance - this adjustment is called rotation

108 Inserting into an AVL Search Tree 9 0 Insert(9) 1 0 00 0 1 1 0 0 0 10 40 3045 2035 25 60 7 38 15 Where is 9 going to be inserted into? After the insertion, is the tree still an AVL search tree? (i.e., still balanced?)

109 Imbalance Types After an insertion, when the balance factor of node A is –2 or 2, the node A is one of the following four imbalance types 1. LL: new node is in the left subtree of the left subtree of A 2. LR: new node is in the right subtree of the left subtree of A 3. RR: new node is in the right subtree of the right subtree of A 4. RL: new node is in the left subtree of the right subtree of A

110 Rotation Definition To switch children and parents among two or three adjacent nodes to restore balance of a tree. A rotation may change the depth of some nodes, but does not change their relative ordering.

111 Left Rotation Definition In a binary search tree, pushing a node A down and to the left to balance the tree. A's right child replaces A, and the right child's left child becomes A's right child. Left Rotation 15 229 124 9 415 1222 A

112 Right Rotation Definition In a binary search tree, pushing a node A down and to the right to balance the tree. A's left child replaces A, and the left child's right child becomes A's left child. 9 415 1222 Right Rotation 15 229 124 A

113 AVL Rotations To balance an unbalanced AVL tree (after an insertion), we may need to perform one of the following rotations: LL, RR, LR, RL Figure 15.3 Inserting into an AVL search tree

114 An LL Rotation Figure 15.4 An LL Rotation

115 An LR Rotation Figure 15.5 An LR Rotation

116 Single and Double Rotations Single rotations: the transformations done to correct LL and RR imbalances Double rotations: the transformations done to correct LR and RL imbalances The transformation to correct LR imbalance can be achieved by an RR rotation followed by an LL rotation The transformation to correct RL imbalance can be achieved by an LL rotation followed by an RR rotation (do Exercise 15.13) See Figure 15.6 for the AVL-search-tree-insertion algorithm

117 Deletion from an AVL Search Tree To delete an element from an AVL search tree, we can use Program 14.6 Deletion of a node may also produce an imbalance Imbalance incurred by deletion is classified into the types R0, R1, R-1, L0, L1, and L-1 Rotation is also needed for rebalancing

118 An R0 Rotation Figure 15.7 An R0 rotation (single rotation)

119 An R1 Rotation Figure 15.8 An R1 rotation (single rotation)

120 An R-1 Rotation Figure 15.9 An R-1 rotation (double rotation)

121 Graphs

122 Topics related to Graphs Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected, directed, weighted Graph representations: adjacency matrix, array adjacency lists, linked adjacency lists Graph search methods: breath-first, depth-first search Algorithms: – to find a path in a graph – to find the connected components of an undirected graph – to find a spanning tree of a connected undirected graph

123 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two vertices. Edges are also called arcs and lines. Vertices i and j are adjacent vertices iff (i, j) is an edge in the graph The edge (i, j) is incident on the vertices i and j

124 Graphs Undirected edge has no orientation (no arrow head) Directed edge has an orientation (has an arrow head) Undirected graph – all edges are undirected Directed graph – all edges are directed u v directed edge u v undirected edge

125 Path and Simple Path A sequence of vertices P = i 1, i 2, …, i k is an i 1 to i k path in the graph G=(V, E) iff the edge (i j, i j+1 ) is in E for every j, 1≤ j < k A simple path is a path in which all vertices, except possibly in the first and last, are different

126 Length (Cost) of a Path Each edge in a graph may have an associated length (or cost). The length of a path is the sum of the lengths of the edges on the path What is the length of the path 5, 9, 11, 10?

127 Subgraph & Cycle Let G = (V, E) be an undirected graph A graph H is a subgraph of graph G iff its vertex and edge sets are subsets of those of G A cycle is a simple path with the same start and end vertex

128 Spanning Tree Let G = (V, E) be an undirected graph A connected undirected graph that contains no cycles is a tree A subgraph of G that contains all the vertices of G and is a tree is a spanning tree A spanning tree has n vertices and n-1 edges The spanning tree that costs the least is called the minimum-cost spanning tree

129 Bipartite Graph A bipartite graph is a special graph where the set of vertices can be divided into two disjoint sets U and V such that no edge has both end-points in the same set. A simple undirected graph G = (V, E) is called bipartite if there exists a partition of the vertex set V = V 1 U V 2 so that both V 1 and V 2 are independent sets.

130 Graph Properties The degree of vertex i is the no. of edges incident on vertex i. The sum of vertex degrees = 2e (where e is the number of edges) In-degree of vertex i is the number of edges incident to i Out-degree of vertex i is the number of edges incident from i

131 Complete Undirected/Directed Graphs A complete undirected graph has n(n-1)/2 edges (i.e., all possible edges) and is denoted by K n A complete directed graph (also denoted by K n ) on n vertices contains exactly n(n-1) edges

132 Path Finding Path between 1 and 8 What is a possible path & its length? A path is 1, 2, 5, 9, 8 and its length is 20.

133 Connected Graph Let G = (V, E) be an undirected graph G is connected iff there is a path between every pair of vertices in G

134 Representation of Unweighted Graphs The most frequently used representations for unweighted graphs are – Adjacency Matrix – Linked adjacency lists – Array adjacency lists

135 Adjacency Matrix 0/1 n x n matrix, where n = # of vertices A(i, j) = 1 iff (i, j) is an edge.

136 Adjacency Matrix Properties Diagonal entries are zero. Adjacency matrix of an undirected graph is symmetric (A(i,j) = A(j,i) for all i and j).

137 Adjacency Matrix for Digraph Diagonal entries are zero. Adjacency matrix of a digraph need not be symmetric. See Figure 16.9 for more adjacency matrices

138 Adjacency Lists Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. An array of n adjacency lists for each vertex of the graph.

139 Linked Adjacency Lists Each adjacency list is a chain. Array length = n. # of chain nodes = 2e (undirected graph) # of chain nodes = e (digraph) See Figure 16.11 for more linked adjacency lists

140 Array Adjacency Lists Each adjacency list is an array list. Array length = n. # of chain nodes = 2e (undirected graph) # of chain nodes = e (digraph) See Figure 16.12 for more array adjacency lists

141 Representation of Weighted Graphs Weighted graphs are represented with simple extensions of those used for unweighted graphs The cost-adjacency-matrix representation uses a matrix C just like the adjacency-matrix representation does Cost-adjacency matrix: C(i, j) = cost of edge (i, j) Adjacency lists: each list element is a pair (adjacent vertex, edge weight)

142 Graph Search Methods

143 Many graph problems solved by a search method – Finding a path from one vertex to another. – Determining whether a graph is connected – Find a spanning tree – Finding a minimum-cost path/spanning tree Breadth-first search (BFS) – Method of starting at a vertex and identifying all vertices reachable from it – Similar to the level-order traversal of a binary tree Depth-first search (DFS) – an alternative to BFS – Similar to the pre-order traversal of a binary tree

144 Tips on the Final Exam Make sure you do all the exercises mentioned in the lecture notes Make sure you understand on how to solve the problems in the assignments Good luck!

145 Tips on your LIFE Have a Life Plan – Yearly life plan at the beginning of each year – Plan for 5, 10, 20, 30, 40, 50 years ahead Have a Role Model – Bill Gates, Steve Jobs, Larry Page and Sergey Brin – Alan Turing, Vincent Cerf and Leonard Kleinrock – 안철수, 손정의, 진대제 – 홍원기 ? Have a Wonderful Life!


Download ppt "CSE233 Course Review CSE, POSTECH. What we studied in the 1 st half Week 1 : Overview of C++, Program performance Week 2 : Array-based and linked representations."

Similar presentations


Ads by Google