Linear Lists Data Structures and Algorithms (60-254)

47
Linear Lists Data Structures and Algorithms (60- 254)

Transcript of Linear Lists Data Structures and Algorithms (60-254)

Linear ListsData Structures and Algorithms (60-254)

2

Topics

• Stacks• Queues• Linked Lists (Singly and Doubly Linked Lists)

3

Stacks

• A stack is a LIFO (Last In First Out) list of elements.• Operations allowed in a stack:

• Addition (Push)• Deletion (Pop)• Inspect Top

• Allowed at the end of the list, known as stacktop.• Real-life examples of stacks are very common.• Here are a few:

• A stack of dishes,• A stack of paper,• A stack of books.

4

GraphicallyAdditions and Deletions at the stacktop

A stack of books

5

Balanced Parentheses Checking

Given a string of Open “(” parentheses, andClosed “)” parentheses.

An interesting problem: To design an algorithm that checks if balanced.

When is a string of parentheses balanced? If it is either of the form:

(s) s() or ()swhere s is• a string of balanced parentheses, or• the empty string.

6

Examples

Examples of strings of balanced parentheses:()()()()()()()()((((((((())))))))(()((()()())(())))

and many more… Examples of strings of non-balanced parentheses:

)(((()()())))(((((((()))))))()()())()()()

and many more…

7

Goal

We want to design a stack-based algorithm that checks if a string of parentheses is balanced.How does it work?

Scan the string from left to right.If character is opening bracket

Push it onto the stack.If character is closing bracket, and stack is empty

The string is not balanced.Otherwise

Pop the opening bracket from stack.If scanning ends and stack is not empty

The string is not balanced.

8

Example

The string ((()(())()) is not balanced.An opening bracket will be left on the stack.

The string )() is not balanced either.

A closing bracket found and stack is empty. The string (()) is balanced.

Both scanning will end and stack will be empty.

9

Postfix Arithmetic Expression EvaluationThree ways of writing arithmetic expressions:•Prefix: + a b

Operands are preceded by operator.• Infix: a + b

Usual way of writing:Operand, then Operator, and finally Operand.

•Postfix: a b + Operands are followed by operator.

10

Examples of Arithmetic Expressions

Infix Postfixa + b a b +a + b * c a b c * +(a + b) * c a b + c *

Advantages of infix form:• Can use brackets to change operator precedence. Stacks lead to an interesting algorithm to evaluate arithmetic expressions in postfix form.

11

Postfix Expression Evaluation

Algorithm is simple:Scans characters in given expression from left to right.

If character is an operandPush it onto stack

If character is an operator

Pop two operands from stack.Apply operator to these two operands.Push the result onto stack.

12

Example

Given postfix expression: a b c * +

Push aPush bPush cPop cPop bd = b * cPush dPop dPop ae = a + dPush e

13

Another Example

Given postfix arithmetic expression: a b + c *

Push aPush bPop bPop ad = a + bPush dPush cPop cPop de = c * dPush e

14

Implementation of a Stack

Using a variety of data structures …available in programming language used.

Possible data structures:• Array• Linked List

Complexity of stack operations:• Independent of data structure and programming language.• Push and Pop can be done in O(1) time

Array-based implementation:• Array’s size needs to be pre-determined.• Specify a maximum for the size, say N = 1,000• Our stack is an N-element array, A.

15

Array Implementation

a b d * +

0 1 2 3 t=4 5 6 7 8 9

The stack represented by elements from 0 … t Pop an element:

if t 0element A[t] t t – 1

elsestack is empty

16

Array Implementation

Push an element:if t < N - 1

t t + 1A[t] element

elseStack is full

17

Reversing Digits of a Number

• A very interesting problem.• It involves the use of stacks. Given a non-negative integer, n.Want to output a number m … whose digits are the reverse of those of n. For example:

Input is n = 1234Output is m = 4321

An algorithm for this problem?

18

