OPIM 915 Fall 2010 Data Structures 23-38,

Slides:



Advertisements
Similar presentations
Advanced Data Structures
Advertisements

Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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.
15.082J/6.855J/ESD.78J September 14, 2010 Data Structures.
Important Problem Types and Fundamental Data Structures
Binary Trees Chapter 6.
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.
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.
15.082J & 6.855J & ESD.78J September 23, 2010 Dijkstra’s Algorithm for the Shortest Path Problem.
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.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
School of Computing Clemson University Fall, 2012
Data Structure By Amee Trivedi.
Data Structures Michael J. Watts
Chapter 11 Heap.
Top 50 Data Structures Interview Questions
Heaps And Priority Queues
Priority Queues and Heaps
Network Simplex Animations
Programming Abstractions
Hashing Exercises.
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
i206: Lecture 13: Recursion, continued 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.
Dijkstra’s Algorithm with two levels of buckets
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 8 – Binary Search Tree
structures and their relationships." - Linus Torvalds
Part-D1 Priority Queues
Heapsort Heap & Priority Queue.
Priority Queues.
i206: Lecture 14: Heaps, Graphs intro.
Graphs Representation, BFS, DFS
CMSC 341 Lecture 14 Priority Queues & Heaps
Priority Queues.
ITEC 2620M Introduction to Data Structures
15.082J & 6.855J & ESD.78J Visualizations
Lecture 6 Shortest Path Problem.
Dijkstra’s Algorithm for the Shortest Path Problem
Ch. 8 Priority Queues And Heaps
Breadth first search animation
Lecture 4 Graph Search.
Spanning Tree Algorithms
and 6.855J Flow Decomposition
Priority Queues & Heaps
15.082J & 6.855J & ESD.78J Radix Heap Animation
Network Optimization Depth First Search
Introduction to Algorithms
and 6.855J Dijkstra’s Algorithm
Network Simplex Animations
and 6.855J Topological Ordering
Priority Queues & Heaps
The Minimum Cost Spanning Tree Problem
and 6.855J Depth First Search
Important Problem Types and Fundamental Data Structures
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Breadth first search animation
Heaps & Multi-way Search Trees
EMIS 8374 Search Algorithms Updated 12 February 2008
structures and their relationships." - Linus Torvalds
Heaps Chapter 6 Section 6.9.
Class 11 Max Flows Obtain a network, and use the same network to illustrate the shortest path problem for communication networks, the max flow.
Presentation transcript:

OPIM 915 Fall 2010 Data Structures 23-38, 765-773 Obtain a network, and use the same network to illustrate the shortest path problem for communication networks, the max flow problem, the minimum cost flow problem, and the multicommodity flow problem. This will be a very efficient way of introducing the four problems. (Perhaps under 10 minutes of class time.)

Overview of this Lecture 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 Illustrated using animation We are concerned with O( ) computation counts, and so do not need to get down to C- level (or Java level).

Two standard data structures 1 2 3 4 5 6 7 8 9 10 Array: a vector: stored consecutively in memory, and typically allocated in advance 1 3 4 6 8 first This is a singly linked list cells: hold fields of numbers and pointers to implement lists.

Two standard data structures 1 2 3 4 5 6 7 8 9 10 Each has its advantages. Each can be more efficient than the other. It depends on what operations you want to perform. 1 3 4 6 8 first Let’s consider which data structure to use for working with node subsets, or arc subsets.

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} FindElement(S, s): s := some element of S IsElement(S, s): returns true if s  S Data structure: usually describes the high level implementation of the abstract data types, and can be analyzed for running time. doubly linked list, etc

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

Storing a subset S of Nodes Operations create an empty set S S =  S = {2, 4, 7} S = {2} S = {2, 7} add an element to S delete an element from S S = {2, 4} determine if a node is in S Is 7  S? Array Bucket Bucket(i) points to i if i  S use a doubly linked list to store the set S

Animating the List of Nodes 1 2 3 4 5 6 7 8 9 10 Bucket 2 4 first This is a doubly linked list Operation: insert node 8 into the list

Operation: Insert node 8 into list 1 2 3 4 5 6 7 8 9 10 Bucket 2 4 first 4 2 8 Insert node 8 as the first node on the list. Update pointers on the doubly linked list. Update the pointer in the array. The entire insertion takes O(1) steps.

