1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null 7 65 42.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Linked Lists.
Data Structures ADT List
MATH 224 – Discrete Mathematics
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Algorithms, Lists and Pseudocode Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Administrivia- Introduction CSE 373 Data Structures.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Computer Science 2 Data Structures and Algorithms V section 2 Intro to “big o” Lists Professor: Evan Korth New York University 1.
EE Data Structures and Algorithms N Radhakrishnan Assistant Professor Anna University, Chennai.
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
Computer Science 2 Data Structures and Algorithms V Intro to “big o” Lists Professor: Evan Korth New York University 1.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
CS223 Algorithms D-Term 2013 Instructor: Mohamed Eltabakh WPI, CS Introduction Slide 1.
Lecture No.01 Data Structures Dr. Sohail Aslam
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Design and Analysis of Algorithms CSC201 Shahid Hussain 1.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
ITEC 2620A Introduction to Data Structures
More on Lists CSE 373 Data Structures Lecture 4. 1/13/03More on Lists - Lecture 42 Alternative Addition Use an auxiliary function ›AddAux(p,q : node pointer,
DATA STRUCTURES (CS212D) Week # 1: Overview & Review.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
Data Structure Introduction.
Data Structure & Algorithm
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
Linked List by Chapter 5 Linked List by
Elementary Data Organization. Outline  Data, Entity and Information  Primitive data types  Non primitive data Types  Data structure  Definition 
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Data Structures and Algorithms in Java AlaaEddin 2012.
DATA STRUCTURES (CS212D) Overview & Review Instructor Information 2  Instructor Information:  Dr. Radwa El Shawi  Room: 
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Mohammed I DAABO COURSE CODE: CSC 355 COURSE TITLE: Data Structures.
Introduction toData structures and Algorithms
Lecture 6 of Computer Science II
Elementary data structures
Lectures linked lists Chapter 6 of textbook
Data Structure Interview Question and Answers
CE 221 Data Structures and Algorithms
LINKED LISTS CSCD Linked Lists.
Computation.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Data Structures ADT List
CS212D: Data Structures Week 5-6 Linked List.
Lists CSE 373 Data Structures.
Administrivia- Introduction
Introduction to Data Structure
Lists CSE 373 Data Structures.
Administrivia- Introduction
(1 - 2) Introduction to C Data Structures & Abstract Data Types
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null

2 Welcome to Data Structures Spring 2008  Instructor  Yael Moses office hours: Thu. 10:30-11:30 or by appointment  TA  Ran Eshel

3 Textbook  Cormen, Leiserson, Rivest and Stein Introduction to Algorithms. (CLRS)  You will need this book also next year (for Algorithms) and maybe for additional elective courses.  A Hebrew translation exists (by Open univ.)  You will really need to read!!

4 Student's Responsibilities: During the semester you will need to submit:  6-7 theoretical assignments  3 programming projects

5 Final Grade If final exam < 60 then grade = fail else grade = 0.2 *assignmnets * final exam. Warning: DS is (considered) a difficult course.

6 Today  What is data structure?  What is it good for?  Getting started  An overview of the course

7 Solving a problem Input  manipulation  output

8 input  algorithm  output

9 Algorithm Data {input}  {data manipulated}  Data {output} 2,1,12,7 1,2,7,12 Shimon    

10 Solving a problem Data: information for analysis. e.g., numbers, words, songs, movies Algorithm: A well-defined procedure to solve a problem Data structure: the way the data is organized

11 The Sorting Problem Input: A sequence of n numbers Output: A permutation (reordering) of the input sequence such that b 1 ≤b 2 … ≤b n Note: the sets {a 1,a 2,..,a n }= {b 1,b 2,..,b n } Example:  input:  output:

12 Insertion Sort  Picking the elements 1 by 1 from the sequence  For each element insert it into correct place in the sequence of already sorted elements  Until last element inserted into place

13 Insertion Sort

14 What is missing?  How the data (numbers) are represented:  Each number: binary  The sequence of numbers: ?  The operations required in the chosen representation?  Picking the elements 1 by 1 from the sequence  For each element insert it into correct place in the sequence of already sorted elements  Until last element inserted into place

15 Insertion Sort (use array) Insertion-Sort(A) for i  2 to length[A] key  A[i] j  i-1 while j>0 and A[j]>key A[j+1]  A[j] j  j-1 end A[j+1]  key end What is the worse case?

16 Pseudocode  In the lectures I will be presenting algorithms in pseudocode.  This is very common in the computer science literature  Pseudocode is usually easily translated to real code.  Pseudocode should also be used for homework

17 Pseudocode  The algorithms you design in homework will be read by a person, not a computer  The No Code Rule:  Do not turn in Java or C code when asked for pseudocode  Explain algorithm precisely, but without all the details needed for a computer code

18 Pseudocode example (good) Insertion-Sort(A) for i  2 to length[A] key  A[i] j  i-1 while j>0 and A[j]>key A[j+1]  A[j] j  j-1 end A[j+1]  key end

19 Java code public static void insertionSort (int[] array){ for (int j = 1; j < array.length; j++) { int key = array[j]; int k = j - 1; while(k >= 0 && array[k] > key) { array[k+1] = array[k]; k--; } array[k+1] = key; }

20 What is a Data Structure? The way the data is organized  What do we mean by “way”?  The organization should allow an efficient use of the data

21 Abstract Data Type An abstract mathematical definition of objects, with operations defined on them

22 Elementary Data Structures  Arrays  Lists  Stacks  Queues  Trees RF In some languages these are basic data types – in others they need to be implemented head

23 Examples  Basic Types  integer, real (floating point), boolean (0,1), character  Arrays  A[0..99] : integer array  A[0..99] : array of images A … …

24 A mapping from an index set, such as {0,1,2,…,n}, into a cell type Objects: set of cells Operations:  create(A,n)  put(A,v,i) or A[i]  v  value(A,i) ADT: Array

25 Simple Linked List Group data together in a linear and flexible way. Example: ABAC R Add an element (Firsy in the list)

26 Examples of Lists  List of numbers  List of names  List of lists  List of arrays  Representing a sparse high order polynomial

27 Objects: Sets of elements (e.g., integers) Operations:  Create a set  Insert an element  Remove an element  Check membership  Minimum  Maximum {5,1,2,2…,3} ADT: Finite Dynamic Sets

28 Data Structures  Representation of objects of an Abstract Data Type (ADT)  Algorithms manipulate the data structures to implement the operations of the ADT ADT: An abstract mathematical definition of objects, with operations defined on them

29 Objects: Finite sequences of nodes Operations:  Create  On a list  Get the first node  On a node  Get data  Get next node  Assign value to data  Assign next node ADT: Basic Linked List ABA nil

Examples of using linked list Operations 30

31 Basic List Operations: in pseudo code  head[L] - the first node of L (in Java: L.head)  next[x] - the next node in L or NIL if x is the tail of L (in Java: x.next)  data[x] – the node data (in Java: x.data)

32 More list operations Low level:  List-Search(L,k)  List-head-Insert(L,x)  Insert-after-key(L,k,x)  List-tail-Insert(L,x)  List-Delete(L,k) Higher level:  Sort(L)  Reverse(L) Can be implemented using the basic list operations

33 Searching List-Search(L,k) x  head[L] while (x  NIL) and (data[x]  k) dox  next[x] return(x)

34 Inserting into the head List-head-Insert(L,x) next[x]  head[L] head[L]  x

35 Insert after a key k Value NULL L node ValueNext node k Insert the value v after P Next Value v Next

36 Insert after a key Insert-after-key(L,k,x) y  List-Search(L,k) if y  NIL next[x]  next[y] next[y]  x end

37 Linked List Delete a key L ValueNext node Value Next node k NULL ValueNext

38 Linked List Delete a key L ValueNext node To delete the node pointed to by Q, need a pointer to the previous node; Value Next node Q NULL

39 Linked List Delete L ValueNext node Value Next node NULL k prev_x

40 Deleting a key List-Delete ( L,k) x  head[L] prev_x= head[L] while (x  NIL) and (data[x]  k) do prev_x  x x  next[x] end if prev_x  NIL next[prev_x]  next[x] end

41 Doubly Linked Lists  As mentioned, for delete we need ‘findPrevious’. This is a slow [O(N)] function - because we cannot go directly to previous node  Solution: Keep a "previous" pointer at each node. head prev

42 Double Link Pros and Cons  Advantage  Delete (not DeleteAfter) and FindPrev are faster  Disadvantages:  More space used up (double the number of pointers at each node)  More book-keeping for updating the two pointers at each node (pretty negligible overhead)

43 Insertion Sort Using Linked Lists

44 Data Structure: linked list implementation  Using records and pointers  Using arrays

45 Pointer Implementation Basic Idea  Data: For each node allocate memory  Pointer: Keep a pointer to the memory location. Value NULL L node ValueNext node Next

46 Records A record (also called a struct, similar to object)  Group data together that are related  To access the fields we use “dot” notation. Example: x: name and image Example: x.name x.image imagename Elton John

47 Pointer  A pointer is a reference to a variable or record (or to an object in Java world).  In C, if X is of type pointer to Y then *X is of type Y – same for pseudocode Example: x : record pointer record*x

48 A node: a record which contains a pointer  Group data and a pointer  To access the fields we use Name: text Image: image Example: x : complex number Example: x.name x.image x.next next: node reference (a pointer) name[x] image [x] next[x] OR

49 Simple Linked List A linked list  Group data together in a flexible, dynamic way L : node pointer record node : ( data : integer (or any other structure) next : node pointer )

50 Pointer Implementation Issues  Whenever you break a list, your code should fix the list up as soon as possible  Draw pictures of the list to visualize what needs to be done  Pay special attention to boundary conditions:  Empty list  Single item – same item is both first and last  Two items – first, last, but no middle items  Three or more items – first, last, and middle items

51 Implementing Pointers using Arrays  This is needed in languages like Fortran, Basic, and assembly language  Easiest when number of records is known ahead of time.  Each record field of a basic type is associated with an array.  A pointer field is an unsigned integer indicating an array index.

52 Idea data next n nodes n-1 data : basic type next : node pointer D N D[ ] : basic type array N[ ] : integer array Pointer is an integer nil is -1 p.data is D[p] p.next is N[p] Two variables: L (head of list) Free (used for node allocation) Pointer WorldNonpointer World

53 Initialization n-2 D N Free = n-1 means n-1 D N nil Free

54 Example of Use abc L null c a D N b n = 8 L = 3 Free = 6 InsertFront (L, x) if (Free ≠ -1) then q  Free else return “overflow”; Free  N[Free]; D[q]  x; N[q]  L; L  q;

55 Try DeleteFront  Define the cursor implementation of DeleteFront which removes the first member of the list when there is one.  Remember to add garbage to free list. DeleteFront (L) { ??? }

56 DeleteFront Solution DeleteFront (L) if L = -1 then return “underflow” else q  L; L  N[L]; N[q]  Free; Free  q; c a D N b n = 8 L = 3 Free = 6 abc L null n = 8 L = 5 Free = 4

57 Insert Running Time  List implementation by array  Worst case is insert at position 0. Must move all N items one position before the insert  On average, must move half the elements to make room – assuming insertions at positions are equally likely  Probably too slow  List implementation by Pointers:  Independent on the list length

58 Summary  ADT: An abstract mathematical definition of objects, with operations defined on them  Data Structures:  Representation of objects of an Abstract Data Type (ADT)  Algorithms manipulate the data structures to implement the operations of the ADT  Examples

59 Next Class  Application example of lists: Sparse Polynomial  Stacks  Queues

60 Course Overview  Introduction to many of the basic data structures used in computer software  Understand the data structures  How to implement them  Analyze the algorithms that use them  Know when to apply them  Practice design and analysis of data structures.  Practice using these data structures by writing programs.

61 Application example: Sparse Polynomials  x x x P record poly : ( exp : integer, coef : integer, next : poly pointer ) exp coef next