Scientific Programming. Data structures

Size: px
Start display at page:

Download "Scientific Programming. Data structures"

Transcription

1 Scientific Programming Data structures Alberto Montresor Università di Trento 2017/11/21 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

2 Table of contents 1 Abstract data types (PSADS 1.8, 1.13) Sequences Sets Dictionaries 2 Elementary data structures implementations Linked List (PSADS ) Dynamic vectors 3 Sets, Dictionary implementations in Python Direct access tables Hash functions Hashtable implementation 4 Stack and queues Stack (PSADS ) Queue (PSADS , ) 5 Some code examples

3 Abstract data types (PSADS 1.8, 1.13) Definitions Introduction Data In a programming language, a piece of data is a value that can be assigned to a variable. Abstract data type (ADT) A mathematical model, given by a collection of values and a set of operators that can be performed on them. Primitive abstract data types Primitive abstract data types are provided directly by the language Type Operators Notes int +,-,*,/, //, % bits (2 64 1) long +,-,*,/, //, % Unlimited integer precision float +,-,*,/, //, % bits boolean and, or, not True, False Alberto Montresor (UniTN) SP - Data structure 2017/11/21 1 / 69

4 Abstract data types (PSADS 1.8, 1.13) Definitions Data types "Specification" and "implementation" of an abstract data type Specification: its manual, hides the implementation details from the user Implementation: the actual code that realizes the abstract data type Example Real numbers vs IEEE-754 Don t try this at home (or try it?): >>> Alberto Montresor (UniTN) SP - Data structure 2017/11/21 2 / 69

5 Abstract data types (PSADS 1.8, 1.13) Definitions Data structure Data structures Data structures are collections of data, characterized more by the organization of the data rather than the type of contained data. How to describe data structures a systematic approach to organize the collection of data a set of operators that enable the manipulation of the structure Characteristics of the data structures Linear / Non linear (sequence) Static / Dynamic (content variation, size variation) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 3 / 69

6 Abstract data types (PSADS 1.8, 1.13) Definitions Data structures Type Java C++ Python Sequences List, Queue, Deque list, forward_list list LinkedList, vector tuple ArrayList, Stack, ArrayDeque stack queue, deque deque Sets Dictionaries Set TreeSet, HashSet, LinkedHashSet Map HashTree, HashMap, LinkedHashMap set unordered_set map unordered_map Trees set, frozenset dict Graphs Alberto Montresor (UniTN) SP - Data structure 2017/11/21 4 / 69

7 Abstract data types (PSADS 1.8, 1.13) Sequences Sequence Sequence A dynamic data structure representing an "ordered" group of elements The ordering is not defined by the content, but by the relative position inside the sequence (first element, second element, etc.) Values could appear more than once Example: [0.1, "alberto", 0.05, 0.1] is a sequence Alberto Montresor (UniTN) SP - Data structure 2017/11/21 5 / 69

8 Abstract data types (PSADS 1.8, 1.13) Sequences Sequence Operators It is possible to add / remove elements, by specifying their position s = s 1, s 2,..., s n the element s i is in position pos i It is possible to access directly some of the elements of the sequence the beginning and/or the end of the list having a reference to the position It is possible to sequentially access all the other elements Alberto Montresor (UniTN) SP - Data structure 2017/11/21 6 / 69

9 Abstract data types (PSADS 1.8, 1.13) Sequences Sequence Specification Sequence % Return True if the sequence is empty boolean isempty() % Returns the position of the first element Pos head() % Returns the position of the last element Pos tail() % Returns the position of the successor of p Pos next(pos p) % Returns the position of the predecessor of p Pos prev(pos p) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 7 / 69

10 Abstract data types (PSADS 1.8, 1.13) Sequences Sequence Specification Sequence (continue) % Inserts element v of type object in position p. % Returns the position of the new element Pos insert(pos p, object v) % Removes the element contained in position p. % Returns the position of the successor of p, which % becomes successor of the predecessor of p Pos remove(pos p) % Reads the element contained in position p object read(pos p) % Writes the element v of type object in position p write(pos p, object v) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 8 / 69

11 Abstract data types (PSADS 1.8, 1.13) Sets Sets Set A dynamic, non-linear data structure that stores an unordered collection of values without repetitions. We can consider a total order between elements as the order defined over their abstract data type, if present. Operators Basic operators: insert delete contains Sorting operators Maximum Minimum Set operators union intersection difference Iterators: for x in S: Alberto Montresor (UniTN) SP - Data structure 2017/11/21 9 / 69

12 Abstract data types (PSADS 1.8, 1.13) Sets Sets Specifications Set % Returns the size of the set int len() % Returns True if x belongs to the set; Python: x in S boolean contains(object x) % Inserts x in the set, if not already present add(object x) % Removes x from the set, if present discard(object x) % Returns a new set which is the union of A and B Set union(set A, Set B) % Returns a new set which is the intersection of A and B Set intersection(set A, Set B) % Returns a new set which is the difference of A and B Set difference(set A, Set B) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 10 / 69

13 Abstract data types (PSADS 1.8, 1.13) Dictionaries Dictionaries Dictionary Abstract data structure that represents the mathematical concept of partial function R : D C, or key-value association Set D is the domain (elements called keys) Set C is the codomain (elements called values) Operators Lookup the value associated to a particular key, if present, None otherwise Insert a new key-value association, deleting potential association that are already present for the same key Remove an existing key-value association Alberto Montresor (UniTN) SP - Data structure 2017/11/21 11 / 69

14 Abstract data types (PSADS 1.8, 1.13) Dictionaries Dictionaries Specification Dictionary % Returns the value associated to key k, if present; returns none otherwise object lookup(object k) % Associates value v to key k insert(object k, object v) % Removes the association of key k remove(object k) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 12 / 69

15 Abstract data types (PSADS 1.8, 1.13) Dictionaries Comments Concepts of sequences, sets, dictionaries are linked Key sets/ value sets Iterate over the set of keys Some realizations are "natural" Sequence list Queue queue as a list Alternative implementations exist Set as boolean vector Queue as circular vector The choice of the data structure has implications on the performances Dictionary as a hash table: lookup O(1), minimum search O(n) Dictionary as a tree: lookup O(log n), minimum search O(1) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 13 / 69

16 Contents 1 Abstract data types (PSADS 1.8, 1.13) Sequences Sets Dictionaries 2 Elementary data structures implementations Linked List (PSADS ) Dynamic vectors 3 Sets, Dictionary implementations in Python Direct access tables Hash functions Hashtable implementation 4 Stack and queues Stack (PSADS ) Queue (PSADS , ) 5 Some code examples

17 Elementary data structures implementations Linked List (PSADS ) List List (Linked List) A sequence of memory objects, containing arbitrary data and 1-2 pointers to the next element and/or the previous one Note Contiguity in the list contiguity in memory All the operations require O(1), but in some cases you need a lot of single operations to complete an action Possible implementations Bidirectional / Monodirectional With sentinel / Without sentinel Circular / Non-circular Alberto Montresor (UniTN) SP - Data structure 2017/11/21 14 / 69

18 Elementary data structures implementations Linked List (PSADS ) List L v1 v2 v3 vn nil Monodirectional L nil v1 v2 v3 vn nil Bidirectional L v1 v2 v3 vn Bidirectional, circular L v1 v2 vn nil Monodirectional, with sentinel Alberto Montresor (UniTN) SP - Data structure 2017/11/21 15 / 69

