Objectives Data structure: Heaps Data structure: Graphs Submit - - PDF document

objectives
SMART_READER_LITE
LIVE PREVIEW

Objectives Data structure: Heaps Data structure: Graphs Submit - - PDF document

1/25/19 Objectives Data structure: Heaps Data structure: Graphs Submit problem set 2 Jan 25, 2019 CSCI211 - Sprenkle 1 Review What is a priority queue? What is a heap? Properties Implementation What is the process for


slide-1
SLIDE 1

1/25/19 1

Objectives

  • Data structure: Heaps
  • Data structure: Graphs

Jan 25, 2019 1 CSCI211 - Sprenkle

Submit problem set 2

Review

  • What is a priority queue?
  • What is a heap?

Ø Properties Ø Implementation

  • What is the process for finding the smallest

element in a heap?

  • What is the process for adding to a heap?

Ø What is the runtime of adding to a heap?

Jan 25, 2019 CSCI211 - Sprenkle 2

slide-2
SLIDE 2

1/25/19 2

Review: Heap Defined

  • Combines benefits of sorted array and list
  • Balanced binary tree

Jan 25, 2019 3

root

  • Each node has at most 2 children
  • Node value is its key

Heap order: each node’s key is at least as large as its parent’s Note: not a binary search tree

CSCI211 - Sprenkle

Review: Implementing a Heap

  • Option 1: Use pointers

Ø Each node keeps

  • Element it stores (key)
  • 3 pointers: 2 children, parent
  • Option 2: No pointers

Ø Requires knowing upper bound on n Ø For node at position i

  • left child is at 2i
  • right child is at 2i+1

Jan 25, 2019 4 CSCI211 - Sprenkle

slide-3
SLIDE 3

1/25/19 3

Review: Implementing a Heap

  • Finding the minimal element

Ø First element Ø O(1)

Jan 25, 2019 5 CSCI211 - Sprenkle

Review: Heapify-Up

Jan 25, 2019 6

Heapify-up(H, i): if i > 1 then j=parent(i)=floor(i/2) if key[H[i]] < key[H[j]] then swap array entries H[i] and H[j] Heapify-up(H, j)

Heap Position where node added

CSCI211 - Sprenkle

slide-4
SLIDE 4

1/25/19 4

Heapify-Up

  • Claim. Assuming array H is almost a heap with

key of H[i] too small, Heapify-Up fixes the heap property in O(log i) time

Ø Can insert a new element in a heap of n elements in O(log n) time

  • Proof. By induction

Ø If i=1 …

Jan 25, 2019 7 CSCI211 - Sprenkle

Heapify-Up

  • Claim. Assuming array H is almost a heap with

key of H[i] too small, Heapify-Up fixes the heap property in O(log i) time

Ø Can insert a new element in a heap of n elements in O(log n) time

  • Proof. By induction

Ø If i=1, is already a heap à O(1) Ø If i>1, …

Jan 25, 2019 8 CSCI211 - Sprenkle

slide-5
SLIDE 5

1/25/19 5

Heapify-Up

  • Claim. Assuming array H is almost a heap with

key of H[i] too small, Heapify-Up fixes the heap property in O(log i) time

Ø Can insert a new element in a heap of n elements in O(log n) time

  • Proof. By induction

Ø If i=1, is already a heap à O(1) Ø If i>1,

  • Swaps are O(1)
  • Swaps continue up to root (max) à log i

Jan 25, 2019 9 CSCI211 - Sprenkle

Deleting an Element

Jan 25, 2019 CSCI211 - Sprenkle 10

Delete at position 3 w

slide-6
SLIDE 6

1/25/19 6

Deleting an Element

  • Delete at position i
  • Removing an element:

Ø Messes up heap order Ø Leaves a “hole” in the heap

  • Not as straightforward as Heapify-Up
  • Algorithm:
  • 1. Fill in element where hole was
  • Patch hole: move nth element into ith spot
  • 2. Adjust heap to be in order
  • At position i because moved nth item up to i

Jan 25, 2019 11 CSCI211 - Sprenkle

Deleting an Element

  • Two “bad” possibilities: element w is

Ø Too small: violation is between it and parent à Heapify-Up Ø Too big: with one or both children à Heapify-Down (example: w becomes 12)

Jan 25, 2019 12 CSCI211 - Sprenkle

Delete at position 3 w

Example of OK: 11 deleted, replaced by 16

slide-7
SLIDE 7

1/25/19 7

