Data Structures and Algorithms in C#
Transcript
Welcome to Learn Data Structures & Algorithm using C# language step by step lesson by lesson and exercise by exercise. This is going to be a one hour of video i.e 1st Lab. then we will have Lab 2, Lab3 & so on. There are 10 points which we are going to cover in this Lab 1. First will define what is Data Structures and then will discuss about ADT.
ADT helps to think Data Structure in a more generic & categorized way. Will define what is Algorithms. Every code what we write is not an Algorithm. To say something is an Algorithm it has to follow 5 characteristics which we will discuss about later. Will talk about Big O Notations O(2n), On^2, O(logn) and so on Then will start with the first data structure i.e LinkList. In this will discuss about Simple, Double and Circular LinkList. Will write practical code and create a simple LinkList using C# Language.
We will go into this LinkList by using head variable. Will further include this LinkList by using a Generic LinkList. This video is going to cover all these 10 Points. There are two important points The first one is this complete source code and complete implementation of Data Structures in this labs is using C# Language.
People who are from Java background, C++ background or some other language don't have to worry. The syntaxes of C#is almost same like Java and C++. Whatever code we are writing here we can replicate the same using C++ and same using Java. Second thing is in all these tutorials we will be having theory part. We will be having labs like in this create a simple link list using C# will demonstrate source code then will also be having some exercises.
Whenever we say this is an exercise means try to implement it yourself. When we start implementing things yourself even though if it is wrong or the code is bad we can understand Algorithm and Data Structure in a better way.
Here is a small gift to you if you share this video on your facebook account, twitter account, Linkedin Account or anywhere. and email the shared link on questpond@questpond.com and will provide you notes, power point presentation which is well commented, well documented to go through the source code and understand it.
Will start with Data Structures. A Data Structure is a particular way of organizing and storing data in a computer it can be accessed and modified in an efficient and easy way. We have a scenario where we want to read a data sequentially. We can create a LinkList Data Structure. In a LinkList Data Structure the data points towards the next data.
We have data as 9, 8, 7 & 2. Every one of these data is pointing towards the next data. We have scenario where we want to display an organizational structure. In organizational structure at the top we have a CEO, Manager, Technician inside technician we have helpers. For such kind of a Data Structure - A tree or graph data structure is more efficient.
We have one more scenario where we want to have a data structure in a Queue way, a queue means the way the data is pushed in into the data structure the data should be retrieved in the same sequence. For that, probably a Queue data structure or a FIFO data structure is much better. Data Structure is a way of organizing and storing data as per the scenarios and needs it can be accessed and modified in an efficient way.
In today's scenario we have more than 500-600 Known data structures. We have situation where we want to ----on one of these data structures for the situation, we want to make a choice of one of these data structures it would be so difficult to remember each one of these data structures If we can categorize them or we can think it in an abstract way that would be great.
We are talking about tress we can have a mango tree, coconut tree, short tree, long tree and so on. If we think in an abstract way - These are just trees. They have roots, leaves, Branches. With abstract thinking we don't have to get into each implementation and we know abstractly what we want to do. From the Data Structure point of view rather than going all those 500-600 implementation it would be great if we can categorize that or we can think it Abstract data type. ADT, Abstract data type.
Array is one more data structure In Array we can have a Single Dimension Array, Two Dimension Array, Multi-Dimension Array It has values it can be of size 1, size 2 and multi-dimension size. When we start thinking Abstract we do not have to get into each one of these data structures. There are lot of type of LinkList like Single LinkList, Double LinList and Circular LinkList. If we think in an abstract way
it is just LinkList it has Add(), Remove(), Sort() and Update() Methods. with this those 500-600 data types can come into just .5 or 6 ADT categories ADT means Abstract Data Types. Rather than understanding each one of these implementation we think in an Abstract way. By thinking in an Abstract way it helps to categorize the Data Structure and map the Data Structure in an efficient way with the problem domain.
Algorithm is a step by step procedure or a formula to solve problem. This step by step procedure is an ambiguous. For example: - We have a problem of sorting or problem of doing some mathematical calculations for that there are time tested Algorithms wherein the precised step, precise input, and precise output is defined and by following that Algorithm we can solve that problem.
To add to numbers the steps are crystal clear, there is no ambiquette about it. First take num1 then num2 and add num1 with num2. That's why algorithm means all goes in a rythm. Every code which we write in a project does not classify as an Algorithm.
If we write an insert operation, update operation and delete operation it does not classify as an Algorithm. To classify something as an Algorithm it has to satisfy 5 important characteristics. The first is there are well defined inputs and well defined outputs. For an Algorithm the inputs & outputs should be precisely clear. The next thing, the steps of an Algorithm are precise unambiguous and to the points.
To add two numbers and Algorithm steps are : - Input Num1, then execute num2 and Num1+Num2. The steps are precise, clear and finite. Finite means this algorithm will start and it will end after certain time. The last thing is the Uniqueness Uniqueness means every step of Algorithm The output of that step goes as input to the next step.
This is an Input Num1 and somewhere Num1 will be used down below later on. Every output of the first step normally goes as an input to the next step. To say something is an Algorithm, there are 5 important things which it should satisfy. One of the another connected topic which is discussed will discuss Algorithm is Big O Notation. Will always hear these words this algorithm is performing O(n), O(1), On^2 What exactly is Big O Notation? Big O Notation
is a symbolic notation which says how the Algorithm will perform if the input data increases. When we talk about Algorithm, Algorithm has three important pillars. The first one is Input, the second one is the Processing which takes place by Algorithm and the final one is the Output. Big O Notation says if the input data increases how in what rate will the processing time will increase? It tells the relationship between the processing time & the increase of Data input.
Assume that we have an Algorithm for that Algorithm if we give 5 records it takes 27 seconds. If we increase the records to 10 it takes 105 seconds. Here, the relationship the time required to process by the Algorithm is the number of records square.
We have used the unit as records, this can be bytes or bits. We have used the unit as seconds and it can minutes, hours or days. The units which are displayed here is only for understanding purpose. Rather than writing such kind of Big equations we can say O (Rec^2) or O(n^2) This n stands for data input it can be records, bytes, collections, bits or anything.
When we say O(n^2) it means the processing time required by the Algorithm will be the square of the data input. Now will try to understand what are different ways of writing Big O Notation. The one which we have seen is O(n^2) Take a simple method or a function as Function1() This Function1() takes some list of data.
In the processing it fetches the first record. This Function1 takes a collection of data and fetches the first record and does some processing in the first record. If we increase the list of data the processing time won't change.
If we put 100 list of strings the time required for 100 list of string, 1000 list of string will be almost same. The symbolic way of representing the performance of this function is O(1). This Algorithm performance will not change with the data input increase. Now will take a second example of Function2.
In Function2 there is a for loop. In Function2 we are searching something There is a for loop which iterates through the whole list of string. If it finds particular string in this like "shiv" it will return. This function2 is looping through this string in a sequential way and trying to find something.
In this case for 10 records it will take some X time but if the number of records increases then the number of for loops and time will also increase. The time if directly proportionate to the number of records. Rather the increase of records will also increase the time required for process. This kind of performance we denote by O (n).
O(1) indicates that the performance will remain constant. O(n) indicates as the number of records increases the time will also increase. It is more of Linear kind of a representation here. The performance will degrade or the time required for processing will increase Linearly with the amount of data coming in. Will take one more example of double for loop.
In Function3 for every record in the data we are looping through all records of the data so its a double for loop. This Algorithm is going to take twice the time of the record. It is also possible this can be O( n^2).
If the data is too high and lot of processing happening this - O(n^2) also is a possibility. Here some examples are given wherein it says how Big O Notation will look like. First example is more of a constant. It is represented by O(1). It says the function performance will remain constant irrespective of the input data size. O(n) is a Linear and this is Quadratic.
Big O Notation says how will Algorithm perform if the data size increases? When we see a Big O Notation symbol we can classify how the Algorithm will work from the performance point of view. Here is a plot of Time vs the number of data inputs. First one is O(1) is constant Even if the number of records or the data inputs size increases the time will be constant.
O(1) is a good Algorithm and we should embrace it. From the practical point of view if the number of records increases tremendously there is going to be a performance drop. If there is a performance drop we can use O(logn). In log initially the performance decreases but later on the performance is the same The time taken for 100 records it will be x mili seconds 200 it will increase after 4000 or 5000 it will increase very minimally. Even if we see O(logn) These two are good zones.
When we start seeing linear like this O(n) in this case it is worrying because if the number of records increasing the time will increase linearly. If we see exponential and quadratic kind of things like O(n^2) O(2^n) this is a bad zone. We do not want to see Algorithm in these kind of Zones. When we look at the Big O Notation we can start classifying is the Algorithm in the good zone or in a worse/bad zone. that's what Big O Notation helps to look at Algorithm from the perspective of performance. One of the most important part about Data Structures & Algorithms is balancing Balancing.
Lot of people do one data structure after another data structure and until they start Algorithm they are standing nice out. Other kind of students just do Algorithms and do not pay attention to Data Structures. In this training will balancing it out. Take one or two Data Structures and take one or two Algorithms. will take again one or two Data Structures and take one or two Algorithms.
The first Data Structure is Link List. Link List is an ADT - Abstract Data Type. In the previous section of the video we discussed about Abstract Data Type. Link List is an ADT which represents family of Data Structures where Data Structure is represented in a Linear & Sequential way. Link List has something called as Element. We can term this as an Element or Node.
This node has two things as Data and Pointer. The pointer points towards the next node/Element. The first node is A pointing towards next node which is B. The B pointing towards the next node C. Link List ADT has three famous implementation. First one is a Simple Link List Second one is a Double Link List and the last one is a Circular Link List. In a simple Link List we have Node the Node point towards the next Node.
In Double Link List the Node point towards next Node also it point towards the previous node. In Double Link List we can traverse back & forth very easily. There is also Circular Link List, in Circular Link List every node point towards the next node & the last node will point again back to the first node. When we travel to the last we can know which is the first node from where we started. In Circular Link List we can have Circular simple Link List. and Circular Double Link List. In the Diagram we have shown one way but it is very much possible we can have two way.
Link List ADT has three famous implementation Simple, Double & Circular. Create a simple C# code to demonstrate Simple Link List. Incase you are new to C#, watch our C# 8 hours video series. and if you are from C++ & Java background you will be able to relate the code C# is a very generic language.
Before we start coding plan that how do we intend to write the code and the logic? If we think logically in the Simple Link List we have a node and the node point towards other node. This Node has two things inside it, it has Data and the pointer which point towards the next node. First we need to create a class and term that as a Node Class. In the Node Class we need to create two things Data and the pointer that points to next next node.
The second class which we need is for the links. Each one of these links are connected with each other. We need one class which is a wrapper on all the Nodes and says this node is connected with the other node. Create two classes and term it as a Node Class and a Link List Class. In a Visual Studio create a separate folder.
In this project will add all the Data Structures and Algorithms code in this project itself. Create a separate folder for every ADT(Abstract Data Type) and term this as a LinkedList. First we need to create one class which represents the Node.
This Node Class will have two properties one is Data the data can be anything so will name this data as object data type, it can be string, int or anything.
and another things we need is a Pointer to the next Node. We can create a self reference of the node class itself. This is pointing towards the next node. Make it as NULL so initially when the Nodes starts it has no data and no pointer. First we have created a class which represents the Nodes.
in the linked list. The other class which we need is Node List class. This Node List class will define the linkages between each node. Add one more class this class will have the code which connects each node.
The goal of this link list class is as follows: - This class will have the logic to connect the node objects to create the link list structure. Before we start coding this link list class will talk about two important elements of the linked list class. One is the Head and the other one is the Tail. The head is a start of the node of the link list chain. The Tail is the end node of the link list chain.
In this link list class we need to create the head Through the head we get hold of any of the data because everybody has the linkages. At least we need one variable code which says this is the start of the link list and from there we can traverse wherever we wish. In this link list class will create one variable here and this variable is of type Node and it the head.
In this LinkedList class use class Node here and it is a head. Head means this Create a three methods in this LinkedList, the first one is AddLast(), of the LinkedList.
The end node is called as the Tail and we want to add after the tail AddFirst() that means add before the Head. and one more operation is ReadAll(), read all the linked list data First start with AddLast() when anybody is using this LinkedList class will actually pass the data in this object.
The Node class has two variables, one which point towards the next node and it has created this variable as object type. Object type means it can be string, number or any kind of the object.

Ask anything
We've read this whole study set, so questions about it get the best answers.