0% found this document useful (0 votes)
41 views51 pages

Unit 1 DS

Uploaded by

sirvi2409
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views51 pages

Unit 1 DS

Uploaded by

sirvi2409
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Data Structures

Subject Teacher
Prof. Ms. Y. A. Thakare
(Department of Computer Science and
Engineering, SIPNA COET, Amravati)
Proposed Scheme
SANT GADGE BABA AMRAVATI UNIVERSITY,AMRAVATI

FOUR YEAR DEGREE COURSE IN BACHELOR OF ENGINEERING


BRANCH: COMPUTER SCIENCE & ENGINEERING - SEMESTER PATTERN (CREDIT GRADE SYSTEM)
SEMESTER: THIRD
Examination Scheme
Teaching Scheme
Practical
Hours per Theory
Max Marks
Week

Total Hours/Week
Max Max
Subject Code

Credit
Duratio
Marks Marks Min Min

Tutorial
Lecture
Sr No

n of

P/D
Subject Name Theor College Total Passing External Internal Total Passing
paper
y Assess Marks Marks
(Hr)
Paper ment

Theory
1 3KS04 Data Structures 3 3 3 3 80 20 100 40
Practicals
1 3KS07 Data Structures Lab 2 2 1 25 25 50 25
* As per the Ordinance No. 42 of 2005
# C Skill Lab I - based on technology like -Python/Django etc. to be decided by Individual Dept. of respective College
Course Information

Text Book:
Seymour Lipschutz: Data Structures with C, Schaum’s Outline Series, Mc Graw-Hill,
International Editions.

References:
1. Forouzan, Gilberg, Mahalle, Jogalekar: Data Structures and Algorithms, CENGAGE
Learning.
2. Reema Thareja: Data Structures using C, Oxford University Press, 2011.
3. Arpita Gopal: Magnifying Data structures, PHI(EEE), 2010.
4. Ellis Horowitz, Sartaj Sahni: Fundamentals of Data Structures, CBS Publications.
5. Trembley, Sorenson: An Introduction to Data Structures with Applications, McGraw Hill.
6. Standish: Data Structures in Java, Pearson Education.
OBJECTIVES:

• Study the representation and use of primitive data types and built
in data structures.

• Study how the data structures are allocated and used in memory.

• Study common applications of each of the data structures.

• Implement the user defined data structures in a high level


language
Why there is a need of learning Data Structure
• If you want to make high quality software's then you must
know data structures.
• For Technical interviews
• For GATE preparation etc…
Need of Data Structures

• As applications are getting complexed and amount of data is increasing day by day, there may arrise
the following problems:
• Processor speed: To handle very large amount of data, high speed processing is required, but as the
data is growing day by day to the billions of files per entity, processor may fail to deal with that much
amount of data.
• Data Search: Consider an inventory size of 106 items in a store, If our application needs to search for
a particular item, it needs to traverse 106 items every time, results in slowing down the search
process.
• Multiple requests: If thousands of users are searching the data simultaneously on a web server, then
there are the chances that a very large server can be failed during that process
• in order to solve the above problems, data structures are used. Data is organized to form a data
structure in such a way that all items are not required to be searched and required data can be searched
instantly.
What is Data Structure:
• Data Structure is a way to store and organize data so that it can be
used efficiently.

• Some examples of Data Structures are arrays, Linked List, Stack,


Queue, etc.

• Data Structures are the main part of many computer science


algorithms as they enable the programmers to handle the data in an
efficient way. It plays a vital role in enhancing the performance of a
software or a program as the main function of the software is to store
and retrieve the user's data as fast as possible.
Basic Terminology
• Data: Data can be defined as a value.
• Data Item: Data item refers to single unit of value.
• Group Items: Data items that are divided into sub items are
called Group item.
• Elementary Items: Data items that are not divided into sub items
are called Elementary item.
• Entity: An entity is something that has certain attributes to which
can assigned values.
• Entity Set: Entities with similar attributes form entity set.
Basic Terminology cont.…
• Field: Field is a single elementary unit of information
representing an attribute of an entity.
• Record: Record can be defined as the collection of field values
of a given entity.
• File: A File is a collection of various records of one type of
entity.
• Primary key: Primary key uniquely identifies record in the file
Data Structure Classification

