Data Structures – Stacks and Queus

Slides:



Advertisements
Similar presentations
Graph algorithms Prof. Noah Snavely CS1114
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Clustering and greedy algorithms — Part 2 Prof. Noah Snavely CS1114
Linked lists Prof. Noah Snavely CS1114
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 1114: Graphs and Blobs Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Blobs and Graphs Prof. Noah Snavely CS1114
Connected components and graph traversal Prof. Noah Snavely CS1114
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Breadth-first and depth-first traversal CS1114
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Data Structures and Algorithms Lists, Stacks, Queues, and Graphs Sorting and searching algorithms.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Blobs and Graphs Prof. Noah Snavely CS1114
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Implementing stacks Prof. Ramin Zabih
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
COSC160: Data Structures: Lists and Queues
Top 50 Data Structures Interview Questions
Data Structures Graphs - Terminology
Graphs Representation, BFS, DFS
Chapter 15 Lists Objectives
CS 1114: Implementing Search
CSC317 Graph algorithms Why bother?
Csc 2720 Instructor: Zhuojun Duan
Stacks and Queues.
Introduction to Data Structure
Chapter 17 Object-Oriented Data Structures
CC 215 Data Structures Graph Searching
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
CSE 421: Introduction to Algorithms
Stacks, Queues, and Deques
Lesson Objectives Aims
ITEC 2620M Introduction to Data Structures
Lesson 6. Types Equality and Identity. Collections.
Elementary Graph Algorithms
Graphs.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CSE 373 Data Structures Lecture 16
Richard Anderson Autumn 2016 Lecture 5
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Stacks and Queues CSE 373 Data Structures.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Linked lists Prof. Noah Snavely CS1114
Richard Anderson Winter 2019 Lecture 6
Analysis and design of algorithm
Richard Anderson Winter 2019 Lecture 5
structures and their relationships." - Linus Torvalds
Prof. Ramin Zabih Graph Traversal Prof. Ramin Zabih
Richard Anderson Autumn 2015 Lecture 6
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Data Structures – Stacks and Queus Prof. Noah Snavely CS1114 http://www.cs.cornell.edu/courses/cs1114

Administrivia Assignment 2, Part 2, due tomorrow Please sign up for a Friday slot Assignment 3 will be out Friday Prelim 1! Next Thursday, 3/1, in class There will be a review session Wednesday evening, 7pm, Upson 315

Finding blobs 1

Blobs are connected components! Finding blobs 1 Blobs are connected components!

Finding components Pick a 1 to start with, where you don’t know which component it is in When there aren’t any, you’re done Give it a new component color Assign the same component color to each pixel that is part of the same component Basic strategy: color any neighboring 1’s, have them color their neighbors, and so on

From Section For each vertex we visit, we color its neighbors and remember that we need to visit them at some point Need to keep track of the vertices we still need to visit in a todo list After we visit a vertex, we’ll pick one of the vertices in the todo list to visit next This is also called graph traversal

Stacks and queues Two ways of representing a “todo list” Stack: Last In First Out (LIFO) (Think cafeteria trays) The newest task is the one you’ll do next Queue: First In First Out (FIFO) (Think a line of people at the cafeteria) The oldest task is the one you’ll do next

Stacks Two operations: Push: add something to the top of the stack Pop: remove the thing on top of the stack

Queue Two operations: Enqueue: add something to the end of the queue Dequeue: remove something from the front of the queue

Graph traversal London Frankfurt Paris Hamburg Vienna Prague Berlin Oslo Stockholm Frankfurt Paris Hamburg Berlin Vienna Rome Prague Naples Warsaw

Depth-first search (DFS) 2 5 1 3 10 6 8 4 7 9 Call the starting node the root We traverse paths all the way until we get to a dead-end, then backtrack (until we find an unexplored path) Corresponds to using a stack

Another strategy Explore all the cities that are one hop away from the root Explore all cities that are two hops away from the root Explore all cities that are three hops away from the root … This corresponds to using a queue

Breadth-first search (BFS) 2 4 1 5 3 7 9 8 10 6 We visit all the vertices at the same level (same distance to the root) before moving on to the next level

Breadth-first (queue) BFS vs. DFS 2 4 1 5 3 7 9 8 10 6 2 5 1 3 10 6 8 4 7 9 Breadth-first (queue) Depth-first (stack)

