Main.Assignment1 History

Hide minor edits - Show changes to output

Changed line 69 from:
print 'number of paths of length 2 between a and b: ', g.num_paths('a', 'b', 2)
to:
print g.num_paths('a', 'b', 2)
Deleted line 21:
Changed lines 9-12 from:
@@import HW2@@

@@g = HW2.load_dot(file_name)@@
to:
@@import HW1@@

@@g = HW1.load_dot(file_name)@@
Changed lines 44-45 from:
returns the number of paths of length k between node1 and
node2
to:
returns the number of paths of length AT MOST k
between node1 and node2
Added line 56:
n = 0 # the number of nodes
Added line 64:
pass
Changed line 42 from:
def num_paths(node1, node2, k) :
to:
def num_paths(self, node1, node2, k) :
Added line 68:
print 'number of paths of length 2 between a and b: ', g.num_paths('a', 'b', 2)
Changed lines 17-18 from:
The graph is loaded from a file in @@dot@@ format. An example graph is provided [[Path:../../data/test.dot|here]], and the provided function should work for the limited subset of @@dot@@ markup that we will use. Make sure your graph class implements the interface required for it to work.
to:
The graph is loaded from a file in @@dot@@ format. An example graph is provided [[Path:../../data/test.dot|here]], and the provided function should work for the limited subset of @@dot@@ markup that we will use.
Changed lines 44-45 from:
returns the number of paths of length k between node1 and node2
to:
returns the number of paths of length k between node1 and
node2
Added lines 22-66:


class graph (object) :
def __init__(self, n) :
"""constructor for the graph class.
takes a single parameter which is the number of nodes
in the graph
"""
# set the number of nodes
self.n = n
# the number of edges
self.m = 0
# the adjacency matrix
self.matrix = [ [0 for i in range(n)] for i in range(n) ]
# a dictionary that maps node ids to their indices
self.node_id_map = {}

def __len__(self) :
return self.n

def num_paths(node1, node2, k) :
"""
returns the number of paths of length k between node1 and node2
node1 and node2 are the names of the two nodes as they
appear in the input file that contains the graph
"""
pass


def load_dot(file_name) :

file_handle = open(file_name, 'U')
file_handle.readline()
# first you need to process the dot file to determine
# the number of nodes in the graph
g = graph(n)
file_handle.seek(0)
# go back to the beginning of the file
file_handle.readline()
for line in file_handle :
# process each edge
return g

if __name__ == '__main__' :
g = load_dot('test.dot')
Changed lines 1-17 from:
!!Programming Assignment 1 - Heaps

!!!Due: 1/30 at 5pm.

In this assignment you will program a heap. A heap is a complete binary tree that satisfies
the ''heap property'', namely that the value stored in a given node in the tree is smaller than the values in the children. Your heap class should support the following methods:

*
@@h = Heap()@@ - construct an empty heap
* @@insert(item)@@ - insert an item
*
@@delete()@@ - remove the item at the root of the heap
*
@@find_min()@@ - return the item at the root of the heap

The heap operations are implemented with
the help of the @@heapify_down@@ and @@heapify_up@@ methods. These are described in pages 59-64 of the textbook. The items in the heap should be stored in a Python list. In your implementation please use 0-based indexing instead of the 1-based indexing used in the book. You may find it useful to implement the methods @@parent(i)@@, @@left_child(i)@@, and @@right_child(i)@@.

In addition to be above methods
, you need to implement a @@__len__@@ method that returns the number of items in the heap. When this method is implemented, the @@len@@ operator will return the number of elements in the heap.

Your code should be contained in a single python file called @@hw1.py@@, and its structure should follow the skeleton code below. In addition to the @@Heap@@ class, your module should include a method called @@heap_sort@@ that sorts a list using the heap-sort algorithm. Some of comments are place-holders for your own comments.
to:
!!Programming Assignment 1 - Graphs and Adjacency Matrices

!!!Due: Sep 6th at 5pm.

