+ All Categories
Home > Documents > DSA Lecture 1 Final

DSA Lecture 1 Final

Date post: 26-Nov-2023
Category:
Upload: independent
View: 0 times
Download: 0 times
Share this document with a friend
29
Data Structures and Algorithms Dr. Javed Iqbal Assistant Professor Department of Computer Science University of Engineering and Technology (UET) Taxila
Transcript

Data Structures and Algorithms

Dr. Javed IqbalAssistant Professor

Department of Computer Science

University of Engineering and Technology (UET)

Taxila

IntroductionAlgorithms and Data StructuresStatic Data Structures

Searching AlgorithmsSorting AlgorithmsList implementation through ArrayADT: StackADT: Queue

Dynamic Data Structures (Linear)Linked List (Linear Data Structure)

Dynamic Data Structures (Non-Linear)Trees, Graphs, Hashing

What is a Computer Program?To exactly know, what is data structure? We must

know:What is a computer program?

Input

Some mysterious processing Output

DataData are simply collection of facts and figures. Data are values or set of values. A data item

refers to a single unit of values. Data items that are divided into sub items are

group items; those that are not are called elementary items. For example, a student’s name may be divided into three sub items – [first name, middle name and last name] but the ID of a student would normally be treated as a single item.

DefinitionAn organization of information, usually in memory,

for better algorithm efficiencysuch as queue, stack, linked list and tree.

In computer science, a data structure is a particular way of storing and organizing data in a computer’s memory so that it can be used efficiently.

Data may be organized in many different ways; the logical or mathematical model of a particular organization of data is called a data structure.

3 steps in the study of data structures

Logical or mathematical description of the structure

Implementation of the structure on the computer

Quantitative analysis of the structure, which includes determining the amount of memory needed to store the structure and the time required to process the structure

Data Types

o Set of possible values for variableso Operations on those values

o Ex : int, float, char ……….

An example of several common data structures:

• arrays, • linked lists, • stacks,• queues, • trees, • and Graph

ArrayLinear array (One dimensional array) : A list of finite number n of similar data elements referenced respectively by a set of n consecutive numbers, usually 1, 2, 3,…..n. That is a specific element is accessed by an index.Let, Array name is A then the elements of A is : a1,a2….. an

Or by the bracket notation A[1], A[2], A[3],…………., A[n]The number k in A[k] is called a subscript and A[k] is called a subscripted variable.

element

Array ExamplesExample:A linear array A[8] consisting of numbers is pictured in following figure.

Array Examples

Decleration of the Arrays: Any array declaration contains: the array name, the element type and the array size.Examples:int a[20], b[3],c[7];float f[5], c[2];char m[4], n[20];Initialization of an array is the process of assigning initial values. Typically declaration and initialization are combined.Examples:float, b[3]={2.0, 5.5, 3.14};char name[4]= {‘E’,’m’,’r’,’e’};int c[10]={0};

Array (con…)Linear arrays are called one dimensional arrays because each element in such an array is referenced by one subscript. (Two dimensional array) : Two dimensional array is a collection of similar data elements where each element is referenced by two subscripts.Such arrays are called matrices in mathematics and tables in business applications.Multidimensional arrays are defined analogously

1 2 3 45 6 7 89 10 11 1213 14 15 16

12

1 2 3 4

34

MATRICES

Here, MATRICES[3,3]=11

Array Data Structure It can hold multiple values of a single type. Elements are referenced by the array name

and an ordinal index. Each element is a valueIndexing begins at zero. The array forms a contiguous list in

memory. The name of the array holds the address of

the first array element. We specify the array size at compile time,

often with a named constant.

Linked lists• A linked list, or one way list, is a linear collection of

data elements, called nodes, where the linear order is given by means of pointers.

• Dynamically allocate space for each element as needed.

In linked listEach node of the list contains the data item

a pointer to the next node

Collection structure has a pointer to the list Start

Initially NULL

NextData

Node

Start

Linked lists

Start

Data Nextnode node

Data

Linked list with 2 nodes

Linked lists

1

2

3

4

5

6

7

8

A

M

G

N

O

3 5

2

7

4

0

START START=3, INFO[3]=MLINK[3]=2, INFO[2]=ALINK[2]=5, INFO[5]=NLINK[5]=4, INFO[4]=GLINK[4]=7, INFO[7]=OLINK[7]=0, NULL value, So the list has ended

INFO LINK

Stacks Stacks are a special form of collection

with LIFO semantics Two methods

- add item to the top of the stack - remove an item from the top of the stack

Like a plate stacker

Collection with access only to the last element inserted

Last in first outinsert/pushremove/poptopmake empty

TopData4

Data3

Data2

Data1

QueuesCollection with access only to the item that

has been present the longestLike a stack, a queue is also a list.

However, with a queue, insertion is done at one end, while deletion is performed at the other end

The insertion end is called rearThe deletion end is called front

InsertRemoverearfront

edges

Data Structure Operations

1. Traversing: Accessing each record exactly once so that certain items in the

record may be processed.

2. Searching: Finding the location of the record with a given key value.

3. Inserting: Adding a new record to the structure.

4. Deleting: Removing a record from the structure.

5. Sorting: Arranging the records in some logical order.

6. Merging: Combing the records in two different sorted files into a single

sorted file.

The data appearing in our data structure is processed by means of certain operations. In fact, the particular data structure that one chooses for a given situation depends largely on the frequency with which specific operations are performed. The following four operations play a major role:

The following two operations, which are used in special situations, will also be considered:Sorting:Arranging the records in some logical orderMerging:Combining the records in two different sorted files into a single sorted files

Data structure operations (Continued)

AlgorithmsAn essential aspect to data structures is algorithms. Data structures are implemented using algorithms. An Algorithm is a finite step – by – step list of well defined instructions for solving a particular problem. It is used to manipulate the data contained in the data structures as in searching and sorting. It states explicitly how the data will be manipulated.

Perform data structure operations

ComplexityThe complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the amount of data the algorithm must process. There are two main complexity measures of the efficiency of an algorithm:Time complexity is a function describing

the amount of time an algorithm takes in terms of the amount of input to the algorithm.

Space complexity is a function describing the amount of memory (space) an algorithm takes in terms of the amount of input to the algorithm.

• Once data is organized into forms such as trees, stacks and queues, standard algorithms can be used to process them.

• Properly organized data lead to easy-to understand user interfaces

Discussion

Hash TablesTake a key, apply functionf(key) = hash valuestore data or object based on hash valueSorting O(N), access O(1) if a perfect hash

function and enough memory for tablehow deal with collisions?

Other ADTsGraphs

Nodes with unlimited connections between other nodes


Recommended