Docsity

Elementary Data Structures - Data Structures and Algorithm - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Elementary Data Structures - Data Structures and Algorithm - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Elementary Data Structures

Problem of the Day

True or False?

  1. 2 n^2 + 1 = O(n^2 )

n = O(log n)

  1. log n = O(

n)

  1. n^2 (1 +

n) = O(n^2 log n)

  1. 3 n^2 +

n = O(n^2 )

n log n = O(n)

  1. log n = O(n−^1 /^2 )

Federal Sentencing Guidelines

2F1.1. Fraud and Deceit; Forgery; Offenses Involving Altered or Counterfeit Instruments other than Counterfeit Bearer Obligations of the United States.(a) Base offense Level: 6 (b) Specific offense Characteristics (1) If the loss exceeded $2,000, increase the offense level as follows: Loss(Apply the Greatest)(A) $2,000 or less Increase in Levelno increase (B) More than $2,000(C) More than $5,000 add 1add 2 (D) More than $10,000(E) More than $20,000 add 3add 4 (F) More than $40,000(G) More than $70,000 add 5add 6 (H) More than $120,000(I) More than $200,000 add 7add 8 (J) More than $350,000(K) More than $500,000 (^) add 10add 9 (L) More than $800,000(M) More than $1,500,000 add 11add 12 (N) More than $2,500,000(O) More than $5,000,000 add 13add 14 (P) More than $10,000,000(Q) More than $20,000,000 add 15add 16 (R) More than $40,000,000(Q) More than $80,000,000 add 17add 18

Make the Crime Worth the Time

The increase in punishment level grows logarithmically in the amount of money stolen. Thus it pays to commit one big crime rather than many small crimes totalling the same amount.

Data Abstraction

That we can describe the behavior of our data structures in terms of abstract operations is why we can use them without thinking. That there are different implementations of the same abstract operations enables us to optimize performance.

Contiguous vs. Linked Data Structures

Data structures can be neatly classified as either contiguous or linked depending upon whether they are based on arrays or pointers:

  • Contiguously-allocated structures are composed of single slabs of memory, and include arrays, matrices, heaps, and hash tables.
  • Linked data structures are composed of multiple distinct chunks of memory bound together by pointers , and include lists, trees, and graph adjacency lists.

Dynamic Arrays

Unfortunately we cannot adjust the size of simple arrays in the middle of a program’s execution. Compensating by allocating extremely large arrays can waste a lot of space. With dynamic arrays we start with an array of size 1, and double its size from m to 2 m each time we run out of space. How many times will we double for n elements? Only dlog 2 ne.

How Much Total Work?

The apparent waste in this procedure involves the recopying of the old contents on each expansion. If half the elements move once, a quarter of the elements twice, and so on, the total number of movements M is given by

M =

lg∑ n i=

i · n/ 2 i^ = n

lg∑ n i=

i/ 2 i^ ≤ n

∞∑ i=

i/ 2 i^ = 2n

Thus each of the n elements move an average of only twice, and the total work of managing the dynamic array is the same O(n) as a simple array.

Linked List Structures

typedef struct list { item type item; struct list *next; } list;

Lincoln Jefferson Clinton NIL

Searching a List

Searching in a linked list can be done iteratively or recursively.

list (^) *search list(list (^) *l, item type x) { if (l == NULL) return(NULL); if (l− >item == x) return(l); else return( search list(l− >next, x) ); }

Deleting from a List

delete list(list (^) **l, item type x) { list (^) p; ( item pointer (^) *) list (^) last = NULL; ( predecessor pointer (^) *) p = (^) l; while (p− >item != x) { ( find item to delete (^) ) last = p; p = p− >next; } if (last == NULL) ( splice out of the list (^) *) else l^ =^ p−^ >next; last− >next = p− >next; free(p); ( return memory used by the node (^) *) }

Advantages of Linked Lists

The relative advantages of linked lists over static arrays include:

  1. Overflow on linked structures can never occur unless the memory is actually full.
  2. Insertions and deletions are simpler than for contiguous (array) lists.
  3. With large records, moving pointers is easier and faster than moving the items themselves.

Dynamic memory allocation provides us with flexibility on how and where we use our limited storage resources.

Impact on Tree Traversal

Both can be used to store nodes to visit in a tree, but the order of traversal is completely different.

1

2 3

4 5 6 7

1

5 2

7 6 4 3 Queue Stack

Which order is friendlier for WWW crawler robots?

Dictonary / Dynamic Set Operations

Perhaps the most important class of data structures maintain a set of items, indexed by keys.

  • Search(S,k) – A query that, given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or nil if no such element belongs to S.
  • Insert(S,x) – A modifying operation that augments the set S with the element x.
  • Delete(S,x) – Given a pointer x to an element in the set S, remove x from S. Observe we are given a pointer to an element x, not a key value.