Sei sulla pagina 1di 52

Lecture No.

0001

Data Structures & Algorithms


Data Structures
 It is a logical way of storing data and it also
define mechanism of retrieve data.

In computer science, a data structure is a data


organization, management, and storage format that
enables efficient access and modification. More
precisely, a data structure is a collection of data
values, the relationships among them, and the functions
or operations that can be applied to the data
Why focus on Data Structures
Once a fox invited a crane to dinner. The crane was
ecstatic, but the fox had done so only to insult the crane.
When the crane came over, the fox served it a bowl of
soup. It smelled delicious, but the crane couldn’t drink
any of it. It’s beak was ill suited for a bowl. While it
snapped, frustrated, the fox lapped up the soup. The fox
was done in minutes.
Why focus on Data Structures

The crane’s algorithm to peck food was working on the wrong data
structure.
Why focus on Data Structures
The crane soon gave up, and took the fox’s leave.
Before leaving however, the crane invited the fox to
dinner again at it’s place. The fox was over confident
and accepted immediately.
At the crane’s place, the fox was served some fish in a
jar.
Why focus on Data Structures
Why focus on Data Structures
The fox’s mouth wasn’t long enough to reach the bottom,
where the fish were waiting invitingly. The thin jar
opening was perfect for the crane. It pecked out all the
fish while the fox hung with it’s mouth in the jar,
desperate to get a bite.
That’s what happens when you use the right data
structure.
A data structure defines a contract for storing, retrieving
and manipulating the data inside it. They can speed up
algorithms and are critical to their success.
Need for Data Structures
 Data structures organize data  more
efficient programs.
 More powerful computers  more
complex applications.
 More complex applications demand more
calculations.
Organizing Data
 Any organization for a collection of records
that can be searched, processed in any
order, or modified.
 The choice of data structure and algorithm
can make the difference between a
program running in a few seconds or many
days.
Efficiency
 A solution is said to be efficient if it solves
the problem within its resource constraints.
– Space
– Time

 The cost of a solution is the amount of


resources that the solution consumes.
Efficiency
 A solution is said to be efficient if it solves
the problem within its resource constraints.
 Space
 Time

 The cost of a solution is the amount of


resources that the solution consumes.
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the
resource constraints a solution must
meet.
2. Determine the basic operations that must
be supported. Quantify the resource
constraints for each operation.
3. Select the data structure that best meets
these requirements.
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the
resource constraints a solution must
meet.
2. Determine the basic operations that must
be supported. Quantify the resource
constraints for each operation.
3. Select the data structure that best meets
these requirements.
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the
resource constraints a solution must
meet.
2. Determine the basic operations that must
be supported. Quantify the resource
constraints for each operation.
3. Select the data structure that best meets
these requirements.
Types of Data Structure
Types of Data Structure
• Linear Data Structures: A linear data
structure traverses the data elements
sequentially, in which only one data element
can directly be reached.
• Examples: Arrays, Linked Lists
• Non-Linear Data Structures: Every data item
is attached to several other data items in a way
that is specific for reflecting relationships. The
data items are not arranged in a sequential
structure.
• Example: Trees, Graphs
Data Structure Operations
 Traversing:
 Accessing each record exactly once so
that certain item in the record may be
processed.
 Searching: finding the location of the
record with a given key value .
 Insertion : add a new record to the
structure
 Deletion : removing a record from the
structure
Linear Data Structure
1.Array
2.Stack
3.Queue
4.Linked List
Arrays
 An array is a collection of homogeneous type of
data elements.
 An array is consisting of a collection of elements .

main( int argc, char** argv )


{
int x[6];
int j;
for(j=0; j < 6; j++)
x[j] = 2*j;
}
Arrays
 Array declaration: int x[6];
 An array is collection of cells of the same type.
 The collection has the name ‘x’.
 The cells are numbered with consecutive
integers.
 To access a cell, use the array name and an
index:
x[0], x[1], x[2], x[3], x[4], x[5]
Array Layout
x[0]
Array cells are contiguous
x[1]
in computer memory.
x[2]
The memory can be
thought of as an array x[3]

x[4]

x[5]
What is Array Name?
 ‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.
 For example, if we have the code

int a, b;

then we can write

b = 2;
a = b;
a = 5;

But we cannot write

2 = a;
What is Array Name?
 ‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.
 For example, if we have the code

int a, b;

then we can write

b = 2;
a = b;
a = 5;

But we cannot write

2 = a;
What is Array Name?
 ‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.
 For example, if we have the code

int a, b;

then we can write

b = 2;
a = b;
a = 5;

But we cannot write

2 = a;
Array Name
 ‘x’ is not an lvalue
int x[6];
int n;
x[0] = 5;
x[1] = 2;
x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
Array Name
 ‘x’ is not an lvalue
