Docsity

Data Structures and Algorithms: Assignment 1 and Quiz 1, Lecture notes of Algorithms and Programming

Partial preview of the text

Download Data Structures and Algorithms: Assignment 1 and Quiz 1 and more Lecture notes Algorithms and Programming in PDF only on Docsity!

COMP9007: Algorithms

› Assignment 1:

› Due by 22 March 11:59PM.

› Quiz 1:

› Will be on conducted during tutorial time Friday the 22nd^ of March.

Last Week

› Upper bounds. T(n) is O(f(n)) if there exist constants c > 0 and n (^0) ≥ 0 such that for all n ≥ n 0 we have T(n) ≤ c · f(n). › Lower bounds. T(n) is Ω(f(n)) if there exist constants c > 0 and n (^0) ≥ 0 such that for all n ≥ n 0 we have T(n) ≥ c · f(n). › Tight bounds. T(n) is Θ(f(n)) if T(n) is both O(f(n)) and Ω(f(n)). 3 Asymptotic Order of Growth 1 10 100 1, 10, 1 10 100 1,

n

3n 2n+ n

Why data structures?

› Programs manipulate data

› Data should be organized so manipulations will be efficient

  • Search (e.g. Finding a word/file/web page)

› Better programs are powered by good data structures

› Naïve choices are often much less efficient than clever choices

› Data structures are existing tools that can help you

  • Guide your design
  • and save you time (avoid re-inventing the wheel)

Linked list

› A linked list is

  • a collection of items (stored in “positions” in the list)
  • that supports the following operations
    • addFirst ( newItem )
      • Add newItem at the beginning of the list
    • addLast ( newItem )
      • Add newItem at the end of the list
    • addAfter ( existingPosition, newItem )
      • Add NewItem after existingPosition
    • getFirst ( )
    • getLast ( )
    • …

next

elem node

Singly Linked Lists

› "Head" points to the first element in the list.

› “Tail” points to the last element in the list.

A B C D

Head Tail

Inserting at the Head

1. Allocate a new node

2. Insert new element

3. Have new node point to

first element

4. Have Head point to new

node

5. Extra checks…

A
B C D

Head ∅

Tail

Inserting at the Tail

1. Allocate a new node

2. Insert new element

3. Have new node point to

null

4. Have old last node point to

new node

5. Update tail to point to new

node

Linked Lists (^12)

Doubly Linked List

› A doubly linked list is often more convenient!

› Nodes store:

  • element
  • link to the previous node
  • link to the next node

› Special trailer and header nodes

prev next

elem

header nodes/positions^ trailer

elements

node

Deletion

› We visualize remove(p), where p == last()

A B C D

p

A B C

D

p

A B C

Worst-cast running time

› In a doubly linked list

  • insertion at head or tail is O(1)
  • deletion at head or tail is O(1)
  • Find element requires O(n)

Applications of Queues

› Direct applications

  • Waiting lists
  • Access to shared resources (e.g., printer)
  • Simulation

› Indirect applications

  • Auxiliary data structure for algorithms
  • Component of other data structures

Queue implementation using linked lists

Parentheses Matching

› Each “(”, “{”, or “[” must be paired with a

matching “)”, “}”, or “[”

  • correct: ( )(( ))([( )])
  • correct: ( )( )(( ))([( )])
  • incorrect: )(( ))([( )])
  • correct: ([ ])
  • incorrect: (

Parentheses Matching Algorithm

Algorithm ParenMatch(X,n):

I nput: An arrayX ofn tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number

Output: true if and only if all the grouping symbols inX match

LetS be an empty stack for i=0 ton-1 do if X[i] is an opening grouping symbol then S.push(X[i]) else if X[i] is a closing grouping symbol then if S.isEmpty() then return false {nothing to match with} if^ S.pop() does not match the type ofX[i]^ then return false {wrong type} if S.isEmpty() then return true {every symbol matched} else return false {some symbols were never matched}