CH 1 Intro and Elementary Data Structure
CH 1 Intro and Elementary Data Structure
Regular Program
O(n)
X = X+i;
else O(1)
X=0;
Algorithm(A, B, n)
{
for(i=0; i<n; i++){………………………………..n+1
for(j=0; j<n; j++)…………………………………..n*(n+1)
{
C[i, j]= A[i, j]+ B[i, j];………….. n*n
}}
• Time complexity is f(n)=2n2+2n+1O(n2)
• The space complexity is A=n2, B=n2, C=n2 , n=1, i=1, j=1
• hence S(n)=3n2 +3 O(n2)
i=0……………….. 1 a=1
While(i<n)…….n+1 While(a<b)
{ {
Statement;…………..n Statement;
i++;………………………………n a=a*2;
} }
T(n)=3n+2O(n) T(n)=log2bO(logn)
Algorithm 1 Algorithm 2
arr[0] = 0; for(i=0; i<N; i++)
arr[1] = 0; arr[i] = 0;
arr[2] = 0;
arr[3] = 0;
...
arr[N-1] = 0;
52
Big- Visualization
57
Heap Types
• Max-heaps (largest element at root), have the max-heap
property:
– for all nodes i,
A[PARENT(i)] ≥ A[i]
• Min-heaps (smallest element at root), have the min-heap
property:
– for all nodes i,
A[PARENT(i)] ≤ A[i]
58
Array Representation of Heaps
• A heap can be stored as an array A.
– Root of tree is A[1]
– Left child of A[i] = A[2i]
– Right child of A[i] = A[2i + 1]
– Parent of A[i] = A[ i/2 ]
• Creating a heap from the given
array element is done in O(nlogn)
time
59
Adding/Deleting Nodes
• New nodes are always inserted at the bottom level (left to
right)
• Nodes are removed from the root then replace the node with
the bottom element (right to left) then maintain the heap.
• Deleting or inserting an element from a heap is done in
O(logn) time
60
Operations on Heaps
1. Maintain/Restore the max-heap property
- MAX-HEAPIFY
2. Create a max-heap from an unordered array
- BUILD-MAX-HEAP
3. Sort an array in place
- HEAPSORT
61
1. Maintaining the Heap Property (MAX-HEAPIFY)
• Suppose a node is smaller
than a child
– Left and Right sub trees of i
are max-heaps
• To eliminate the violation:
– Exchange with larger child
– Move down the tree
– Continue until node is not
smaller than children
62
Example
MAX-HEAPIFY(A, 2, 10)
A[2] A[4]
A[2] violates the heap property A[4] violates the heap property
A[4] A[9]
65
2. Building a Heap- BUILD-MAX-HEAP
• Convert an array A[1 … n] into a max-heap (n = length[A])
• The elements in the subarray A[(n/2+1) .. n] are leaves
• Apply MAX-HEAPIFY on elements between 1 and n/2
Alg: BUILD-MAX-HEAP(A) 1
1. n = length[A] 4
3. do MAX-HEAPIFY(A, i, n) 8
2 9 10
16 9 10
14 8 7
A: 4 1 3 2 16 9 10 14 8 7
66
Example: A 4 1 3 2 16 9 10 14 8 7
4 4 4
2 3 2 3 2 3
1 3 1 3 1 3
4 5 6 7 4 5 6 7 4 5 6 7
8
2 9 10
16 9 10 8 2 9 10
16 9 10 8 14 9 10
16 9 10
14 8 7 14 8 7 2 8 7
i=2 i=1
1 1 1
4 4 16
2 3 2 3 2 3
1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7
8
14 9 10
16 9 3 8
14 9 10
7 9 3 8
8 9 10
7 9 3
2 8 7 2 8 1 2 4 1
67
Running Time of BUILD MAX HEAP
Alg: BUILD-MAX-HEAP(A)
1. n = length[A]
2. for i ← n/2 downto 1
O(n)
3. do MAX-HEAPIFY(A, i, n) O(logn)
68
3. Heap Sort
• A conceptually simple sorting strategy which continually removes the
maximum value from the remaining unsorted elements.
MAX-HEAPIFY(A, 1, 1)
70
Alg: HEAPSORT(A)
1. BUILD-MAX-HEAP(A) O(n)
2. for i ← length[A] downto 2
3. do exchange A[1] ↔ A[i] n-1 times
4. MAX-HEAPIFY(A, 1, i - 1) O(logn)
71
Set Representation
• Trees can be used to represent sets.
• A set is a collection of objects.
72
Union and Find operations
• Disjoint set Union
• Need to form union of two different sets of a partition
– If Si and Sj are two disjoint sets, then their union
Si ∪Sj = {all elements x such that x is in Si or Sj}.
• Find (i)
– Find the set containing element i.
• Need to find out which set an element belongs to
73
Possible Tree Representation of Sets
Set 2 = {4 , 1 ,9}
S1 S2 S3
0 4 2
6 7 8 1 9 3 5
Set I = {0 , 6 ,7 ,8 } Set 3 = {2 , 3 , 5}
74
Unions of Sets
• To obtain the union of two sets, just set the parent field of one of
the roots to the other root.
• To figure out which set an element is belonged to, just follow its
parent link to the root and then follow the pointer in the root to the
set name.
• It is useful to identify the existence of a cycle in the graph.
75
Possible Representations of S1 ∪S2
S1 S2
0 4
S2
6 7 8 4 1 9
1 9
76
Possible Representations of S1 ∪S2
S1 S2
0 4
S1
6 7 8 0 1 9
6 7 8
77
Hashing
• A symbol table is a data structure which allows one to easily determine the
presence or absence of an arbitrary element.
• Hashing is the most practical technique for maintaining a symbol table.
• The implementation of hash tables is called hashing.
• Hashing is a technique used for performing insertions, deletions and finds in
constant average time (i.e. O(1))
• This data structure, however, is not efficient in operations that require any
ordering information among the elements, such as findMin, findMax and
printing the entire table in sorted order.
78
Example
Hash
Table
0
1
Items
2
john 25000
3 john 25000
phil 31250 key Hash 4 phil 31250
Function
dave 27500 5
mary 28200 6 dave 27500
7 mary 28200
key 8
9
79
Operations
• Initialization: all entries are set to NULL
• Find:
– locate the cell using hash function.
– sequential search on the linked list in that cell.
• Insertion:
– Locate the cell using hash function.
– (If the item does not exist) insert it as the first item in the list.
• Deletion:
– Locate the cell using hash function.
– Delete the item from the linked list.
80
Do you see any problems
with this approach?
0
U
(universe of keys) h(k1)
h(k4)
K k1
h(k2) = h(k5)
(actual k4 k2
keys) Collisions!
k5 k3 h(k3)
m-1
•Two or more keys hash to the same slot!!
•Avoiding collisions completely is hard,
even with a good hash function
•If there are too many collisions, performance of the hash table will suffer
dramatically. 81
Review literature on the following topics
1. Array vs. linked list data structure: Overview, pros & cons, types of arrays/linked list, algorithm for
basic operations, analyze their complexity
2. Stack vs. Queue data structures: Overview, pros & cons, types of stack/queue: algorithm for basic
operations, analyze their complexity
3. Tree data structures: Overview, unbalanced & balanced tree, tree traversal, algorithm for basic
operations, analyze their complexity
4. Sorting algorithms (include advanced algorithms in your discussion): Overview, discuss sorting
algorithm, show bubble & quick algorithm, analyze their complexity
5. Searching algorithms (include advanced algorithms in your discussion): Overview, discuss searching
algorithms, show linear & binary algorithms, analyze their complexity