The interaction between algorithm and data structure

  • Recall:

    • Computer program = algorithm + data structure(s)

  • Algorithms can produce and consume information in different ways:

    • Some algorithms consume the data in the same order as how the data was produced

        Production  order:   A  B  C  D
        Consumption order:   A  B  C  D

    • Some algorithms consume the data in the reverse order as was produced:

        Production  order:   A  B  C  D
        Consumption order:   D  C  B  A

    • Etc, etc

Intro to the queue data structure

  • There are 2 commonly used data structures in computer science:

    1. Stack     (LIFO)         We have studied this
    2. Queue    (FIFO)

    We will now study the queue...

  • The queue data structure:

    • A queue is a data structure that organize the stored data in a First In First Out (FIFO) manner

  • To achieve the FIFO behavior, the queue only provide the following 2 methods to access the data stored in a queue:

    • enqueue(x): add x to the "tail/back" of the queue

    • dequeue(): remove the item at the "head/front" of the queue and return it.

Analogy for the queue data structure

  • A queue data structure is like a queue of people:

Analogy for the queue data structure

  • A queue data structure is like a queue of people:

  • The enqueue( ) operation will "insert"/add an item on tail of the queue:

Analogy for the queue data structure

  • A queue data structure is like a queue of people:

  • Dequeue( ) will remove the item at the head of the queue and returns it.

Some computer algorithms/processes with a natural LIFO behavior

  • Scheduling for fairness:

    • FIFO is a service ordering that is fair

    • Scheduling algorithms that serve requests from different clients often implements a FIFO service policy using a queue

  • The Breath First Search (BFS) algorithm in graph applications

    • The BFS algorithm will probe nodes that are nearest to the source nodes first

    • To implement the "search the nearest nodes first" behavior, the BFS algorithm use a queue to store nodes to visit next

    • You will study the BFS algorithm in CS 253...