Integer Float Character


Linear Data Structure
Arrays:
• An array is defined as a set of finite number of homogeneous elements or same data
items.
• It means an array can contain one type of data only, either all integer, all float-point
number or all character.
• Declaration of array is as follows: int num[5]
• The number of elements that can be stored in an array, that is the size of array or its
length is given by the following equation:
(Upperbound - lowerbound)+1
Linear Data Structure cont..

Linked List:
• It can be seen as the collection of nodes stored at non-contiguous
memory locations.
• Each node of the list contains two fields, one for storing data or
information and other for storing address of next element.

Head

AAA BBB CCC

Information field Pointer field


Linear Data Structure cont..
Stack:
• Stack is a linear list in which insertion and deletions are allowed only at one end, called top.
• Due to this property it is also called as last in first out type of data structure (LIFO).
• It could be through of just like a stack of plates placed on table in a party, a guest always
takes off a fresh plate from the top and the new plates are placed on to the stack at the top.
• It is a non-primitive data structure.
• When an element is inserted into a stack or removed from the stack, its base remains fixed
where the top of stack changes.
• Insertion of element into stack is called PUSH and deletion of element from stack is called
POP.
• The bellow show figure how the operations take place on a stack:

PUSH POP
Linear Data Structure cont..
Queue:
• Queue is a linear list in which elements can be inserted only at one end called rear
and deleted only at the other end called front.
• Queue is opened at both end therefore it follows First-In-First-Out (FIFO)
methodology for storing the data items.
• The people standing in a railway reservation row are an example of queue.
• Each new person comes and stands at the end of the row and person getting their
reservation confirmed get out of the row from the front end.
• The bellow show figure how the operations take place on a stack:

10 20 30 40 50

front rear
Non-Linear Data Structure cont..
Trees:
• Trees are multilevel data structures with a hierarchical relationship among its
elements known as nodes.
• The bottommost nodes in the hierarchy are called leaf node while the topmost
node is called root node. Each node contains pointers to point adjacent nodes.
• A tree can be defined as finite set of data items (nodes).
• The tree structure organizes the data into branches, which related the
information.
A root

B C

D E F G
Non-Linear Data Structure cont..
Graphs:
• Graphs can be defined as the pictorial representation of the set of elements
(represented by vertices) connected by the links known as edges.
• A graph is different from tree in the sense that a graph can have cycle while the tree
can not have the one.
• Definition: A graph G(V,E) is a set of vertices V and a set of edges E.
• Vertices on the graph are shown as circles and edges are drawn as line segment.
6
v2 v5
v1 v3
10

v1 8 11
15
9 v2
v3 v4 v4

[a] Directed & Weighted Graph [b] Undirected Graph


Operations on data structure
1) Traversing: Every data structure contains the set of data elements. Traversing the data
structure means visiting each element of the data structure in order to perform some specific
operation like searching or sorting.
2) Insertion: Insertion can be defined as the process of adding the elements to the data
structure at any location.
3) Deletion: The process of removing an element from the data structure is called Deletion.
4) Searching: The process of finding the location of an element within the data structure is
called Searching. There are two algorithms to perform searching, Linear Search and Binary
Search.
5) Sorting: The process of arranging the data structure in a specific order is known as Sorting.
There are many algorithms that can be used to perform sorting, for example, insertion sort,
selection sort, bubble sort, etc.
6) Merging: When two lists List A and List B of size M and N respectively, of similar type of
elements, clubbed or joined to produce the third list, List C of size (M+N), then this process is
called merging
University Asked Questions:
Q1. Hospital maintains a patient file in which each record
contains the following data:
Name, Admission Date, Social Security Number, Room,
Bed Number, Doctor
• Which item can serve as primary key?
• Which pair of items can serve as a primary key?
• Which items can be group items?
University Asked Questions:

