Introduction to Data Structures and Algorithms with Python
Published: 2022-02-21 15:50:12

In Computer Science, data structures and algorithms in Python are two of the most basic notions. For any programmer, they are essential tools. In Python, data structures are used to organize and store data in memory while a program is processing it. Python algorithms, on the other hand, are a set of instructions that aid in the processing of data for a certain purpose.

#Data structures in Python
Data structures are a technique of organizing and storing information; they describe the relationship between data and the many logical operations that can be done on it. Data structures can be classified as either built-in Data-structures or user-defined data structures. 

##Built-in Data-structures:
The basic built-in data structures in Python include:

**Lists**
This is Python's most flexible data structure, represented as a list of comma-separated components enclosed in square brackets. Some of the methods applicable on a List are `index()`, `append()`, `extend()`, `insert()`, `remove()`, and `pop()`. Lists are changeable, which means that their content can be modified while maintaining their identity.
```py
b = ["banana", "apple", "microsoft"]
print(b)
```
Output:
```
['banana', 'apple', 'microsoft']
```
**Tuples**
Tuples are similar to Lists, except that they are unchangeable and are declared within parentheses. In a Tuple once an element has been defined, it cannot be deleted or modified. It ensures that the data structure's specified values are neither tampered with or overridden.

```py
my_tuple = (1, 2, 3)
print(my_tuple)
```
Output:

```
(1, 2, 3)
```
**Dictionaries**
Dictionaries consist of key-value pairs.  The 'key' is used to identify an item, while the 'value' is used to store the item's value. The key and its value are separated by a colon. The items are separated by commas, and everything is encased in curly brackets. While keys cannot be changed, values can be of any type.
```py
user = {
    "name" : "Janet",
    "age" : 22,
    "country" : "Kenya"
}

print(user["age"])
```
Output:
```
22
```

**Sets**
Sets are an unordered collection of unique elements. Sets, like Lists, are mutable and represented in square brackets, but they can't have the same value twice. Some methods that are applied on Sets include `count()`, `index()`, `any()`, and `all()`.
```py
myset = {1, 2, 3, 3, 1, 7,3, 6, 5, 4, 4}

print(myset)
```
Output:
``` 
{1, 2, 3, 4, 5, 6, 7}
```

##User-defined Data-structures:

####Stack
Stacks are linear data structures that work on the Last-In-First-Out (LIFO) principle, which means that data inserted last is the first to be accessed. It is constructed using an array structure and includes activities such as pushing (adding) elements, popping (deleting) elements, and only accessing elements from the TOP of the stack. This TOP is a pointer to the stack's current location. Recursive programming, reversing words, undo systems in word editors, are some of the applications that employ stacks extensively.

####Linked List
Linked lists are linear Data Structures that are linked together via pointers rather than being stored sequentially. A linked list node is made up of data and a pointer named next. These structures are most commonly employed in image viewer, music player, and other applications.

####Queue
A queue is a linear data structure based on the First-In-First-Out (FIFO) principle, which states that the data in first will be accessed first. It is constructed using an array structure and includes actions that can be performed from both the head and tail ends of the Queue, i.e., front-back and back-to-head. En-Queue and De-Queue operations are used to add and delete elements, and accessing the elements is possible. Queues are used as Network Buffers to manage traffic congestion, as well as Job Scheduling in Operating Systems.

####Tree
Trees are non-linear Data Structures which have root and nodes. The root is the node from which the data comes, and the nodes are the other data points we have access to. The parent is the node that comes before the kid, and the child is the node that comes after it. A tree must have levels to demonstrate the depth of information. The leaves are the final nodes in a chain. Trees generate a hierarchy that can be utilized in a variety of real-world applications, such as identifying which tag belongs to which block in HTML pages. It's also useful for searching and a variety of other things.

####Graph
Graphs are used to store data in the form of vertices (nodes) and edges (connections). The most accurate representation of a real-world map can be found in graphs. They are used to discover the least path by calculating the various cost-to-distance between the various data points known as nodes. Many programs, including Google Maps, Uber, and others, employ graphs to discover the shortest path and maximize earnings in the most efficient way possible.

####HashMaps
In Python, HashMaps are the same as dictionaries. They may be used to create apps like phonebooks, populate data based on lists, and much more.

#Algorithms
Algorithms are a set of rules or instructions written in a finite, sequential order to solve problems and provide the desired results. While data structures aid in data organization, algorithms aid in the solving of intractable data analysis problems.

In this article, we will discuss the following algorithms: Tree Traversal Algorithm, Sorting Algorithm, Searching Algorithm, and Graph Algorithm.

###Tree Traversal Algorithms 
Tree Traversal refers to visiting each node present in the tree exactly once in order to update or check them.

###Searching Algorithm
Searching algorithms aid in the verification and retrieval of data elements from various data structures. One sort of searching algorithm is the sequential search approach, in which the list is traversed progressively and each member is checked. Examples of searching algorithms are:

**Binary Search** – The search interval is divided in half on a regular basis. The interval is narrowed to the lower half if the element to be searched is lower than the central component of the interval. It is narrowed to the upper half otherwise. The technique is continued until the desired result is obtained.
**Linear Search**– In this algorithm, each item is sequentially searched one by one.

###Sorting Algorithm
Sorting algorithms denote the ways to arrange data in a particular format. Sorting ensures that data searching is optimized to a high level and that the data is presented in a readable format. Types of sorting algorithms in Python include: `Bubble Sort`, `Merge Sort`, `Insertion Sort`, `Shell Sort`, `Selection Sort`

###Graph Algorithm
There are two methods of traversing graphs using their edges. These are:

**Depth-first Traversal (DFS)** – A graph is traversed in depth-ward motion in this algorithm. When an iteration comes to a halt, a stack is used to jump to the next vertex and begin the search. The set data types are used to implement DFS in Python.
**Breadth-first Traversal (BFS)** – A graph is traversed in a breadth-ward motion in this algorithm. When an iteration comes to a halt, a queue is used to advance to the next vertex and begin the search. The queue data structure is used to implement BFS in Python.