Operation: Delete node 2 from the list 1 2 3 4 5 6 7 8 9 10 Bucket 2 4 first 4 2 8 Delete the cells containing 2 plus the pointers Update the pointers on the doubly linked list. Update the pointer on the array The entire deletion takes O(1) steps.

Summary on maintaining a subset Operations Determine if j  S Add j to S Delete j from S Each operation takes O(1) steps using a doubly linked list and an array. Can be used to maintain a subset of arcs. Can be used to maintain multiple subsets of nodes Set1, Set2, Set3, …, Setk Use an index to keep track of the set for each node

A network 1 2 3 4 5 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 static. They stay the same. Common operations: find the first arc in A(i) find the arc CurrentArc(i) on A(i)

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 j cij uij 1 2 25 30 5 15 40 3 23 35 2 4 15 40 3 2 45 10 5 60 4 5 3 25 20 4 35 50

Current Arc CurrentArc(i) is a placeholder for when one scans through the arc list A(i) CurrentArc(1) CurrentArc(1) CurrentArc(1) 1 2 25 30 5 15 40 3 23 35 1 2 3 5 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) 1 2 3 4 2 3 4 1 a b c d e A Directed Graph 1 2 3 4 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)? 15

The Adjacency Matrix (for undirected graphs) 2 3 4 1 a b c d e An Undirected Graph 1 2 3 4 degree 1 2 3 4 2 3 2 3 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 The degree of a node is the number of incident arcs 16

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

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. 1 2 3 6 5 4 3 This is an undirected tree. To store trees efficiently, we hang the tree from a root node.

Trees Lists of children 1 2 3 6 5 4 2 3 5 4 3 1 6 2 4 5 node pred 6 2 4 5 node pred predecessor (parent) array

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

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

Priority Queues Each element j has a value key(j) create(P, S) initializes P with elements of S Insert(P, j) adds j to P Delete(P, j) deletes j from P FindMin(P, k) k: index with minimum key value ChangeKey(j, D) key(j):= key(j) + D. A common data structure, the first one that cannot be implemented as a doubly linked list. Goal: always locate the minimum key, the index with the highest priority. (Think deli counter.)

Implementing Priority Queues using Leaf-Heaps Note: if there are n leaves, the running time to create P is O(n) and the height is O(log n). Parent node stores the smaller key of its children. 2 2 4 6 2 4 7 6 9 2 5 8 4 7 1 3

Find Min using Leaf-Heaps Start at the top and sift down. Time = O(log n). 2 2 2 2 4 6 2 2 4 7 6 9 2 2 5 8 4 7 1 2 3 4 5 6 7

Deleting node 3 using Leaf-Heaps Start at the bottom and sift up. Time is O(log n). 2 4 2 5 4 6 5 2 4 7 6 9 2 5 8 4 7 1 2 3 4 5 6 7

Insert node 8 using Leaf-Heaps Insert in the last place, and sift up. 4 2 Running time = O(log n) 5 2 4 6 5 2 4 6 7 6 9 2 5 8 4 7 6 8 1 2 3 4 5 6 7

Summary of Heaps create(P, S) O( n ) time; |S| = O(n) Insert(P, j) O( log n ) time Delete(P, j) O( log n ) time FindMin(P, k) O( log n ) time ChangeKey(j, D) O( log n) time Can be used to sort in O(log n) by repeatedly finding and deleting the minimum. The standard data structure to implement heaps is binary heaps, which is very similar, and more efficient in practice.

Sorting using Priority Queues 6 9 2 5 8 4 7 6 9 2 5 8 4 7 2 4 5 6 7 8 9 Each find min and delete min take O(log n) steps. So the running time for sorting is O(n log n). There are many other comparison based sorting methods that take O(n log n).

Binary Search In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list. 2 4 6 9 13 17 22 24 27 31 33 36 42 45 1 3 5 7 8 10 11 12 14 Left Right

Binary Search In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list. 2 4 6 9 13 17 22 24 27 31 33 36 42 45 1 3 5 7 8 10 11 12 14 Left Left Right

Binary Search In the ordered list of numbers below, stored in an array, determine whether the number 25 is on the list. 7 8 9 10 11 12 13 14 22 24 27 31 33 36 42 45 Left Right Right 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.

Summary Review of data structures Lists, stacks, queues, sets priority queues binary search