python: advanced objects

28
Python: Advanced Objects http://www.flickr.com/photos/iamthestig2/3925864142/sizes/l/in/ photostream/

Upload: ashley-carpenter

Post on 11-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python: Advanced Objects

Python: Advanced Objects

http://www.flickr.com/photos/iamthestig2/3925864142/sizes/l/in/photostream/

Page 2: Python: Advanced Objects

Data Structure

Python has some basic data types built-in list tuple complex dictionary etc..

Which of these would you use to represents an Organizational chart? A book outline? An XML file? A folder system?

CEO

Marketing Director

Manufacturing Director

Quality Control Director

Page 3: Python: Advanced Objects

Trees

A tree is a collection of elements The elements are in a non-linear, hierarchical

arrangement The elements are not accessible by position

Mirrors common real-world objects A book : chapters – sections – paragraphs – sentences

– words. Org charts Folder systems

3

Page 4: Python: Advanced Objects

Terminology

Trees are formed from nodes and edges. Nodes are also known as vertices Edges are also known as branches

An edge establishes a relationship between nodes This is a parent-child relationship Given nodes A and B, the edge {A, B} means that A is the

parent of B. A node can have at most one parent. The children are ordered from left to right.

Each tree has one root node. The root node is the only node that has no parent.

4

Page 5: Python: Advanced Objects

Definitions

Trees can be defined recursively (inductively) A single node is a tree. It is the root of the tree. If N is a node and T1, T2, …, Tk are trees with root

nodes N1, N2, …, Nk, then so is the tree having N as the root and adding edges {N, N1}, {N, N2}, …, {N, Nk}.

Trees are usually drawn “upside” down. Root at the top.

5

Page 6: Python: Advanced Objects

Binary Search Tree

A binary search tree is a binary tree with the following property Each node is associated with a value or key For each node in the tree, the nodes key is greater than or

equal to all keys in the left subtree and is less than or equal to all keys in the right subtree.

6

Page 7: Python: Advanced Objects

Binary Search Tree Operations

A binary search tree is a collection that supports efficient operations such as: searching! inserting removing finding the minimum finding the maximum

7

Page 8: Python: Advanced Objects

Min and Max

Give a binary search tree node T (that has left, right and value attributes), describe an algorithm for finding the minimum value in T

Describe an algorithm for finding the maximum key in a binary search tree.

algorithm getMin(T): if T is empty then return None else if T has a left child then getMin(T.left) else return T.value

algorithm getMax(T): if T is empty then return None else if T has a right child then getMax(T.right) else return T.value

Page 9: Python: Advanced Objects

Insertion

Give a binary search tree node T, describe an algorithm for inserting into T

# This returns the tree that results from inserting V into Talgorithm insert(T, V): if T is empty then return a new node (V, None, None) else if V < T.value then T.left = insert(T.left, V) else T.right = insert(T.right, V) return T

Page 10: Python: Advanced Objects

Searching

Give a binary search tree node T, describe an algorithm for finding a value

# returns true if T contains V and false otherwisealgorithm contains(T, V): if T is empty then return False else if T.value = V then return True else if V < T.value then return contains(T.left, V) else return contains(T.right, V)

Page 11: Python: Advanced Objects

Search Tree as List

We will eventually construct a binary search tree class. But for now, consider representing a binary search tree as a list. The empty tree is the empty list A non-empty tree is a list of length 3:

[VALUE LEFT-TREE RIGHT-TREE] Write functions named

insert min max size contains

Page 12: Python: Advanced Objects

Nodes

Can also write a tree class that uses "nodes" Explicitly represent a node object Each node has three attributes

A left node A right node A value

The empty node is different: Has no value Has no left Has no right

Use two difference classes for nodes NonEmptyNode EmptyNode

Page 13: Python: Advanced Objects

Node Class

In addition to attributes each node should support some operations insert remove size contains height

These methods should be supported even for EmptyNodes.

A BinarySearch tree is then an object with one attribute: The root node

Page 14: Python: Advanced Objects

Empty Node

class EmptyNode: def __init__(self): pass

def insert(self, value): return SearchTree.NonEmptyNode(value)

def size(self): return 0

def height(self): return -1

def remove(self, value): return self

def isEmpty(self): return True

def min(self): return None

def max(self): return None

class NonEmptyNode: def __init__(self, value): self.left = SearchTree.EmptyNode() self.right = SearchTree.EmptyNode() self.value = value

def insert(self, value): ???

def size(self): ???

def height(self): ???

def remove(self, value): ???

def isEmpty(self): ???

def min(self): ???

def max(self): ???

Page 15: Python: Advanced Objects

Search Tree

What would the search tree class look like? How many attributes? How many methods?

class SearchTree: def __init__(self): self.root = EmptyNode()

def size(self): ??? etc…

Page 16: Python: Advanced Objects

A graph G is a pair of sets (V, E) Let V be a set of vertices. A vertex is some object. Let E be a set of edges. A single edge is a pair of vertices. An edge establishes