Q2. Consider the linear array NAME in fig which is sorted alphabetically

•Find NAME [2], NAME [4] and NAME [7].


•Suppose Davis is to be inserted into the array. How many names
must be moved to new locations?
•Suppose Ricky is to be deleted from the array. How many names
must be moved to new locations?

NAME Adam Clark Evans Ricky Jones Lane Pace Smith

1 2 3 4 5 6 7 8
• Algorithm:

Algorithm is a finite step by step list of well defined


instructions to achieve a required result.
Algorithmic Notations
• Identifying Number
Each algorithm is assigned an identifying number. Eg. Algorithm 5.6

• Steps, Control, Exit


Actual algorithm is made up of sequence of numbered steps.
Control may be transfer to step n of the algorithm by the statement
“Go to Step n”
The algorithm is completed when the statement Exit is encounter.
Algorithmic Notations contd…
• Comments
Specifies the main purpose of step. Represented by bracket ([…])
• Variable Names
Variable names will use Capital letter. Eg. DATA, MAX.
Single letter name of variable used as a counter. Eg. K,N.
• Assignment statements
Eg: MAX:= DATA[1]
• Input and Output
Read: Variable Names
Write: Message/Variable Names
Example of Algorithm
Algorithm 1: (Addition of Two Numbers)

Step 1. [Initialize.] Set AB:=1, BC:=2 and CD:=0.


Step 2. [Addition.] Set CD:= AB+BC.
Step 3. [Result Display.] Write: CD.
Step 4. Exit
Control Structures
1) Sequence Logic / Sequential Flow
2) Selection Logic / Conditional Flow
3) Iteration Logic / Repetitive Flow

1)Sequence Logic / Sequential Flow

Module A

Module B

Module C
2) Selection Logic / Conditional Flow
Single Alternative Double Alternative Multiple Alternative
If condition, then: If condition, then: If condition(1), then:
[ Module A ] [ Module A ] [ Module A1 ]
[ End of If structure ] Else: Else If condition(2), then:
[ Module B ] [ Module A2 ]
NO [ End of If structure ] .
Condition
.
Yes Condition
NO .
Module A Else If condition(M), then:
Yes [ Module Am ]
Module A Module B Else:
[ Module B ]
[ End of If structure ]
3) Iteration Logic / Repetitive Flow
Repeat For Loop Repeat While Loop

Repeat for K = R to S by T: Repeat while condition:


[ Module ] [ Module ]
[ End of Loop ] [ End of Loop ]

K R
Condition?
Yes
Is K>S
Module (Body of Loop)
No
Module

K K+T
• Write an algorithm for finding largest element in Array…..
1. Set K := 1, LOC := 1, MAX : = DATA[1]
2. Repeat steps 3 and 4 while k <= N
3. If MAX < DATA[K] then:
Set LOC := K, MAX := DATA[K]
[End of If Structure]
4. Set K := K + 1
[End of step 2 loop]
5. Write LOC, MAX
6. Exit.
String operations
 Substrings
 Indexing
 Concatenation
 Length
 Text Processing Operations
 Insertion
 Deletion
 Replacing