19 Elementary data structures implementations Linked List (PSADS ) Monodirectional list, Python class Node: def init (self,initdata): self.data = initdata self.next = None def getdata(self): return self.data def getnext(self): return self.next def setdata(self,newdata): self.data = newdata def setnext(self,newnext): self.next = newnext Alberto Montresor (UniTN) SP - Data structure 2017/11/21 16 / 69

20 Elementary data structures implementations Linked List (PSADS ) Monodirectional list, Python class UnorderedList: def init (self): self.head = None def add(self,item): temp = Node(item) temp.setnext(self.head) self.head = temp Alberto Montresor (UniTN) SP - Data structure 2017/11/21 17 / 69

21 Elementary data structures implementations Linked List (PSADS ) Monodirectional list, Python class UnorderedList: def init (self): self.head = None def add(self,item): temp = Node(item) temp.setnext(self.head) self.head = temp Alberto Montresor (UniTN) SP - Data structure 2017/11/21 18 / 69

22 Elementary data structures implementations Linked List (PSADS ) Monodirectional list, Python class UnorderedList: # Continued def search(self,item): current = self.head found = False while current!= None and not found: if current.getdata() == item: found = True else: current = current.getnext() return found Alberto Montresor (UniTN) SP - Data structure 2017/11/21 19 / 69

23 Elementary data structures implementations Linked List (PSADS ) Monodirectional list, Python def remove(self,item): current = self.head previous = None found = False while not found: if current.getdata() == item: found = True else: previous = current current = current.getnext() if previous == None: self.head = current.getnext() else: previous.setnext(current.getnext()) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 20 / 69

24 Elementary data structures implementations Dynamic vectors Dynamic vectors Lists in Python implemented through dynamic vectors A vector of a given size (initial capacity) is allocated When inserting an element before the end, all elements have to be moved - cost O(n) When inserting an element at the end (append), the cost is O(1) (just writing the element at first available slot) Problem: Solution: It is not known how many elements have to be stored The initial capacity could be insufficient A new (larger) vector is allocated, the content is copied in the new vector, the old vector is released Alberto Montresor (UniTN) SP - Data structure 2017/11/21 21 / 69

25 Elementary data structures implementations Dynamic vectors Dynamic vectors Question Which is the best approach? Approach 1 If the old vector has size n, allocate a new vector of size dn. For example, d = 2 Approach 2 If the old vector has size n, allocate a new vector of size n + d, where d is a constant. For example, d = 16 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 22 / 69