a connection between two vertices. A graph is undirected if the edge is unordered A graph is directed if the edge is ordered.

Consider the following graph: V = {1,2,3,4,5,6} E = {{1,2}, {1,5}, {2,3}, {2,5}, {3, 4}, {4, 5}, {4, 6}}

Vertex a is adjacent to vertex b iff {a,b} is in E. If {u,v} is a directed edge

u is the predecessor to v v is the successor to u The in-degree of a vertex is the number of its successors The out-degree of a vertex is the number of its predecessors

Graphs

Page 17: Python: Advanced Objects

A weighted graph is a graph (directed or undirected) where each edge is associated with a weight. A weight is a numeric value. Consider the following graph: V = {1,2,3,4,5,6} E = {{1,2}:12, {1,5}:18, {2,3}:1, {2,5}:3, {3, 4}:-5,

{4, 5}:6, {4, 6}:9}

Graph Properties

Page 18: Python: Advanced Objects

Define GIs [F, E, C, D] a path?

length? weight?

Example

Page 19: Python: Advanced Objects

Associate every node with an airport and every edge with a flight and every weight with a time/distance. This is a reasonable model of an airline.

Associate every node with a router and every edge with a connection and every weight with a time. This is a reasonable model of a computer network.

Associate every node with a course in the CS-major and a directed edge as a pre-requisite. This is a reasonable model of a college curriculum. You could even weight the edges with the credit hours.

Applications

Page 20: Python: Advanced Objects

Representation

There are two basic ways to represent a graph G = (V,E)1. Adjacency matrix: Form a 2D graph of VxV in size called A.

Each vertex is labeled with an integer 0 to (V-1) If G is not weighted: A[i][j] is true if edge (i,j) is in E and false

otherwise If G is weighted: A[i][j] is the weight of edge (i,j). If there is no edge

(i,j) then A[i][j] is set to some sentinel value (perhaps 0 or negative infinity or positive infinity or null depending on the specific needs of the application).

2. Adjacency list: For each vertex we form a list of vertices to which it is adjacent.

These lists can be contained within the vertex itself These lists can be stored in an array and accessed by vertex number If ordered, the list for a vertex contains only the outgoing vertices.

Page 21: Python: Advanced Objects

Example

http://compgeom.cs.uiuc.edu/~jeffe/teaching/algorithms/notes/17-graphs.pdf

Page 22: Python: Advanced Objects

Structure

Consider writing a graph class such that A vertex is known by a label A vertex keeps a list of direct neighbors Can perform operations such as

addVertex removeVertex getVertices addEdge removeEdge

Page 23: Python: Advanced Objects

Vertex

A vertex is an object that has what attributes? a list of adjacent vertices a label

An edge is not a "thing" in an adjacency list representation

What operations does a vertex support? getLabel getSuccessors addSuccessor

Page 24: Python: Advanced Objects

Vertex

class Vertex: def __init__(self, label): self.label = label self.successors= set() def getLabel(self): return self.label def getSuccessors(self): return self.successors def addSuccessor(self, v): self.successors.add(v)

Page 25: Python: Advanced Objects

Graph

class Graph: def __init__(self): self.vertices = dict() def addVertex(self, label): self.vertices[label] = self.Vertex(label) def getVertex(self, label): return self.vertices.get(label) def containsVertex(self, label): return label in self.vertices def addEdge(self, label1, label2): self.getVertex(label1).addSuccessor(self.getVertex(label2))

Page 26: Python: Advanced Objects

Consider iterating (or visiting) every vertex in a connected graph. There are various strategies that can be generalized as:

Depth first search uses a stack (LIFO) rather than a set Breadth first search uses a queue (FIFO) rather than a set

Traversal

algorithm traverse(Graph G, Vertex S): Let UNVISITED be a an empty set of vertices UNVISITED.add(S) while size of UNIVISITED > 0: V = UNVISITED.remove() visit v for each edge (v, w) in G: if W has not been visited then UNVISITED.add(W)

Page 27: Python: Advanced Objects

Consider a directed acyclic graph such that ever node is a course in the CS major and each edge represents a pre-requisite structure. Determine in which order courses could be taken to complete the major.

A topological sort of a directed acyclic graph G is an ordering of the vertices such that if there is a path from v[i] to v[j] then vertex v[i] occurs before v[j] in the ordering.

Topological sort

Page 28: Python: Advanced Objects

Give a topological sort of this graphGive an algorithm to topologically sort a

graph G.

Topological sort

List<Vertex> algorithm(Graph G): Let Q be an empty list Let L be an empty list for each V in G: if(inDegree(V) == 0): Q.addLast(V) while(!q.isEmpty()): V = q.removeFirst() L.addLast(V) for each W adjacent to V: inDegree(W)— if inDegree(W) == 0: Q.addLast(W)

if L.size() != |V|: threw new Exception("Cycle found") return L;