##### Document information

Data and File Structures

Queue • Properties

– Elements removed in order of insertion – First-in, First-out (FIFO) – Must track Front (first in) and Back (last in)

• Queue operations – Enqueue = add element (to back) – Dequeue = remove element (from front)

Queue Implementations

• Linked list – Add to tail (Back) of list – Remove from head (Front) of list

• Array • Circular array

Circular Queue

• Circular array (ring) – q[ 0 ] follows q[ MAX – 1 ] – Index using q[ i % MAX ]

• Problem – Detecting difference

between empty and nonempty queue

Circular Queue • Approach 1

– Keep Front at first in – Keep Back at last in

• Problem – Empty queue identical to queue with 1 element

Circular Queue • Approach 2

– Keep Front at first in – Keep Back at last in – 1

• Problem – Empty queue identical to full queue

Circular Queue • Inherent problem for queue of size N

– Only N possible (Front – Back) pointer locations – N+1 possible queue configurations

• Queue with 0, 1, … N elements

• Solutions – Maintain additional state information

• Use state to recognize empty / full queue • Examples

– Record Size – Record QueueEmpty flag

– Leave empty element in queue – Store marker in queue

Graph: Definition • A graph G consists of two sets

– a finite, nonempty set of vertices V(G) – a finite, possible empty set of edges E(G) – G(V,E) represents a graph

• An undirected graph is one in which the pair of vertices in a edge is unordered, (v0, v1) = (v1,v0)

• A directed graph is one in which each edge is a directed pair of vertices, <v0, v1> <v1,v0>

**Data Structures - Graph
**

**THESE ARE ALL GRAPHS.**

**Graph Terms
**• **Path** –

– A path from vertex *u *and *v* of a graph G exists if there exists a set
adjacent edges that starts with *u* and ends with *v
*

• **Simple Graph **–
– A graph without loops and with at most one edge between any two

vertices is called a simple graph
• **Length of a path **–

– The number of edges in the aforementioned set

Graph Terms
• **Connected –
**

– iff, for every pair of its vertices, <u,v>, there is a path from u to v.
• **Connected Components –
**

– If a graph is not connected, it is made of two or more connected components. – The “maximal” connected subgraph of a given graph.

• **Cycle –
**– A simple path, of non-zero length, that starts & ends at the same vertex.

• **Acyclic Graph –
**– A graph with no cycles

Connected graph Connected components Cyclic graph Acyclic graph

Examples
**0
**

**1 2
**

**3
**

**0
**

**1
**

**2
**

**0
**

**1 2
**

**3 4 5 6**G1
G2 G3

V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}

complete undirected graph: *n *(*n*–1)/2 edges
complete directed graph: *n *(*n*–1) edges

complete graph incomplete graph

Representing Graphs • Graphs –

– Inherently abstract – Directed or Undirected – Two common representations:

• **Adjacency matrix
**• **Adjacency linked list
**

A B C A 0 1 1 B 1 1 0 C 0 0 1

{ (A,B),(A,C),(B,A),(B,B),(C,C) }

• **Adjacency Matrices
**– A graph with ** n** vertices:

**G**= (

**V**,

**E**) – Adjacency matrix of G (

*n*

*n*array):

**adj_mat**

• If the edge (*v i*,

*v*) is in E(G), adj_mat[

**j***i*][

*j*]=1 • If there is no such edge in E(G), adj_mat[

*i*][

*j*]=0 • adj_mat of an undirected graph is symmetric • adj_mat of a digraph need not be symmetric

0 1 1 1

1 0 1 1

1 1 0 1

1 1 1 0

0 1 0

1 0 0

0 1 0

**0
**

**1 2
**

**3
**

**0
**

**1
**

**2**

• **Adjacency Lists
[0]
**

**[1]
**

**[2]
**

**0
**

**1
**

**2
**

**0 2
**

**1
**

**NULL
**

**NULL
**

**NULL**

Weighted Graphs • Weighted Graph –

– A graph G, with the added property that the edges between nodes is assigned a “weight”

– Edge weight may represent: cost, distance, time…

A B C A 0 1.5 2.3 B 1.3 2.0 0 C 0 0 1.1

{ (A,B,1.5),(A,C,2.3), (B,A,1.3), (B,B,2.0),(C,C,1.1) }

• **Tree
**– A connected graph with *n-1* nodes and *n* edges
– A graph in which any two vertices are connected by

*exactly one* path.
• **Forest
**

– A set of trees – An acyclic graph that not necessarily connected

Rooted Trees • Rooted Tree

– A tree, with a “root” node – Root nodes are selected arbitrarily (any vertex)

• Tree genealogy – Ancestors – Children – Siblings – Parents – Descendents – Leaf nodes

Height & Depth of Trees • Depth

– Depth of vertex v is the length of the simple path from the root to v

– Count the edges • Height

– Height of a tree is the length of the longest simple path from the root to the deepest leaf nodes

HEAP & PRIORITY QUEUE

Max-Heapify routine
**Max-Heapify**(*A*, *i*)
*l = left*(*i*)
*r = right*(*i*)
**if (***l <=* *heapsize*[*A*]) && (*A*[*l*] > *A*[*i*])

*largest* = *l
***else
**

*largest* = *r
***if (***r <=* *heapsize*[*A*]) && (*A*[*r*] > *A*[*largest*])

*largest* = *r
***if (***largest *!*= i***)
**

exchange(*A*[*i*],*A*[*largest*])
Max-Heapify(*A,largest*)

**Build-Max-Heap**(*A*)
*heapsize*[*A*] = *length*(*A*)**
for ***i =* **to** 1

Max-Heapify(*A,i*)
2/][*Alength*

Heap-Sort

**Heap-Sort**(*A*)
Build-Max-Heap(*A*)
*heapsize*[*A*] = *length*(*A*)**
for ***i *= *length*[*A*] t**o** 2

exchange(*A*[*1*],*A*[*i*])**
***heapsize*[*A*] = *length*(*A*) –1 ** **
Max-Heapify(*A,*1)

*put maximum
at the root*

**Heap-Maximum**(*A*)

**return** *A*[1]**
**

**Heap-Extract-Max**(*A*)
**if** (*heapsize*[*A*] < 1)

**error **“heap underflow”**
***max*** = ***A*[*1*]**
***A*[*1*] =* A*[*heapsize*[*A*]]
*heapsize*[*A*]= *heapsize*[*A*] –1
Max-Heapify(*A,*1)
**return** *max*

**Heap-Increase-key**(*A, i, key*)
**if** (*key*** < ***A*[*i*]** )
**

**error **“new key smaller than existing one”**
***A*[*i*] =* key
***while*** *(*i > *1) &&* *(*A*[*parent*(*i*)] **<** *A*[*i*])

exchange(*A*[*i*], *parent*(*A*[*i*]))**
*** i* = *parent*(*i*) ** **

**Max-Heap-Insert **(*A, key*)
*heapsize*[*A*]= *heapsize*[*A*] + 1

*A*[*heapsize*[*A*]] = **–** **∞
**Heap-Increase-Key(*A, heapsize*[*A*], *key*)