# Class 1 and 2: Code walkthrough

• Structuring a list min/max problem over multiple timescales
• Building and using a structured nearest neighbor graph

# Example Codes

## List Min/Max

See CompSciWeek1.

## Using Graphs

<source lang="python">

1. !/usr/bin/env python

from numpy import array, sum, reshape

1. input - list of names
2. - (3n)x 3 coordinate array
3. output - Graph G

def make_G(names, x):

```   assert x.shape%3 == 0
assert x.shape == 3
assert len(names) == len(x)
```
```   print x.shape
n = len(x)/3
G = {}
D2 = sum((reshape(x, (1,3*n,3)) - reshape(x, (3*n,1,3)))**2, -1)
print D2
for i,n in enumerate(names):
print i,n
G[i] = set()
if n != 'O': continue
for j,m in enumerate(names):
print j,m
if m != 'H': continue
# all O-H distances, 1 at a time
if D2[i,j] < 1.1:
G[j].add(i) # Question for class: why does this cause an error?
return G
```
1. input - list of names (1 O per 2 H-s)
2. - graph from atom number to atom numbers
3. output - list of atom numbers ordered O, H, H, O, H, H, ...

def outp_graph(names, G):

```   out = []
for i,n in enumerate(names):
if n != "O":
continue
out.append(i) # 'O' number
bonds = G[i]
assert len(bonds) == 2, "Bad number (%d) of O-bonds."%(len(bonds))
```
```       for j in bonds: # 'H' numbers
out.append(j)
return out
```

names = ['H', 'O', 'H', 'H', 'H', 'O'] x = array([[1,0,0],[0,0,0],[-1,0,0]]) G = make_G(names[:3], x) print G

1. G = {0:{1}, 1:{0,4, 1}, 2:{5}, 3:{5}, 4:{1}, 5:{2,3} }
2. l = outp_graph(names, G)
3. print l

</source>