15.082J/6.855J/ESD.78J September 14, 2010 Data Structures.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
15.082J, 6.855J, and ESD.78J Sept 16, 2010 Lecture 3. Graph Search Breadth First Search Depth First Search Intro to program verification Topological Sort.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees CS 110: Data Structures and Algorithms First Semester,
Advanced Data Structures
Data Structures Michael J. Watts
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
The Most Commonly-used Data Structures
Transforming Infix to Postfix
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Important Problem Types and Fundamental Data Structures
Binary Trees Chapter 6.
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
& 6.855J & ESD.78J Algorithm visualizations Modified Label Correcting Algorithm.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
15.082J & 6.855J & ESD.78J September 23, 2010 Dijkstra’s Algorithm for the Shortest Path Problem.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Graphs Upon completion you will be able to:
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
15.082J & 6.855J & ESD.78J September 30, 2010 The Label Correcting Algorithm.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
15.082J and 6.855J and ESD.78J Network Simplex Animations.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Chapter 12 Abstract Data Type.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Data Structure Interview Question and Answers
Heaps And Priority Queues
CS 1114: Implementing Search
Lecture 22 Binary Search Trees Chapter 10 of textbook
i206: Lecture 13: Recursion, continued Trees
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
structures and their relationships." - Linus Torvalds
Chapter 1.
Chapter 21: Binary Trees.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Lectures on Graph Algorithms: searching, testing and sorting
Graphs.
Lecture 12 CS203 1.
Network Optimization Depth First Search
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
Important Problem Types and Fundamental Data Structures
OPIM 915 Fall 2010 Data Structures 23-38,
structures and their relationships." - Linus Torvalds
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:

15.082J/6.855J/ESD.78J September 14, 2010 Data Structures

2 Overview of this Lecture u A very fast overview of some data structures that we will be using this semester lists, sets, stacks, queues, networks, trees a variation on the well known heap data structure binary search u Illustrated using animation u We are concerned with O( ) computation counts, and so do not need to get down to C ++ - level (or Java level).

3 Two standard data structures Array: a vector: stored consecutively in memory, and typically allocated in advance cells: hold fields of numbers and pointers to implement lists first This is a singly linked list

Representations of subsets of a set Array A subset S = {1, 3, 4, 6, 8} first List L The choice of data structure depends on what operations need to be carried out.

Example 1: Creating an empty set 5 0 first Initialize: subset S = ∅ n … O(n) steps O(1) steps

Example 2: Is x ∈ S? first To determine if 9 ∈ S?, one needs to scan the entire list. Is 9 ∈ S? O(1) steps O(n) steps

7 Representing a linked list as an array 68 ∅ first Array: Next If Next(j) is empty, then j is not on the list If Next(j) = ∅, then j is the last element on the list

8 Two key concepts Abstract data types: a descriptor of the operations that are permitted, e.g., abstract data type: set S initialize(S): creates an empty set S add(S, s): replaces S by S ∪ {s}. delete(S, s): replaces S by S \{s} IsElement(S, s): returns true if s ∈ S etc. Data structure: usually describes the high level implementation of the abstract data types, and can be analyzed for running time. doubly linked list, etc

9 A note on data structures u Preferences Simplicity Efficiency In case there are multiple good representations, we will choose one