Basic algorithms BREADTH-FIRST SEARCH (Graph G) While there is an uncolored node r Choose a new color Create an empty queue Q Let r be the root node, color it, and add it to Q While Q is not empty Dequeue a node v from Q For each of v’s neighbors u If u is not colored, color it and add it to Q

Basic algorithms DEPTH-FIRST SEARCH (Graph G) While there is an uncolored node r Choose a new color Create an empty stack S Let r be the root node, color it, and push it on S While S is not empty Pop a node v from S For each of v’s neighbors u If u is not colored, color it and push it onto S

Queues and Stacks Examples of Abstract Data Types (ADTs) ADTs fulfill a contract: The contract tells you what the ADT can do, and what the behavior is For instance, with a stack: We can push and pop If we push X onto S and then pop S, we get back X, and S is as before Doesn’t tell you how it fulfills the contract

Implementing DFS How can we implement a stack? Needs to support several operations: Push (add an element to the top) Pop (remove the element from the top) IsEmpty 256 42 17

Implementing a stack IsEmpty Push (add an element to the top) Pop (remove an element from the top)

Implementing BFS How can we implement a queue? Needs to support several operations: Enqueue (add an element to back) Dequeue (remove an element from front) IsEmpty Not quite as easy as a stack… 256 42 17

Efficiency Ideally, all of the operations (push, pop, enqueue, dequeue, IsEmpty) run in constant (O(1)) time To figure out running time, we need a model of how the computer’s memory works

Computers and arrays Computer memory is a large array We will call it M In constant time, a computer can: Read any element of M (random access) Change any element of M to another element Perform any simple arithmetic operation This is more or less what the hardware manual for an x86 describes

… Computers and arrays M Arrays in Matlab are consecutive subsequences of M … M A = zeros(8)

Memory manipulation How long does it take to: Read A(8)? Set A(7) = A(8)? Copy all the elements of an array (of size n) A to a new part of M? Shift all the elements of A one cell to the left?

Implementing a queue: Take 1 First approach: use an array Add (enqueue) new elements to the end of the array When removing an element (dequeue), shift the entire array left one unit Q = [];

Implementing a queue: Take 1 IsEmpty Enqueue (add an element) Dequeue (remove an element)

What is the running time? IsEmpty Enqueue (add an element) Dequeue (remove an element)

Implementing a queue: Take 2 Second approach: use an array AND Keep two pointers for the front and back of the queue Add new elements to the back of the array Take old elements off the front of the array front back Q = zeros(1000000); front = 1; back = 1;

Implementing a queue: Take 2 IsEmpty Enqueue (add an element) Dequeue (remove an element)

What is the running time? IsEmpty Enqueue (add an element) Dequeue (remove an element)

Implementing a queue: Take 2 What problems can occur?

Questions?

Linked lists Alternative to an array Every element (cell) has two parts: A value (as in an array) A link to the next cell

Linked lists Values 8 4 1 3 Links

Linked lists as memory arrays … M We’ll implement linked lists using M A cell will be represented by a pair of adjacent array entries

A few details I will draw odd numbered entries in blue and even ones in red Odd entries are values Number interpreted as list elements Even ones are links Number interpreted as index of the next cell AKA location, address, or pointer The first cell is M(1) and M(2) (for now) The last cell has 0, i.e. pointer to M(0) Also called a “null pointer”

Example 8 4 1 3 8 3 4 1 2 5 7 6 X 9 8 5 1 2 3 7 4 6 X 9

Traversing a linked list Start at the first cell, [M(1),M(2)] Access the first value, M(1) The next cell is at location c = M(2) If c = 0, we’re done Otherwise, access the next value, M(c) The next cell is at location c = M(c+1) Keep going until c = 0

Inserting an element – arrays How can we insert an element x into an array A? Depends where it needs to go: End of the array: A = [A x]; Middle of the array (say, between elements A(5) and A(6))? Beginning of the array?

Inserting an element – linked lists Create a new cell and splice it into the list Splicing depends on where the cell goes: How do we insert: At the end? In the middle? At the beginning? 8 4 1 3 M(1) 5

Adding a header We can represent the linked list just by the initial cell, but this is problematic Problem with inserting at the beginning Instead, we add a header – a few entries that are not cells, but hold information about the list A pointer to the first element A count of the number of elements

Questions?