int x[6];
int n;
x[0] = 5;
x[1] = 2;
x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
Dynamic Arrays
 You would like to use an array data structure
but you do not know the size of the array at
compile time.
 You find out when the program executes that
you need an integer array of size n=20.
 Allocate an array using the new operator:

int* y = new int[20]; // or int* y = new int[n]


y[0] = 10;
y[1] = 15; // use is the same
Dynamic Arrays
 You would like to use an array data structure
but you do not know the size of the array at
compile time.
 You find out when the program executes that
you need an integer array of size n=20.
 Allocate an array using the new operator:

int* y = new int[20]; // or int* y = new int[n]


y[0] = 10;
y[1] = 15; // use is the same
Dynamic Arrays
 You would like to use an array data structure
but you do not know the size of the array at
compile time.
 You find out when the program executes that
you need an integer array of size n=20.
 Allocate an array using the new operator:

int* y = new int[20]; // or int* y = new int[n]


y[0] = 10;
y[1] = 15; // use is the same
Dynamic Arrays
 ‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
 It can be assigned a value. The new operator
returns as address that is stored in y.
 We can write:

y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Dynamic Arrays
 ‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
 It can be assigned a value. The new operator
returns as address that is stored in y.
 We can write:

y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Dynamic Arrays
 ‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
 It can be assigned a value. The new operator
returns as address that is stored in y.
 We can write:

y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Dynamic Arrays
 We must free the memory we got using the
new operator once we are done with the y
array.

delete[ ] y;

 We would not do this to the x array because we


did not use new to create it.
2. Stack
 A Stack is a list of elements in which an element
may be inserted or deleted at one end which is
known as TOP of the stack.

 Operations performed on Stack


 Push: add an element in stack
 Pop: remove an element in stack
Stack Representation
3. Queue
 A queue is two ended data structure in which items
can be inserted from one end and taken out from the
other end. Therefore , the first item inserted into
queue is the first item to be taken out from the queue.
This property is called First in First out [FIFO].
Operations performed on Queue
 Insertion : add a new element in queue
 Deletion: Removing an element in queue
Linked List
 A Linked list is a linear collection of data elements
.It has two part one is info and other is link part.info
part gives information and link part is address of
next node
Operations performed on Linked
List
1.Traversing
2.Searching
3.Insertion
4.Deletion
Linked List Representation
Non Linear

1.Tree
2.Graph
Tree
 Tree: is a non-linear data structure which is
mainly used to represent data containing a
hierarchical relationship between elements.
 Binary Tree: A binary tree is a tree such that
every node has at most 2 child and each
node is labeled as either left of right child.
Operations on Tree
1.Insertion
2.Deletion
3.Searching
Tree Representation
Graph
• A graph data structure may also associate to
each edge some edge value, such as a
symbolic label or a numeric attribute (cost,
capacity, length, etc.).
• Operations performed Graph
1.Searching
2.Insertion
3.Deletion
Graph
• A graph data structure may also associate to
each edge some edge value, such as a
symbolic label or a numeric attribute (cost,
capacity, length, etc.).
• Operations performed Graph
1.Searching
2.Insertion
3.Deletion
Graph Representation
Algorithm
• An algorithm is a step by step method of
solving a problem. It is commonly used for
data processing, calculation and other related
computer and mathematical operations.
• An algorithm is also used to manipulate data
in various ways, such as inserting a new data
item, searching for a particular item or sorting
an item.
All Algorithms must satisfy the
following criteria -
1) Input : There are more quantities that are extremely
supplied.
2) Output: At least one quantity is produced.
3) Definiteness: Each instruction of the algorithm
should be clear and unambiguous.
4) Finiteness: The process should be terminated after a
finite number of steps.
5) Effectiveness: Every instruction must be basic
enough to be carried out theoretically or by using paper
and pencil.
Properties of Algorithm
1) Input : There are more quantities that are extremely
supplied.
2) Output: At least one quantity is produced.
3) Definiteness: Each instruction of the algorithm
should be clear and unambiguous.
4) Finiteness: The process should be terminated after a
finite number of steps.
5) Effectiveness: Every instruction must be basic
enough to be carried out theoretically or by using paper
and pencil.
Algorithm (or program) Specification
Specification is the process of translating a solution of a
problem into an algorithm. This can be done in three
ways:
1.Steps or instructions can be written by using a Natural
Language like English. In this case we have to ensure
that the resulting instructions are definite.
2. 2. Graphic representations called Flowcharts can also
be used for specifications, but they work well only if the
algorithm is small and simple.
3.3. Use of Pseudo Code is the most preferred option to
specify an algorithm. It is represented by using a Natural
Language (English) and C / C++.

Potrebbero piacerti anche