Data Structures: Queues and Graphs, Study notes of Data Structures and Algorithms

An overview of two essential data structures: queues and graphs. Queues are a linear data structure that follows the first-in-first-out (fifo) principle, with properties such as elements removed in order of insertion. Graphs, on the other hand, are non-linear data structures consisting of vertices and edges, representing connections between different entities. Various topics related to graphs, including paths, simple graphs, connected components, cycles, and acyclic graphs. It also discusses different graph representations like adjacency matrices and linked lists.

Typology: Study notes

2010/2011

Uploaded on 09/01/2011

visir66
visir66 🇮🇳

4.4

(74)

97 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data and File Structures
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

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)

  • (^) Adjacency Lists

[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