Download Data Structures: Queues and Graphs and more Study notes Data Structures and Algorithms in PDF only on Docsity!
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 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
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
Examples
0
1 2
3
0
1
2
0
1 2
G 1^3 4 5
G 2
G 3
V(G 1 )={0,1,2,3} E(G 1 )={(0,1),(0,2),(0,3),(1,2),(1,3),(2, V(G 2 )={0,1,2,3,4,5,6} E(G 2 )={(0,1),(0,2),(1,3),(1,4),(2,5),(2, V(G 3 )={0,1,2} E(G 3 )={<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)
[0]
[1]
[2]
0
1
2
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.
B 1.3 2.0 0
C 0 0 1.
(A,B,1.5),(A,C,2.3),
(B,A,1.3),
(B,B,2.0),(C,C,1.1)
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