+ All Categories
Home > Documents > CSE233 Course Review CSE, POSTECH. What we studied in the 1 st half Week 1 : Overview of C++,...

CSE233 Course Review CSE, POSTECH. What we studied in the 1 st half Week 1 : Overview of C++,...

Date post: 04-Jan-2016
Category:
Upload: darren-carter
View: 212 times
Download: 1 times
Share this document with a friend
145
CSE233 Course Review CSE, POSTECH
Transcript
Page 1: 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.

CSE233 Course Review

CSE, POSTECH

Page 2: 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.

What we studied in the 1st 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

Page 3: 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.

What we studied in the 2nd 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

Page 4: 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.

Trees

Page 5: 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.

Tree Terminology A tree t is a finite nonempty set of eleme

nts The element at the top is called the root The remaining elements, if any, are partit

ioned 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.

Page 6: 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.

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}

Page 7: 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.

Levels and Height

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

level 1

level 2

level 3

level 4

Page 8: 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.

Node Degree

Node degree is the number of children it has

Page 9: 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.

Tree Degree

Tree degree is the maximum of node degrees

tree degree = 3

Page 10: 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.

Binary Trees

Page 11: 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.

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

Page 12: 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.

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

b c

a

c b

- different when viewed as a binary tree

- same when viewed as a tree

Page 13: 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.

Binary Tree for Mathematical Expressions

Figure 11.5 Expression Trees

Page 14: 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.

Full Binary Tree

A full binary tree of height h has exactly 2h-1 nodes. Numbering the nodes in a full binary tree

– Number the nodes 1 through 2h-1– Number by levels from top to bottom– Within a level, number from left to right

Page 15: 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.

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

Page 16: 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.

Example of Complete Binary Tree

Complete binary tree with 10 nodes.

Page 17: 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.

Incomplete Binary Trees

Complete binary tree with some missing elements

Fig. 11.8 Incomplete binary trees

Page 18: 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.

Binary Tree Representation

Array representation Linked representation

Page 19: 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.

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.

Page 20: 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.

Right-Skewed Binary Tree

An n node binary tree needs an array whose length is between n+1 and 2n.

Right-skewed binary tree wastes the most space What about left-skewed binary tree?

Page 21: 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.

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

Page 22: 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.

Priority Queues

Page 23: 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.

Priority Queues

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

Page 24: 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.

Priority Queues

Operations performed on priority queues1) 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

Page 25: 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.

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

Page 26: 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.

Min (Max) Trees

Page 27: 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.

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)

Page 28: 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.

Max Tree Example

Page 29: 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.

Min Tree Example

Page 30: 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.

Heaps

Page 31: 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.

Heaps - Definitions

A max heap (min heap) is a max (min) tree that is also a complete binary tree

Page 32: 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.

Max Heap with 9 Nodes

Page 33: 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.

Min Heap with 9 Nodes

Page 34: 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.

Array Representation of Heap

A heap is efficiently represented as an array.

Page 35: 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.

9

8

6

7

7 2 6

5 1 20

• New element is 20• Are we finished?

Insertion into a Max Heap

Page 36: 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.

9

8

6

7

2 6

5 1 7

20

• Exchange the positions with 7• Are we finished?

Insertion into a Max Heap

Page 37: 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.

9

6

7

2 6

5 1 7

8

20

Insertion into a Max Heap

• Exchange the positions with 8• Are we finished?

Page 38: 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.

6

7

2 6

5 1 7

8

9

20

Insertion into a Max Heap

• Exchange the positions with 9• Are we finished?

Page 39: 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.

Deletion from a Max Heap

• Max element is in the root• What happens when we delete an element?

20

6

7

2 6

5 1 7

15

8

9

Page 40: 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.

Deletion from a Max Heap

• After the max element is removed.• Are we finished?

6

7

2 6

5 1 7

15

8

9

Page 41: 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.

Deletion from a Max Heap

• Heap with 10 nodes. • Reinsert 8 into the heap.

6

7

2 6

5 1 7

15

8

9

Page 42: 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.

Deletion from a Max Heap

• Reinsert 8 into the heap.• Are we finished?

6

7

2 6

5 1 7

15

9

8

Page 43: 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.

Deletion from a Max Heap

• Exchange the position with 15• Are we finished?

6

7

2 6

5 1 7

9

15

8