Algorithm ReverseDigit

Input: Non-negative integer, nOutput: Non-negative integer, m, whose digits are the reverse of digits of n.While (n > 0)

Push n mod 10 to stackn n div 10

m 0; i 1while (stack not empty)

Pop d from stackm d * i + mi i * 10

Print m and STOP

19

Example

n = 1234Push 1234 mod 10 = 4, n 1234 div 10 = 123Push 123 mod 10 = 3, n 123 div 10 = 12Push 12 mod 10 = 2, n 12 div 10 = 1Push 1 mod 10 = 1, n 1 div 10 = 0Stack =

Pop 1, m 1 * 1 + 0 = 1Pop 2, m 2 * 10 + 1 = 21Pop 3, m 3 * 100 + 21 = 321Pop 4, m 4 * 1000 + 321 = 4321

1234

20

Time Complexity of Algorithm ReverseDigitAssumption: Let k be the number of digits in the value n

• Operators div, mod, + and * take constant time, O(1)

Number of steps of first while loop:2 * k

Number of steps of second while loop:3 * k

Also, k = log10 n

But, we know that log10 n = log2 n / log2 10, and log2 10 3.32Then,

21

Queues

A queue is a FIFO (First In First Out) list of elements.It is a close “cousin” of the stack • Addition to this list (Enqueue) is done at one end, called the BACK of the queue.• Deletion (Dequeue) is done at the other end, called the FRONT of the queue.

Graphically:

22

Real-life examples of queues

•A queue at a bus-stop•A line at a bank machine•A printer queue

Food for thought: A bank has three tellers. Would it be better to have a queue for each teller, or one queue that feeds all three tellers. How could you set up an experiment to test your hypothesis?

23

Application of Queues: Pathfinding

Start        

         

       Finis

h

         

         

A rat, started off at the START square of the maze.

• Has to find the shortest path to the FINISH square.• Shaded squares are blocked off

to the rat.• Length of path is the number

of squares the rat visits.

24

Using a queue

From a given square, the rat can move left, right, up or down.

Assumption: “our” rat knows about the data structure.

It does the following:• It labels all the squares it can visit,• from the START square being number 0, and• saves positions at the back of the queue.

Then, it takes all positions (coordinates) from the front of queue, and Assuming this position is labeled i,

• It marks all positions it can visit with i + 1• It continues until it visits the FINISH square.

25

Finding the shortest path

It does this by moving backwards, as follows. If the FINISH square would have been labeled k,• It moves back to square labeled k – 1,• then, to a square labeled k – 2, • and so on,• until it reaches the START square.

Each move is possible, since to reach square k one would have to reach square k – 1

26

Graphically

Traversal of example mesh looks like:

This shows the worst case; among all squares labeled 5, the one adjacent to square FINISH is examined last.

Start 1 2 3 4

1 2 3   5

  3 4 5 Finish

    5 6  

    6    

27

Array Implementations of a Queue

9 5 16 -1 32 -7        0 1 2 3 4 5 6 7 8 9

Some problems arise in implementing a queue when using an arrayLet’s see a solution (Fixed Front Approach):• Fix the front of the queue at 0• Let the back be movable.

front back

28

Time complexity of enqueue and dequeue?Enqueue operation:

Takes constant, O(1), time.Why?Very simple:

Assign element to A[back]Set back to back + 1 = 7

Dequeue operation:Takes linear, O(n), time.Why?Complicated:

Shift elements A[1]..A[back-1] one position to the left.Set back to back - 1

Quite inefficient!!!

29

Moveable Front Approach

    9 5 16 -1 32 -7    0 1 2 3 4 5 6 7 8 9

Can we do better?Yes… Make the front of the queue movable as well.

front back

30

Time complexity of enqueue and dequeue?Enqueue operation:

Takes constant, O(1), time.Why?Very simple:

Assign element to A[back]Set back to back + 1 = 9

