Introduction to Data Structures

Similar documents
Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

8. Fundamental Data Structures

811312A Data Structures and Algorithms, , Exercise 1 Solutions

if (Q.head = Q.tail + 1) or (Q.head = 1 and Q.tail = Q.length) then throw F ullqueueexception if (Q.head = Q.tail) then throw EmptyQueueException

Algorithms and Data Structures for Mathematicians

CS532: Design and Analysis of Algorithms. Overview. Linked Lists. Data Structures Intro. Classes, Objects, and Pointers.

Linked Structures. See Section 3.2 of the text.

CS2012 Programming Techniques II

1 P age DS & OOPS / UNIT II

THE UNIVERSITY OF WESTERN AUSTRALIA

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Week 4 Stacks & Queues

CS350 - Exam 1 (100 Points)

Linked Lists and Abstract Data Structures A brief comparison

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Algorithms. 演算法 Data Structures (focus on Trees)

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Linked List. April 2, 2007 Programming and Data Structure 1

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Linked lists (6.5, 16)

CMSC Introduction to Algorithms Spring 2012 Lecture 7

PA3 Design Specification

CS171 Midterm Exam. October 29, Name:

Elementary Data Structures and Hash Tables

Stack and Queue. Stack:

3. Fundamental Data Structures

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Information Science 2

.:: UNIT 4 ::. STACK AND QUEUE

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

CSC 1052 Algorithms & Data Structures II: Linked Queues

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

Data Structures and Algorithms

CS 171: Introduction to Computer Science II. Linked List. Li Xiong

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Linear Data Structures

Data Structure Advanced

Stacks Stack Examples:

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

LIFO : Last In First Out

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

16. Dynamic Data Structures

data structures and algorithms lecture 6

csci 210: Data Structures Stacks and Queues

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Week 6. Data structures

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

CMPT 125: Practice Midterm Answer Key

Dynamic Storage Allocation

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Basic Data Structures (Version 7) Name:

Elementary Data Structures and Hash Tables

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Lecture 4 Stack and Queue

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

CP2 Revision. theme: dynamic datatypes & data structures

Ashish Gupta, Data JUET, Guna

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Data Structures Question Bank Multiple Choice

Procedural Programming & Fundamentals of Programming

Motivation for Queues

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

Information Science 2

Abstract Data Types. Definitions, Implementations, and Uses

DS L9: Queues

Dynamic Data Structures

Lists and Arrays. Algorithms and Data Structures. (c) Marcin Sydow. Introduction. Linked Lists. Abstract Data Structure. Amortised Analysis

Lesson 3: Understanding General Software Development

CSE 230 Intermediate Programming in C and C++

Algorithms. Algorithms 1.3 STACKS AND QUEUES. stacks resizing arrays queues generics iterators applications ROBERT SEDGEWICK KEVIN WAYNE.

Abstract Data Types CHAPTER 12. Review Questions

Cpt S 122 Data Structures. Data Structures

CSCA48 Term Test 1 Seminar

Data structure and algorithm in Python

Linked List Implementation of Queues

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Standard ADTs. Lecture 19 CS2110 Summer 2009

infix expressions (review)

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

Lecture Data Structure Stack

DNHI Homework 3 Solutions List, Stacs and Queues

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Apply to be a Meiklejohn! tinyurl.com/meikapply

CSE 214 Computer Science II Stack

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

Interfaces & Generics

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Transcription:

Introduction to Data Structures Data structures Stacks and Queues Linked Lists Dynamic Arrays Philip Bille

Introduction to Data Structures Data structures Stacks and Queues Linked Lists Dynamic Arrays

Data Structures Data structure. Method for organizing data for efficient access, searching, manipulation, etc. Goal. Fast. Compact Terminology. Abstract vs. concrete data structure. Dynamic vs. static data structure.

Introduction to Data Structures Data structures Stacks and Queues Linked Lists Dynamic Arrays

