Download presentation
Presentation is loading. Please wait.
1
Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear
2
John Chuang2 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
3
John Chuang3 Heap A specialized binary tree that satisfies -Heap-order property -Complete binary tree property Useful for implementing priority queue, heap-sort algorithm
4
John Chuang4 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,H 14,E 25,J16,X 9,F 5,A 15,K 8,W 11,S 7,Q 20,B 6,Z 4,C last node root node
5
John Chuang5 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
6
John Chuang6 Example: Insert(2,T) 12,H 14,E 25,J16,X 9,F 5,A 15,K 8,W 11,S 7,Q 20,B 6,Z 4,C 2,T 12,H 14,E 25,J16,X 9,F 5,A 15,K 8,W 11,S 7,Q 2,T 6,Z 4,C 20,B 12,H 14,E 25,J16,X 9,F 5,A 15,K 8,W 11,S 7,Q 6,Z 2,T 4,C 20,B 12,H 14,E 25,J16,X 9,F 5,A 15,K 8,W 11,S 7,Q 6,Z 4,C 2,T 20,B
7
John Chuang7 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
8
John Chuang8 Example: Remove 12,H 14,E 25,J16,X 9,F 5,A 15,K 13,W 11,S 7,Q 20,B 6,Z 4,C 12,H 14,E 25,J16,X 9,F 5,A 15,K 11,S 7,Q 20,B 6,Z 4,C 13,W 12,H 14,E 25,J16,X 9,F 5,A 15,K 11,S 7,Q 20,B 6,Z 13,W 12,H 14,E 25,J16,X 9,F 13,W 15,K 11,S 7,Q 20,B 6,Z 5,A 12,H 14,E 25,J16,X 13,W 9,F 15,K 11,S 7,Q 20,B 6,Z 5,A 13,W 14,E 25,J16,X 12,H 9,F 15,K 11,S 7,Q 20,B 6,Z 5,A
9
John Chuang9 Heap Storage Heap data easily stored in contiguous array [0] [0] [1] [2] [3] [4] 4,C5,A6,Z 15,K9,F … 12,H 14,E 25,J16,X 9,F 5,A 15,K 8,W 11,S 7,Q 20,B 6,Z 4,C last node root node
10
John Chuang10 Heap Running Times What is the running time of each operation? Insert O(logN) Remove O(logN)
11
John Chuang11 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/
12
John Chuang12 Outline What is a data structure Basic building blocks: arrays and linked lists Data structures (uses, methods, performance): -List, stack, queue -Dictionary -Tree -Graph
13
John Chuang13 Internet Graphs Source: Cheswick and Burch
14
John Chuang14 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.
15
John Chuang15 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
16
John Chuang16 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 )
17
John Chuang17
18
John Chuang18 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
19
John Chuang19 Dijkstra’s Algorithm Source: Doug Comer
20
John Chuang20 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
21
John Chuang21 Dijkstra’s Algorithm Example Distance Predecessor
22
John Chuang22 Node A’s Routing Table Destination Address Next Hop (Cost) BB (2) CD (3) DD (1) ED (2) FD (4)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.