Data Structure In C#

35,792 views

Published on

Topic: Data Structure for C# programmer.
By Shahzad Sarwar
Dated: 28th Feb 2010

Published in: Technology, Business
2 Comments
13 Likes
Statistics
Notes
No Downloads
Views
Total views
35,792
On SlideShare
0
From Embeds
0
Number of Embeds
832
Actions
Shares
0
Downloads
817
Comments
2
Likes
13
Embeds 0
No embeds

No notes for slide

Data Structure In C#

  1. 1. Data Structure in C# By: Shahzad Sarwar
  2. 2. Scope <ul><li>This presentation covers data structure for C# programmers. </li></ul><ul><li>Basic data structure like Queue and Stacks are discussed as implementation provided by C#. </li></ul><ul><li>This presentation will not cover advance Data structure like B+ Tree. </li></ul><ul><li>In order to explore, self created data structure, see the following URLs. </li></ul><ul><ul><li>http://www.brpreiss.com/books/opus6/html/page10.html </li></ul></ul><ul><ul><li>http://en.wikibooks.org/wiki/Computer_Science:Data_Structures:All_Chapters </li></ul></ul>
  3. 3. Asymptotic analysis <ul><li>Asymptotic analysis: Examines how the efficiency of a data structure changes as the data structure's size approaches infinity. </li></ul><ul><li>With an array, this process takes time proportional to the number of elements in the array. With binary search trees or SkipLists, the time required is sub-linear. When searching large amounts of data, the data structure chosen can make a difference in the application's performance that can be visibly measured in seconds or even minutes. </li></ul><ul><li>Big-Oh notation : The notation commonly used in asymptotic analysis </li></ul><ul><li>To describe the performance of searching an array : O ( n ). </li></ul><ul><li>The n indicates that the number of steps required to search an array grows linearly as the size of the array grows. </li></ul><ul><li>Examples: O (log2 n ), O ( n log2 n ), O ( n 2), O (2n), </li></ul><ul><li>The lower the term inside the parenthesis for large values of n , the better the data structure's operation's performance. </li></ul>
  4. 4. <ul><li>Array(Linear, Direct Access, Homogeneous Data Structure) </li></ul><ul><li>Contiguous memory. </li></ul><ul><li>Must be of the same type. </li></ul><ul><li>Can be directly accessed. </li></ul><ul><li>Declared in C# </li></ul><ul><li>bool [] booleanArray; </li></ul><ul><li>Allocation of array </li></ul><ul><li>booleanArray = new bool[10]; </li></ul><ul><li>Generically: </li></ul><ul><li>arrayName = new arrayType[allocationSize]; </li></ul>Array
  5. 5. Representation
  6. 6. Resizing array <ul><li>. // Create an integer array with three elements </li></ul><ul><li>int [] fib = new int[3]; </li></ul><ul><li>fib[0] = 1; </li></ul><ul><li>fib[1] = 1; </li></ul><ul><li>fib[2] = 2; </li></ul><ul><li>// Redimension message to a 10 element array </li></ul><ul><li>int [] temp = new int[10]; </li></ul><ul><li>// Copy the fib array to temp </li></ul><ul><li>fib.CopyTo(temp, 0); </li></ul><ul><li>// Assign temp to fib </li></ul><ul><li>fib = temp; </li></ul><ul><li>An algorithm called binary search to search the array in O(log n) running time. </li></ul><ul><li>The Array class contains a static BinarySearch() </li></ul><ul><li>The binary search algorithm is known as a recursive algorith </li></ul><ul><li>To search through an n-element single dimensional array was denoted O(n). </li></ul><ul><li>A k-dimensional array has a search running time of O(nk). </li></ul>
  7. 7. Array Search <ul><li>For non-sorted, there is Linear time. Linear time since the amount of time required for the algorithm to run is linearly proportional to the length of the array. </li></ul><ul><li>The CompareCustomDataType class provided below performs a comparison between two Student instances. It implements the IComparer interface and provides a Compare function that compares two Student instances by their Name property: </li></ul><ul><li>using System; </li></ul><ul><li>using System.Collections; </li></ul><ul><li>public class CompareCustomDataType : IComparer </li></ul><ul><li>{ public int Compare(Object x, Object y) </li></ul><ul><li>{ if (x == null) return -1; </li></ul><ul><li>if (y == null) return 1; </li></ul><ul><li>Student xStudent = (Student) x; </li></ul><ul><li>Student yStudent = (Student) y; </li></ul><ul><li>return String.Compare(xStudent.Name, yStudent.Name); </li></ul><ul><li>} } </li></ul>
  8. 8. Code <ul><li>'Create an array of 5 students </li></ul><ul><li>Dim myArray(4) as Student </li></ul><ul><li>myArray(0) = New Student(&quot;Scott&quot;, 24) </li></ul><ul><li>myArray(1) = New Student(&quot;Jisun&quot;, 23) </li></ul><ul><li>myArray(2) = New Student(&quot;Spacey&quot;, 21) </li></ul><ul><li>myArray(3) = New Student(&quot;John&quot;, 23) </li></ul><ul><li>myArray(4) = New Student(&quot;Josh&quot;, 20) </li></ul><ul><li>'Sort the array </li></ul><ul><li>Array.Sort(myArray, New CompareCustomDataType) </li></ul><ul><li>'Determine if a particular student exists in the array </li></ul><ul><li>Dim studentTemp as New Student() </li></ul><ul><li>studentTemp.Name = &quot;Jisun&quot; </li></ul><ul><li>location = Array.BinarySearch(myArray, studentTemp, New CompareCustomDataType) </li></ul><ul><li>If location < 0 then </li></ul><ul><li>'Student Jisun does not exist in the array </li></ul><ul><li>Else </li></ul><ul><li>'Student Jisun exists in the array at position location </li></ul><ul><li>End If </li></ul>
  9. 9. The ArrayList <ul><li>A Heterogeneous </li></ul><ul><li>Self-Redimensioning Array </li></ul><ul><li>(At no point do we have to concern ourselves with redimensioning the ArrayList.) </li></ul><ul><li>ArrayList countDown = new ArrayList(); </li></ul><ul><li>countDown.Add(5); </li></ul><ul><li>countDown.Add(4); </li></ul><ul><li>countDown.Add(3); </li></ul><ul><li>countDown.Add(2); </li></ul><ul><li>countDown.Add(1); </li></ul><ul><li>countDown.Add(&quot;blast off!&quot;); </li></ul><ul><li>countDown.Add(new ArrayList()); </li></ul>
  10. 10. Representation
  11. 11. Advantages of Generics <ul><li>The main advantages of Generics : </li></ul><ul><li>Type-safety: a developer using the TypeSafeList class can only add elements that are of the type or are derived from the type specified. For example, trying to add a string to the fib TypeSafeList in the example above would result in a compile-time error. </li></ul><ul><li>Performance: Generics remove the need to type check at run-time, and eliminate the cost associated with boxing and unboxing. </li></ul><ul><li>Reusability: Generics break the tight-coupling between a data structure and the application for which it was created. This provides a higher degree of reuse for data structures. </li></ul>
  12. 12. Queue <ul><li>FIFO </li></ul><ul><li>The System.Collections.Queue Class </li></ul><ul><li>Enqueue() and Dequeue() methods </li></ul><ul><li>Peek() method. </li></ul><ul><li>Contains() method </li></ul><ul><li>By default, the Queue's initial capacity is 32 elements, although this is customizable when calling the Queue's constructor. </li></ul><ul><li>Since the Queue is maintained with an object array, variables of any type can be queued up. </li></ul><ul><li>Example </li></ul>
  13. 13. Stack <ul><li>LIFO </li></ul><ul><li>System.Collections.Stack </li></ul><ul><li>Push(item) and Pop methods </li></ul><ul><li>Default capacity is 10 elements, although this is customizable when calling the Stack's constructor. </li></ul><ul><li>Since the Stack is maintained with an object array, variables of any type can be queued up. </li></ul><ul><li>Like the ArrayList, when the Stack's internal array needs to be resized it is automatically increased by twice the initial size. </li></ul><ul><li>Example </li></ul>
  14. 14. Analysis <ul><li>The Limitations of Ordinal Indexing </li></ul><ul><li>Searching through all of the elements in the employee array—a O ( n ) operation. A somewhat better approach is to sort the employees by their social security numbers, which reduces the asymptotic search time down to O (log n ). For reading or writing as O (1). </li></ul><ul><li>Ideally, what we would like to be able to do is access an employee's records in O (1) time. One way to accomplish this is to build a huge array, with an entry for each possible social security number value. </li></ul><ul><li>The disadvantage of this approach is its extreme waste—there are a total of 109, which is one billion (1,000,000,000)—different social security numbers. For a company with 1,000 employees, only 0.0001% of this array would be utilized. </li></ul>
  15. 15. Analysis <ul><li>Compressing Ordinal Indexing with a Hash Function </li></ul><ul><li>One option would be to reduce the social security number span by only using the last four digits of an employee's social security number. </li></ul>
  16. 16. Analysis <ul><li>The mathematical transformation of the nine-digit social security number to a four-digit number is called hashing . An array that uses hashing to compress its indexers space is referred to as a hash table . </li></ul><ul><li>A hash function is a function that performs this hashing. For the social security number example, our hash function, H, can be described as follows: </li></ul><ul><li>H(x) = last four digits of x </li></ul>
  17. 17. Hash Table <ul><li>The System.Collections.Hashtable Class </li></ul><ul><li>Items are added to the Hashtable using the Add() method. </li></ul><ul><li>// Add some values to the Hashtable, indexed by a string key </li></ul><ul><li>ages.Add(&quot;Scott&quot;, 25); </li></ul><ul><li>ages.Add(&quot;Sam&quot;, 6); </li></ul><ul><li>ages.Add(&quot;Jisun&quot;, 25); </li></ul><ul><li>// Access a particular key </li></ul><ul><li>if (ages.ContainsKey(&quot;Scott&quot;)) </li></ul><ul><li>{ </li></ul><ul><li>int scottsAge = (int) ages[&quot;Scott&quot;]; </li></ul><ul><li>Console.WriteLine(&quot;Scott is &quot; + scottsAge.ToString()); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>Console.WriteLine(&quot;Scott is not in the hash table...&quot;); </li></ul><ul><li>// Step through all items in the Hashtable </li></ul><ul><li>foreach(string key in ages.Keys) </li></ul><ul><li>Console.WriteLine(&quot;Value at ages[&quot;&quot; + key + &quot;&quot;] = &quot; + ages[key].ToString()); </li></ul>
  18. 18. List <ul><li>List<> </li></ul><ul><ul><li>Is in essence an ArrayList, with the difference being that only one type of object can be stored. </li></ul></ul><ul><ul><li>Program's speed and readability. </li></ul></ul><ul><ul><li>List<int> intList = new List<int>();intList.Add(45);intList.Add(34); </li></ul></ul><ul><ul><li>int listValue = intList[0]; </li></ul></ul><ul><li>LinkedList<> </li></ul><ul><ul><li>Consists of &quot;nodes&quot; that carry three values: the object it is storing, a reference to a previous node, and a reference to the next node. </li></ul></ul><ul><ul><li>LinkedList<int> list = new LinkedList<int>();list.AddLast(6); </li></ul></ul><ul><ul><li>list.First.Value or list.Last.Value </li></ul></ul>
  19. 19. Dictionary<,> <ul><li>The Dictionary structure is like a List<> structure but the index can be set. Meaning instead of the indecies being automatically generated (0, 1, 2, 3, etc...) they are set by the user and can be of any type. </li></ul><ul><li>Dictionary<string, int> myDictionary = new Dictionary<string, int>(); </li></ul><ul><li>myDictionary.Add(&quot;one&quot;, 1);myDictionary.Add(&quot;twenty&quot;, 20); </li></ul><ul><li>int myInt = myDictionary[&quot;one&quot;]; </li></ul><ul><li>//List<[same type as index]>List<string> </li></ul><ul><li>keyList = new List<string>(myDictionary.Keys); </li></ul><ul><li>for (int i = 0; i < keyList.Count; i++){ int myInt = myDictionary[keyList[i]]; } </li></ul>
  20. 20. HashSet <ul><li>So first off, a Set data structure is a list with no duplicate values. It is more or less modeled after a mathematical set, which has only unique elements. </li></ul><ul><li>Since a C#.NET HashSet is modeled after a mathematical set, certain functions were implemented such as Union, Intersection, IsSubsetOf, IsSupersetOf. </li></ul><ul><li>A resulting difference between a HashSet and a List is that the HashSet's Add method is a boolean function, which returns true of an element was added and false if it wasn't (because it was not unique). </li></ul><ul><li>Although a HashSet is a list of elements, it does not inherit the IList interface. Instead it just inherits the ICollection interface. What this means is elements inside a HashSet cannot be accessed through indices, only through an enumerator (which is an iterator). </li></ul>
  21. 21. Special DS <ul><li>SynchronizedCollection( T ) Generic Class </li></ul><ul><li>Provides a thread-safe collection that contains objects of a type specified by the generic parameter as elements. </li></ul><ul><li>Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe. </li></ul><ul><li>SynchronizedKeyedCollection( K , T ) Generic Class </li></ul><ul><li>Provides a thread-safe collection that contains objects of a type specified by a generic parameter and that are grouped by keys. </li></ul><ul><li>Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe. </li></ul>
  22. 22. Special DS <ul><li>SynchronizedReadOnlyCollection( T ) Generic Class </li></ul><ul><li>Provides a thread-safe, read-only collection that contains objects of a type specified by the generic parameter as elements. </li></ul><ul><li>Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe. </li></ul><ul><li>SortedDictionary( TKey , TValue ) Generic Class </li></ul><ul><li>Represents a collection of key/value pairs that are sorted on the key. </li></ul>
  23. 23. Trees <ul><li>A binary tree: is a special kind of tree, in which all nodes have at most two children. </li></ul><ul><li>The .NET Framework does not contain a binary tree class </li></ul><ul><li>Improving the Search Time with Binary Search Trees (BSTs) </li></ul><ul><ul><li>A binary search tree is a special kind of binary tree designed to improve the efficiency of searching through the contents of a binary tree. </li></ul></ul><ul><ul><li>For any node n , every descendant node's value in the left subtree of n is less than the value of n , and every descendant node's value in the right subtree is greater than the value of n . </li></ul></ul>
  24. 24. Tree Traversal <ul><li>Traversal: A traversal visits the nodes of a tree in a systematic manner </li></ul><ul><li>Preorder Traversal In a preorder traversal, a node is visited before its descendants. </li></ul><ul><li>Postorder Traversal: In a postorder traversal, a node is visited after its descendants </li></ul><ul><li>Inorder Traversal: In an inorder traversal a node is visited after its left subtree and before its right subtree </li></ul><ul><li>Example </li></ul>
  25. 25. Graph <ul><li>Graphs are composed of a set of nodes and edges, just like trees, but with graphs there are no rules for the connections between nodes. </li></ul><ul><li>With graphs there is no concept of a root node, nor is there a concept of parents and children. Rather, a graph is just a collection of interconnected nodes. </li></ul><ul><li>A tree is a special case of a graph. </li></ul><ul><li>Graphs are a collection of nodes and edges, but there are different kinds of edges: </li></ul><ul><ul><li>Directed versus undirected edges </li></ul></ul><ul><ul><li>Weighted versus unweighted edges </li></ul></ul><ul><li>Graphs with bidirectional edges are said to be undirected graphs. </li></ul><ul><li>Graphs that use unidirectional edges are said to be directed graphs . </li></ul>
  26. 26. Graph <ul><li>Associate some cost with the connection from one node to another. </li></ul><ul><li>If self-edges are allowed, the total number of edges for a directed graph would be n 2. </li></ul><ul><li>A graph with close to the maximum number of edges is said to be dense . </li></ul><ul><li>If a graph has significantly less than n 2 edges, the graph is said to be sparse . </li></ul><ul><li>For a connected, undirected graph, there exists some subset of the edges that connect all the nodes and does not introduce a cycle. Such a subset of edges would form a tree called a spanning tree . </li></ul><ul><li>A minimum spanning tree , is the spanning tree with a minimum cost. </li></ul>
  27. 27. Examples
  28. 28. Usages of Graph: <ul><li>Finding a minimum spanning tree </li></ul>
  29. 29. Usages of Graph: <ul><li>Computing the shortest path from a source node to all other nodes. </li></ul><ul><li>Dijkstra's Algorithm </li></ul>
  30. 30. References: <ul><li>http://msdn.microsoft.com/en-us/library/ms364091(VS.80).aspx </li></ul><ul><li>http://www.brpreiss.com/books/opus6/html/page10.html </li></ul><ul><li>http://en.wikibooks.org/wiki/Computer_Science:Data_Structures:All_Chapters </li></ul>
  31. 31. <ul><li>A soft copy will be available at: </li></ul><ul><li>http:// softarchitect.wordpress.com </li></ul><ul><li>For Future discussion, join </li></ul><ul><li>http:// tech.groups.yahoo.com/group/SoftArchitect / </li></ul>

×