Deleting an Element

  • Delete 9
  • Replace with 5 (from other side of heap)
  • But 5 < 6, so need to Heapify-Up

Jan 25, 2019 13

Example where new key is too small

3 4 7 5 6 2 10

CSCI211 - Sprenkle

Heapify-Down

Jan 25, 2019 14

Heapify-down(H, i): n = length(H) if 2i > n then Terminate with H unchanged else if 2i < n then left=2i and right=2i+1 j be index that minimizes key[H[left]] and key[[H[right]] else if 2i = n then j=2i if key[H[j]] < key[H[i]] then swap array entries H[i] and H[j] Heapify-down(H, j)

CSCI211 - Sprenkle

Why can we stop?

slide-8
SLIDE 8

1/25/19 8

Heapify-Down

Jan 25, 2019 15

Heapify-down(H, i): n = length(H) if 2i > n then Terminate with H unchanged else if 2i < n then left=2i and right=2i+1 j be index that minimizes key[H[left]] and key[[H[right]] else if 2i = n then j=2i if key[H[j]] < key[H[i]] then swap array entries H[i] and H[j] Heapify-down(H, j)

CSCI211 - Sprenkle

i is a leaf – nowhere to go

Practice: Heapify-Down

Jan 25, 2019 16

Moved 21 to where element was removed

21

CSCI211 - Sprenkle

slide-9
SLIDE 9

1/25/19 9

Practice: Heapify-Down

Jan 25, 2019 17

21 21 7

CSCI211 - Sprenkle

Practice: Heapify-Down

Jan 25, 2019 18

21 7 8 7 21

CSCI211 - Sprenkle

slide-10
SLIDE 10

1/25/19 10

Runtime of Heapify-Down?

Jan 25, 2019 19

Heapify-down(H, i): n = length(H) if 2i > n then Terminate with H unchanged else if 2i < n then left=2i and right=2i+1 j be index that minimizes key[H[left]] and key[[H[right]] else if 2i = n then j=2i if key[H[j]] < key[H[i]] then swap array entries H[i] and H[j] Heapify-down(H, j)

CSCI211 - Sprenkle

O(1) O(1) Num swaps: O(log n)

Implementing Priority Queues with Heaps

Jan 25, 2019 20

Operation Description Run Time StartHeap(N)

Creates an empty heap that can hold N elements

Insert(v)

Inserts item v into heap

FindMin()

Identifies minimum element in heap but does not remove it

Delete(i)

Deletes element in heap at position i

ExtractMin()

Identifies and deletes an element with minimum key from heap

CSCI211 - Sprenkle

slide-11
SLIDE 11

1/25/19 11

Implementing Priority Queues with Heaps

Jan 25, 2019 21

Operation Description Run Time StartHeap(N)

Creates an empty heap that can hold N elements

O(N) Insert(v)

Inserts item v into heap

O(log n) FindMin()

Identifies minimum element in heap but does not remove it

O(1) Delete(i)

Deletes element in heap at position i

O(log n) ExtractMin()

Identifies and deletes an element with minimum key from heap

O(log n)

CSCI211 - Sprenkle

Comparing Data Structures

Jan 25, 2019 22

Operation Heap Unsorted List Sorted List Start(N) O(1) O(1) Insert(v) O(1) O(n) FindMin() O(1) O(1) Delete(i) O(n) O(1) ExtractMin() O(n) O(1)

CSCI211 - Sprenkle

slide-12
SLIDE 12

1/25/19 12

Comparing Data Structures

Jan 25, 2019 23

Operation Heap Unsorted List Sorted List Start(N) O(N) O(1) O(1) Insert(v) O(log n) O(1) O(n) FindMin() O(1) O(1) O(1) Delete(i) O(log n) O(n) O(1) ExtractMin() O(log n) O(n) O(1)

CSCI211 - Sprenkle

Putting It All Together…

  • 1. Add elements into PQ with the number’s value

as its priority

  • 2. Then extract the smallest number until done

Ø Come out in sorted order

Jan 25, 2019 24 CSCI211 - Sprenkle

What is the running time of sorting numbers using a PQ implemented with a heap?

O(n log n)

slide-13
SLIDE 13

1/25/19 13

Additional Heap Operations

  • Access elements in PQ by “name”

Ø Maintain additional array Position that stores current position of each element in heap

  • Operations:

Ø Delete(Position[v])

  • Does not increase overall running time

Ø ChangeKey(v, α)

  • Changes key of element v to α
  • Identify position of element v in array (Position array)
  • Change key, heapify

Jan 25, 2019 25 CSCI211 - Sprenkle

Key 2 4 5 6 9 20 Value 3542 5143 8712 1264 9123 5954

Process id Priority

GRAPHS

Jan 25, 2019 CSCI211 - Sprenkle 26

slide-14
SLIDE 14

1/25/19 14

Undirected Graphs G = (V, E)

  • V = nodes (vertices)
  • E = edges between pairs of nodes
  • Captures pairwise relationship between
  • bjects
  • Graph size parameters: n = |V|, m = |E|

27

V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Jan 25, 2019 CSCI211 - Sprenkle CSCI211 - Sprenkle

Social Networks

  • Node: people; Edge: relationship between 2 people
  • Everything Bad Is Good for You: How Today's

Popular Culture Is Actually Making Us Smarter

28

Television shows have complex plots, complex social networks

Social network of Game of Thrones

http://www.cs.duke.edu/csed/harambeenet/modules.html

Jan 25, 2019

slide-15
SLIDE 15

1/25/19 15

Facebook: Visualizing Friends

Jan 25, 2019 CSCI211 - Sprenkle 29

http://www.facebook.com/notes/facebook- engineering/visualizing-friendships/469716398919

World Wide Web

  • Web graph

Ø Node: web page Ø Edge: hyperlink from one page to another

30 cnn.com people.com bleacherreport.com netscape.aol.com time.com hbo.com gameofthrones.com

Directed Graph:

Jan 25, 2019 CSCI211 - Sprenkle

slide-16
SLIDE 16

1/25/19 16

Ecological Food Web

  • Food web graph

Ø Node = species Ø Edge = from prey to predator

32

Reference:

https://www.msu.edu/course/isb/202/ebe rtmay/images/foodweb.jpg

Directed Graph:

Jan 25, 2019 CSCI211 - Sprenkle

Graph Applications

33

transportation

Graph

street intersections

Nodes Edges

highways communication computers fiber optic cables World Wide Web web pages hyperlinks social people relationships food web species predator-prey software systems functions function calls scheduling tasks precedence constraints circuits gates wires

Jan 25, 2019 CSCI211 - Sprenkle

slide-17
SLIDE 17

1/25/19 17

Graph Representation: Adjacency Matrix

  • nn matrix with Auv = 1 if (u, v) is an edge

Ø Two representations of each edge (symmetric matrix) Ø Space? Ø Checking if (u, v) is an edge? Ø Identifying all edges?

34

1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 0 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0

Jan 25, 2019 CSCI211 - Sprenkle

Graph Representation: Adjacency Matrix

  • nn matrix with Auv = 1 if (u, v) is an edge

Ø Two representations of each edge (symmetric matrix) Ø Space: Q(n2) Ø Checking if (u, v) is an edge: Q(1) time Ø Identifying all edges: Q(n2) time

35 Jan 25, 2019 CSCI211 - Sprenkle

1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 0 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0

slide-18
SLIDE 18

1/25/19 18

Graph Representation: Adjacency List

  • Node indexed array of lists

Ø Two representations of each edge Ø Space? Ø Checking if (u, v) is an edge? Ø Identifying all edges?

36

1 2 3 2 3 4 2 5 5 6 7 3 8 8 1 3 4 5 1 2 5 8 7 2 3 4 6 5 3 7

node edges

Jan 25, 2019 CSCI211 - Sprenkle

What are the extremes?

Graph Representation: Adjacency List

  • Node indexed array of lists

Ø Two representations of each edge Ø Space = 2m + n = O(m + n) Ø Checking if (u, v) is an edge takes O(deg(u)) time Ø Identifying all edges takes Q(m + n) time

Jan 25, 2019 CSCI211 - Sprenkle 37

degree = number of neighbors of u node edges

1 2 3 2 3 4 2 5 5 6 7 3 8 8 1 3 4 5 1 2 5 8 7 2 3 4 6 5 3 7

slide-19
SLIDE 19

1/25/19 19

Paths and Connectivity

  • Def. A path in an undirected graph G = (V, E) is a

sequence P of nodes v1, v2, …, vk-1, vk

Ø Each consecutive pair vi, vi+1 is joined by an edge in E

  • Def. A path is simple if all nodes are distinct
  • Def. An undirected graph is connected if ∀ pair
  • f nodes u and v, there is a path between u and v

38

  • Short path
  • Distance

Jan 25, 2019 CSCI211 - Sprenkle