Datastructures and Algorithms
Datastructures and Algorithms
Jennifer Rexford !
The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 2!
Motivating Quotations!
Every program depends on algorithms and data structures, but few programs depend on the invention of brand new ones.! -- Kernighan & Pike!
I will, in fact, claim that the difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships.! -- Linus Torvalds!
2
A Common Task!
Maintain a table of key/value pairs!
Each key is a string; each value is an int Unknown number of key-value pairs!
Examples!
(student name, grade)! (john smith, 84), (jane doe, 93), (bill clinton, 81)! (baseball player, number)! (Ruth, 3), (Gehrig, 4), (Mantle, 7)! (variable name, value)! (maxLength, 2000), (i, 7), (j, -10)!
Algorithms!
Create: Create the data structure! Add: Add a key/value pair! Search: Search for a key/value pair, by key! Free: Free the data structure!
Algorithms:!
Create: Allocate Table structure to point to rst node! Add: Insert new node at front of list! Search: Linear search through the list! Free: Free nodes while traversing; free Table structure!
6
struct! Table!
struct! Node!
"Gehrig" 4
struct! Node!
"Ruth" 3 NULL 7
t!
t!
NULL
"Ruth" 3 NULL 10
p!
t!
"Gehrig" 4 "Ruth" 3 NULL 11
p!
"Mantle" 7 "Gehrig" 4
t!
"Ruth" 3 NULL 12
p!
"Mantle" 7 "Gehrig" 4
t!
"Ruth" 3 NULL 13
p!
"Mantle" 7 "Gehrig" 4
t!
"Ruth" 3 NULL 14
t!
"Mantle" 7 "Gehrig" 4 "Ruth" 3 NULL 15
p! t!
"Mantle" 7 "Gehrig" 4 "Ruth" 3 NULL 16
p! t!
"Mantle" 7 "Gehrig" 4 "Ruth" 3 NULL 17
t!
"Mantle" 7
p!
"Gehrig" 4 "Ruth" 3 NULL 18
t!
"Mantle" 7
p!
"Gehrig" 4 "Ruth" 3 NULL 19
t!
"Mantle" 7
p!
"Gehrig" 4 "Ruth" 3 NULL
20
t!
"Mantle" 7 "Gehrig" 4 "Ruth" 3 NULL 21
p! t!
"Mantle" 7 "Gehrig" 4 "Ruth" 3 NULL 22
p! t!
"Mantle" 7
nextp!
"Gehrig" 4 "Ruth" 3 NULL 23
p! nextp! t!
"Mantle" 7 "Gehrig" 4 "Ruth" 3 NULL 24
p! t!
"Mantle" 7 "Gehrig" 4
nextp!
"Ruth" 3 NULL 25
p! nextp! t!
"Mantle" 7 "Gehrig" 4 "Ruth" 3 NULL 26
p! t!
"Mantle" 7 "Gehrig" 4 "Ruth" 3 NULL
nextp!
27
t!
"Mantle" 7 "Gehrig" 4 "Ruth" 3 NULL
p!
nextp!
28
t!
"Mantle" 7 "Gehrig" 4 "Ruth" 3 NULL
p!
nextp!
29
31
1776 Revolution
1861 Civil
1939 WW2
32
This is OK:!
0
ARRAYSIZE-1
33
ARRAYSIZE-1
35
unsigned int hash(const char *x) { int i; unsigned int h = 0U; for (i=0; x[i]!='\0'; i++) h = h * 65599 + (unsigned char)x[i]; return h % 1024; }
Can be more clever than this for powers of two!! (Described in Appendix)!
36
Search the linked list table[1] for the string the; not found.!
0 1 2 3 4 5 6
37
Search the linked list table[1] for the string the; not found! Now: table[1] = makelink(key, value, table[1])! 0 1 2 3 4 5 6
the
38
Search the linked list table[2] for the string cat; not found! Now: table[2] = makelink(key, value, table[2])!
0 1 2 3 4 5 6
the
39
0 1 2 3 4 5 6
the cat
40
0 1 2 3 4 5 6
the cat in
41
0 1 2 3 4 5 6
the cat in
42
0 1 2 3 4 5 6
43
struct! Table!
0 NULL 1 NULL 23 723
struct! Node!
"Ruth" 3 NULL
struct! Node!
"Gehrig" 4 NULL
44
t!
45
t!
0 NULL 1 NULL 23 723
"Ruth" 3 NULL
46
t!
0 NULL 1 NULL 23 723
"Ruth" 3 NULL
"Gehrig" 4 NULL
p!
47
t!
0 NULL 1 NULL 23 723
"Ruth" 3 NULL
p!
"Mantle" 7 48
t!
0 NULL 1 NULL 23 723
"Ruth" 3 NULL
h = 806!
"Gehrig" 4 NULL
p!
"Mantle" 7 NULL
49
t!
0 NULL 1 NULL 23 723 806
"Ruth" 3 NULL
h = 806!
"Gehrig" 4 NULL
p!
"Mantle" 7 NULL
1023 NULL
50
t!
"Ruth" 3 NULL
"Gehrig" 4 NULL
1023 NULL
"Mantle" 7 NULL
51
t!
"Ruth" 3 NULL
1023 NULL
"Mantle" 7 NULL
52
t!
p!
"Ruth" 3 NULL "Gehrig" 4 NULL
h = 723!
1023 NULL
"Mantle" 7 NULL
53
t!
p!
"Ruth" 3 NULL "Gehrig" 4 NULL
h = 723!
1023 NULL
"Mantle" 7 NULL
54
t!
p!
"Ruth" 3 NULL "Gehrig" 4 NULL
h = 723!
1023 NULL
"Mantle" 7 NULL
55
t!
0 NULL 1 NULL 23 723 806
"Ruth" 3 NULL
"Gehrig" 4 NULL
1023 NULL
"Mantle" 7 NULL
56
t!
0 NULL 1 NULL 23 723 806
b = 0!
"Ruth" 3 NULL
"Gehrig" 4 NULL
1023 NULL
"Mantle" 7 NULL
57
t!
0 NULL 1 NULL 23 723 806
p! b = 0!
"Ruth" 3 NULL
"Gehrig" 4 NULL
1023 NULL
"Mantle" 7 NULL
58
t!
0 NULL 1 NULL 23 723 806
p! b = 1, , 23!
"Ruth" 3 NULL
"Gehrig" 4 NULL
1023 NULL
"Mantle" 7 NULL
59
t!
0 NULL 1 NULL 23 723 806
p! b = 23!
"Ruth" 3 NULL
"Gehrig" 4 NULL
1023 NULL
"Mantle" 7 NULL
60
t!
0 NULL 1 NULL 23 723 806
p!
nextp!
b = 23!
"Ruth" 3 NULL
"Gehrig" 4 NULL
1023 NULL
"Mantle" 7 NULL
61
t!
0 NULL 1 NULL 23 723 806
p! nextp!
"Ruth" 3 NULL
b = 23!
"Gehrig" 4 NULL
1023 NULL
"Mantle" 7 NULL
62
t!
0 NULL 1 NULL 23 723 806
"Ruth" 3 NULL
1023 NULL
"Mantle" 7 NULL
63
t!
0 NULL 1 NULL 23 723 806
b = 1024!
"Ruth" 3 NULL
"Gehrig" 4 NULL
1023 NULL
"Mantle" 7 NULL
64
Key Ownership!
Note: Table_add() functions contain this code:!
void Table_add(struct Table *t, const char *key, int value) { struct Node *p = (struct Node*)malloc(sizeof(struct Node)); p->key = key; }
Caller passes key, which is a pointer to memory where a string resides! Table_add() function stores within the table the address where the string resides!
66
Via Table_add(), table contains memory address k! Client changes string at memory address k! Thus client changes key within table!
67
Summary!
Common data structures & associated algorithms!
Linked list! Fast insert, slow search! Hash table! Fast insert, (potentially) fast search! Invaluable for storing key/value pairs! Very common!
Related issues!
Hashing algorithms! Memory ownership!
69
Appendix!
Stupid programmer tricks related to hash tables!
70
Previous! version!
unsigned int hash(const char *x) { int i; unsigned int h = 0U; for (i=0; x[i]!='\0'; i++) h = h * 65599 + (unsigned char)x[i]; return h & 1023; }
Faster!
much faster?!
74