Stack Stack. Maintain dynamic sequence (stack) S supporting the following operations: PUSH(x): add x to S. POP(): remove and return the most recently added element in S. ISEMPTY(): return true if S is empty. 28 3 3 3 7 4 4 4 4 4 18 18 18 18 16 PUSH(28) 16 POP() 16 POP() 16 PUSH(7) 18 16

Queue Queue. Maintain dynamic sequence (queue) Q supporting the following operations: ENQUEUE(x): add x to Q. DEQUEUE(): remove and return the first added element in Q. ISEMPTY(): return true if S is empty. 8 4 15 ENQUEUE(1) 8 4 15 1 ENQUEUE(22) 8 4 15 1 22 DEQUEUE() 4 15 1 22 DEQUEUE() 15 1 22 ENQUEUE(6) 15 1 22 6

Applications Stacks. Virtual machines Parsing Function calls Backtracking Queues. Scheduling processes Buffering Breadth-first searching

Stack Implementation Stack. Stack with capacity N Data structure. Array S[0..N-1] Index top. Initially top = -1 Operations. PUSH(x): Add x at S[top+1], top = top + 1 POP(): return S[top], top = top - 1 ISEMPTY(): return true if top = -1. Check for overflow and underflow in PUSH and POP. N = 10 16 18 4 3 28 top

Stack Implementation N = 10 16 18 4 3 28 top Time PUSH in Θ(1) time. POP in Θ(1) time. ISEMPTY in Θ(1) time. Space. Θ(N) space. Limitations. Capacity must be known. Wasting space.

Queue Implementation Queue. Queue with capacity N. Data structure. Array Q[0..N-1] Indices head and tail and a counter. Operations. ENQUEUE(x): add x at S[tail], update count og tail cyclically. DEQUEUE(): return Q[head], update count og head cyclically. ISEMPTY(): return true if count = 0. Check for overflow and underflow in DEQUEUE and ENQUEUE. count = 5 N = 10 8 4 15 1 22 head tail

Queue Implementation count = 5 N = 10 8 4 15 1 22 head tail Time. ENQUEUE in Θ(1) time. DEQUEUE in Θ(1) time. ISEMPTY in Θ(1) time. Space. Θ(N) space. Limitations. Capacity must be known. Wasting space.

Stacks and Queues Stack. Time. PUSH, POP, ISEMPTY in Θ(1) time. Space. Θ(N) Queue. Time. ENQUEUE, Dequeue, ISEMPTY in Θ(1) time. Space. Θ(N) 16 18 4 3 28 8 4 15 1 22 head top count = 5 tail N = 10 N = 10 Challenge. Can we get linear space and constant time?

Introduction to Data Structures Data structures Stacks and Queues Linked Lists Dynamic Arrays

Linked Lists Linked lists. Data structure to maintain dynamic sequence of elements in linear space. Sequence order determined by pointers/references called links. Fast insertion and deletion of elements and contiguous sublists. Singly-linked vs doubly-linked. head 7 42 18 23 5 null head null 7 42 18 23 5 null

Linked Lists Doubly-linked lists in Java. class Node { int key; Node next; Node prev; } Node head = new Node(); Node b = new Node(); Node c = new Node(); head.key = 7; b.key = 42; c.key = 18; head.prev = null; head.next = b; b.prev = head; b.next = c; c.prev = b; c.next = null; prev key next head b c null 7 null null 42 null null 18 null head b c null 7 42 18 null

Linked Lists Simple operations. SEARCH(head, k): return node with key k. Return null if it does not exist. INSERT(head, x): insert node x in front of list. Return new head. DELETE(head, x): delete node x in list. head 7 42 18 23 5 null head null 7 42 18 23 5 null

Linked Lists Operations in Java. head Node Search(Node head, int value) { Node x = head; while (x!= null) { if (x.key == value) return x; x = x.next; } return null; } Node Insert(Node head, Node x) { x.prev = null; x.next = head; head.prev = x; return x; } Node Delete(Node head, Node x) { if (x.prev!= null) x.prev.next = x.next; else head = x.next; if (x.next!= null) x.next.prev = x.prev; return head; } null 7 42 18 23 5 null Ex. Let p be a new with key 10 and let q be node with key 23 in list. Trace execution of Search(head,18), Insert(head,p) og Delete(head,q).

