Data-Structure-and-Algorithm (2)
Data-Structure-and-Algorithm (2)
Data Structures
Are the programmatic way of storing data so that data can be used efficiently.
Is a systematic way to organize data in order to use it efficiently.
Interface − each data structure has an interface. Interface represents the set of operations that a
data structure supports. An interface only provides the list of supported operations, type of
parameters they can accept and return type of these operations.
To solve the above-mentioned problems, data structures come to rescue. Data can be organized
in a data structure in such a way that all items may not be required to be searched, and the
required data can be searched almost instantly.
• Worst Case − Maximum time required for program execution. Average Case − Average
time required for program execution.
• Best Case − Minimum time required for program execution.
`
BASIC TERMINOLOGY
1. Data − Data are values or set of values.
2. Data Item − Data item refers to single unit of values.
3. Group Items − Data items that are divided into sub items are called as Group Items.
4. Elementary Items − Data items that cannot be divided are called as Elementary Items.
5. Attribute and Entity − An entity is that which contains certain attributes or properties,
which may be assigned values.
ReymarS.Bellosillo P a g e 1 | 18
DATA STRUCTURE AND ALGORITHM
Ex. Student as an Entity and Student ID, First Name, Last Name, Middle Name, Address,
Contact Number are the Attributes.
6. Entity Set − Entities of similar attributes form an entity set.
7. Field − Field is a single elementary unit of information representing an attribute of an
entity.
8. Record − Record is a collection of field values of a given entity.
9. File − File is a collection of records of the entities in a given entity set.
a. Text Editor
This will be used to type your program.
Examples of few editors include Windows Notepad, OS Edit command, Brief, Epsilon,
EMACS, and vim or vi.
The files you create with your editor are called source files and contain program source code.
The source files for C programs are typically named with the extension ".c". Before starting your
programming, make sure you have one text editor in place and you have enough experience to
write a computer program, save it in a file, compile it, and finally execute it.
b. The C Compiler
The source code written in the source file is the human readable source for your
program. It needs to be "compiled", to turn into machine language so that your CPU can
actually execute the program as per the given instructions.
This C programming language compiler will be used to compile your source code into a
final executable program.
Most frequently used and free available compiler is GNU C/C++ compiler. Otherwise, you can
have compilers either from HP or Solaris if you have respective (personal/own) Operating
Systems (OS).
Algorithm
Is a step-by-step procedure, which defines a set of instructions to be executed in a
certain order to get the desired output.
Algorithms are generally created independent of underlying languages, i.e. an algorithm
can be implemented in more than one programming language.
From the data structure point of view, following are some important categories of algorithms.
1. Search − Algorithm to search an item in a data structure.
2. Sort − Algorithm to sort items in a certain order.
3. Insert − Algorithm to insert item in a data structure.
4. Update − Algorithm to update an existing item in a data structure.
5. Delete − Algorithm to delete an existing item from a data structure.
CHARACTERISTICS OF AN ALGORITHM
Not all procedures can be called an algorithm. An algorithm should have the following
characteristics.
ReymarS.Bellosillo P a g e 2 | 18
DATA STRUCTURE AND ALGORITHM
• Unambiguous − Algorithm should be clear and unambiguous (). Each of its steps (or
phases), and their inputs/outputs should be clear and must lead to only one meaning.
• Input − An algorithm should have 0 or more well-defined inputs.
• Output − An algorithm should have 1 or more well-defined outputs, and should match
the desired output.
• Finiteness − Algorithms must terminate after a finite number of steps.
Example:
Let's try to learn algorithm-writing by using an example.
Problem − Design an algorithm to add two numbers and display the result.
Step 1: START
Step 2: Declare three integers a, b & c
Step 3: Define values of a & b
Step 4: Add values of a & b
Step 5: Store output of step 4 to c
Step 6: Print c
Step 7: STOP
Algorithms tell the programmers how to code the program. Alternatively, the algorithm can be
written as:
Algorithm Analysis
Efficiency of an algorithm can be analyzed at two different stages, before implementation and
after implementation. They are the following:
A Priori Analysis
This is a theoretical (imaginary) analysis of an algorithm. Efficiency of an algorithm is
measured by assuming that all other factors, for example, processor speed, are constant
and have no effect on the implementation.
A Posteriori Analysis
This is an empirical (experimental) analysis of an algorithm. The selected algorithm is
implemented using programming language. This is then executed on target computer
machine. In this analysis, actual statistics like running time and space required, are
collected.
ReymarS.Bellosillo P a g e 3 | 18
DATA STRUCTURE AND ALGORITHM
Suppose X is an algorithm and n is the size of input data, the time and space used by the
algorithm X are the two main factors, which decide the efficiency of X.
Time Factor – Time is measured by counting the number of key operations such as comparisons
in the sorting algorithm.
Space Factor − Space is measured by counting the maximum memory space required by the
algorithm.
Space Complexity
Space complexity of an algorithm represents the amount of memory space required by
the algorithm in its life cycle.
Following is a simple example that tries to explain the concept:
Algorithm: SUM(A, B)
Step 1: START
Step 2: C ← A + B + 10
Step 3: Stop
Time Complexity
An algorithm represents the amount of time required by the algorithm to run to
completion.
Asymptotic Notations
Following are the commonly used asymptotic notations to calculate the running time complexity
of an algorithm.
• Ο Notation
• Ω Notation
• θ Notation
Big Oh Notation, Ο
Is the formal way to express the upper bound of an algorithm's running time. It
measures the worst case time complexity or the longest amount of time an algorithm
can possibly take to complete.
Omega Notation, Ω
Is the formal way to express the lower bound of an algorithm's running time. It measures
the best case time complexity or the best amount of time an algorithm can possibly take
to complete.
Theta Notation, θ
Is the formal way to express both the lower bound and the upper bound of an
algorithm's running time.
ReymarS.Bellosillo P a g e 4 | 18
DATA STRUCTURE AND ALGORITHM
Greedy algorithms
Try to find a localized optimum (best) solution, which may eventually lead to globally
optimized solutions. However, generally greedy algorithms do not provide globally
optimized solutions.
Though, it seems to be working fine, for this count we need to pick only 4 coins. But if we
slightly change the problem then the same approach may not be able to produce the same
optimum result.
For the currency system, where we have coins of 1, 7, 10 value, counting coins for value 18 will
be absolutely optimum but for count like 15, it may use more coins than necessary.
For example, the greedy approach will use 10 + 1 + 1 + 1 + 1 + 1, total 6 coins. Whereas the
same problem could be solved by using only 3 coins (7 + 7 + 1)
Hence, we may conclude that the greedy approach picks an immediate optimized solution and
may fail where global optimization is a major concern.
Another Example:
1, 5, 7, 9 = total value 22
ReymarS.Bellosillo P a g e 5 | 18
DATA STRUCTURE AND ALGORITHM
(7+5+5) – Can do but it used repetition of numbers.
Examples
Most networking algorithms use the greedy approach. Here is a list of few of them −
Travelling Salesman Problem
• Prim's Minimal Spanning Tree Algorithm
• Kruskal's Minimal Spanning Tree Algorithm
• Dijkstra's Minimal Spanning Tree Algorithm
• Graph - Map Coloring
• Graph - Vertex Cover
• Knapsack Problem
• Job Scheduling Problem
There are lots of similar problems that uses the greedy approach to find an optimum solution.
Divide/Break
This step involves breaking the problem into smaller sub-problems. Sub-problems should
represent a part of the original problem. This step generally takes a recursive approach to divide
the problem until no sub-problem is further divisible. At this stage, sub-problems become
atomic in nature but still represent some part of the actual problem.
Conquer/Solve
This step receives a lot of smaller sub-problems to be solved. Generally, at this level, the
problems are considered 'solved' on their own.
ReymarS.Bellosillo P a g e 6 | 18
DATA STRUCTURE AND ALGORITHM
Merge/Combine
When the smaller sub-problems are solved, this stage recursively combines them until they
formulate a solution of the original problem. This algorithmic approach works recursively and
conquer & merge steps works so close that they appear as one.
Examples
The following computer algorithms are based on divide-and-conquer programming approach −
Merge Sort
• Quick Sort
• Binary Search
• Strassen's Matrix Multiplication
• Closest Pair (points)
There are various ways available to solve any computer problem, but the mentioned are a good
example of divide and conquer approach.
DYNAMIC PROGRAMMING
Dynamic programming approach
Is similar to divide and conquer in breaking down the problem into smaller and yet
smaller possible sub-problems. But unlike, divide and conquer, these sub-problems are
not solved independently (individually). Rather, results of these smaller sub-problems
are remembered and used for similar or overlapping sub-problems.
Is used where we have problems, which can be divided into similar sub-problems, so
that their results can be re-used. Mostly, these algorithms are used for optimization.
Before solving the in-hand sub-problem, dynamic algorithm will try to examine the
results of the previously solved sub-problems. The solutions of sub-problems are
combined in order to achieve the best solution.
So we can say −
• The problem should be able to be divided into smaller overlapping sub-problem.
• An optimum solution can be achieved by using an optimum solution of smaller
subproblems.
• Dynamic algorithms use memorization.
Comparison
In contrast (difference) to greedy algorithms, where local optimization is addressed, dynamic
algorithms are motivated for an overall optimization of the problem.
In contrast to divide and conquer algorithms, where solutions are combined to achieve an
overall solution, dynamic algorithms use the output of a smaller sub-problem and then try to
optimize a bigger sub-problem. Dynamic algorithms use memorization to remember the output
of already solved sub-problems.
Example
The following computer problems can be solved using dynamic programming approach −
• Fibonacci number series
• Knapsack problem
• Tower of Hanoi
ReymarS.Bellosillo P a g e 7 | 18
DATA STRUCTURE AND ALGORITHM
• All pair shortest path by Floyd-Warshall
• Shortest path by Dijkstra
• Project scheduling
DATA DEFINITION
Data Definition defines a particular data with the following characteristics.
• Atomic − Definition should define a single concept.
• Traceable − Definition should be able to be mapped to some data element.
• Accurate − Definition should be unambiguous.
• Clear and Concise − Definition should be understandable.
DATA OBJECT
Data Object represents an object having a data.
DATA TYPE
Data type is a way to classify various types of data such as integer, string, etc. which determines
the values that can be used with the corresponding type of data, the type of operations that can
be performed on the corresponding type of data.
Basic Operations
ReymarS.Bellosillo P a g e 8 | 18
DATA STRUCTURE AND ALGORITHM
The data in the data structures are processed by certain operations. The particular data
structure chosen largely depends on the frequency of the operation that needs to be performed
on the data structure.
• Traversing
• Searching
• Insertion
• Deletion
• Sorting
• Merging
Array
Is a container which can hold a fix number of items and these items should be of the
same type. Most of the data structures make use of arrays to implement their
algorithms.
Array Representation
Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
Basic Operations
Following are the basic operations supported by an array.
• Traverse − Prints all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the value.
• Update − Updates an element at the given index.
ReymarS.Bellosillo P a g e 9 | 18
DATA STRUCTURE AND ALGORITHM
In C, when an array is initialized with size, then it assigns defaults values to its elements in
following order.
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on then
requirement, a new element can be added at the beginning, end, or any given index of array.
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of
an array.
Search Operation
You can perform a search for an array element based on its value or its index.
Update Operation
Update operation refers to updating an existing element from the array at a given index.
Linked List
Is a sequence of links which contains items.
Each link contains a connection to another link.
Linked list is the second most-used data structure after array.
Following are the important terms to understand the concept of Linked List.
Link − Each link of a linked list can store a data called an element.
ReymarS.Bellosillo P a g e 10 | 18
DATA STRUCTURE AND ALGORITHM
Next − Each link of a linked list contains a link to the next link called Next.
Linked List − A Linked List contains the connection link to the first link called First.
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.
Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode).
Then point B. next to C –
ReymarS.Bellosillo P a g e 11 | 18
DATA STRUCTURE AND ALGORITHM
It should look like this –
Now, the next node at the left should point to the new node.
This will put the new node in the middle of the two. The new list should look like this −
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 –
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.
ReymarS.Bellosillo P a g e 12 | 18
DATA STRUCTURE AND ALGORITHM
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 lost 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.
Fibonacci Sequence
Are the numbers in the following integer sequence.
The first two numbers in the Fibonacci sequence are either 1 and 1 or o and 1,
depending on the chosen starting point of the sequence and each subsequent number is
the sum of the of the previous two.
Leonard Fibonacci
ReymarS.Bellosillo P a g e 13 | 18
DATA STRUCTURE AND ALGORITHM
Was an Italian mathematician from the Republic of Pisa, considered to be "the most
talented Western mathematician of the Middle Ages".
He is also known as Leonardo Bonacci, Leonardo of Pisa, Leonardo Pisano Bigollo, or
Leonardo Fibonacci.
Binary Tree
Is a tree data structure in which each node has at most two children, which are referred
to as the left child and the right child.
Consist of node called the root together with two binary trees called left subtree and the
right subtree of the root.
ReymarS.Bellosillo P a g e 14 | 18
DATA STRUCTURE AND ALGORITHM
Complete Binary
Tree
A Binary Tree is complete Binary Tree if all levels are completely filled except possibly the
last level and the last level has all keys as left as possible.
ReymarS.Bellosillo P a g e 15 | 18
DATA STRUCTURE AND ALGORITHM
SIZE AND DEPTH OF A BINARY TREE
Position: 0123456789
Elements: 5934627185
7
4 6 2
Stacks
Removal must happen on top.
The element to be deleted is the one which is recently inserted.
Insert into a stack is often called a push operation and deletion from a stack is called a
POP operation.
Implements the Last-In-First-Out or LIFO.
ReymarS.Bellosillo P a g e 16 | 18
DATA STRUCTURE AND ALGORITHM
Application:
• Undo in an editor
• Balanced parentheses { () }
Queues
The elements to be deleted is the one which has been in set for the longest time.
Insert into a queue is often called an Enqueue (Push) operation an deletion from a
queue is called a Dequeue (Pop) operation.
Implements the First-In-First-Out or FIFO policy.
Operations:
• Enqueue (x) or Push (x) In (Push) 5 4 3 2 1 Out (Pop)
• Dequeue () or Pop ()
• Front () or Peek ()
IsEmpty () Rear or Tail
• Push (5)
3 5
• Push (3)
• Pop () Queue
Perform operations: • Front () = 5
Push (2) • IsEmpty () = False
Application:
• Print documents (Print queue)
• Process scheduling
ReymarS.Bellosillo P a g e 17 | 18
DATA STRUCTURE AND ALGORITHM
• Simulating wait
ReymarS.Bellosillo P a g e 18 | 18