Page 44: 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.

Deletion from a Max Heap

6

7

2 6

5 1 7

8

15

9

• Exchange the position with 9• Are we finished?

Page 45: 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.

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]

Page 46: 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.

Max Heap Initialization

- Start at rightmost array position that has a child.

Page 47: 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.

Max Heap Initialization

Page 48: 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.

Max Heap Initialization

Page 49: 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.

Max Heap Initialization

Page 50: 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.

Max Heap Initialization

Page 51: 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.

Max Heap Initialization

•Are we finished?•Done!

Page 52: 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.

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

Page 53: 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.

Exercise 12.7 (b)

12.7 (b) – The heapified tree

Page 54: 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.

Exercise 12.7 (c)

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

Page 55: 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.

Exercise 12.7 (c)

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

Page 56: 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.

Exercise 12.7 (c)

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

Page 57: 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.

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

Page 58: 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.

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

Page 59: 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.

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

Page 60: 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.

Leftist Trees

Page 61: 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.

Leftist Trees

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 add

ed

Page 62: 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.

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 eve

ry 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

Page 63: 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.

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 ev

ery 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

Page 64: 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.

Extended Binary Tree

Figure 12.6 s and w values

Page 65: 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.

Melding max HBLTs

Figure 12.7 Melding (combining) max HBLTs

Page 66: 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.

Applications of Heaps

Sort (heap sort) Machine scheduling Huffman codes

Page 67: 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.

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

Page 68: 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.

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

Page 69: 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.

The class of problems for which no one has developed a polynomial time algorithm.

No algorithm whose complexity is O(nk ml) 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 app

roximation algorithms), which do not guarantee optimal solutions

Longest Processing Time (LPT) rule is a good heuristic for minimum finish time scheduling.

NP-hard Problems

Page 70: 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.

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)

Page 71: 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.

Tournament Trees

Page 72: 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.

Tournament Trees

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

Page 73: 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.

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

Page 74: 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.

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

Page 75: 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.

Bin Packing Problems

Page 76: 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.

Bin Packing Problem We have bins that have a capacity binCapacity and n obje

cts that need to be packed into these bins Object i requires objSize[i], where 0 < objSize[i] binCapa

city, 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 n

umber of bins Goal: pack objects with the minimum number of bins The bin packing problem is an NP-hard problem

Page 77: 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.

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

Page 78: 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.

Bin Packing Approximation Algorithms

First Fit (FF) First Fit Decreasing (FFD) Best Fit (BF) Best Fit Decreasing (BFD)

Page 79: 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.

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.

Page 80: 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.

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 unusedCapacit

y that is at least objSize[i] If there is no bin into which current object fits,

start a new bin.

Page 81: 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.

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

Page 82: 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.

Best Fit Decreasing (BFD) Bin Packing

This method is the same as BF except that the objects are reordered as for FFD

Page 83: 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.

Binary Search Trees

Page 84: 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.

Binary Search Tree A binary tree that may be empty. A nonempty binary sear

ch tree satisfies the following properties:1. Each node has a key (or value), and no two nodes have t

he same key (i.e., all keys are distinct).2. For every node x, all keys in the left subtree of x are small

er than that in x.3. For every node x, all keys in the right subtree of x are larg

er than that in x.4. The left and right subtrees of the root are also binary sear

ch trees

Page 85: 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.

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

Page 86: 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.

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)

Page 87: 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.

Indexed Binary Search Tree Example

•LeftSize values are in red.

20

4010

6

2 8

15 30

25 35

7

18

7

4

1

0

0

1

0

3

1

0 0 0

Page 88: 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.

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

2 8

15 30

25

Page 89: 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.

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 searche

d If key equals the root, then the search terminates successf

ully The time complexity for search is O(height) See Program 11.4 for the search operation code

Page 90: 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.

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

Page 91: 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.

Insert Example

We wish to insert an element with the key 35.Where should it be inserted?

20

4010

6

2 8

15 30

25 35

Page 92: 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.

Insert Example

Insert an element with the key 7.

20

4010

6

2 8

15 30

25 35

7

Page 93: 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.

Insert Example

Insert an element with the key 18.

20

4010

6

2 8

15 30

25 35

7

18

Page 94: 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.

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

Page 95: 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.

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

2 8

15 30

25 35

7

18

Page 96: 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.

