data structures and algorithms lecture 1 instructor: quratulain date: 1 st sep, 2009

21
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

Upload: simon-hodge

Post on 04-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

Data Structures and Algorithms

Lecture 1

Instructor: Quratulain

Date: 1st Sep, 2009

Page 2: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

2

Introduction to Data Structures

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

For Example◦ Imagine that you are hired by company

XYZ to organize all of their records into a computer database.

◦The first thing you are asked to do is create a database of names with all the company's management and employees.

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 3: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

3

Introduction

You also want your database to represent the relationships between management and employees at XYZ

These two diagrams are examples of different data structures

This is very useful for keeping the names of the employees in alphabetical order so that we can locate the employee's record very quickly. However, this structure is not very useful for showing the relationships between employees. A tree structure is much better suited for this purpose.CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 4: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

4

IntroductionDuring this course, you will learn how data

structures are created inside a computer. You will find there is quite a difference

between your mental picture of a data structure and the actual way a computer stores a data structure in memory.

You will also discover that there are many different ways of creating the same data structure in a computer.

These various approaches are tradeoffs that programmers must consider when writing software.

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 5: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

5

Computer MemoryEvery piece of data that is stored in a

computer is kept in a memory cell with a specific address.

We can think of these memory cells as being a long row of boxes where each box is labeled with an address.

Computer memory is linear.The computer can store many different types

of data in its memory. These include basic data types integers, float and characters etc.

Once the computer stores data in the memory cells, it can access the data by using the address of the data cells.

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 6: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

6

Computer Memory

For example, consider the following instructions for adding two integers together.

For example, suppose we want a data structure that can store a group of characters as a word. In many computer languages, this data structure is called a string. If we store the characters for the string 'apple' in the computer's memory

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 7: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

7

Computer MemoryBut what happens when we try to

represent our tree diagram of company XYZ? It doesn't make sense to store the names one after the other because the tree is not linear.

Now we have a problem. We want to represent a nonlinear data structure using computer memory that is linear

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 8: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

8

Elementary Data structuresElementary Data Structure are

fundamental approaches to organizing data. These are the building blocks that will be used to implement more complex Abstract Data Types.

1. Scalar (built-in) data types2. Arrays3. Linked Lists4. Strings

CSE 246 Data Structures and Algorithms Fall2009 Lecture #1

Page 9: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

9

ArraysData structures that store a large

collection of data and allow direct access to the elements by using an index.◦Have a fixed length. A programmer

must specify the size of an array at the point of its declaration.

◦Applications often need storage structures, which grow and contract as data is added and removed.

CSE 246 Data Structures and Algorithms Fall2009 Lecture #1

Page 10: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

10

Array ListIndexing features of an arrayCreated with initial capacity that

designates the available space to hold elements.

Ability to grow dynamically to meet the runtime needs of a program

CSE 246 Data Structures and Algorithms Fall2009 Lecture #1

Page 11: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

11

Array ListNot an efficient storage structure for general

insertion and deletion of items at an arbitrary position in a list.◦ Insert requires shifting a block of elements to the

right to make room for a new item.◦ Deletion requires shifting a block of elements to

the left to fill in the gap created by the missing item.

CSE 246 Data Structures and Algorithms Fall2009 Lecture #1

Page 12: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

12

Data Structures and AlgorithmsData structures have operations

that manipulate the data.The design and implementation

of these operations involve algorithms that consist of a finite sequence of instructions to perform a task.

CSE 246 Data Structures and Algorithms Fall2009 Lecture #1

Page 13: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

13

Data structures and AlgorithmsThere is a famous saying by the

renowned teacher and scholar, Nicolas Wirth that

“Algorithms + Data Structures = Programs” (Wirth)

“For many applications, the choice of the proper data structure is the only major decision involving the implementation: once the choice is made, the necessary algorithms are simple.” (Sedgewick)

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 14: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

14

Data StructureSuppose we have a list of sorted data on which