Substring
A substring is the basic unit of access in a string.
Unlike other array elements, substrings have their
own meaning. To access a substring we need the
following information,
Name of the string.
Position of the first character of the substring in the
given string.
length of the substring.
If string A=“WELCOME”, then after
• Indexing
• Indexing operation is used to find the position of
the first appearance of a string pattern in a given
string. It is also called pattern matching. INDEX
operation returns a 0 if there is no such pattern in
that string.
• if A=’MORNING’ then INDEX(A,’NI’) will return, 4,
the first appearance of pattern ‘NI’ in the given
string ‘MORNING’.
• Concatenation
• if A and B are two strings, then the concatenation
of two strings, A//B is the string that consisting of
the characters of A followed by the characters of
B.
• if A=’GOOD’ and B=’MORNING’ then A//B will be
‘GOODMORNING’
• A//[]//B= GOOD MORNING
• Length
• The number of characters in a string is called the
length of that string.
• if A=”DEC EMBER” then LENGTH(A) will return 9.
• Text Processing Operations
• The processing of printed matter such as letters
and articles are called word processing. Basic
operations associated with word processing are,
• Replacement
• Insertion
• Deletion
• Word processing operations can be done using
string operations.
• Insertion
• INSERT(TEXT,POSITION,STRING) operation
inserts STRING in TEXT so that STRING begins in
POSITION.
• For example INSERT(‘ABCDE’,2,’PQR’) =
• ‘APQRBCDE’
• INSERT operation can be implemented using string
operations as follows,
• INSERT(T,K,S) = SUBSTRING(T,1,K-1) // S //
SUBSTRING(T,K,LENGTH(T)-K+1)
• That is the substring before the position K is
• Deletion
• DELETE(TEXT,POSITION,LENGTH) delete the
substring which begins at POSITION and has
length LENGTH.
• For example DELETE(‘ABCDEF’,2,3) = ‘AEF’
• DELETE operation can be implemented using
string operations as follows,
• DELETE(T,K,L) = SUBSTRING(T,1,K-1) //
SUBSTRING(T,K+L,LENGTH(T)-K-L+1)
• =A//(T,5,(6-2-3+1))=(T,5,2)=A//EF=AEF
• That is the substring before position k is
• Replacing
• REPLACE(TEXT,PATTERN1,PATTERN2) replaces
the first occurrence of PATTERN1 by PATTERNP2 in
TEXT.
• For example REPLACE(‘PQRSTU’,’RS’,’X’) = PQXTU
University Asked Question
Consider the following algorithm.
Set K := INDEX(T,P)
Repeat while k != 0
Set T := DELETE(T,INDEX(T,P),LENGTH(P))
Set K := INDEX(T,P)
Write :T
Exit.

Answer the following with reference to above algorithm.


• What is the purpose of the algorithm.
• What is T & P?
• Suppose T = XABYABZ and P = AB. If algorithm is now run, how many time will the loop
be repeated? What is the output.
• Now, T = XAAAABBBY and P = AB, if the algorithm is now run, how many time will the
loop be repeated? What is the output.
University Asked Question

Q..Let S & T be character variables such that S=’JOHN PUAL


JONES’ and T=’A THING OF BEAUTY IS A JOY FOREVER’.

• Find (a) LENGTH(s) (b) LENGTH(T) (c) SUBSTRING(S,4,8)


(d) SUBSTRING(T,10,5) (e) INDEX(s, 'JO') (f) INDEX(s, 'JOY')
(g) SUBSTRING(s,11,5)//SUBSTRING(S,1,9)
JONES//JOHN PUAL
= JONESJOHN PUAL
Complexity of Algorithm
• The complexity of an algorithm M is the function f(n) which
gives the running time and/or storage space requirement of the
algorithm in terms of the size n of the input data.

• Time Complexity: Time for the key operations


Log n < N< n log n<n2<n3<2n
Space Complexity: Storage Space
Three cases in complexity theory:
• Worst Case:
The maximum value of f(n) for any possible input
• Average Case:
The expected value of f(n)
• Best Case:
The minimum possible value of f(n)
Algorithmic Notations for Complexity of Algorithms
• Omega Notations (Ω)
Omega notation defines an lower bound function g(n) for
f(n).
• Theta Notation (ϴ)
Theta notation is used when the function f(n) is bounded
both from above and below by the function g(n)
• Big-Oh Notation (о)
Big-Oh notation defines an upper bound function g(n) for
f(n) f(n) 12n2+12n+1= g(n)= o(n2)
University Asked Question
Q. Consider the following algorithm.
1. Set K := 1, LOC := 1, MAX : = DATA[1]
2. Repeat steps 3 and 4 while k <= N
3. If MAX < DATA[K] then:
Set LOC := K, MAX := DATA[K]
[End of If Structure]
4. Set K := K + 1
[End of step 2 loop]
5. Write LOC, MAX
6. Exit.
Answer the following with reference to above algorithm.
1. Describe and find C(N) for the worst case. ==== c(n)=n-1
2. Describe and find C(N) for the best case.=== c(n)=0
3. Find C(N) for the average case when N =3 .Assume all arrangements of the elements in
DATA are equally likely.= 5/6
• Pattern Matching Algorithm
-Pattern matching is the problem of deciding whether or not a
given string pattern p appears in a string text T.
-Length of P does not exceed the length of T.
-In pattern matching algorithms, characters are sometimes
denoted by lower letters (a, b, c, d…) and exponents may be used
to denote repetition eg: a2 b3 c = aabbbc
Types:
1)First Pattern Matching Algorithm
2)Second Pattern Matching Algorithm
• First Pattern Matching Algorithm
• We compare a given pattern p with each of the substring
of T, moving from left to right, until we get match.
• Wk=SUBSTRING(T, K, LENGTH(P))
• A)Suppose P=aaba T=(cd)10

