Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst,...

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

    20-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst,...

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

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

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

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

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

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

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

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

John Chuang 10

Heap Running Times

What is the running time of each operation?

InsertO(logN)

RemoveO(logN)

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/

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

John Chuang 13

Internet Graphs

Source: Cheswick and Burch

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.

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

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)

John Chuang 17

QuickTime™ and a decompressor

are needed to see this picture.

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

John Chuang 19

Dijkstra’s Algorithm

Source: Doug Comer

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

John Chuang 21

Dijkstra’s Algorithm Example

DistancePredecessor

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)