You are on page 1of 64

1

Data Structures and Algorithms

BITS-Pilani K. K. Birla Goa Campus

1
Material for the presentation taken from Cormen, Leiserson, Rivest and
Stein, Introduction to Algorithms, Third Edition; 1
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Part III : Data Structures

I Many algorithms manipulate sets of items.

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Part III : Data Structures

I Many algorithms manipulate sets of items.


I These sets must be able to grow or shrink (i.e. must be
dynamic).

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Part III : Data Structures

I Many algorithms manipulate sets of items.


I These sets must be able to grow or shrink (i.e. must be
dynamic).
I Operations on dynamic sets

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Part III : Data Structures

I Many algorithms manipulate sets of items.


I These sets must be able to grow or shrink (i.e. must be
dynamic).
I Operations on dynamic sets
I Queries

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Part III : Data Structures

I Many algorithms manipulate sets of items.


I These sets must be able to grow or shrink (i.e. must be
dynamic).
I Operations on dynamic sets
I Queries
I Modifying operations

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch. 10 : Elementary Data Structures

I Stack : last-in, first-out (LIFO)

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch. 10 : Elementary Data Structures

I Stack : last-in, first-out (LIFO)


I There are many ways of implementing stacks.

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Elementary data structure: Stack

I PUSH and POP

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Elementary data structure: Stack

I PUSH and POP

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Elementary data structure: Stack

I PUSH and POP

I PUSH(S,17) , PUSH(S,3)

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Elementary data structure: Stack

I PUSH and POP

I PUSH(S,17) , PUSH(S,3)

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Elementary data structure: Stack

I PUSH and POP

I PUSH(S,17) , PUSH(S,3)

I POP(S)

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Stack operation pseudocode

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Stack operation pseudocode

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Stack procedure pseudocode

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Stack operations

I Stack operations can lead to overflows and underflows.

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Stack operations

I Stack operations can lead to overflows and underflows.


I Each stack operation takes O(1) time.

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Balanced Parentheses

I Input : [ ( ) ] { } { [ ( ) ( ) ] ( ) }

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Balanced Parentheses

I Input : [ ( ) ] { } { [ ( ) ( ) ] ( ) }
Output : Balanced

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Balanced Parentheses

I Input : [ ( ) ] { } { [ ( ) ( ) ] ( ) }
Output : Balanced
I Input : { ( ) [ } ]

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Balanced Parentheses

I Input : [ ( ) ] { } { [ ( ) ( ) ] ( ) }
Output : Balanced
I Input : { ( ) [ } ]
Output : Not Balanced

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Balanced Parentheses

I Input : [ ( ) ] { } { [ ( ) ( ) ] ( ) }
Output : Balanced
I Input : { ( ) [ } ]
Output : Not Balanced
I Can we use a Stack to solve this problem?

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Balanced Parentheses

I Input : [ ( ) ] { } { [ ( ) ( ) ] ( ) }
Output : Balanced
I Input : { ( ) [ } ]
Output : Not Balanced
I Can we use a Stack to solve this problem?
I Use vector in C++ STL for Stacks.

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Elementary data structure: Queue

I Queue : first-in, first-out (FIFO)

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Elementary data structure: Queue

I Queue : first-in, first-out (FIFO)


I There are many ways of implementing queues.

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Elementary data structure: Queue

I Queue : first-in, first-out (FIFO)


I There are many ways of implementing queues.
I ENQUEUE and DEQUEUE

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Queue array implementation

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Queue array implementation

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Queue array implementation

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Enqueue and Dequeue

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Enqueue and Dequeue

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Queue operations

I Each operation can be performed in O(1) time.

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Queue operations

I Each operation can be performed in O(1) time.


I Queue can be used to perform Breadth-first-search in Graphs.

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Queue operations

I Each operation can be performed in O(1) time.


I Queue can be used to perform Breadth-first-search in Graphs.
I Use vector in C++ STL for Queues.

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Linked lists

I A data structure in which objects are ordered linearly using a


pointer.

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Linked lists

I A data structure in which objects are ordered linearly using a


pointer.

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Linked lists

I A data structure in which objects are ordered linearly using a


pointer.

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Linked lists

I A data structure in which objects are ordered linearly using a


pointer.

I Linked list can support all the operations on a dynamic set.

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
C++ program

I C++ program for singly linked list

13
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
C++ program

I C++ program for singly linked list


I insertNode function (L. 109)

13
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
C++ program : Doubly linked list node

14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Doubly linked node : More satellite data

15
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Doubly Linked List

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Doubly Linked List

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Doubly Linked List

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Doubly Linked List

I NIL: prev of head element, and next of tail element

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Doubly Linked List

I NIL: prev of head element, and next of tail element


I Singly linked list does not have a prev pointer

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Doubly Linked List

I NIL: prev of head element, and next of tail element


I Singly linked list does not have a prev pointer
I Circular linked list

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Doubly Linked List

I NIL: prev of head element, and next of tail element


I Singly linked list does not have a prev pointer
I Circular linked list
I We will assume that we are working with an unsorted, doubly
linked list.

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List search

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List search

I LIST-SEARCH takes Θ(n) time in the worst case.

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List insert

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List insert

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List insert

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List insert

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List insert

LIST-INSERT takes O(1) time


18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List delete

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List delete

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List delete

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List delete

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List delete

LIST-DELETE takes O(1) time.

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
List delete

LIST-DELETE takes O(1) time. Deleting an element with a given


key would take Θ(n) time.
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
I

20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms

You might also like