1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.

Slides:



Advertisements
Similar presentations
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Advertisements

Generics, Lists, Interfaces
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
Data Structures and Collections
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Collection types Collection types.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
FEN UCN T&B: IT Technology1 Session 11: Data Structures and Collections Lists ( Array based, linked) Sorting and Searching Hashing Trees System.Collections.Generic.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Dictionaries, Hash Tables, Hashing, Collisions, Sets Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
111 © 2002, Cisco Systems, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Hashing Hashing is another method for sorting and searching data.
Hashing 8 April Example Consider a situation where we want to make a list of records for students currently doing the BSU CS degree, with each.
Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.
Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; dic= new XXX(); application class: Dictionary SortedDictionary ----
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
C# Collections & Generics C#.NET Software Development Version 1.0.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Chapter 5 Record Storage and Primary File Organizations
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
C# Collections & Generics
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Introduction toData structures and Algorithms
Sort & Search Algorithms
Computing with C# and the .NET Framework
COMP 53 – Week Eleven Hashtables.
CSCI 210 Data Structures and Algorithms
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Hashing Exercises.
Collections 24: Collections Programming C# © 2003 DevelopMentor, Inc.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Data Structures and Database Applications Hashing and Hashtables in C#
Advanced Associative Structures
CS313D: Advanced Programming Language
Data Structures and Database Applications Hashing and Hashtables in C#
Lesson 6. Types Equality and Identity. Collections.
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Introduction to Data Structure
Fundaments of Game Design
Presentation transcript:

1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections

2 interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary m; m= new XXXDic(); application class: Hash Table Search Tree ---- ADT Data structure and algorithms Choose and use an adt, e.g. IDictionary Choose and use a data structure, e.g. SortedDicitonary Know about Read and write (use) specifications Data Structures and Collections

3 Collections Library System.Collections Data structures in.NET are normally called Collections Are found in namespace System.Collections Compiled into mscorlib.dll assembly Uses object and polymorphism for generic containers. Deprecated! Classes: Array ArrayList Hashtable Stack Queue

4 Collection Interfaces System.Collections implements a range of different interfaces in order to provide standard usage of different containers Classes that implements the same interface provides the same services Makes it easier to learn and to use the library Makes it possible to write generic code towards the interface Interfaces: ICollection IEnumerable IEnumerator IList IComparer IComparable

5 ArrayList ArrayList stores sequences of elements. duplicate values are ok – position- (index-) based Elements are stored in an resizable array. Implements the IList interface public class ArrayList : IList, IEnumerable,... { // IList services... // additional services int Capacity { get... set... } void TrimToSize() int BinarySearch(object value) int IndexOf (object value, int startIndex) int LastIndexOf (object value, int startIndex)... } control of memory in underlying array searching

6 IList Interface IList defineres sequences of elements Access through index public interface IList : ICollection { int Add (object value); void Insert(int index, object value); void Remove (object value); void RemoveAt(int index); void Clear (); bool Contains(object value); int IndexOf (object value); object this[int index] { get; set; } bool IsReadOnly { get; } bool IsFixedSize { get; } } add new elements remove containment testing read/write existing element (see comment) structural properties

7 Hashtable Hashtable supports collections of key/value pairs keys must be unique, values holds any data stores object references at key and value GetHashCode method on key determine position in the table. Hashtable ages = new Hashtable(); ages["Ann"] = 27; ages["Bob"] = 32; ages.Add("Tom", 15); ages["Ann"] = 28; int a = (int) ages["Ann"]; create add update retrieve

8 Hashtable Traversal Traversal of Hashtable each element is of type DictionaryEntry (struct) data is accessed using the Key and Value properties Hashtable ages = new Hashtable(); ages["Ann"] = 27; ages["Bob"] = 32; ages["Tom"] = 15; foreach (DictionaryEntry entry in ages) { string name = (string) entry.Key; int age = (int) entry.Value;... } enumerate entries get key and value

9.NET 2: System.Collections.Generics ICollection IList LinkedList IDictionary List Dictionary SortedDictionary Index able Array-based Balanced search tree Hashtabel (key, value) -pair

10 Demos Lists Maps LinkedList in C#

How does they work? Array-based list Linked list 11 used Count Free (waste)

Dynamic vs. Static Data Structures Array-Based Lists: Fixed (static) size (waste of memory). May be able to grown and shrink (ArrayList), but this is very expensive in running time (O(n)) Provides direct access to elements from index (O(1)) Linked List Implementations: Uses only the necessary space (grows and shrinks as needed). Overhead to references and memory allocation Only sequential access: access by index requires searching (expensive: O(n)) 12

Hashing Keys are converted to indices in an array. A hash function, h maps a key to an integer, the hash code. The hash code is divided by the array size and the remainder is used as index If two or more keys gives the same index, we have a collision. 13

Chaining The array doesn’t hold the element itself, but a reference to a collection (a linked list for instance) of all colliding elements. On search that list must be traversed 14

Efficiency of Hashing Worst case (maximum collisions): retrieve, insert, delete all O(n) Average number of collisions depends on the load factor, λ, not on table size λ = (number of used entries)/(table size) But not on n. Typically (linear probing): numberOfCollisions avg = 1/(1 - λ) Example: 75% of the table entries in use: λ = 0.75: 1/(1-0.75) = 4 collisions in average (independent of the table size). 15

When Hashing Is Inefficient Traversing in key order. Find smallest/largest key. Range-search (Find all keys between high and low). Searching on something else than the designated primary key. 16

17 (Binary) Search Trees Value based container: The search tree property: For any internal node: the value is greater than the value in the left child For any internal node: the value is less than the value in the right child Note the recursive nature of this definition: It implies that all sub trees themselves are search trees Every operation must ensure that the search tree property is maintained

18 Example: A Binary Search Tree Holding Names

19 InOrder: Traversal Visits Nodes in Sorted Order

20 Efficiency insert retrieve delete All operations depend on the depth of the tree If balanced: O(log n) Most libraries use a balanced version, for instance Red-Black Trees