26 Elementary data structures implementations Dynamic vectors Amortized analysis - doubling Actual cost{ of an append() operation: i k Z + 0 c i = : i = 2k otherwise Assumptions: Initial capacity: 1 Writing cost: Θ(1) n cost = = = = = 17 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 23 / 69

27 Elementary data structures implementations Dynamic vectors Amortized analysis - doubling Actual cost of n operations append(): n T (n) = i=1 c i log n = n + j=0 2 j = n + 2 log n +1 1 n + 2 log n+1 1 = n + 2n 1 = O(n) Amortized cost of a single append(): T (n)/n = O(n) n = O(1) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 24 / 69

28 Elementary data structures implementations Dynamic vectors Amortized analysis - increment Actual cost of an append() operation: { i (i mod d) = 1 c i = 1 altrimenti Assumptions: Increment: d Initial size: d Writing cost: Θ(1) In the example: d = 4 n cost d = d = d = d = 17 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 25 / 69

29 Elementary data structures implementations Dynamic vectors Amortized analysis - increment Actual cost of n operations append(): n T (n) = i=1 c i n/d = n + d j j=1 n/d = n + d j j=1 ( n/d + 1) n/d = n + d 2 (n/d + 1)n n + = O(n 2 ) 2 Amortized cost of a single append(): T (n)/n = O(n2 ) = O(n) n Alberto Montresor (UniTN) SP - Data structure 2017/11/21 26 / 69

30 Elementary data structures implementations Dynamic vectors Reality check Language Data structure Expansion factor GNU C++ std::vector 2.0 Microsoft VC vector 1.5 Python list Oracle Java ArrayList 2.0 OpenSDK Java ArrayList 1.5 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 27 / 69

31 Elementary data structures implementations Dynamic vectors Python List Operator Worst case Worst case amortized L.copy() Copy O(n) O(n) L.append(x) Append O(n) O(1) L.insert(i,x) Insert O(n) O(n) L.remove(x) Remove O(n) O(n) L[i] Index O(1) O(1) for x in L Iterator O(n) O(n) L[i:i+k] Slicing O(k) O(k) L.extend(s) Extend O(k) O(n + k) x in L Contains O(n) O(n) min(l), max(l) Min, Max O(n) O(n) len(l) Get length O(1) O(1) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 28 / 69

32 Contents 1 Abstract data types (PSADS 1.8, 1.13) Sequences Sets Dictionaries 2 Elementary data structures implementations Linked List (PSADS ) Dynamic vectors 3 Sets, Dictionary implementations in Python Direct access tables Hash functions Hashtable implementation 4 Stack and queues Stack (PSADS ) Queue (PSADS , ) 5 Some code examples

33 Sets, Dictionary implementations in Python Dictionary: possible implementations Unordered array Ordered array Linked List RB Tree Ideal impl. insert() O(1), O(n) O(n) O(1), O(n) O(log n) O(1) lookup() O(n) O(log n) O(n) O(log n) O(1) remove() O(n) O(n) O(n) O(log n) O(1) Ideal implementation: hash tables Choose a hash function h that maps each key k U to an integer h(k) The key-value k, v is stored in a list at position h(k) This vector is called hash table Alberto Montresor (UniTN) SP - Data structure 2017/11/21 29 / 69

34 Sets, Dictionary implementations in Python Hash table Definitions All the possible keys are contained in the universe set U of size u The table is stored in list T [0... m 1] with size m An hash function is defined as: h : U {0, 1,..., m 1} Keys Alberto Montresor Cristian Consonni Alessio Guerrieri Edsger Dijkstra Hash function Hash table m-3 m-2 m-1 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 30 / 69

35 Sets, Dictionary implementations in Python Collisions When two or more keys in the dictionary have the same hash values, we say that a collision has happened Ideally, we want to have hash functions with no collisions Keys Hash functions Hash table Alberto Montresor Cristian Consonni Alessio Guerrieri Edsger Dijkstra Collision m-3 m-2 m-1 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 31 / 69

36 Sets, Dictionary implementations in Python Direct access tables Direct access tables In some cases: the set U is already a (small) subset of Z + The set of year days, numbered from 1 to 366 The set of Kanto s Pokemons, numbered from 1 to 151 Direct access tables We use the identity function h(k) = k as hash function We select m = u Problems If u is very large, this approach may be infeasible If u is not large but the number of keys that are actually recorded is much smaller than u = m, memory is wasted Alberto Montresor (UniTN) SP - Data structure 2017/11/21 32 / 69

37 Sets, Dictionary implementations in Python Hash functions Perfect hash functions Definition A hash function h is called perfect if h is injective, i.e. k 1, k 2 U : k 1 k 2 h(k 1 ) h(k 2 ) Examples Students ASD N. matricola in [ , ] h(k) = k , m = Studentes enrolled in 2014 N. matricola in [ , ] h(k) = k , m = Problems Universe space is often large, sparse, unknown To obtain a perfect hash function is difficult Alberto Montresor (UniTN) SP - Data structure 2017/11/21 33 / 69

38 Sets, Dictionary implementations in Python Hash functions Hash functions If collisions cannot be avoided Let s try to minimize their number We want hash functions that uniform distribute the keys into hash indexes [0... m 1] Simple uniformity Let P (k) be the probability that a key k is inserted in the table Let Q(i) be the probability that a key ends up in the i-th entry of the table Q(i) = P (k) k U:h(k)=i An hash function h has simple uniformity if: i [0,..., m 1] : Q(i) = 1/m Alberto Montresor (UniTN) SP - Data structure 2017/11/21 34 / 69

39 Sets, Dictionary implementations in Python Hash functions Hash functions To obtain a hash function with simple uniformity, distribution P should be known the probability Example if U is given by real number in [0, 1[ and each key has the same probability of being selected, then H(k) = km has simple uniformity In the real world The key distribution may unknown or partially known Heuristic techniques are used to obtain an approximation of simple uniformity Alberto Montresor (UniTN) SP - Data structure 2017/11/21 35 / 69

40 Sets, Dictionary implementations in Python Hash functions How to realize a hash function Assumption Each key can be translated in a numerical, non-negative values, by reading their internal representation as a number. Example: string transformation ord(c): ordinal binary value of character c in ASCII bin(k): binary representation of key k, by concatenating the binary values of its characters int(b): numerical value associated to the binary number b int(k) = int(bin(k)) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 36 / 69

41 Sets, Dictionary implementations in Python Hash functions Division method Division method Let m be a prime number H(k) = int(k) mod m Example m = 383 H( Alberto ) = mod 383 = 221 H( Alessio ) = mod 383 = 77 H( Cristian ) = mod 383 = 130 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 37 / 69

42 Sets, Dictionary implementations in Python Hash functions Multiplication method Multiplication method Le m be any size, for example 2 k Let C be a real constant, 0 < C < 1 Let i = int(k) H(k) = m(c i C i ) Example m = 2 16 C = H( Alberto ) = = H( Alessio ) = = H( Cristian ) = = Alberto Montresor (UniTN) SP - Data structure 2017/11/21 38 / 69

43 Sets, Dictionary implementations in Python Tecniche di risoluzione delle collisioni Separate chaining Liste di trabocco (chaining) Idea Gli elementi con lo stesso The valore keys hash with h vengono the same value hmemorizzati are storedin inuna a lista monodirectional Si memorizza un puntatore list / dynamic alla testa della vector lista nello slot A[h] della tabella hash The H(k)-th slot in the hash Operazioni: table contains the list/vector associated Insert: to k inserimento in testa Lookup, Delete: richiedono di scandire la lista alla ricerca della chiave Hashtable implementation 0 k 1 k 4 k 5 k 2 k 6 k 7 k 3 k 8 m 1 Alberto Montresor 20 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 39 / 69

44 Sets, Dictionary implementations in Python Hashtable implementation Separate chaining: complexity analysis n m α = n/m I(α) S(α) Number of keys stored in the hash table Size of the hash table Load factor Average number of memory accesses to search a key that is not in the table (insuccess) Average number of memory accesses to search a key that is not in the table (success) Worst case analysis All the keys are inserted in a unique list insert(): Θ(1) lookup(), remove(): Θ(n) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 40 / 69

45 Sets, Dictionary implementations in Python Hashtable implementation eorema: Separate chaining: complexity analysis In tavola hash con concatenamento, una ricerca senza successo richiede un tempo Average atteso Ө(1 case + α) analysis imostrazione: Let s assume the hash function has simple uniformity Una chiave non presente nella tabella può essere collocata in uno qualsiasi degli m slot Hash function computation: Θ(1), to be added to all searches How Una ricerca long the senza lists successo are? tocca tutte le chiavi nella lista corrispondente Tempo di hashing: 1 + lunghezza The expected attesa lista: length α of Θ(1+α) a list is equal to α = n/m 1 k 1 k 4 α Montresor 2 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 41 / 69

46 Sets, Dictionary implementations in Python Hashtable implementation Separate chaining: complexity analysis Insuccess Success When searching for a missing key, all the keys in the list must be read When searching for a key included in the table, on average half of the keys in the list must be read. Expected cost: Θ(1) + α Expected cost: Θ(1) + α/2 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 42 / 69

47 Sets, Dictionary implementations in Python Hashtable implementation Separate chaining: complexity analysis What is the meaning of the load factor? The cost factor of every operation is influenced by the cost factor If m = O(n), α = O(1) In such case, all operations are O(1) in expectation If α becomes too large, the size of the hash table can be doubled through dynamic vectors Alberto Montresor (UniTN) SP - Data structure 2017/11/21 43 / 69

48 Sets, Dictionary implementations in Python Hashtable implementation Python and hash function/tables Python sets and dict Are implemented through hash tables Sets are degenerate forms of dictionaries, where there are no values, only keys Unordered data structures Order between keys is not preserved by the hash function; this is why you get unordered results when you print them Alberto Montresor (UniTN) SP - Data structure 2017/11/21 44 / 69

49 Sets, Dictionary implementations in Python Hashtable implementation Python set Operation Average case Worst case x in S Contains O(1) O(n) S.add(x) Insert O(1) O(n) S.remove(x) Remove O(1) O(n) S T Union O(n + m) O(n m) S&T Intersection O(min(n, m)) O(n m) S-T Difference O(n) O(n m) for x in S Iterator O(n) O(n) len(s) Get length O(1) O(1) min(s), max(s) Min, Max O(n) O(n) n = len(s), m = len(t) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 45 / 69

50 Sets, Dictionary implementations in Python Hashtable implementation Python dict Operation Average case Worst case x in D Contains O(1) O(n) D[] = Insert O(1) O(n) = D[] Lookup O(1) O(n) del D[] Remove O(1) O(n) for x in S Iterator O(n) O(n) len(s) Get length O(1) O(1) n = len(s), m = len(t) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 46 / 69

51 Sets, Dictionary implementations in Python Hashtable implementation Implementing hash functions Rule: If two objects are equal, then their hashes should be equal If you implement eq (), then you should implement function hash () as well Rule: If two objects have the same hash, then they are likely to be equal You should avoid to return values that generate collisions in your hash function. Rule In order for an object to be hashable, it must be immutable The hash value of an object should not change over time Further details what-happens-when-you-mess-with-hashing-in-python/ Alberto Montresor (UniTN) SP - Data structure 2017/11/21 47 / 69

52 Sets, Dictionary implementations in Python Hashtable implementation Example class Point: def init (self, x, y): self.x = x self.y = y def eq (self, other): return self.x == other.x and self.y == other.y def hash (self): return hash( (self.x,self.y) ) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 48 / 69

53 Contents 1 Abstract data types (PSADS 1.8, 1.13) Sequences Sets Dictionaries 2 Elementary data structures implementations Linked List (PSADS ) Dynamic vectors 3 Sets, Dictionary implementations in Python Direct access tables Hash functions Hashtable implementation 4 Stack and queues Stack (PSADS ) Queue (PSADS , ) 5 Some code examples

54 Stack and queues Stack Stack Stack A linear, dynamic data structure, in which the operation "remove" returns (and removes) a predefined element: the one that has remained in the data structure for the least time (LIFO - Last-in, First-out) Stack % Returns True if the stack is empty boolean isempty() % Returns the size of the stack boolean size() % Inserts v on top of the stack push(object v) % Removes the top element of the stack and returns it to the caller object pop() % Read the top element of the stack, without modifying it object peek() Alberto Montresor (UniTN) SP - Data structure 2017/11/21 49 / 69

55 Stack and queues Stack Stack example Stack Operation Stack Contents Return Value s.isempty() [] True s.push(4) [4] s.push( dog ) [4, dog ] s.peek() [4, dog ] dog s.push(true) [4, dog,true] s.size() [4, dog,true] 3 s.isempty() [4, dog,true] False s.push(8.4) [4, dog,true,8.4] s.pop() [4, dog,true] 8.4 s.pop() [4, dog ] True s.size() [4, dog ] 2 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 50 / 69

56 Stack and queues Stack Stack Possible uses In languages like Python: In the compiler: To balance parentheses In the the interpreter: A new activation record is created for each function call top In graph analysis: To perform visits of the entire graph Possible implementations Through bidirectional lists reference to the top element Through vectors limited size, small overhead top A Alberto Montresor (UniTN) SP - Data structure 2017/11/21 51 / 69

57 Stack and queues Stack Stack in procedural languages def func3(y): z = y y = y*y return z+y def func2(x): y = x x = x*3 y = x * func3(y) return y+x def func1(w): x = w w = w*2 x = w + func2(x) return x+w Alberto Montresor (UniTN) SP - Data structure 2017/11/21 52 / 69

58 Stack and queues Stack Stack in Python class Stack: def init (self): self.items = [] def size(self): return len(self.items) def isempty(self): return self.items == [] def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1] def push(self, item): self.items.append(item) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 53 / 69

59 Stack and queues Stack Example of application: balanced parenthesis Check whether the following sets of parentheses are balanced { { ( [ ] [ ] ) } ( ) } [ [ { { ( ( ) ) } } ] ] [ ] [ ] [ ] ( ) { } ( [ ) ] ( ( ( ) ] ) ) [ { ( ) ] These parentheses could be associated to sets, lists, tuples and/or arithmetic operations Alberto Montresor (UniTN) SP - Data structure 2017/11/21 54 / 69

60 Stack and queues Stack Example of application: balanced parenthesis def parchecker(parstring): s = Stack() index = 0 for symbol in parstring: if symbol in "([{": s.push(symbol) else: if s.isempty(): return False else: top = s.pop() if not matches(top,symbol): return False return s.isempty() Alberto Montresor (UniTN) SP - Data structure 2017/11/21 55 / 69

61 Stack and queues Stack Example of application: balanced parenthesis def matches(openpar,closepar): opens = "([{" closers = ")]}" return opens.index(openpar) == closers.index(closepar) print(parchecker( {{([][])}()} )) print(parchecker( [{()] )) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 56 / 69

62 Stack and queues Queue Queue Queue A linear, dynamic data structure, in which the operation "remove" returns (and removes) a predefined element: the one that has remained in the data structure for the longest time (FIFO - First-in, First-out) Queue % Returns True if queue is empty boolean isempty() % Returns the size of the queue int size() % Inserts v at the end of the queue enqueue(object v) % Extracts q from the beginning of the queue object dequeue() % Reads the element at the top of the queue object top() Alberto Montresor (UniTN) SP - Data structure 2017/11/21 57 / 69

63 Stack and queues Queue Queue example Queue Operation Queue Contents Return Value q.isempty() [] True q.enqueue(4) [4] q.enqueue( dog ) [ dog,4] q.enqueue(true) [True, dog,4] q.size() [True, dog,4] 3 q.isempty() [True, dog,4] False q.enqueue(8.4) [8.4,True, dog,4] q.dequeue() [8.4,True, dog ] 4 q.dequeue() [8.4,True] dog q.size() [8.4,True] 2 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 58 / 69

64 Stack and queues Queue Queue Possible uses To queue requests performed on a limited resource (e.g., printer) To visit graphs head tail Possible implementations Through lists add to the tail remove from the head Through circular array limited size, small overhead Alberto Montresor (UniTN) SP - Data structure 2017/11/21 59 / 69

65 Stack and queues Queue Queue in Python (wrong) class Queue: def init (self): self.items = [] def isempty(self): return self.items == [] def enqueue(self, item): self.items.insert(0,item) def dequeue(self): return self.items.pop() def size(self): return len(self.items) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 60 / 69

66 Stack and queues Queue Queue based on circular array Implementation based on the modulus operation Pay attention to overflow problems (full queue) head+n head head+n enqueue(12) head head+n dequeue() 3 head+n head enqueue(15,17,33) head Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

67 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

68 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

69 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

70 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

71 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

72 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

73 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

74 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

75 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

76 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

77 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

78 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

79 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

80 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

81 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

82 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

83 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

84 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

85 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

86 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

87 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

88 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

89 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

90 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

91 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

92 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

93 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

94 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

95 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

96 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

97 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

98 Stack and queues Queue Queue based on circular array By MuhannadAjjan [CC BY-SA 4.0] ( via Wikimedia Commons Alberto Montresor (UniTN) SP - Data structure 2017/11/21 61 / 69

99 Stack and queues Queue Queue based on circular array Pseudocode Queue A size head cap % Elements % Current size % Head of the queue % Maximum size Queue(self, dim) self.a new int[0... dim 1] self.cap dim self.head 0 self.size 0 top() if size > 0 then return A[head] dequeue() if size > 0 then temp A[head] head (head + 1)%cap size size 1 return temp enqueue(v) if size < cap then A[(head + size)%cap] v size size + 1 size() return size isempty() return size = 0 Alberto Montresor (UniTN) SP - Data structure 2017/11/21 62 / 69

100 Stack and queues Queue Python data structure - Dequeue >>> from collections import deque >>> Q = deque(["eric", "John", "Michael"]) >>> Q.append("Terry") # Terry arrives >>> Q.append("Graham") # Graham arrives >>> Q.popleft() # The first to arrive now leaves Eric >>> Q.popleft() # The second to arrive now leaves John >>> Q # Remaining queue in order of arrival deque([ Michael, Terry, Graham ]) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 63 / 69

101 Contents 1 Abstract data types (PSADS 1.8, 1.13) Sequences Sets Dictionaries 2 Elementary data structures implementations Linked List (PSADS ) Dynamic vectors 3 Sets, Dictionary implementations in Python Direct access tables Hash functions Hashtable implementation 4 Stack and queues Stack (PSADS ) Queue (PSADS , ) 5 Some code examples

102 Some code examples Reverse string def reverse(s): n = len(s)-1 res = "" while n >= 0: res = res + s[n] n -= 1 return res Alberto Montresor (UniTN) SP - Data structure 2017/11/21 64 / 69

103 Some code examples Reverse string def reverse(s): n = len(s)-1 res = "" while n >= 0: res = res + s[n] n -= 1 return res Complexity: Θ(n 2 ) n string sums Each sum copies all the characters in a new string Alberto Montresor (UniTN) SP - Data structure 2017/11/21 64 / 69

104 Some code examples Reverse string def reverse(s): res = [] for c in s: res.insert(0, c) return "".join(res) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 65 / 69

105 Some code examples Reverse string def reverse(s): res = [] for c in s: res.insert(0, c) return "".join(res) Complexity: Θ(n 2 ) n list inserts Each insert moves all characters one position up in the list Alberto Montresor (UniTN) SP - Data structure 2017/11/21 65 / 69

106 Some code examples Reverse string def reverse(s): n = len(s)-1 res = [] while n >= 0: res.append(s[n]) n -= 1 return "".join(res) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 66 / 69

107 Some code examples Reverse string def reverse(s): n = len(s)-1 res = [] while n >= 0: res.append(s[n]) n -= 1 return "".join(res) Complexity: Θ(n) n list append Each append has an amortized cost of O(1) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 66 / 69

108 Some code examples Reverse string def reverse(s): n = len(s)-1 res = [] while n >= 0: res.append(s[n]) n -= 1 return "".join(res) Complexity: Θ(n) n list append Each append has an amortized cost of O(1) Better solution def reverse(s): return s[::-1] Alberto Montresor (UniTN) SP - Data structure 2017/11/21 66 / 69

109 Some code examples Remove duplicates def deduplicate(l): res=[] for item in L: if item not in res: res.append(item) return res Alberto Montresor (UniTN) SP - Data structure 2017/11/21 67 / 69

110 Some code examples Remove duplicates def deduplicate(l): res=[] for item in L: if item not in res: res.append(item) return res Complexity: Θ(n 2 ) n list append n checks whether an element is already present Each check costs O(n) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 67 / 69

111 Some code examples Remove duplicates def deduplicate(l): res=[] present=set() for item in L: if item not in present: res.append(item) present.add(item) return res Alberto Montresor (UniTN) SP - Data structure 2017/11/21 68 / 69

112 Some code examples Remove duplicates def deduplicate(l): res=[] present=set() for item in L: if item not in present: res.append(item) present.add(item) return res Complexity: Θ(n) n list append n checks whether an element is already present Each check costs O(1) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 68 / 69

113 Some code examples Remove duplicates def deduplicate(l): res=[] present=set() for item in L: if item not in present: res.append(item) present.add(item) return res Complexity: Θ(n) n list append n checks whether an element is already present Each check costs O(1) Other possibility destroy original order def deduplicate(l): return list(set(l)) Alberto Montresor (UniTN) SP - Data structure 2017/11/21 68 / 69

114 Some code examples Exercise Queues and Priority Queues are data structures which are known to most computer scientists. The Italian Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the cafeteria is an italian queue, for example. In an italian queue each element belongs to a group. If an element enters the queue, it first searches the queue from head to tail to check if some of its group members (elements of the same group) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the italian queue. Alberto Montresor (UniTN) SP - Data structure 2017/11/21 69 / 69

Scientific Programming. Data structures

Scientific Programming. Data structures Scientific Programming Data structures Alberto Montresor Università di Trento 2018/11/21 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Table of contents

More information

What is an algorithm?

What is an algorithm? Announcements CS 142 Stacks & Queues Program 3 has been assigned due 10/6 by 11:55pm Program details on website 2 Creating new Data Structures We ve used objects to create new data types such as Point,

More information

Lecture 12 ADTs and Stacks

Lecture 12 ADTs and Stacks Lecture 12 ADTs and Stacks Modularity Divide the program into smaller parts Advantages Keeps the complexity managable Isolates errors (parts can be tested independently) Can replace parts easily Eliminates

More information

Stack. Stack theory. Stack exercises. Algolab (index.html#chapters) Out[1]: Chapter 2.2: Data Structures - Stacks and.

Stack. Stack theory. Stack exercises. Algolab (index.html#chapters) Out[1]: Chapter 2.2: Data Structures - Stacks and. Algolab (index.html#chapters) Out[1]: Chapter 2.2: Data Structures - Stacks and linked lists Chapter 2.2: Data Structures - Stacks and linked lists Stack Stack theory See theory here: http://disi.unitn.it/~montreso/sp/slides/04-strutture.pdf

More information

Chapter 8 : Computer Science. Datastructures: Class XII ( As per CBSE Board) lists, stacks, queues. Visit : python.mykvs.in for regular updates

Chapter 8 : Computer Science. Datastructures: Class XII ( As per CBSE Board) lists, stacks, queues. Visit : python.mykvs.in for regular updates Chapter 8 : Computer Science Class XII ( As per CBSE Board) Datastructures: lists, stacks, queues It a way of organizing and storing data in such a manner so that it can be accessed and work over it can

More information

Standard ADTs. Lecture 19 CS2110 Summer 2009

Standard ADTs. Lecture 19 CS2110 Summer 2009 Standard ADTs Lecture 19 CS2110 Summer 2009 Past Java Collections Framework How to use a few interfaces and implementations of abstract data types: Collection List Set Iterator Comparable Comparator 2

More information

Algorithms in Systems Engineering ISE 172. Lecture 5. Dr. Ted Ralphs

Algorithms in Systems Engineering ISE 172. Lecture 5. Dr. Ted Ralphs Algorithms in Systems Engineering ISE 172 Lecture 5 Dr. Ted Ralphs ISE 172 Lecture 5 1 References for Today s Lecture Required reading Chapter 3 ISE 172 Lecture 5 2 Delving into the Lists As we have seen

More information

III Data Structures. Dynamic sets

III Data Structures. Dynamic sets III Data Structures Elementary Data Structures Hash Tables Binary Search Trees Red-Black Trees Dynamic sets Sets are fundamental to computer science Algorithms may require several different types of operations

More information

Queues. Queue ADT Queue Implementation Priority Queues

Queues. Queue ADT Queue Implementation Priority Queues Queues Queue ADT Queue Implementation Priority Queues Queue A restricted access container that stores a linear collection. Very common for solving problems in computer science that require data to be processed

More information

COMPSCI 105 S Principles of Computer Science. 17 Linked List(1)

COMPSCI 105 S Principles of Computer Science. 17 Linked List(1) COMPSCI 105 S1 2017 Principles of Computer Science 17 Linked List(1) Agenda Agenda & Readings Introduction The Node class The UnorderedList ADT Comparing Implementations Reference: Textbook: Problem Solving

More information

Data Structure in Python for Data Analytics Prof. Zhang CISC5835, Fall Basic Built-in Python Data Structures

Data Structure in Python for Data Analytics Prof. Zhang CISC5835, Fall Basic Built-in Python Data Structures Data Structure in Python for Data Analytics Prof. Zhang CISC5835, Fall 2018 It s important to know basic data structures and understand the performance impacts of choosing a certain data structure (i.e.,

More information

CSc 120. Introduction to Computer Programming II. 14: Stacks and Queues. Adapted from slides by Dr. Saumya Debray

CSc 120. Introduction to Computer Programming II. 14: Stacks and Queues. Adapted from slides by Dr. Saumya Debray CSc 120 Introduction to Computer Programming II Adapted from slides by Dr. Saumya Debray 14: Stacks and Queues linear data structures 2 Linear data structures A linear data structure is a collec6on of

More information

Week 6. Data structures

Week 6. Data structures 1 2 3 4 5 n Week 6 Data structures 6 7 n 8 General remarks We start considering Part III Data Structures from CLRS. As a first example we consider two special of buffers, namely stacks and queues. Reading

More information

Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada

Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada CS102 Hash Tables Prof. Tejada 1 Vectors, Linked Lists, Stack, Queues, Deques Can t provide fast insertion/removal and fast lookup at the same time The Limitations of Data Structure Binary Search Trees,

More information

CSI33 Data Structures

CSI33 Data Structures Department of Mathematics and Computer Science Bronx Community College Outline Chapter 5: Stacks and 1 Chapter 5: Stacks and Chapter 5: Stacks and A Queue ADT A Container Class for First-In-First-Out Access

More information

Computer Science Fundamentals 107

Computer Science Fundamentals 107 Computer Science Fundamentals 107 OrderedList ADT opera0ons OrderedList ADT using a singly linked list OrderedList implementa0on analysis Doubly linked lists a first taste What is an ordered list? 2 unordered

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS LECTURE 11 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 9-10... Hash tables ADT Stack ADT Queue ADT Deque ADT Priority Queue Hash tables Today Hash tables 1 Hash

More information

Hashing. Hashing Procedures

Hashing. Hashing Procedures Hashing Hashing Procedures Let us denote the set of all possible key values (i.e., the universe of keys) used in a dictionary application by U. Suppose an application requires a dictionary in which elements

More information

CMPSCI 187: Programming With Data Structures. Review for Final Exam David Mix Barrington 10 December 2012

CMPSCI 187: Programming With Data Structures. Review for Final Exam David Mix Barrington 10 December 2012 CMPSCI 187: Programming With Data Structures Review for Final Exam David Mix Barrington 10 December 2012 Exam Overview Thursday 13 December, 1:30-3:30 p.m., Marcus 131 Format is the same as the Fall 2011

More information

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces Taking Stock IE170: Algorithms in Systems Engineering: Lecture 7 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University January 29, 2007 Last Time Practice Some Sorting Algs

More information

Apply to be a Meiklejohn! tinyurl.com/meikapply

Apply to be a Meiklejohn! tinyurl.com/meikapply Apply to be a Meiklejohn! tinyurl.com/meikapply Seeking a community to discuss the intersections of CS and positive social change? Interested in facilitating collaborations between students and non-profit

More information

CS 302 ALGORITHMS AND DATA STRUCTURES

CS 302 ALGORITHMS AND DATA STRUCTURES CS 302 ALGORITHMS AND DATA STRUCTURES A famous quote: Program = Algorithm + Data Structure General Problem You have some data to be manipulated by an algorithm E.g., list of students in a school Each student

More information

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1 Lab 1 available very

More information

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn Chapter 5 Data Structures Algorithm Theory WS 06/ Fabian Kuhn Examples Dictionary: Operations: insert(key,value), delete(key), find(key) Implementations: Linked list: all operations take O(n) time (n:

More information

Walk through previous lectures

Walk through previous lectures Walk through previous lectures Tuple tuple_name = (value, value,..., value) A way of packing multiple values into a variable >>> x = 3 >>> y = -5 >>> p = (x, y, 42) >>> p (3, -5, 42) name, name,..., name

More information

Algorithms and Data Structures

Algorithms and Data Structures Lesson 4: Sets, Dictionaries and Hash Tables Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo)

More information

COMP171. Hashing.

COMP171. Hashing. COMP171 Hashing Hashing 2 Hashing Again, a (dynamic) set of elements in which we do search, insert, and delete Linear ones: lists, stacks, queues, Nonlinear ones: trees, graphs (relations between elements

More information

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1 Lab 1 is available.

More information

Data structure and algorithm in Python

Data structure and algorithm in Python Data structure and algorithm in Python Stacks, Queues and Deques Xiaoping Zhang School of Mathematics and Statistics, Wuhan University Table of contents 1. Stacks 2. Queue 3. Double-Ended Queues 1 Stacks

More information

CSE 332: Data Structures & Parallelism Lecture 10:Hashing. Ruth Anderson Autumn 2018

CSE 332: Data Structures & Parallelism Lecture 10:Hashing. Ruth Anderson Autumn 2018 CSE 332: Data Structures & Parallelism Lecture 10:Hashing Ruth Anderson Autumn 2018 Today Dictionaries Hashing 10/19/2018 2 Motivating Hash Tables For dictionary with n key/value pairs insert find delete

More information

Priority Queue ADT. Revised based on textbook author s notes.

Priority Queue ADT. Revised based on textbook author s notes. Priority Queue ADT Revised based on textbook author s notes. Priority Queues Some applications require the use of a queue in which items are assigned a priority. higher priority items are dequeued first.

More information

CSC148 Week 2. Larry Zhang

CSC148 Week 2. Larry Zhang CSC148 Week 2 Larry Zhang 1 Admin Discussion board is up (link on the course website). 2 Outline for this week Abstract Data Type Stack Queue 3 Abstract Data Type (ADT) 4 What is an abstract data type

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

CSC148H Week 3. Sadia Sharmin. May 24, /20

CSC148H Week 3. Sadia Sharmin. May 24, /20 CSC148H Week 3 Sadia Sharmin May 24, 2017 1/20 Client vs. Developer I For the first couple of weeks, we have played the role of class designer I However, you are also often in the opposite role: when a

More information

COSC160: Data Structures Hashing Structures. Jeremy Bolton, PhD Assistant Teaching Professor

COSC160: Data Structures Hashing Structures. Jeremy Bolton, PhD Assistant Teaching Professor COSC160: Data Structures Hashing Structures Jeremy Bolton, PhD Assistant Teaching Professor Outline I. Hashing Structures I. Motivation and Review II. Hash Functions III. HashTables I. Implementations

More information

Basic Data Structures (Version 7) Name:

Basic Data Structures (Version 7) Name: Prerequisite Concepts for Analysis of Algorithms Basic Data Structures (Version 7) Name: Email: Concept: mathematics notation 1. log 2 n is: Code: 21481 (A) o(log 10 n) (B) ω(log 10 n) (C) Θ(log 10 n)

More information

Collections (Java) Collections Framework

Collections (Java) Collections Framework Collections (Java) https://docs.oracle.com/javase/tutorial/collections/index.html Collection an object that groups multiple elements into a single unit. o store o retrieve o manipulate o communicate o

More information

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions. DATA STRUCTURES COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges book. Data

More information

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials)

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials) CSE100 Advanced Data Structures Lecture 21 (Based on Paul Kube course materials) CSE 100 Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table cost

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s. 1. (5 points) Determine the stack contents at the points indicated below during the following operations on the stack ADT. Write down the stack contents after the operation on the given line is executed.

More information

Agenda & Reading. COMPSCI 105 SS 2015 Principles of Computer Science

Agenda & Reading. COMPSCI 105 SS 2015 Principles of Computer Science Agenda & Reading Agenda Variations of Linked Lists Singly Linked Lists with Head and Tail Doubly Linked Lists with Dummy node Extra Reading: http://en.literateprograms.org/singly_linked_list_(python) COMPSCI

More information

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011 CS/ENGRD 110 Object-Oriented Programming and Data Structures Spring 011 Thorsten Joachims Lecture 16: Standard ADTs Abstract Data Types (ADTs) A method for achieving abstraction for data structures and

More information

Round 1: Basics of algorithm analysis and data structures

Round 1: Basics of algorithm analysis and data structures Round 1: Basics of algorithm analysis and data structures Tommi Junttila Aalto University School of Science Department of Computer Science CS-A1140 Data Structures and Algorithms Autumn 2017 Tommi Junttila

More information

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer) COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back

More information

Queues The Queue ADT. The Queue ADT is defined by the following operations: init Initialize a new empty queue.

Queues The Queue ADT. The Queue ADT is defined by the following operations: init Initialize a new empty queue. Queues This chapter presents two ADTs: the Queue and the Priority Queue. In real life, a queue is a line of customers waiting for service of some kind. In most cases, the first customer in line is the

More information

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu

More information

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40 Lecture 16 Hashing Hash table and hash function design Hash functions for integers and strings Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table

More information

stacks operation array/vector linked list push amortized O(1) Θ(1) pop Θ(1) Θ(1) top Θ(1) Θ(1) isempty Θ(1) Θ(1)

stacks operation array/vector linked list push amortized O(1) Θ(1) pop Θ(1) Θ(1) top Θ(1) Θ(1) isempty Θ(1) Θ(1) Hashes 1 lists 2 operation array/vector linked list find (by value) Θ(n) Θ(n) insert (end) amortized O(1) Θ(1) insert (beginning/middle) Θ(n) Θ(1) remove (by value) Θ(n) Θ(n) find (by index) Θ(1) Θ(1)

More information

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists Chapters 3.1-3.3, 5.1-5.2, 6.1-1 - Core Collection Interfaces - 2 - The Java Collections Framework Interface Abstract

More information

Script language: Python Data structures

Script language: Python Data structures Script language: Python Data structures Cédric Saule Technische Fakultät Universität Bielefeld 3. Februar 2015 Immutable vs. Mutable Previously known types: int and string. Both are Immutable but what

More information

csci 210: Data Structures Maps and Hash Tables

csci 210: Data Structures Maps and Hash Tables csci 210: Data Structures Maps and Hash Tables Summary Topics the Map ADT Map vs Dictionary implementation of Map: hash tables READING: GT textbook chapter 9.1 and 9.2 Map ADT A Map is an abstract data

More information

Name: After line 1: After line 3: After line 5: After line 8: After line 11:

Name: After line 1: After line 3: After line 5: After line 8: After line 11: 1. (5 points) Determine the stack contents at the points indicated below during the following operations on the stack ADT. Write down the stack contents after the operation on the given line is executed.

More information

Stacks, Queues (cont d)

Stacks, Queues (cont d) Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1

More information

CS 270 Algorithms. Oliver Kullmann. Generalising arrays. Direct addressing. Hashing in general. Hashing through chaining. Reading from CLRS for week 7

CS 270 Algorithms. Oliver Kullmann. Generalising arrays. Direct addressing. Hashing in general. Hashing through chaining. Reading from CLRS for week 7 Week 9 General remarks tables 1 2 3 We continue data structures by discussing hash tables. Reading from CLRS for week 7 1 Chapter 11, Sections 11.1, 11.2, 11.3. 4 5 6 Recall: Dictionaries Applications

More information

Data Structures I: Linked Lists

Data Structures I: Linked Lists Lab 4 Data Structures I: Linked Lists Lab Objective: Analyzing and manipulating data are essential skills in scientific computing. Storing, retrieving, and rearranging data take time. As a dataset grows,

More information

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Queues CSE 2011 Fall 2009 9/28/2009 7:56 AM 1 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Applications,

More information

Data Structures and Algorithms. Roberto Sebastiani

Data Structures and Algorithms. Roberto Sebastiani Data Structures and Algorithms Roberto Sebastiani roberto.sebastiani@disi.unitn.it http://www.disi.unitn.it/~rseba - Week 07 - B.S. In Applied Computer Science Free University of Bozen/Bolzano academic

More information

Data Structures and Algorithms. Chapter 7. Hashing

Data Structures and Algorithms. Chapter 7. Hashing 1 Data Structures and Algorithms Chapter 7 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples

More information

Student Number: Lab day:

Student Number: Lab day: CSC 148H1 Summer 2008 Midterm Test Duration 60 minutes Aids allowed: none Last Name: Student Number: Lab day: First Name: Lecture Section: L0101 Instructor: R. Danek Do not turn this page until you have

More information

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Cpt S 223 Fall Cpt S 223. School of EECS, WSU Course Review Cpt S 223 Fall 2012 1 Final Exam When: Monday (December 10) 8 10 AM Where: in class (Sloan 150) Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes

More information

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix CS 21 Final Review About the Final Saturday, 7-10pm in Science Center 101 Closed book, closed notes Not on the final: graphics, file I/O, vim, unix Expect Questions That Ask You To: Evaluate Python expressions

More information

Understand how to deal with collisions

Understand how to deal with collisions Understand the basic structure of a hash table and its associated hash function Understand what makes a good (and a bad) hash function Understand how to deal with collisions Open addressing Separate chaining

More information

Lecture 7: Efficient Collections via Hashing

Lecture 7: Efficient Collections via Hashing Lecture 7: Efficient Collections via Hashing These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole. 1 Announcements Lab 6 due Friday Lab 7 out tomorrow

More information

Week 9. Hash tables. 1 Generalising arrays. 2 Direct addressing. 3 Hashing in general. 4 Hashing through chaining. 5 Hash functions.

Week 9. Hash tables. 1 Generalising arrays. 2 Direct addressing. 3 Hashing in general. 4 Hashing through chaining. 5 Hash functions. Week 9 tables 1 2 3 ing in ing in ing 4 ing 5 6 General remarks We continue data structures by discussing hash tables. For this year, we only consider the first four sections (not sections and ). Only

More information

CIS 190: C/C++ Programming. Lecture 12 Student Choice

CIS 190: C/C++ Programming. Lecture 12 Student Choice CIS 190: C/C++ Programming Lecture 12 Student Choice Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources Hash Maps (AKA Hash Tables)

More information

Course Review for Finals. Cpt S 223 Fall 2008

Course Review for Finals. Cpt S 223 Fall 2008 Course Review for Finals Cpt S 223 Fall 2008 1 Course Overview Introduction to advanced data structures Algorithmic asymptotic analysis Programming data structures Program design based on performance i.e.,

More information

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 Prof. John Park Based on slides from previous iterations of this course Today s Topics Overview Uses and motivations of hash tables Major concerns with hash

More information

Stacks. Revised based on textbook author s notes.

Stacks. Revised based on textbook author s notes. Stacks Revised based on textbook author s notes. Stacks A restricted access container that stores a linear collection. Very common for solving problems in computer science. Provides a last-in first-out

More information

Hashing Algorithms. Hash functions Separate Chaining Linear Probing Double Hashing

Hashing Algorithms. Hash functions Separate Chaining Linear Probing Double Hashing Hashing Algorithms Hash functions Separate Chaining Linear Probing Double Hashing Symbol-Table ADT Records with keys (priorities) basic operations insert search create test if empty destroy copy generic

More information

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER

More information

Review. CSE 143 Java. A Magical Strategy. Hash Function Example. Want to implement Sets of objects Want fast contains( ), add( )

Review. CSE 143 Java. A Magical Strategy. Hash Function Example. Want to implement Sets of objects Want fast contains( ), add( ) Review CSE 143 Java Hashing Want to implement Sets of objects Want fast contains( ), add( ) One strategy: a sorted list OK contains( ): use binary search Slow add( ): have to maintain list in sorted order

More information

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1 Queues ADT description Implementations Cinda Heeren / Geoffrey Tien 1 Queues Assume that we want to store data for a print queue for a student printer Student ID Time File name The printer is to be assigned

More information

Quiz 1 Practice Problems

Quiz 1 Practice Problems Introduction to Algorithms: 6.006 Massachusetts Institute of Technology March 7, 2008 Professors Srini Devadas and Erik Demaine Handout 6 1 Asymptotic Notation Quiz 1 Practice Problems Decide whether these

More information

Abstract Data Types. Abstract Data Types

Abstract Data Types. Abstract Data Types Abstract Data Types Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria Wolfgang.Schreiner@risc.jku.at http://www.risc.jku.at Wolfgang Schreiner

More information

Priority Queue Sorting

Priority Queue Sorting Priority Queue Sorting We can use a priority queue to sort a list of comparable elements 1. Insert the elements one by one with a series of insert operations 2. Remove the elements in sorted order with

More information

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING 4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING 4.1.2 ALGORITHMS ALGORITHM An Algorithm is a procedure or formula for solving a problem. It is a step-by-step set of operations to be performed. It is almost

More information

CSCE 2014 Final Exam Spring Version A

CSCE 2014 Final Exam Spring Version A CSCE 2014 Final Exam Spring 2017 Version A Student Name: Student UAID: Instructions: This is a two-hour exam. Students are allowed one 8.5 by 11 page of study notes. Calculators, cell phones and computers

More information

B+ Tree Review. CSE332: Data Abstractions Lecture 10: More B Trees; Hashing. Can do a little better with insert. Adoption for insert

B+ Tree Review. CSE332: Data Abstractions Lecture 10: More B Trees; Hashing. Can do a little better with insert. Adoption for insert B+ Tree Review CSE2: Data Abstractions Lecture 10: More B Trees; Hashing Dan Grossman Spring 2010 M-ary tree with room for L data items at each leaf Order property: Subtree between keys x and y contains

More information

Quiz 1 Practice Problems

Quiz 1 Practice Problems Introduction to Algorithms: 6.006 Massachusetts Institute of Technology March 7, 2008 Professors Srini Devadas and Erik Demaine Handout 6 1 Asymptotic Notation Quiz 1 Practice Problems Decide whether these

More information

12 Abstract Data Types

12 Abstract Data Types 12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define

More information

Measuring Input size. Last lecture recap.

Measuring Input size. Last lecture recap. Measuring Input size Last lecture recap. Linear data structures Compiled from http://www.eagle.tamut.edu/faculty/igor/cis-305.htm Abstract Data Type Data Structures. Main Notions and Definitions. Data

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech

More information

CS 331 Midterm Exam 2

CS 331 Midterm Exam 2 CS 331 Midterm Exam 2 Friday, November 4 th, 2016 Please bubble your answers in on the provided answer sheet. Also be sure to write and bubble in your student ID number (without the leading A ). 1. What

More information

USAL1J: Java Collections. S. Rosmorduc

USAL1J: Java Collections. S. Rosmorduc USAL1J: Java Collections S. Rosmorduc 1 A simple collection: ArrayList A list, implemented as an Array ArrayList l= new ArrayList() l.add(x): adds x at the end of the list l.add(i,x):

More information

DATA STRUCTURE AND ALGORITHM USING PYTHON

DATA STRUCTURE AND ALGORITHM USING PYTHON DATA STRUCTURE AND ALGORITHM USING PYTHON Advanced Data Structure and File Manipulation Peter Lo Linear Structure Queue, Stack, Linked List and Tree 2 Queue A queue is a line of people or things waiting

More information

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3 UNIT 3 LINEAR DATA STRUCTURES 1. Define Data Structures Data Structures is defined as the way of organizing all data items that consider not only the elements stored but also stores the relationship between

More information

Linear Data Structures

Linear Data Structures Linear Data Structures Arrays Arrays are stored in contiguous memory locations and contain similar data An element can be accessed, inserted or removed by specifying its position (number of elements preceding

More information

Hash Table and Hashing

Hash Table and Hashing Hash Table and Hashing The tree structures discussed so far assume that we can only work with the input keys by comparing them. No other operation is considered. In practice, it is often true that an input

More information

CS61B Lecture #24: Hashing. Last modified: Wed Oct 19 14:35: CS61B: Lecture #24 1

CS61B Lecture #24: Hashing. Last modified: Wed Oct 19 14:35: CS61B: Lecture #24 1 CS61B Lecture #24: Hashing Last modified: Wed Oct 19 14:35:49 2016 CS61B: Lecture #24 1 Back to Simple Search Linear search is OK for small data sets, bad for large. So linear search would be OK if we

More information

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions Adam Blank Lecture 1 Winter 2017 CSE 332 Data Abstractions CSE 332: Data Abstractions Welcome to CSE 332! Outline 1 Administrivia 2 A Data Structures Problem 3 Review of Stacks & Queues What Am I Getting

More information

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal Queues COL 106 Slides by Amit Kumar, Shweta Agrawal The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out (FIFO) scheme Insertions are at the rear

More information

CS2113 Lab: Collections 10/29/2018

CS2113 Lab: Collections 10/29/2018 CS2113 Lab: Collections Yawei Wang 10/29/2018 Install and Use IntelliJ on Mac or Window If you haven t installed JDK before, go to https://www.oracle.com/technetwork/java/javaseproducts/downloads/in dex.html

More information

811312A Data Structures and Algorithms, , Exercise 1 Solutions

811312A Data Structures and Algorithms, , Exercise 1 Solutions 811312A Data Structures and Algorithms, 2018-2019, Exercise 1 Solutions Topics of this exercise are stacks, queues, and s. Cormen 3 rd edition, chapter 10. Task 1.1 Assume that L is a containing 1000 items.

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 3 Data Structures Graphs Traversals Strongly connected components Sofya Raskhodnikova L3.1 Measuring Running Time Focus on scalability: parameterize the running time

More information

Dictionary. Dictionary. stores key-value pairs. Find(k) Insert(k, v) Delete(k) List O(n) O(1) O(n) Sorted Array O(log n) O(n) O(n)

Dictionary. Dictionary. stores key-value pairs. Find(k) Insert(k, v) Delete(k) List O(n) O(1) O(n) Sorted Array O(log n) O(n) O(n) Hash-Tables Introduction Dictionary Dictionary stores key-value pairs Find(k) Insert(k, v) Delete(k) List O(n) O(1) O(n) Sorted Array O(log n) O(n) O(n) Balanced BST O(log n) O(log n) O(log n) Dictionary

More information

Course Review. Cpt S 223 Fall 2009

Course Review. Cpt S 223 Fall 2009 Course Review Cpt S 223 Fall 2009 1 Final Exam When: Tuesday (12/15) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program

More information

Data structure and algorithm in Python

Data structure and algorithm in Python Data structure and algorithm in Python Linked Lists Xiaoping Zhang School of Mathematics and Statistics, Wuhan University Table of contents 1. Singly Linked Lists 2. Circularly Linked Lists 3. Doubly Linked

More information

Course Review. Cpt S 223 Fall 2010

Course Review. Cpt S 223 Fall 2010 Course Review Cpt S 223 Fall 2010 1 Final Exam When: Thursday (12/16) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program

More information

[2:3] Linked Lists, Stacks, Queues

[2:3] Linked Lists, Stacks, Queues [2:3] Linked Lists, Stacks, Queues Helpful Knowledge CS308 Abstract data structures vs concrete data types CS250 Memory management (stack) Pointers CS230 Modular Arithmetic !!!!! There s a lot of slides,

More information