we have to perform the following operations:◦ Search for an item◦ delete a specified item◦ insert (add) a specified item

Example: suppose we begin with the following list:data: 345 358 490 501 513 555 561 701 724 797

location: 0 1 2 3 4 5 6 7 8 9

What is a list?◦ A list is a data structure where data is represented

linearly. ◦ Finite sequence of items from the same data type◦ If arrays are used, items are stored contiguously in

the memory

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 15: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

15

List implementation using an Array

Example: suppose we begin with the following list:data: 345 358 490 501 513 555 561 701 724 797

location: 0 1 2 3 4 5 6 7 8 9

Now, delete item 358 from the above listQ: What is the algorithm to delete an item?

Q: What is the cost of deleting an item?data: 345 358 490 501 513 555 561 701 724 797

location: 0 1 2 3 4 5 6 7 8 9

Q: When we delete 358, what happens to that location?

Now, add item 498 onto the above listQ: Where would that item go?

data: 345 358 490 501 513 555 561 701 724 797

location: 0 1 2 3 4 5 6 7 8 9

Q: What is the cost of inserting an item?

Conclusion:Using a list representation of data, what is the overall efficiency of searching, adding, and deleting items?

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 16: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

16

Deletion of an Element from a List Algorithm:

1. locate the element in the list (this involves searching)

2. delete the element

3. reorganize the list and indexExample:

data: 345 358 490 501 513 555 561 701 724 797

location: 0 1 2 3 4 5 6 7 8 9

Delete 358 from the above list:

4. Locate 358: if we use ‘linear search’, we’ll compare 358 with each element of the list starting from the location 0.

5. Delete 358: remove it from the list (space=10)data: 345 490 501 513 555 561 701 724 797

location: 0 1 2 3 4 5 6 7 8 9

6. Reorganize the list: move the remaining elements. (space=9)

data: 345 490 501 513 555 561 701 724 797 ?(797)

location: 0 1 2 3 4 5 6 7 8 9

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 17: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

17

Insertion of an Element in List Algorithm:

1. locate the position where the element in to be inserted (position may be user-specified in case of an unsorted list or may be decided by search for a sorted list)

2. reorganize the list and create an ‘empty’ slot3. insert the elementExample: (sorted list)

data: 345 358 490 501 513 555 561 701 724 797

location: 0 1 2 3 4 5 6 7 8 9

Insert 505 onto the above list:4. Locate the appropriate position by performing a binary

search. 505 should be stored in location 4. 5. Create an ‘empty’ slot

data: 345 358 490 501 513 555 561 701 724 797

location: 0 1 2 3 4 5 6 7 8 9 10

3. Insert 505data: 345 358 490 501 505 513 555 561 701 724

797

location: 0 1 2 3 4 5 6 7 8 9 10

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 18: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

18

Methods for defining a collection of objectsArray

◦successive items locate a fixed distancedisadvantage

◦data movements during insertion and deletion

◦waste space in storing n ordered lists of varying size

possible solution◦linked list◦linked lists are dynamically allocated and

make extensive use of pointersCSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 19: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

19

Lab # 01Task 1: Find max number from array list

of 10 item. Find total number of comparisons need to perform this task.

CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1

Page 20: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

20

Lab # 01Design a program that records

and reports the weekly sales amounts for the salespeople of a company. Each of the n salespeople is assigned a unique identification number in the range of 1 to n. The program prompts the user to enter the number of salespeople, and it uses that value to create an array of sales amount.CSE 246 Data Structures and Algorithms Fall2009 Lecture #1

Page 21: Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

CSE 246 Data Structures and Algorithms Fall2009 Lecture #1

21

Sample code import java.util.Scanner; public static void main(String args[]) { Scanner

input = new Scanner(System.in); System.out.print("Enter TWO dates: "); String myStr = "";

String myStr2 = ""; while (input.hasNext()) { myStr = input.next(); myStr2 = myStr2 + myStr; } input.close(); System.out.print("You entered:" + myStr2); } }