Top Banner
Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear
22

Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

Data Structures Heaps and Graphs

i206 Fall 2010

John Chuang

Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear

Page 2: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 2

Outline

What is a data structure Basic building blocks: arrays and linked lists

Data structures (uses, methods, performance):- List, stack, queue- Dictionary- Tree --> Heap- Graph

Page 3: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 3

Heap

A specialized binary tree that satisfies- Heap-order property- Complete binary tree property

Useful for implementing priority queue, heap-sort algorithm

Page 4: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 4

Heap

Heap-order property: for every node v other than the root, the key stored at v is greater than or equal to the key stored at v’s parent.

Complete binary tree property: A binary tree with height h is a complete binary tree if levels 0, 1, 2, …, h-1 of the tree have the maximum number of nodes, and in level h-1, all the internal nodes are to the left of the external nodes, and there is at most one node with one child, which must be a left child.

12,H14,E25,J16,X

9,F

5,A

15,K

8,W11,S

7,Q 20,B

6,Z

4,C

last node

root node

Page 5: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 5

Heap Methods

Insert- Insert element as last node of the heap- May need to perform up-heap bubbling to restore heap-order property

Remove- Remove and return element at root node- Move last node to root node- May need to perform down-heap bubbling to restore heap-order property

Page 6: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 6

Example: Insert(2,T)

12,H14,E25,J16,X

9,F

5,A

15,K

8,W11,S

7,Q 20,B

6,Z

4,C

2,T 12,H14,E25,J16,X

9,F

5,A

15,K

8,W11,S

7,Q 2,T

6,Z

4,C

20,B

12,H14,E25,J16,X

9,F

5,A

15,K

8,W11,S

7,Q 6,Z

2,T

4,C

20,B 12,H14,E25,J16,X

9,F

5,A

15,K

8,W11,S

7,Q 6,Z

4,C

2,T

20,B

Page 7: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 7

Heap Methods

Insert- Insert element as last node of the heap- May need to perform up-heap bubbling to restore heap-order property

Remove- Remove and return element at root node- Move last node to root node- May need to perform down-heap bubbling to restore heap-order property

Page 8: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 8

Example: Remove

12,H14,E25,J16,X

9,F

5,A

15,K

13,W11,S

7,Q 20,B

6,Z

4,C

12,H14,E25,J16,X

9,F

5,A

15,K

11,S

7,Q 20,B

6,Z

4,C

13,W

12,H14,E25,J16,X

9,F

5,A

15,K

11,S

7,Q 20,B

6,Z

13,W

12,H14,E25,J16,X

9,F

13,W

15,K

11,S

7,Q 20,B

6,Z

5,A

12,H14,E25,J16,X

13,W

9,F

15,K

11,S

7,Q 20,B

6,Z

5,A

13,W14,E25,J16,X

12,H

9,F

15,K

11,S

7,Q 20,B

6,Z

5,A

Page 9: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 9

Heap Storage

Heap data easily stored in contiguous array

[0][0] [1] [2] [3] [4]

4,C 5,A 6,Z 15,K 9,F …

12,H14,E25,J16,X

9,F

5,A

15,K

8,W11,S

7,Q 20,B

6,Z

4,C

last node

root node

Page 10: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 10

Heap Running Times

What is the running time of each operation?

InsertO(logN)

RemoveO(logN)

Page 11: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 11

Heapsort

Given an unsorted list of n elements:- Insert n elements, then- Remove n elements

What is run-time of heapsort algorithm?

How does it compare to insertion sort?

http://www.cs.pomona.edu/~marshall/courses/2002/spring/cs50/BigO/

Page 12: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 12

Outline

What is a data structure Basic building blocks: arrays and linked lists

Data structures (uses, methods, performance):- List, stack, queue- Dictionary- Tree- Graph

Page 13: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 13

Internet Graphs

Source: Cheswick and Burch

Page 14: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 14

Social Network Graphs

American Journal of Sociology, Vol. 100, No. 1. "Chains of affection: The structure of adolescent romantic and sexual networks," Bearman PS, Moody J, Stovel K.

Page 15: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 15

Graph

A graph consists of a set of nodes (vertices) and a set of links (edges) that establish relationships (connections) between the nodes

Represented/stored using adjacency list or adjacency matrix data structures- Adjacency list for Graph 1: {a,b}, {a,c}, {b,c}- Adjacency matrix for Graph 2:

Edges can be directed/undirected Edges can have weights Tree is a special case of graph

Page 16: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 16

Graph Algorithms

Search/traversal: breadth-first or depth-first -- O(|V|+|E|)

Routing: shortest path between two points (Dijkstra’s algorithm) -- O(|V|2+|E|)

Minimum spanning tree -- O(|E|) Maximum Flow -- O(|V|3), O(|V|2|E|), O(|V||E|2)

Page 17: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 17

QuickTime™ and a decompressor

are needed to see this picture.

Page 18: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 18

Routing

Problem: network routers have to forward data packets toward destination; must determine next hop

Algorithm: Dijkstra’s algorithm- Shortest Path First (SPF) algorithm- Greedy algorithm- Input: graph with nodes and weighted edges

- Output: shortest paths from source node i to every other node; cost of each path

Page 19: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 19

Dijkstra’s Algorithm

Source: Doug Comer

Page 20: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 20

Algorithm Intuition

Start at source node Move outward At each step:

- Find node u that- has not been considered before; and- is closest to source

- Compute:- Distance from u to each neighbor v- If distance shorter, make path to v go through u

Page 21: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 21

Dijkstra’s Algorithm Example

DistancePredecessor

Page 22: Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

John Chuang 22

Node A’s Routing Table

Destination Address

Next Hop (Cost)

B B (2)

C D (3)

D D (1)

E D (2)

F D (4)