Case 2: Delete from a Degree 1 Node

•Example: Delete key=40

20

4010

6

2 8

15 30

2518

Page 97: 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.

Case 3: Delete from a Degree 2 Node

20

4010

6

2 8

15 30

25 35

7

18

• Example: Delete key=10

Page 98: 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.

• 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

2 8

15 30

25 35

7

18

Case 3: Delete from a Degree 2 Node

Page 99: 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.

20

408

6

2 8

15 30

25 35

7

18

The largest key must be in a leaf or degree 1 node.

Case 3: Delete from a Degree 2 Node

Page 100: 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.

AVL Trees

Page 101: 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.

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

Page 102: 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.

AVL Tree

Definition Binary tree. If T is a nonempty binary tree with TL and TR as it

s left and right subtrees, then T is an AVL tree iff1. TL and TR are AVL trees, and

2. |hL – hR| 1 where hL and hR are the heights of TL and TR, respectively

Page 103: 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.

AVL Search Trees

An AVL search tree is a binary search tree that is also an AVL tree

Page 104: 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.

Indexed AVL Search Trees

An indexed AVL search tree is an indexed binary search tree that is also an AVL tree

Page 105: 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.

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 asheight(xleftChild) – height(xrightChild)

Balance factor of each node in an AVL tree must be –1, 0, or 1

Page 106: 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.

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

1

0

0 0

0

1

1

-1 0

-1

0

0

10

40

30 45

20 35

25

60

7

3 8

1 5

Page 107: 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.

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

Page 108: 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.

Inserting into an AVL Search Tree

90

Insert(9) -1

1

0

0 0

0

1

1

-1 0

-1

0

0

10

40

30 45

20 35

25

60

7

3 8

1 5

• Where is 9 going to be inserted into?• After the insertion, is the tree still an AVL search tree? (i.e., still balanced?)

Page 109: 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.

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

Page 110: 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.

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.

Page 111: 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.

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

4 15

12 22

A

Page 112: 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.

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

4 15

12 22

Right Rotation

15

229

124

A

Page 113: 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.

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

Page 114: 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.

An LL Rotation

Figure 15.4 An LL Rotation

Page 115: 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.

An LR Rotation

Figure 15.5 An LR Rotation

Page 116: 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.

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

Page 117: 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.

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

Page 118: 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.

An R0 Rotation

Figure 15.7 An R0 rotation (single rotation)

Page 119: 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.

An R1 Rotation

Figure 15.8 An R1 rotation (single rotation)

Page 120: 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.

An R-1 Rotation

Figure 15.9 An R-1 rotation (double rotation)

Page 121: 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.

Graphs

Page 122: 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.

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

Page 123: 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.

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 th

e graph The edge (i, j) is incident on the vertices i and j

Page 124: 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.

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 vdirected edge

u vundirected edge

Page 125: 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.

Path and Simple Path

A sequence of vertices P = i1, i2, …, ik is an i1 to ik path in the graph G=(V, E) iff the edge (ij, ij+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

Page 126: 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.

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?

Page 127: 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.

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

Page 128: 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.

Spanning Tree

Let G = (V, E) be an undirected graph A connected undirected graph that contains no cycles is a t

ree 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 mini

mum-cost spanning tree

Page 129: 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.

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 = V1 U V2 so that both V1 and V2 are independent sets.

Page 130: 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.

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

Page 131: 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.

Complete Undirected/Directed Graphs A complete undirected graph has n(n-1)/2 edges (i.e., all possible edges) and is denoted by

Kn

A complete directed graph (also denoted by Kn) on n vertices contains exactly n(n-1) edges

Page 132: 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.

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.

Page 133: 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.

Connected Graph

Let G = (V, E) be an undirected graph G is connected iff there is a path between every p

air of vertices in G

Page 134: 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.

Representation of Unweighted Graphs

The most frequently used representations for unweighted graphs are– Adjacency Matrix– Linked adjacency lists– Array adjacency lists

Page 135: 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.

Adjacency Matrix

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

Page 136: 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.

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

Page 137: 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.

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

Page 138: 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.

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.

Page 139: 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.

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

Page 140: 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.

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

Page 141: 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.

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)

Page 142: 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.

Graph Search Methods

Page 143: 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.

Graph Search Methods

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

Page 144: 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.

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!

Page 145: 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.

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!


Recommended