10 Abstract data type: SetOperations Operation (assume S ⊆ {1, 2, 3, … n}. initialize(S): S := ∅ add(S, j): S := S ∪ {j} delete(S, j): S := S\{j} IsElement(S, j): returns TRUE if s ∈ S FindElement(S): if S ≠ ∅, returns j for some j ∈ S Next(S, j) : if j ∈ S, it finds the next element after j on S (viewed as a list)

Implementation using doubly linked list first ∅ 8 4 ∅ Next( ) Prev First = 8

Add element 5 to the set (in first position) 12 42first24 82 ∅ 8 4 ∅ Next( ) Prev( ) Temp:= First Temp First 8 Elt 5 Prev(Temp): = Elt First:= Elt Prev(Elt): = ∅ Next(Elt): = Temp 5 5

Add element 5 to the set (in first position) 13 42first24 82 ∅ 8 4 ∅ Next( ) Prev( ) Temp:= First Temp First 8 8 Elt 5 Prev(Temp): = Elt First:= Elt Prev(Elt): = ∅ Next(Elt): = Temp 5 8 ∅ 5 5 Adding an element to a set takes O(1) steps using this implementation.

Delete element 2 from the set (assume it is neither in first or last position) 14 42first24 82 ∅ 8 4 ∅ Next( ) Prev( ) Temp First 8 Elt ∅ 5 5

Delete element 2 from the set (assume it is neither in first or last position) 15 2first4 82 ∅ ∅ Next( ) Prev( ) First 8 Elt 2 Prev(Elt) := 0 Next(Prev(Elt)):=Next(Elt) Next(Elt): = ∅ 5 5 Deleting an element from the set takes O(1) steps using this implementation. 4 Prev(Next(Elt)):=Prev(Elt) 8

16 Operations using doubly linked lists Operation Number of steps initialize(S): O(n) add(S, j): O(1) delete(S, j): O(1) IsElement(S, j): O(1) FindElement(S): O(1) Next(S, j): O(1) Previous(S, j) :O(1)

Maintaining disjoint subsets of elements 17 42First(1)248 S 1 = {8, 2, 4} S 1 = {5, 7, 1} 42First(2) ∅ Prev( ) ∅∅ 28 4 ∅ Next( ) 724 ∅ 85 First( )

Maintaining ordered lists The doubly linked list is not efficient for maintaining ordered lists of nodes first482 4 ∅ Next( ) 82 ∅ Prev( ) 5 Inserting an element into the set (such as 7) requires finding Prev and Next for 7 (4 and 8), and this requires O(n) time with this implementation.

Mental Break 19 Who created the cartoon characters "The Simpson's? Matt Groening Which country would you be in if you were to ski in the Dolomites? Italy Who was the first U.S. president to adopt the informal version of his first name? Jimmy Carter

Mental Break 20 Which country owns the island of Bermuda? Great Britain What colors do most color blind persons have trouble distinguishing? red and green What bird is used as the sign of peace? the dove

Complete binary tree with n elements Complete binary trees for storing ordered lists of nodes or arcs. We assume that the number of nodes is n (e.g., 8) and the number of arcs is m. 21 S = {2, 4, 8} n =

Complete binary tree with n elements Build up the binary tree. In each parent store the least value of its children S = {2, 4, 8} n =

Complete binary tree with n elements Build up the binary tree. In each parent store the least value of its children S = {2, 4, 8} n =

Complete binary tree with n elements Build up the binary tree. In each parent store the least value of its children S = {2, 4, 8} n =

Find greatest element less than j e.g., find the greatest element less than O(log n) steps for finding greatest element less than j. start at 7 go up the tree until a node has label < 7. Take left branch. Choose the largest label child going down.

Delete an element e.g., delete element S = {4, 5, 8} n = O(log n) steps for an deletion 4 4 Start at node 2 and update it and its ancestors.

Operations using complete binary trees 27 Operation Number of steps initialize(S): O(n) add(S, j): O(log n) delete(S, j): O(log n) IsElement(S, j): O(1) FindElement(S): O(1) Next(S, j): O(log n) Previous(S, j) :O(log n) MinElement(S) O(1) MaxElement(S)O(log n)

28 A network We can view the arcs of a networks as a collection of sets. Let A(i) be the arcs emanating from node i. e.g., A(5) = { (5,3), (5,4) } Note: these sets are usually static. They stay the same. Common operations: scanning the list A(i) one arc at a time starting at the first arc.

29 Storing Arc Lists: A(i) Operations permitted Find first arc in A(i) Store a pointer to the current arc in A(i) Find the arc after CurrentArc(i) i jc ij u ij

30 Scanning the arc list CurrentArc(i) is a pointer to the arc of A(i) that is being scanned or is the next to be scanned CurrentArc(1) Initially, CurrentArc(i) is the first arc of A(i) After CurrentArc(i) is fully scanned, CurrentArc(i) := Next(CurrentArc(i))

31 Scanning the arc list CurrentArc(i) is a pointer to the arc of A(i) that is being scanned or is the next to be scanned CurrentArc(1) Finding CurrentArc and the arc after CurrentArc takes O(1) steps. These are also implemented often using arrays called forward star representations.

The Adjacency Matrix (for directed graphs) a b c d e A Directed Graph Have a row for each node Have a column for each node Put a 1 in row i- column j if (i,j) is an arc What would happen if (4,2) became (2,4)? 32

The Adjacency Matrix (for undirected graphs) Have a row for each node Have a column for each node Put a 1 in row i- column j if (i,j) is an arc a b c d e An Undirected Graph The degree of a node is the number of incident arcs degree

34 Adjacency Matrix vs Arc Lists Adjacency Matrix? Efficient storage if matrix is very “dense.” Can determine if (i,j) ∈ A(i) in O(1) steps. Scans arcs in A(i) in O(n) steps. Adjacency lists? Efficient storage if matrix is “sparse.” Determining if (i,j) ∈ A(i) can take |A(i)| steps Can scan all arcs in A(i) in |A(i)| steps

35 Trees A tree is a connected acyclic graph. (Acyclic here, means it has no undirected cycles.) If a tree has n nodes, it has n-1 arcs This is an undirected tree. To store trees efficiently, we hang the tree from a root node. (In principle, any node can be selected for the root.) 3

Forest A forest is an acyclic graph that includes all of the nodes. A subtree of a forest is a connected component of the forest. To store trees efficiently, each subtree has a root node

37 One way of storing subtrees Lists of children parent (predecessor) array node parent

On storing trees Trees are important parts of flow algorithms Some data structures are expressed as trees The best implementation of trees depends on what operations need to be performed in the abstract data type

39 Stacks -- Last In, First Out (LIFO) Operations: u create(S)creates an empty stack S u push(S, j)adds j to the top of the stack u pop(S)deletes the top element in S u top(S)returns the top element in S pop(S) push(S,9)

40 Queues – First in, First Out (FIFO) Operations: u create(Q)creates an empty queue Q u Insert(Q, j)adds j to the end of the queue u Delete(Q)deletes the first element in Q u first(Q)returns the top element in S Delete(Q) Insert(Q,9) 372 9

41 Binary Search In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list. LeftRight

42 Binary Search In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list. LeftRightLeft

43 Binary Search In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list. RightLeftRight After two more iterations, we will determine that 25 is not on the list, but that 24 and 27 are on the list. Running time is O( log n) for running binary search.

44 Summary Review of data structures Lists, sets, complete binary trees, trees queues, stacks binary search Next Lecture: Search algorithms

MITOpenCourseWare J / 6.855J / ESD.78J Network Optimization Fall 2010 For information about citing these materials or our Terms of Use, visit: