A Heap Is Efficiently Represented As An Array 9 8 7 6 7 2 6 5 - - PowerPoint PPT Presentation

a heap is efficiently represented as an array
SMART_READER_LITE
LIVE PREVIEW

A Heap Is Efficiently Represented As An Array 9 8 7 6 7 2 6 5 - - PowerPoint PPT Presentation

A Heap Is Efficiently Represented As An Array 9 8 7 6 7 2 6 5 1 9 8 7 6 7 2 6 5 1 0 1 2 3 4 5 6 7 8 9 10 Moving Up And Down A Heap 1 9 2 3 8 7 4 7 5 6 6 7 2 6 5 1 8 9 Putting An Element Into A Max


slide-1
SLIDE 1

A Heap Is Efficiently Represented As An Array

9 8 7 6 7 2 6 5 1

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

slide-2
SLIDE 2

Moving Up And Down A Heap

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

slide-3
SLIDE 3

Putting An Element Into A Max Heap

 Complete binary tree with 10 nodes.

9 8 6 7 2 6 5 1 7 7

slide-4
SLIDE 4

Putting An Element Into A Max Heap

 New element is 5.

9 8 6 7 2 6 5 1 7 7 5

slide-5
SLIDE 5

Putting An Element Into A Max Heap

 New element is 20.

9 8 6 7 2 6 5 1 7 7 7

slide-6
SLIDE 6

Putting An Element Into A Max Heap

 New element is 20.

9 8 6 7 2 6 5 1 7 7 7

slide-7
SLIDE 7

Putting An Element Into A Max Heap

 New element is 20.

9 8 6 7 2 6 5 1 7 7 7

slide-8
SLIDE 8

Putting An Element Into A Max Heap

 New element is 20.

9 8 6 7 2 6 5 1 7 7 7 20

slide-9
SLIDE 9

Putting An Element Into A Max Heap

 Complete binary tree with 11 nodes.

9 8 6 7 2 6 5 1 7 7 7 20

slide-10
SLIDE 10

Putting An Element Into A Max Heap

 New element is 15.

9 8 6 7 2 6 5 1 7 7 7 20

slide-11
SLIDE 11

Putting An Element Into A Max Heap

 New element is 15.

9 8 6 7 2 6 5 1 7 7 7 20 8

slide-12
SLIDE 12

Putting An Element Into A Max Heap

 New element is 15.

8 6 7 2 6 5 1 7 7 7 20 8 9 15

slide-13
SLIDE 13

Complexity Of Put

 Complexity is O(log n), where n is heap size.

8 6 7 2 6 5 1 7 7 7 20 8 9 15

slide-14
SLIDE 14

Removing The Max Element

 Max element is in the root.

8 6 7 2 6 5 1 7 7 7 20 8 9 15

slide-15
SLIDE 15

Removing The Max Element

 After max element is removed.

8 6 7 2 6 5 1 7 7 7 8 9 15

slide-16
SLIDE 16

Removing The Max Element

 Heap with 10 nodes.

8 6 7 2 6 5 1 7 7 7 8 9 15

Reinsert 8 into the heap.

slide-17
SLIDE 17

Removing The Max Element

 Reinsert 8 into the heap.

6 7 2 6 5 1 7 7 7 9 15

slide-18
SLIDE 18

Removing The Max Element

 Reinsert 8 into the heap.

6 7 2 6 5 1 7 7 7 9 15

slide-19
SLIDE 19

Removing The Max Element

 Reinsert 8 into the heap.

6 7 2 6 5 1 7 7 7 9 15 8

slide-20
SLIDE 20

Removing The Max Element

 Max element is 15.

6 7 2 6 5 1 7 7 7 9 15 8

slide-21
SLIDE 21

Removing The Max Element

 After max element is removed.

6 7 2 6 5 1 7 7 7 9 8

slide-22
SLIDE 22

Removing The Max Element

 Heap with 9 nodes.

6 7 2 6 5 1 7 7 7 9 8

slide-23
SLIDE 23

Removing The Max Element

 Reinsert 7.

6 2 6 5 1 7 9 8

slide-24
SLIDE 24

Removing The Max Element

 Reinsert 7.

6 2 6 5 1 7 9 8

slide-25
SLIDE 25

Removing The Max Element

 Reinsert 7.

6 2 6 5 1 7 9 8 7

slide-26
SLIDE 26

Complexity Of Remove Max Element

 Complexity is O(log n).