Linked Lists head 7 42 18 23 5 null head null 7 42 18 23 5 null Time. SEARCH in Θ(n) time. INSERT and DELETE in Θ(1) time. Space. Θ(n)

Stack and Queue Implementation Ex. Consider how to implement stack and queue with linked lists efficiently. Stack. Maintain dynamic sequence (stack) S supporting the following operations: PUSH(x): add x to S. POP(): remove and return the most recently added element in S. ISEMPTY(): return true if S is empty. Queue. Maintain dynamic sequence (queue) Q supporting the following operations: ENQUEUE(x): add x to Q. DEQUEUE(): remove and return the first added element in Q. ISEMPTY(): return true if S is empty.

Stack and Queue Implementation Stacks and queues using linked lists Stack. Time. PUSH, POP, ISEMPTY in Θ(1) time. Space. Θ(n) Queue. Time. ENQUEUE, Dequeue, ISEMPTY in Θ(1) time. Space. Θ(n)

Linked Lists Linked list. Flexible data structure to maintiain sequence of elements. Other linked data structures: cyclic lists, trees, graphs, root null 15 8 20 15 null null 8 20 1 14 1 14 null null 3 11 13 3 null null null 11 13 null null

Introduction to Data Structures Data structures Stacks and Queues Linked Lists Dynamic Arrays

Stack Implementation with Array Challenge. Can we implement a stack efficiently with arrays? Do we need a fixed capacity? Can we get linear space and constant time?

Dynamic Arrays Goal. Implement a stack using arrays in Θ(n) space for n elements. As fast as possible. Focus on PUSH. Ignore POP and ISEMPTY for now. Solution 1 Start with array of size 1. PUSH(x): Allocate new array of size + 1. Move all elements to new array. Delete old array.

Dynamic Arrays PUSH(x): Allocate new array of size + 1. Move all elements to new array. Delete old array. Time. Time for n PUSH operations? ith PUSH takes Θ(i) tid. total time is 1 + 2 + 3 + 4 + + n = Θ(n 2 ) Space. Θ(n) Challenge. Can we do better?

Dynamic Arrays Idea. Only copy elements some times Solution 2. Start with array of size 1. PUSH(x): If array is full: Allocate new array of twice the size. Move all elements to new array. Delete old array.

Dynamic Arrays PUSH(x): If array is full: Allocate new array of twice the size. Move all elements to new array. Delete old array. Time. Time for n PUSH operations? PUSH 2 k takes Θ(2 k ) time. All other PUSH operations take Θ(1) time. total time < 1 + 2 + 4 + 8 + 16 + + 2 log n + n = Θ(n) Space. Θ(n)

Dynamic Arrays Stack with dynamic array. n PUSH operations in Θ(n) time and plads. Extends to n PUSH, POP og ISEMPTY operations in Θ(n) time. Time is amortized Θ(1) per operation. With more clever tricks we can deamortize to get Θ(1) worst-case time per operation. Queue with dynamic array. Similar results as stack. Global rebuilding. Dynamic array is an example of global rebuilding. Technique to make static data structures dynamic.

Stack and Queues Data structure PUSH POP ISEMPTY Space Array with capacity N Θ(1) Θ(1) Θ(1) Θ(N) Linked List Θ(1) Θ(1) Θ(1) Θ(n) Dynamic Array 1 Θ(n) Θ(1) Θ(1) Θ(n) Dynamic Array 2 Θ(1) Θ(1) Θ(1) Θ(n) Dynamic Array 3 Θ(1) Θ(1) Θ(1) Θ(n) = amortized

Introduction to Data Structures Data structures Stacks and Queues Linked Lists Dynamic Arrays