• B)Suppose P=aaba T=ababaaba

• C)P=aaba T=a20
For each of the following patterns P and text T , find
the number of Comparison to find INDEX of P in T
using Slow algorithm
a) P=abc T= ababababab
b) P= abc T= aaaabbbbcccc
c) P= aaa T= aabbaabbaabb
e) P=aaa T = abaabbaaabbbaaaabbbb
Q. Using first pattern matching algo solve P=abc T= ababababab
ans:
Max=Length(P)-Length(T)+1 (i.e.Maximum substrings possible)
Max=10-3+1=8

Wk=SUBSTRING(T,K,Length(P))
W1=SUBSTRING(ababababab,1,3)=aba ----- here aba!=Pattern abc ----------------------------------------- =3(No of Comparision
made to match P with substring of T)
W2=SUBSTRING(ababababab,2,3)=bab---- bab!=abc --------------------------------------------------------- =1
W3=SUBSTRING(ababababab,3,3)=aba---- aba!=abc --------------------------------------------------------- =3
W4=SUBSTRING(ababababab,4,3)=bab---- bab!=abc --------------------------------------------------------- =1
W5=SUBSTRING(ababababab,5,3)=aba---- aba!=abc --------------------------------------------------------- =3
W6=SUBSTRING(ababababab,6,3)=bab---- bab!=abc --------------------------------------------------------- =1
W7=SUBSTRING(ababababab,7,3)=aba---- aba!=abc --------------------------------------------------------- =3
W8=SUBSTRING(ababababab,8,3)=bab---- bab!=abc --------------------------------------------------------- =1
Therefore INDEX(T,P)=0 (Pattern P is not Present in Text T)
No of comparison=3+1+3+1+3+1+3+1=16
Consider the pattern P=abc. Use ‘slow’ pattern
matching algorithm to find number of comparisons
to find INDEX of P in each of the following texts T.
a) a20 b) (abc) 10 c) (caab) 10 d) d10
First Pattern Matching Algorithm

1. [Initialize] Set K:=1 and MAX:=S-R+1.


2. Repeat Steps 3 to 5 while K<=Max:
3. Repeat for L= 1 to R:[Test each character of P]
If P[L]!=T[K+L-1], then: Go to step 5.
[End of inner loop]
4. [success] Set INDEX=K, and Exit.
5. Set K:=K+1.
• [End of step 2 outer loop]
6. [Failure] Set INDEX=0
7. Exit
Second Pattern Matching Algorithm
Q. Consider the Pattern P=aaabb . Construct the Table and the
Corresponding labeled directed graph used in the FAST or
Second pattern Matching algorithm.

Q. Consider the Pattern P=ababab . Construct the Table and


the Corresponding labeled directed graph used in the FAST or
Second pattern Matching algorithm.

Q. Consider the Pattern P=abaab . Construct the Table and the


Corresponding labeled directed graph used in the FAST or
Second pattern Matching algorithm.
Q. Consider the Pattern P=aaba . Construct the Table and the Corresponding labeled directed graph used in the FAST or Second
pattern Matching algorithm.
ans:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy