You are on page 1of 21

Elementary Data Structures Lec-3

Fahad Maqbool
1

Quiz 1 - BSCS 6th Reg

What is graph coloring problem? Why is it NP complete?

What is difference between NP and P time problems?

What is O(1) time? How greedy algorithm provide optimal solution?

What is asymptotic complexity?

Problems

Problem: a task to be performed.


Best

thought of as inputs and matching outputs. definition should include constraints on the resources that may be consumed by any acceptable solution.

Problem

Resource (Space)

INPUTS Data Structure ALGORITHM

OUTPUT

Resource (Time)
4

Examples Factors to Consider


Airline Reservations Trans-Fryslan Airlines Attempt 1: Declare as many variables as are the available seat and assign a value to each variable. Horrible algorithms for the basic operations! Attempt 2: Declare an array of length equal to the Maximum seats available and then specify whether the seat is occupied or unoccupied. Nice algorithms for the basic operations!
Tradeoff: simplicity of data organization simplicity/elegance of algorithms Simple (unsophisticated data structure) may require much work for processing data. More complex data organization may yield nicer algorithms for the basic operations
5

Examples - cont.

Searching an online phone directory: Linear search? OK for Calvin College too slow for Grand Rapids or New York
Amount of data is an important factor.

Restructure (order) the data set for efficient processing use binary search or an indexed sequential search

Compiler lookup of an identifier's type, etc. in a symbol table: Linear search? No, too slow Binary search? No, too much work to keep sorted Number of accesses & speed required is an important factor. Use hash tables
Text processing: Store in an array / vector? OK for text analysis word counts, average word length, etc. Not for word-processing Too inefficient if many insertions & deletions
Static vs. dynamic nature of the data is an important factor
6

The Need for Data Structures


Goal: to organize data Criteria: to facilitate efficient


storage

of data retrieval of data manipulation of data

Design Issue:
select

and design appropriate data types. (This is the real essence of OOP.)
7

Organizing Data
The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. The selection of the correct data structure is the difference between success and failure. Any organization and collection of records should be searchable, processable, modifiable in any order.
8

Some Questions to Ask

Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations?

Can data be deleted?


Are all data processed in some well-defined order, or is random access allowed/required?
9

Abstract Data Types (Cont.)


Def. a collection of related data items together with an associated set of operations
e.g. whole numbers (integers) and arithmetic operators for addition, subtraction, multiplication and division.

e.g. Seats for TFA Basic operations: find empty seat, reserve a seat, cancel a seat assignment Why "abstract?" Data, operations, and relations are studied independent of implementation.
What not how is the focus.
10

Dynamic sets
Sets are as fundamental to Computer Sciences as they are to Mathematics. Mathematical sets are fixed, but sets in Computer science can shrink and grow or change over time. We call them dynamic sets. In the domain of algorithms, we need to perform set operations that are different from those of mathematics.
11

Dynamic sets
If we only have to implement INSERT, DELETE and test MEMBERSHIP on a dynamic set, we call such a set as a Dictionary. For a dynamic set we assume that one of the fields is an identifying fields, called as the key field.

12

Dynamic sets
(Operations)
Search (S, k) Insert (S, x) Delete (S, x) Minimum/Maximum (S)

Successor (S, x)
Predecessor (S, x)
13

Array implementation of List


1 2

First Element Second Element


list

last

Last Element
empty

maxlength

14

Linked Lists
Flexible

space use Dynamically allocate space for each element as needed Include a pointer to the next item Linked list Data Each node of the list contains

Next

the data item (an object pointer in our ADT) object a pointer to the next node Collection structure has a pointer HEAD

15

Linked Lists

Add time
Constant

- independent of n i.e. O(1)

Search time
Worst

case O(n)

Collection Head node Data Next node Data Next

object2

object
16

Comparison of methods

Array implementation requires us to specify max size at compile time If can not put a bound on the size of list, may use pointers Certain operations take longer in one implementation than the other. In case of values changing at certain locations, pointers can NOT be used. Array implementation may waste space. Pointer implementation uses only as much space, as needed on the list, BUT requires pointer space.
17

Comparison of List implementations


Operation Update(x) Length() InsertAfter(x) InsertBefore(x) Remove() IncrementCurrent() DecrementCurrent() Find(x) Array O(___) O(___) O(___) O(___) O(___) O(___) O(___) O(___) Singly-linked O(___) O(___) O(___) O(___) O(___) O(___) O(___) O(___)

18

Comparison of List implementations


Operation Update(x) Length() InsertAfter(x) InsertBefore(x) Remove() IncrementCurrent() DecrementCurrent() Find(x) Contiguous O(1) O(1) O(n) O(n) O(n) O(1) O(1) O(n) Singly-linked O(1) O(1) O(1)* O(1)* O(1)* O(1) O(n) O(n)

* Actual operation, excludes the search time.


19

Homework 1 BSCS 6th SS2

Take adjacency matrix as input from the user using a text file, where first two rows will represent #rows and #columns respectively.
Implement the graph using array data structure and linked list. Display the graph with vertices on the parameter of the circle. Perform the following operations on the graph while calculating the time and space requirements

Create Graph Check Adjacency Find degree of a user specified vertex

20

Input format
4 5 1 0 1 0
0 0 1 1 1 0 1 0 1 0 1 1 1 0 1 0

21

You might also like