Algorithms
and
Data
Structures

Spring 2021
course
site
-->

Monday March 1

... start recording.

analysis - O()

Finish up our discussion of this stuff :

I've posted my answers to the homework due today; we'll go over that. (And oops: there was a formatting typo in my notes; for the four functions I wanted \( n \, \log(n), n^2, 2^n, n! \) ).

I would like to briefly talk about counting things ... and what this has to do with O()

Topics to understand and/or discuss depending on your comfort level :

Questions about any of this?

data structures

This week and next we're going to look at some essential data structure building blocks :

You can find this material in

First, what's a "data structure"? See wikipedia's definition

Two ideas :

Different data structures are optimized for different tasks, with various strengths. Your mission is to understand what the O() behavior is for different operations, and their pros and cons.

The ideas we'll look at are :

syntax vs O() behavior

Please note that different languages spell these ideas in different ways. For this course, we care about how fast the operations go, not how they're spelled.

In both Python and C, we can read or write the n'th element of an indexed array with the notation numbers[i] . In lisp, we'd write that as (nth i numbers). We could just as well have numbers.get(i) or whatever.

ADT - objects vs functions

For languages that support objects (which are called "classes" in Python), the typical approach is to write an ADT (Abstract Data Type) as a class, with methods providing the appropriate interface.

C doesn't have objects, but it does allow you to define your own data types. Typically we build up an ADT with a data type and a set of functions, simply passing the data type in rather than using object notation.


All right, enough talking about all this - let's get more specific.

stack

A stack is a "first in, last out" data structure, with a "push" and "pop" method.

I've attached a first python implementation; let's annotate it and test it.

s = Stack()
s.push(1)
s.push(2)
while not s.is_empty():
  print(s.pop())

As a python programming exercise, let's add an __str__ method to this class.

I've also attached a C implementation; let's look at that and discuss.

Both of these implementations use an indexed array as the underlying data store. But that isn't the only way to build it.

linked lists

Another fundamental type of storage is the "linked list". Some languages, lisp in particular, use this idea as one of the core language concepts.

The idea is to create a "node" which has some data and a pointer to the next node. Each one can be anywhere in memory, which avoids allocating large contiguous blocks.

[ node 1 ]  ->  [node 2 ] -> [node 2]

I've attached again both a python and C implementation; we'll walk through the code in class.

These are both "singly linked lists".

Another variation is the "doubly linked list" ... discuss what that would look like.

Many operations on linked lists can be done elegantly with recursion ... such as finding the length of the list.

linked list pros :

linked list cons :

classic job interview question:

what are these things good for?

Lots of things. ;)

One example is parsing parens and brackets :

Given a string made up of these characters :  (  )  [ ] {  } and possibly others,
write a function which returns true if the grouping symbols are nested and matched properly,
and false if not.

Let's talk through how that would work. Can you think of another way to do this?

where are we going next?

Your mission for this week is to (a) read about this stuff, (b) review and practice your C and Python, and (c) try out some of these ideas in code.

The specific assignment for next Monday will depend on how far we get and how comfortable you are with all this.

The related coding tasks could include

... though probably not all of that in a week. ;)

The specific coding topics include

There are many sources online to read about these things; check out my resources page. The following pages may be useful :

If the recursion ideas aren't familiar, now would be a good time to read about that :

Other related resources from wikipedia, with lists of other readings :

https://cs.bennington.college /courses /spring2021 /algorithms /notes /3_data_a
last modified Mon March 1 2021 6:30 pm

attachments [paper clip]

  last modified size
TXT counting_whiteboard.pdf Mon Mar 01 2021 06:30 pm 643K
TXT linked_list.c Mon Mar 01 2021 12:21 am 909B
TXT linked_list.py Mon Mar 01 2021 05:57 pm 1.2K
TXT stack1.c Mon Mar 01 2021 12:01 am 1.0K
TXT stack1.py Mon Mar 01 2021 12:01 am 603B