Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python: Advanced Objects

Similar presentations


Presentation on theme: "Python: Advanced Objects"— Presentation transcript:

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

2 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

3 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

4 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

5 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 T 1, T 2, …, T k are trees with root nodes N 1, N 2, …, N k, then so is the tree having N as the root and adding edges {N, N 1 }, {N, N 2 }, …, {N, N k }.  Trees are usually drawn “upside” down. Root at the top. 5

6 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

7 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

8 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 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 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

9 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 T algorithm 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 # This returns the tree that results from inserting V into T algorithm 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

10 Searching Give a binary search tree node T, describe an algorithm for finding a value # returns true if T contains V and false otherwise algorithm 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) # returns true if T contains V and false otherwise algorithm 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)

11 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

12 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

13 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

14 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 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): ??? 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): ???

15 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… class SearchTree: def __init__(self): self.root = EmptyNode() def size(self): ??? etc…

16 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

17 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

18 Define G Is [F, E, C, D] a path?  length?  weight? Example

19 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

20 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.

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

22 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

23 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

24 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) 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)

25 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)) 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))

26 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) 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)

27 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

28 Give a topological sort of this graph Give an algorithm to topologically sort a graph G. Topological sort List 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; List 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;


Download ppt "Python: Advanced Objects"

Similar presentations


Ads by Google