Dequeue operation:Takes constant, O(1), time !!! Why?Easy:

Assign A[front] to elementSet front to front + 1

Much more efficient, but… still a problem…

31

Problems

We need to keep lots of unused positions:On the right of array … to enqueue.

And, will have lots of unused positions:

on the left of the array … after dequeueing. Still BIG Problems:

Lots of unused positions in the array.Queue may become full, and still unused positions on the left!

32

Can we do even better? Circular array basedYes… Implement

A “circular” queue,or a “circular” array.

We will have two configurations… Normal configuration: front < back

    9 5 16 -1 32 -7    0 1 2 3 4 5 6 7 8 9

front back

33

Circular Queue

“Wrapped around” configuration: front > back

-1 32 -7         9 5 160 1 2 3 4 5 6 7 8 9

back front

34

Implementation

Implementation of operations: SimpleHow to enqueue?

If (queue not full)Increment back as: (back + 1) mod N

Queue full? … when (back + 1) mod n = front

35

Implementation

To dequeue:If (queue not empty)

Increment front as: (front + 1) mod NQueue empty? …

when back = front Now, unused cells will be:

at the extremes of the array, orin the middle portion of the array.

36

Time complexity of enqueue and dequeue?Enqueueing:

Constant time, O(1). Dequeueing:

Constant time, O(1), too!!! Note: full queue still has one wasted slot. It is possible to design this so no slots are wasted by keeping a separate count variable to distinguish empty and full.

37

Singly linked lists

A typical example of a linked list:

• Every node can be accessed via first.• Each node has a link to the next element.

38

Doubly linked Lists

A typical example:

In a doubly linked list, we have:Two “unused” nodes for first and last – Why?.Every node in List can be accessed:

either via first or via lastEach node has two links:

next element,previous element

39

Inserting an element

Find the node … after which element is to be inserted.Create a new nodeSet the new node’s element to elementEstablish the corresponding links:

Set next of node and prev of next of node to new node

We can start searching from first, or can start from last Here is the an algorithm starting from first …

40

Algorithm InsertElement

Input: An element, eCreate new node n’; n’.elem en first while (n.next <> last and n.next.elem < e)

n n.nextn.next.prev n’n’.next n.nextn.next n’n’.prev n

41

Example: Insert 12

last

Observations:How does it work starting from last?

Homework…Other types of objects can be stored in the list.Examples:

Strings, float’s, double’s, or even any Object.

42

Time complexity of Algorithm InsertElement

While loop is executed at most n times, where n is the number of elements

Thus, it takes O(n) to find and insert the element.Insertion only takes O(1) !!!What is time complexity if we started from last?

Homework… What if we had an array instead?

Find takes O(n) and then insert takes O(n) too.Also, an advantage of using a linked list:

Array’s size is fixed, and list’s is not.

43

Advantage of using doubly linked listsIf the list is sorted, and

want to find the maximum (or minimum) of the list:it takes O(n) in a linked list, and O(1) in a doubly linked list!

For any order of elements:

want to insert an element at the end:it takes O(n) in a linked list, whereas in a doubly linked list: O(1)

44

Deleting an element

Find the node … containing element to be removed. Just modify the corresponding links:

Change next of previous of node and previous of next of node What does the algorithm look like?

Again, we can start from first, or can start from last Here is the algorithm starting from first:

45

Algorithm DeleteElement

Input: An element, en first.nextwhile (n <> last and n.elem <> e)

n n.nextif n <> last

n.next.prev n.prevn.prev.next n.next

elsePrint e + “is not in the list”

46

Example: Delete 16

Observations:How does it work starting from last?

Homework … What is time complexity of DeleteElement? …

47

Time complexity of DeleteElementAgain,

To find the element, it takes O(n)(the while loop in Algorithm DeleteElement)

But, To delete the element, it takes O(1)

In an array implementation:

It takes O(n) just to delete the element!!Why?

Homework:Write the algorithm that starts from last.