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

Information about assignment 1 and quiz 1 for the comp9007: algorithms course. It includes details about the due date, quiz schedule, and covered topics such as asymptotic order of growth and data structures like linked lists and queues. Students will learn about the importance of data structures, their operations, and their applications.

Typology: Lecture notes

2018/2019

Uploaded on 04/20/2019

kefart
kefart 🇺🇸

4.4

(11)

55 documents

1 / 101

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP9007: Algorithms
Assignment 1:
Due by 22 March 11:59PM.
Quiz 1:
Will be on conducted during tutorial time Friday the 22nd of March.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

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}