6 2 6 5 1 7 9 8 7

slide-27
SLIDE 27

Note

 Heap is not a search data-structure

 You are interested in minimum or maximum

depending on the heap

 There are efficient ways to convert an array

into a heap.

 Requires O(n) time to convert an array into a

heap instead of O(n log n) as suggested by n insertions.

slide-28
SLIDE 28

Administrivia

 TA Office Hours

 Wednesdays 3 to 5 pm in the PC lab

 Homework 2

 Updated the PDF document

 Made a few things clear; Corrected a few things

 Updated the .zip file

 Makefiles in both directories  Changes to hashT

emplate.c file

slide-29
SLIDE 29

Thank You

slide-30
SLIDE 30

EE717: Advanced Computing foR ELECTRICAL Engineers Lecture 8

Graphs

Shankar Balachandran, IIT Bombay (shankarb@ee.iitb.ac.in)

24 Aug 2014

slide-31
SLIDE 31

Graphs

 G = (V,E)  V is the vertex set.  Vertices are also called nodes and points.  E is the edge set.

 Each edge connects two different vertices.  Captures relationships between vertices

 Directed edge has an orientation (u,v).

u v

slide-32
SLIDE 32

Graph Representation

 Adjacency Matrix  Adjacency Lists

  • Linked Adjacency Lists
  • Array Adjacency Lists
slide-33
SLIDE 33

Adjacency Matrix

 0/1 n x n matrix, where n = # of vertices  A(i,j) = 1 iff (i,j) is an edge

2 3 1 4 5

1 2 3 4 5 1 2 3 4 5 1 1 1 1 1 1 1 1 1 1

slide-34
SLIDE 34

Adjacency Matrix Properties

2 3 1 4 5

1 2 3 4 5 1 2 3 4 5 1 1 1 1 1 1 1 1 1 1

  • Diagonal entries are zero.
  • Adjacency matrix of an undirected graph is

symmetric.

  • A(i,j) = A(j,i) for all i and j.
slide-35
SLIDE 35

Adjacency Matrix (Digraph)

2 3 1 4 5

1 2 3 4 5 1 2 3 4 5 1 1 1 1 1 1

  • Diagonal entries are zero.
  • Adjacency matrix of a digraph need not be

symmetric.

slide-36
SLIDE 36

Adjacency Matrix

 n2 bits of space  For an undirected graph, may store only lower

  • r upper triangle (exclude diagonal).
  • (n-1)n/2 bits

 O(n) time to find vertex degree and/or

vertices adjacent to a given vertex.

slide-37
SLIDE 37

Adjacency Lists

 Adjacency list for vertex i is a linear list of vertices adjacent from vertex i.  An array of n adjacency lists.

2 3 1 4 5

aList[1] = (2,4) aList[2] = (1,5) aList[3] = (5) aList[4] = (5,1) aList[5] = (2,4,3)

slide-38
SLIDE 38

Linked Adjacency Lists

 Each adjacency list is a chain.

2 3 1 4 5

aList[1] aList[5] [2] [3] [4] 2 4 1 5 5 5 1 2 4 3

Array Length = n # of chain nodes = 2e (undirected graph) # of chain nodes = e (digraph)

slide-39
SLIDE 39

Array Adjacency Lists

 Each adjacency list is an array list.

2 3 1 4 5

aList[1] aList[5] [2] [3] [4] 2 4 1 5 5 5 1 2 4 3

Array Length = n # of list elements = 2e (undirected graph) # of list elements = e (digraph)

slide-40
SLIDE 40

Weighted Graphs

 Cost adjacency matrix.

  • C(i,j) = cost of edge (i,j)

 Adjacency lists => each list element is a pair

(adjacent vertex, edge weight)

slide-41
SLIDE 41

Graph Search Methods

  • A vertex u is reachable from vertex v iff there

is a path from v to u.

2 3 8 10 1 4 5 9 11 6 7

slide-42
SLIDE 42

Graph Search Methods

  • A search method starts at a given vertex v and

visits/labels/marks every vertex that is reachable from v.

2 3 8 1 1 4 5 9 11 6 7

slide-43
SLIDE 43

Graph Search Methods

  • Many graph problems solved using a search method.
  • Path from one vertex to another.
  • Is the graph connected?
  • Find a spanning tree.
  • Etc.
  • Commonly used search methods:
  • Breadth-first search.
  • Depth-first search.