Feb 5
Questions and/or discussion of homework?
data structures
This week: material from chapter 3 in the text.
Be aware that the discussion in this section
is full of C jargon. We will use python to talk
about the same ideas, but in practice this issues
are more about the way computers actually work
at a low level (i.e. C) than the abstraction
that languages like python supply.
First though, two asides.
O() equality
O() notion of "equality" is confusing:
f(x) = O(g(x)) means f(x) <= M g(x) for some constant M
(See e.g. http://en.wikipedia.org/wiki/Big_O_notation )
So really "=" means "<=".
n = O(n**2) YES (less-than)
n**2 = O(n) NO (greater-than)
When in doubt, put some big numbers in.
True or false? (These are all from Skienna lecture4.pdf .)
\[ 2n^2 + 1 = O(n^2) \]
\[ \sqrt{n} = O(\log n) \]
\[ log n = O(\sqrt{n}) \]
\[ n^2 (1 + \sqrt{n}) = O(n^2 \log n) \]
\[ 3 n^2 + \sqrt{n} = O(n^2) \]
\[ \sqrt{n} \log n = O(n) \]
\( \log n = O(n^{-1/2}) \)
(Oh, and if you want to enter formulas
in your wiki homework, the square brackets center on
a line; parens do inline formulas like
\( \sqrt{ \frac{1}{2} } \)
).
pointers and linked lists
The data structures chapter talks about "pointers".
What is a pointer look like?
Essentially it's the address of something.
In the C programming language that concept
is explicit: all program data is tied to
explicitly numbered bytes in memory.
Python uses pointers too, but it doesn't call
them that ... and usually we don't think that way.
But consider this
> a = 5
> b = a
> a = 4
> b # What is b now?
compared to this
> a = [2, 3, 4]
> b = a
> a[0] = 100
> b # What is b now?
So in Python, variables can be "pointers"
to lists, dictionaries, and other "object-like"
things with internal parts. Python's id()
function gives a unique identifier to
the data itself, which may have several
variables (e.g. (a,b)) that "point" to it.
Skienna gives the following example in C
of a linked list structure
typedef struct node {
data_type data;
struct node *next;
} node;
(I like the words "node" and "data" here
rather than "list" and "item", but the idea is the same.)
The details of what this syntax means aren't important.
But the idea is : we can build a list (or trees or graphs)
out of elements that contain (1) data, and (2)
a reference (i.e. a pointer) or references
to other pieces of data.
One way to do a similar thing in python looks like this,
along with a "follow along the list and print" function
class Node(object):
def __init__(self, data=None, next=None):
self.data = data
self.next = next
def print_list(self):
"""Print the list from this node onwards"""
print "[",
node = self
while node != None:
print str(node.data) + " ",
node = node.next
print "]"
a = Node(data = 'Lincoln')
b = Node(data = 'Jefferson')
c = Node(data = 'Clinton')
a.next = b
b.next = c
a.print_list()
Structures like this have a number of
interesting properties for implementing
algorithms, which are discussed in this chapter.
slides
in class
... we wrote the attached example of a singly linked
list in python used to implement a stack.