Professional Documents
Culture Documents
What is an Algorithm?
Algorithms are rules or instructions that are formulated in a finite, sequential
order to solve problems and get the required results. They give the pseudocode
for problems and can be implemented in several languages as they are not
language specific.
Step 6: STOP
However, you can manipulate the steps according to your preference. For
instance, you can assign the values to the variables in step 2 itself rather than
taking steps 3 and 4. This way, a single problem can have multiple solutions and
it depends on the problem and the programmer to choose the most feasible and
reliable solution.
Algorithm Examples
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 6: Stop
Algorithm 2: Find the largest number among three
numbers
Step 1: Start
Step 4: If a > b
If a > c
Else
Else
If b > c
Else
Step 5: Stop
Programming is all about data structures and algorithms. Data structures are
used to hold data while algorithms are used to solve the problem using that
data.
Built-in Data-structures:
Lists: Stores indexed elements that are changeable and can contain duplicate
items
Tuples: Stores indexed, unchangeable elements that can have duplicate copies
User-defined Data-structures:
Linked Lists: Linear data structures that are linked with pointers
The types of linear data structures are Array, Queue, Stack, Linked List.
o Array: An array consists of data elements of a same data type. For example,
if we want to store the roll numbers of 10 students, so instead of creating 10
integer type variables, we will create an array having size 10. Therefore, we
can say that an array saves a lot of memory and reduces the length of the
code.
o Stack: It is linear data structure that uses the LIFO (Last In-First Out) rule in
which the data added last will be removed first. The addition of data element
in a stack is known as a push operation, and the deletion of data element
form the list is known as pop operation.
o Queue: It is a data structure that uses the FIFO rule (First In-First Out). In
this rule, the element which is added first will be removed first. There are two
terms used in the queue front end and rear The insertion operation
performed at the back end is known ad enqueue, and the deletion operation
performed at the front end is known as dequeue.
o Linked list: It is a collection of nodes that are made up of two parts, i.e.,
data element and reference to the next node in the sequence.
o Tree
For example, the posts of employees are arranged in a tree data structure like
managers, officers, clerk. In the above figure, A represents a
manager, B and C represent the officers, and other nodes represent the clerks.
o Graph
A graph is a non-linear data structure that has a finite number of vertices and
edges, and these edges are used to connect the vertices. The vertices are used to
store the data elements, while the edges represent the relationship between the
vertices. A graph is used in various real-world problems like telephone networks,
circuit networks, social networks like LinkedIn, Facebook. In the case of facebook, a
single user can be considered as a node, and the connection of a user with others is
known as edges.
Types Arrays, linked list, stack, Trees and graphs are the
queue are the types of a types of a non-linear data
linear data structure. structure.
A stack is a useful data structure in programming. It is just like a pile of plates kept on top of
each other.
Stack representation
similar to a pile of plate
A real-world stack allows operations at one end only. For example, we can place or
remove a card or plate from the top of the stack only. At any given time, we can only
access the top element of a stack.
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here,
the element which is placed (inserted or added) last, is accessed first. In stack
terminology, insertion operation is called PUSH operation and removal operation is
called POP operation.
Stack Representation
The following diagram depicts a stack and its operations −
A stack can be implemented by means of Array, Structure, Pointer, and Linked List.
Stack can either be a fixed size one or it may have a sense of dynamic resizing.
Here, we are going to implement stack using arrays, which makes it a fixed size
stack implementation.
Basic Operations
Stack operations may involve initializing the stack, using it and then de-initializing it.
Apart from these basic stuffs, a stack is used for the following two primary
operations −
push() − Pushing (storing) an element on the stack.
pop() − Removing (accessing) an element from the stack.
When data is PUSHed onto stack.
To use a stack efficiently, we need to check the status of stack as well. For the
same purpose, the following functionality is added to stacks −
peek() − get the top data element of the stack, without removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty.
At all times, we maintain a pointer to the last PUSHed data on the stack. As this
pointer always represents the top of the stack, hence named top. The top pointer
provides top value of the stack without actually removing it.
Working of Stack Data Structure
The operations work as follows:
1. A pointer called TOP is used to keep track of the top element in the stack.
2. When initializing the stack, we set its value to -1 so that we can check if the stack
is empty by comparing TOP == -1 .
3. On pushing an element, we increase the value of TOP and place the new element in
the position pointed to by TOP .
4. On popping an element, we return the element pointed to by TOP and reduce its
value.
5. Before pushing, we check if the stack is already full
To reverse a word - Put all the letters in a stack and pop them out.
Because of the LIFO order of stack, you will get the letters in reverse
order.
In browsers - The back button in a browser saves all the URLs you
have visited previously in a stack. Each time you visit a new page, it
is added on top of the stack. When you press the back button, the
current URL is removed from the stack, and the previous URL is
accessed.
Queue
A queue is a useful data structure in programming. It is similar to the ticket
queue outside a cinema hall, where the first person entering the queue is the
first person who gets the ticket.
Queue follows the First In First Out (FIFO) rule - the item that goes in first is the
item that comes out first.
In the above image, since 1 was kept in the queue before 2, it is the first to be
removed from the queue as well. It follows the FIFO rule.
In programming terms, putting items in the queue is called enqueue, and
removing items from the queue is called dequeue.
A queue is an object (an abstract data structure - ADT) that allows the
following operations:
Peek: Get the value of the front of the queue without removing it
Working of Queue
Queue operations work as follows:
for the first element, set the value of FRONT to 0
increase the REAR index by 1
add the new element in the position pointed to by REAR
Dequeue Operation
check if the queue is empty
Limitations of Queue
As you can see in the image below, after a bit of enqueuing and dequeuing, the
size of the queue has been reduced.
Limitation of a queue
And we can only add indexes 0 and 1 only when the queue is reset (when all the
elements have been dequeued).
After REAR reaches the last index, if we can store extra elements in the empty
spaces (0 and 1), we can make use of the empty spaces. This is implemented by
a modified queue called the circular queue.
Simple Queue
Circular Queue
Priority Queue
Circular Queue
In a circular queue, the last element points to the first element making a circular
link.
The main advantage of a circular queue over a simple queue is better memory
utilization. If the last position is full and the first position is empty, we can insert
an element in the first position. This action is not possible in a simple queue.
As you can see in the above image, after a bit of enqueuing and dequeuing, the
size of the queue has been reduced.
The indexes 0 and 1 can only be used after the queue is reset when all the
elements have been dequeued.
Priority Queue
A priority queue is a special type of queue in which each element is associated
with a priority and is served according to its priority. If elements with the same
priority occur, they are served according to their order in the queue.
Generally, the value of the element itself is considered for assigning the priority.
For example, The element with the highest value is considered as the highest
priority element. However, in other cases, we can assume the element with the
lowest value as the highest priority element. In other cases, we can set priorities
according to our needs.
Removing Highest Priority Element
Deque Representation
Types of Deque
Input Restricted Deque
In this deque, input is restricted at a single end but allows deletion at both the
ends.
Output Restricted Deque
In this deque, output is restricted at a single end but allows insertion at both the
ends.
Linked list Data Structure
A linked list is a linear data structure, in which the elements are not stored at
contiguous memory locations. The elements in a linked list are linked using
pointers as shown in the below image:
In simple words, a linked list consists of nodes where each node contains a data
field and a reference(link) to the next node in the list.
A linked list data structure includes a series of connected nodes. Here, each
node store the data and the address of the next node. For example,
You have to start somewhere, so we give the address of the first node a special
name called HEAD .
Also, the last node in the linked list can be identified because its next portion
points to NULL .
You might have played the game Treasure Hunt, where each clue includes the
information about the next clue. That is how the linked list operates.
Representation of Linked List
Now see how each node of the linked list is represented. Each node consists:
A data item
In just a few steps, we have created a simple linked list with three nodes.
The power of a linked list comes from the ability to break the chain and rejoin it.
E.g. if you wanted to put an element 4 between 1 and 2, the steps would be:
Point its next pointer to the struct node containing 2 as the data value
Doing something similar in an array would have required shifting the positions
of all the subsequent elements.
Types of Linked List
There are three common types of Linked List.
Cir
cular linked list
A circular linked list can be either singly linked or doubly linked.
for singly linked list, next pointer of last item points to the first item
In the doubly linked list, prev pointer of the first item points to the last item as
well.
As per the above illustration, following are the important points to be considered.
Linked List contains a link element called first.
Each link carries a data field(s) and a link field called next.
Each link is linked with its next link using its next link.
Last link carries a link as null to mark the end of the list.
Basic Operations
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
Insertion Operation
Adding a new node in linked list is a more than one step activity. We shall learn this
with diagrams here. First, create a node using the same structure and find the
location where it has to be inserted.
Now, the next node at the left should point to the new node.
LeftNode.next −> NewNode;
This will put the new node in the middle of the two. The new list should look like this
−
Similar steps should be taken if the node is being inserted at the beginning of the
list. While inserting it at the end, the second last node of the list should point to the
new node and the new node will point to NULL.
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial
representation. First, locate the target node to be removed, by using searching
algorithms.
The left (previous) node of the target node now should point to the next node of the
target node −
LeftNode.next −> TargetNode.next;
This will remove the link that was pointing to the target node. Now, using the
following code, we will remove what the target node is pointing at.
TargetNode.next −> NULL;
We need to use the deleted node. We can keep that in memory otherwise we can
simply deallocate memory and wipe off the target node completely.
Reverse Operation
This operation is a thorough one. We need to make the last node to be pointed by
the head node and reverse the whole linked list.
First, we traverse to the end of the list. It should be pointing to NULL. Now, we shall
make it point to its previous node −
We have to make sure that the last node is not the last node. So we'll have some
temp node, which looks like the head node pointing to the last node. Now, we shall
make all left side nodes point to their previous nodes one by one.
Except the node (first node) pointed by the head node, all nodes should point to
their predecessor, making them their new successor. The first node will point to
NULL.
We'll make the head node point to the new first node by using the temp node.
Hashing
Hashing is a technique of mapping a large set of arbitrary data to tabular
indexes using a hash function. It is a method for representing dictionaries for
large datasets.
Hash Table
The Hash table data structure stores elements in key-value pairs where
Hash Collision
When the hash function generates the same index for multiple keys, there will
be a conflict (what value to be stored in that index). This is called a hash
collision.
We can resolve the hash collision using one of the following techniques.
Algorithm Classes:
Class Description
Divides the problem into sub-parts and
Divide and Conquer
solves each one separately
Divides the problem into sub-parts,
Dynamic Programming remembers the results of the sub-parts
and applies it to similar ones
Involves taking the easiest step while
Greedy Algorithms solving a problem without worrying about
the complexity of the future steps