0% found this document useful (0 votes)
2 views

Data-Structure-and-Algorithm (2)

The document provides an overview of data structures and algorithms, highlighting their importance in efficiently storing and manipulating data. It discusses key concepts such as data structure interfaces, implementations, characteristics, and the need for data structures in handling complex applications. Additionally, it covers algorithm types, characteristics, analysis methods, and various algorithmic approaches including greedy algorithms, divide and conquer, and dynamic programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data-Structure-and-Algorithm (2)

The document provides an overview of data structures and algorithms, highlighting their importance in efficiently storing and manipulating data. It discusses key concepts such as data structure interfaces, implementations, characteristics, and the need for data structures in handling complex applications. Additionally, it covers algorithm types, characteristics, analysis methods, and various algorithmic approaches including greedy algorithms, divide and conquer, and dynamic programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

DATA STRUCTURE AND ALGORITHM

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.

Following terms are the foundation terms of a data structure.

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.

Implementation − Implementation provides the internal representation of a data structure.


Implementation also provides the definition of the algorithms used in the operations of the data
structure.

CHARACTERISTICS OF A DATA STRUCTURE


• Correctness − Data structure implementation should implement its interface correctly.
• Time Complexity − Running time or the execution time of operations of data structure
must be as small as possible.
• Space Complexity − Memory usage of a data structure operation should be as little as
possible.

Need for Data Structure


As applications are getting complex and data rich, there are three common problems that
applications face now-a-days.
• Data Search − Consider an inventory of 1 million(106) items of a store. If the application
is to search an item, it has to search an item in 1 million(106) items every time slowing
down the search. As data grows, search will become slower.
• Processor Speed − Processor speed although being very high, falls limited if the data
grows to billion records.
• Multiple Requests − As thousands of users can search data simultaneously on a web
server, even the fast server fails while searching the data.

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.

EXECUTION TIME CASES


There are three cases which are usually used to compare various data structure's execution time
in a relative manner.

• 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.

LOCAL ENVIRONMENT SETUP


If you are still willing to set up your environment for C programming language, you need the
following two tools available on your computer, (a) Text Editor and (b) The C Compiler.

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.

• Feasibility − Should be feasible with the available resources.


• Independent − An algorithm should have step-by-step directions, which should be
independent of any programming code.

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:

Step 1: START ADD


Step 2: Get values of a & b
Step 3: c ← a + b
Step 4: Display c
Step 5: STOP

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.

ALGORITHM COMPLEXITY (DIFFICULTY)

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 analysis of an algorithm refers to defining the mathematical foundation/framing of


its run-time performance. Using asymptotic analysis, we can very well conclude the best case,
average case, and worst case scenario of an algorithm.

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.

Counting Coins Problem


This problem is to count to a desired value by choosing the least possible coins and the greedy
approach forces the algorithm to pick the largest possible coin. If we are provided coins of Php.
1, 2, 5 and 10 and we are asked to count Php. 18 then the greedy procedure will be –

Step 1 − Select one Php. 10 coin, the remaining count is 8


Step 2 − Then select one Php. 5 coin, the remaining count is 3
Step 3 − Then select one Php. 2 coin, the remaining count is 1
Step 4 − And finally, the selection of one Php. 1 coins solves the problem

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

Get the best approach to get a total coin value of 17.


(9+7+1) – Best approach

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 AND CONQUER


In divide and conquer approach, the problem in hand, is divided into smaller sub-problems and
then each problem is solved independently. When we keep on dividing the sub-problems into
even smaller sub-problems, we may eventually reach a stage where no more division is possible.
Those "atomic" smallest possible sub-problem (fractions) are solved. The solution of all
subproblems is finally merged in order to obtain the solution of an original problem.

Broadly, we can understand divide-and-conquer approach in a three-step process.

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 STRUCTURES BASIC CONCEPT


This chapter explains the basic terms related to data structure.

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.

There are two data types −


• Built-in Data Type
• Derived Data Type

Built-in Data Type


Those data types for which a language has built-in support are known as Built-in Data types. For
example, most of the languages provide the following built-in data types.
• Integers
• Boolean (true, false)
• Floating (Decimal numbers)
• Character and Strings

Derived Data Type


Those data types which are implementation independent as they can be implemented in one or
the other way are known as derived data types. These data types are normally built by the
combination of primary or built-in data types and associated operations on them.
For example −
• List
• Array
• Stack
• Queue

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.

Following are the important terms to understand the concept of Array.


• Element − each item stored in an array is called an element.
• Index − each location of an element in an array has a numerical index, which is used to
identify the element.

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.

As per the above


illustration, following are the important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
• Each element can be accessed via its index. For example, we can fetch an element at
index 6 as 9.

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.

Following can be a situation with array insertion:

• Insertion at the beginning of an array


• Insertion at the given index of an array
• Insertion after the given index of an array
• Insertion before the given index of an 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.

Linked List Representation


Linked list can be visualized as a chain of nodes, where every node points to the next node.

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.

Types of Linked List


Following are the various types of linked list.
• Simple Linked List − Item navigation is forward only.
• Doubly Linked List − Items can be navigated forward and backward.
• Circular Linked List − Last item contains link of the first element as next and the first
element has a link to the last element as previous.

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.

Ex. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ….


0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ….

A tiling with squares whose side lengths are


successive Fibonacci numbers.

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

Binary Tree | Set 3 (Types of Binary Tree)

Full Binary Tree


 A Binary Tree is full if every node has 0 or 2 children.

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.

Perfect Binary Tree


 A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all
leaves are at same level.

ReymarS.Bellosillo P a g e 15 | 18
DATA STRUCTURE AND ALGORITHM
SIZE AND DEPTH OF A BINARY TREE

The size of a binary tree is the number of nodes in it


 This tree has size 12.
The depth of a node is its distance from the root
 A is at depth zero.
 E is at depth 2.
The depth of a binary tree is the depth of its deepest node
 This tree has depth 4.

Position: 0123456789
Elements: 5934627185

5 Root (parent node)

Left child 9 3 Right child

7
4 6 2

1 8 5 Leafs (No Child)

STACKS AND QUEUES

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

Ex. Bullets in the magazine, file of hollow blocks

Operations: Push Pop


• Push(x)
• Pop ()  Top ()
• IsEmpty ()
Empty Container

Perform Operation:  Push (2)


• Push (10)
• Push (7)
• Pop ()
• Top () = 10 10
• IsEmpty () = False Stack 2

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.

Ex. Fall in line Head or Front

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

You might also like