In this assignment you will program a graph class and a method for determining
the number of paths of length at most @@k@@ between two nodes. The key fact that you need to know in order to solve this problem is that element @@(i, j)@@ of the @@k@@th power of the adjacency matrix is the number of paths of length exactly @@k@@ between node @@i@@ and node @@j@@. Now what is the @@k@@th power of a matrix? If A is your adjacency matrix, then its @@k@@th power is the product of A with itself @@k@@ times, where product is [[http://en.wikipedia.org/wiki/Matrix_multiplication#Matrix_product_.28two_matrices.29 | matrix product]].

Your code should be structured as a single python file called @@HW1.py@@. We will call to test your code as:

@@import HW2@@

@@g = HW2
.load_dot(file_name)@@

@@num_paths = g.num_paths(node1, node2, k)@@

where @@node1@@ and
@@node2@@ are the IDs of the two nodes, and

The graph is loaded from a file in
@@dot@@ format. An example graph is provided [[Path:../../data/test.dot|here]], and the provided function should work for the limited subset of @@dot@@ markup that we will use. Make sure your graph class implements the interface required for it to work.

Here's some code to get you started:
Changed lines 23-129 from:
"""
Assignment 1 - heaps

Your name goes here
"""
import math

class Heap (object) :
"""
A comment describing the class that you are implementing
"""
def __init__(self) :
"""
This is the constructor
"""
pass # the keyword "pass" means do nothing

def __len__(self) :
"""
This method should return the number of items in the heap
"""
return 0

def __repr__(self) :
"""
This method returns a string representation of a heap instance.
Useful for both debugging purposes, and for providing the user
with information about the instance.
"""
width = max([len(str(item)) for item in self.items])
if width%2==1:
width+=1
rows=math.floor(math.log(len(self),2))
output = []
length=math.pow(2,math.floor(math.log(len(self),2)))*(width+2)
for i in range(len(self)):
s='%'+str(length/2-(width+2)/2)+'s %'+str(width)+'s '+'%'+str(length/2-(width+2)/2)+'s'
output.append(s % ('',self.items[i],'') )
if math.floor(math.log(i+2,2))==math.log(i+2,2):
output.append("\n")
length=length/2

return ''.join(output)

def find_min(self) :
"""
return the smallest element in the heap
"""
raise NotImplemented
def insert(self, item) :
"""
insert item into the heap
"""
raise NotImplemented
def delete(self) :
"""
delete the smallest element from the heap
"""
raise NotImplemented
def heapify_up(self) :
"""
performs a heapify-up operation that starts at the last element in the heap
"""
self.recursive_heapify_up(len(self) - 1)

def recursive_heapify_up(self, i) :
"""
recursively heapify-up
"""
raise NotImplemented

def heapify_down(self) :
"""
Perform heapify-down starting from the root of the tree
"""
self.recursive_heapify_down(0)

def recursive_heapify_down(self, i) :
"""
recursively heapify-down starting from a given node
"""
raise NotImplemented

def heap_sort(in_list) :
"""
returns a sorted version of the list given as input; sorting is performed
using the heap-sort algorithm
Usage:
out_list = heap_sort(in_list)
in_list should be unchanged as a result of the operation
"""
if type(in_list) != list :
raise ValueError, "expecting a list as input"
return

if __name__ == '__main__' :

items = [3, 5, 8, 1, 4, 9, 10, 2, 6, 7, 11]
h = Heap()
for item in items :
print 'adding ', item
h.insert(item)
h.delete()
print h
print heap_sort(items)

(:sourceend:)
to:
(:sourceend:)
Deleted line 56:
print s
Changed line 12 from:
The heap operations are implemented with the help of the @@heapify_down@@ and @@heapify_up@@ methods. These are described in pages 60-64 of the textbook. In your implementation please use 0-based indexing instead of the 1-based indexing used in the book. You may find it useful to implement the methods @@parent(i)@@, @@left_child(i)@@, and @@right_child(i)@@.
to:
The heap operations are implemented with the help of the @@heapify_down@@ and @@heapify_up@@ methods. These are described in pages 59-64 of the textbook. The items in the heap should be stored in a Python list. In your implementation please use 0-based indexing instead of the 1-based indexing used in the book. You may find it useful to implement the methods @@parent(i)@@, @@left_child(i)@@, and @@right_child(i)@@.
Changed line 123 from:
h.delete(7)
to:
h.delete()
Changed lines 3-6 from:
!!!Due: Feb 4th at 5pm.

In this assignment you will program a heap. A heap is a complete binary tree that satisfies the ''heap property'', namely that the value stored in a given node in the tree is smaller than the values in the children. Your heap should support the following operations:
to:
!!!Due: 1/30 at 5pm.

In this assignment you will program a heap. A heap is a complete binary tree that satisfies the ''heap property'', namely that the value stored in a given node in the tree is smaller than the values in the children. Your heap class should support the following methods:
Changed lines 12-13 from:
The heap operations are implemented with the help of the @@heapify_down@@ and @@heapify_up@@ methods. These are described in pages 60-64 of the Kleinberg and Tardos book. In your implementation please use 0-based indexing instead of the 1-based indexing used in the book. You may find it useful to implement the methods @@parent(i)@@, @@left_child(i)@@, and @@right_child(i)@@.
to:
The heap operations are implemented with the help of the @@heapify_down@@ and @@heapify_up@@ methods. These are described in pages 60-64 of the textbook. In your implementation please use 0-based indexing instead of the 1-based indexing used in the book. You may find it useful to implement the methods @@parent(i)@@, @@left_child(i)@@, and @@right_child(i)@@.
Changed lines 16-17 from:
Your code should be structured as a single python file called @@heapy.py@@, and its structure should follow the skeleton code below. In addition to the @@Heap@@ class, your module should include a method called @@heap_sort@@ that sorts a list using the heap-sort algorithm. Some of comments are place-holders for your own comments.
to:
Your code should be contained in a single python file called @@hw1.py@@, and its structure should follow the skeleton code below. In addition to the @@Heap@@ class, your module should include a method called @@heap_sort@@ that sorts a list using the heap-sort algorithm. Some of comments are place-holders for your own comments.
Added line 42:
Added lines 49-52:
width = max([len(str(item)) for item in self.items])
if width%2==1:
width+=1
rows=math.floor(math.log(len(self),2))
Changed lines 54-68 from:
pwr = 0
row = []
row_size = 0
for i in range(len(self)) :
row.append(str(self.items[i]))
row_size += 1
if row_size == int(math.pow(2, pwr)) :
output.append(' '.join(row))
row = []
pwr += 1
row_size = 0
if len(row) > 0 :
output.append(' '.join(row))
return '\n\n'.join(output)
to:
length=math.pow(2,math.floor(math.log(len(self),2)))*(width+2)
for i in range(len(self)):
s='%'+str(length/2-(width+2)/2)+'s %'+str(width)+'s '+'%'+str(length/2-(width+2)/2)+'s'
print s
output.append(s % ('',self.items[i],'') )
if math.floor(math.log(i+2,2))==math.log(i+2,2):
output.append("\n")
length=length/2

return ''.join(output)
Added line 123:
h.delete(7)
Changed line 16 from:
Your code should be structured as a single python file called @@heapy.py@@, and its structure should follow the skeleton code below. In addition to the @@Heap@@ class, your module should include a method called @@heap_sort@@ that sorts a list using the heap-sort algorithm.
to:
Your code should be structured as a single python file called @@heapy.py@@, and its structure should follow the skeleton code below. In addition to the @@Heap@@ class, your module should include a method called @@heap_sort@@ that sorts a list using the heap-sort algorithm. Some of comments are place-holders for your own comments.
Changed line 3 from:
!Due: Feb 4th at 5pm.
to:
!!!Due: Feb 4th at 5pm.
Added lines 3-4:
!Due: Feb 4th at 5pm.
Changed lines 14-15 from:
The following is skeleton code that will help you in implementing your class:
to:
In addition to be above methods, you need to implement a @@__len__@@ method that returns the number of items in the heap. When this method is implemented, the @@len@@ operator will return the number of elements in the heap.

Your code should be structured as a single python file called @@heapy.py@@, and its structure should follow the skeleton code below. In addition to the @@Heap@@
class, your module should include a method called @@heap_sort@@ that sorts a list using the heap-sort algorithm.
Changed line 103 from:
def heap_sort(l) :
to:
def heap_sort(in_list) :
Changed line 111 from:
if type(l) != list :
to:
if type(in_list) != list :
Changed lines 10-121 from:
The heap operations are implemented with the help of the @@heapify_down@@ and @@heapify_up@@ methods. These are described in pages 60-64 of the Kleinberg and Tardos book. In your implementation please use 0-based indexing instead of the 1-based indexing used in the book. You may find it useful to implement the methods @@parent(i)@@, @@left_child(i)@@, and @@right_child(i)@@.
to:
The heap operations are implemented with the help of the @@heapify_down@@ and @@heapify_up@@ methods. These are described in pages 60-64 of the Kleinberg and Tardos book. In your implementation please use 0-based indexing instead of the 1-based indexing used in the book. You may find it useful to implement the methods @@parent(i)@@, @@left_child(i)@@, and @@right_child(i)@@.

The following is skeleton code that will help you in implementing your class:

(:source lang=python:)

"""
Assignment 1 - heaps

Your name goes here
"""
import math

class Heap (object) :
"""
A comment describing the class that you are implementing
"""
def __init__(self) :
"""
This is the constructor
"""
pass # the keyword "pass" means do nothing

def __len__(self) :
"""
This method should return the number of items in the heap
"""
return 0
def __repr__(self) :
"""
This method returns a string representation of a heap instance.
Useful for both debugging purposes, and for providing the user
with information about the instance.
"""
output = []
pwr = 0
row = []
row_size = 0
for i in range(len(self)) :
row.append(str(self.items[i]))
row_size += 1
if row_size == int(math.pow(2, pwr)) :
output.append(' '.join(row))
row = []
pwr += 1
row_size = 0
if len(row) > 0 :
output.append(' '.join(row))
return '\n\n'.join(output)

def find_min(self) :
"""
return the smallest element in the heap
"""
raise NotImplemented
def insert(self, item) :
"""
insert item into the heap
"""
raise NotImplemented
def delete(self) :
"""
delete the smallest element from the heap
"""
raise NotImplemented
def heapify_up(self) :
"""
performs a heapify-up operation that starts at the last element in the heap
"""
self.recursive_heapify_up(len(self) - 1)

def recursive_heapify_up(self, i) :
"""
recursively heapify-up
"""
raise NotImplemented

def heapify_down(self) :
"""
Perform heapify-down starting from the root of the tree
"""
self.recursive_heapify_down(0)

def recursive_heapify_down(self, i) :
"""
recursively heapify-down starting from a given node
"""
raise NotImplemented

def heap_sort(l) :
"""
returns a sorted version of the list given as input; sorting is performed
using the heap-sort algorithm
Usage:
out_list = heap_sort(in_list)
in_list should be unchanged as a result of the operation
"""
if type(l) != list :
raise ValueError, "expecting a list as input"
return

if __name__ == '__main__' :

items = [3, 5, 8, 1, 4, 9, 10, 2, 6, 7, 11]
h = Heap()
for item in items :
print 'adding ', item
h.insert(item)
print h
print heap_sort(items)

(:sourceend:)
Added lines 1-10:
!!Programming Assignment 1 - Heaps

In this assignment you will program a heap. A heap is a complete binary tree that satisfies the ''heap property'', namely that the value stored in a given node in the tree is smaller than the values in the children. Your heap should support the following operations:

* @@h = Heap()@@ - construct an empty heap
* @@insert(item)@@ - insert an item
* @@delete()@@ - remove the item at the root of the heap
* @@find_min()@@ - return the item at the root of the heap

The heap operations are implemented with the help of the @@heapify_down@@ and @@heapify_up@@ methods. These are described in pages 60-64 of the Kleinberg and Tardos book. In your implementation please use 0-based indexing instead of the 1-based indexing used in the book. You may find it useful to implement the methods @@parent(i)@@, @@left_child(i)@@, and @@right_child(i)@@.