T h e J a v a c o l l e c t i o n s f r a me w - - PowerPoint PPT Presentation

t h e j a v a c o l l e c t i o n s f r a me w o r k n o
SMART_READER_LITE
LIVE PREVIEW

T h e J a v a c o l l e c t i o n s f r a me w - - PowerPoint PPT Presentation

T h e J a v a c o l l e c t i o n s f r a me w o r k ( n o t o n e x a m ) J a v a c o l l e c t i o n s f r a m e w o r k M a n y d a t a s t r u c t u r e s i m p l


slide-1
SLIDE 1

T h e J a v a c

  • l

l e c t i

  • n

s f r a me w

  • r

k ( n

  • t
  • n

e x a m )

slide-2
SLIDE 2

J a v a c

  • l

l e c t i

  • n

s f r a m e w

  • r

k

M a n y d a t a s t r u c t u r e s i m p l e m e n t e d a s c l a s s e s , a l l f

  • u

n d i n j a v a . u t i l . *

  • L

i s t s : A r r a y L i s t , L i n k e d L i s t

  • Q

u e u e s / s t a c k s : A r r a y D e q u e , L i n k e d L i s t

  • S

e t s / m a p s : H a s h S e t , H a s h M a p , T r e e S e t , T r e e M a p

– a

l s

  • :

I d e n t i t y H a s h M a p , L i n k e d H a s h M a p , L i n k e d H a s h S e t , a n d m

  • r

e

  • P

r i

  • r

i t y Q u e u e

Tie s e c l a s s e s i m p l e m e n t v a r i

  • u

s i n t e r f a c e s

  • C
  • l

l e c t i

  • n

< E > – a c

  • l

l e c t i

  • n
  • f

v a l u e s

  • I

t e r a t

  • r

< E >

  • f
  • r

i t e r a t i n g

  • v

e r a l l v a l u e s

  • L

i s t < E >

  • f
  • r

s e q u e n t i a l a n d r a n d

  • m

a c c e s s

slide-3
SLIDE 3

T h e C

  • l

l e c t i

  • n

i n t e r f a c e

M

  • s

t d a t a s t r u c t u r e s i m p l e m e n t C

  • l

l e c t i

  • n

, w h i c h p r

  • v

i d e s m e t h

  • d

s f

  • r

a d d i n g a n d r e m

  • v

i n g v a l u e s , c h e c k i n g f

  • r

m e m b e r s h i p

  • f

a v a l u e e t c . : interface Collection<E> extends Iterable<E> { boolean add(E e); boolean addAll(Collection<E> c); void clear(); boolean contains(Object o); boolean isEmpty(); boolean remove(Object o); boolean removeAll(Collection<?> c); int size(); Object[] toArray(); ... }

slide-4
SLIDE 4

T h e L i s t i n t e r f a c e

List i s u s e d f

  • r

c

  • l

l e c t i

  • n

s w h e r e e l e m e n t s

  • c

c u r i n a n

  • r

d e r ( e . g . l i n k e d l i s t s , d y n a m i c a r r a y s ) : interface List<E> extends Collection<E> { // Add an element at a specific index boolean add(int index, E e); // Remove the element at a given index boolean remove(int index); // Get the element at a given index E get(int index); // Modify the element at a given index E set(int index, E element); // Return a slice of the list, // containing only elements from index “from” // to index “to” List<E> sublist(int from, int to); ... }

slide-5
SLIDE 5

U s e

  • f

s u b l i s t

I t e r a t i n g t h r

  • u

g h a l l e l e m e n t s b e t w e e n i n d e x f r

  • m

a n d i n d e x t

  • :

for(E element: list.sublist(from, to)) { … }

D e l e t i n g a l l e l e m e n t s b e t w e e n i n d e x f r

  • m

a n d i n d e x t

  • :

list.sublist(from, to).clear(); Tii s w

  • r

k s b e c a u s e sublist r e t u r n s a v i e w

  • f

t h e u n d e r l y i n g s t r u c t u r e , n

  • t

a c

  • p

y !

slide-6
SLIDE 6

P e r f

  • r

m a n c e

LinkedList a n d ArrayList i m p l e m e n t a l l List

  • p

e r a t i

  • n

s , e v e n t h

  • s

e t h a t h a v e b a d p e r f

  • r

m a n c e ! Tie f

  • l

l

  • w

i n g e x a m p l e s w

  • r

k e v e n t h

  • u

g h t h e y a r e n

  • t

r e a l l y e ffj c i e n t : int sum(LinkedList<Integer> list) { int total = 0; // Using a linked list for random access :( for (int i = 0; i < list.size(); i++) total += list.get(i); return total; } void deleteFirst(ArrayList<Integer> list) { // Deleting from the front of a dynamic array :( list.delete(0); }

slide-7
SLIDE 7

T h e S e t i n t e r f a c e

Tie Set i n t e r f a c e i s j u s t t h e s a m e a s Collection:

interface Set<E> extends Collection<E> { }

Tii s i s b e c a u s e C

  • l

l e c t i

  • n

a l r e a d y c

  • n

t a i n s s e t

  • p

e r a t i

  • n

s : a d d , r e m

  • v

e , c

  • n

t a i n s e t c . Tie d i fg e r e n c e i s : i f a c l a s s i m p l e m e n t s S e t , i t s h

  • u

l d b e g u a r a n t e e d t h a t d u p l i c a t e e l e m e n t s a r e i g n

  • r

e d

slide-8
SLIDE 8

T h e M a p i n t e r f a c e

I m p

  • r

t a n t m e t h

  • d

s

  • f

Map<K,V>:

boolean containsKey(K key); V get(K key); void put(K key, V value); V remove(K key); Set<K> keySet(); Collection<V> valueSet(); Set<Entry<K,V>> entrySet(); // Entry<K,V> has methods // getKey, getValue, setValue

F

  • r

e x a m p l e , y

  • u

c a n i t e r a t e

  • v

e r a l l k e y s

  • f

a m a p l i k e s

  • :

HashMap<K, V> map = …; for (K key: map.keySet()) { … }

slide-9
SLIDE 9

I t e r a t

  • r

s

I n J a v a , y

  • u

c a n l

  • p
  • v

e r a l l k i n d s

  • f

d a t a s t r u c t u r e s : ArrayList<E> list; for (E x: list) { … } LinkedList<E> list; for (E x: list) { … } HashMap<K, V> map; for (K x: map.keySet()) { … } Tia t ’ s b e c a u s e a l l t h e s e s t r u c t u r e s i m p l e m e n t Iterable.

slide-10
SLIDE 10

I t e r a t

  • r

a n d I t e r a b l e

A n Iterator i s a n

  • b

j e c t t h a t r e p r e s e n t s a s e q u e n c e

  • f

i t e m s b e i n g l

  • p

e d

  • v

e r :

interface Iterator<E> { // Return the next item in the iteration E next(); // Return true if the iteration has more elements boolean hasNext(); }

U s a g e : e a c h t i m e y

  • u

c a l l next(), i t g i v e s y

  • u

t h e n e x t e l e m e n t i n t h e s e q u e n c e

  • O

n c e y

  • u

h a v e g

  • n

e t h r

  • u

g h a l l e l e m e n t s i n t h e s e q u e n c e , hasNext() w i l l r e t u r n f a l s e

A n Iterable h a s

  • n

e m e t h

  • d

f

  • r

p r

  • d

u c i n g a n i t e r a t

  • r

:

Iterator<E> iterator();

slide-11
SLIDE 11

I t e r a t

  • r

a n d I t e r a b l e

S u p p

  • s

e l i s t i s a n A r r a y L i s t : ArrayList<Integer> list; Tie n t h e f

  • l

l

  • w

i n g c

  • d

e l

  • p

s t h r

  • u

g h a l l e l e m e n t s

  • f

t h e l i s t : Iterator<Integer> iter = list.iterator(); while (iter.hasNext()) { int x = iter.next(); … } J a v a l e t s u s w r i t e t h i s a s a f

  • r
  • l
  • p

i n s t e a d ! for (int x: list) { … }

slide-12
SLIDE 12

L i s t I t e r a t

  • r

C l a s s e s t h a t i m p l e m e n t L i s t s u p p

  • r

t a s

  • u

p e d

  • u

p i t e r a t

  • r

:

  • L

i s t I t e r a t

  • r

< E > i t e r a t

  • r

( ) ;

L i s t I t e r a t

  • r

i s a n e x t e n s i

  • n
  • f

I t e r a t

  • r

t h a t s u p p

  • r

t s :

  • m
  • d

i f y i n g t h e c u r r e n t e l e m e n t d u r i n g i t e r a t i

  • n
  • a

d d i n g a n d r e m

  • v

i n g e l e m e n t s a t t h e c u r r e n t p

  • s

i t i

  • n
  • g
  • i

n g b a c k a n d f

  • r

w a r d i n t h e l i s t

S

  • y
  • u

c a n u s e t h i s t

  • d
  • a

n y t h i n g y

  • u

w

  • u

l d w a n t t

  • d
  • w

i t h a l i n k e d l i s t

slide-13
SLIDE 13

M i s c e l l a n e

  • u

s b i t s a n d b

  • b

s

S

  • r

t e d S e t , N a v i g a b l e S e t : e x t r a i n t e r f a c e s t h a t a r e i m p l e m e n t e d f

  • r

s e t s t h a t a r e b a s e d

  • n
  • r

d e r i n g

  • S
  • r

t e d S e t < E > s u b S e t ( E f r

  • m

, E t

  • )

: r e t u r n a l l i t e m s b e t w e e n f r

  • m

a n d t

  • E

fm

  • r

( E e ) : r e t u r n t h e g r e a t e s t i t e m l e s s t h a n e

  • E

p

  • l

l F i r s t ( ) : r e m

  • v

e a n d r e t u r n t h e s m a l l e s t e l e m e n t

  • e

t c .

S

  • r

t e d M a p , N a v i g a b l e M a p : s i m i l a r b u t f

  • r

m a p s j a v a . u t i l . C

  • l

l e c t i

  • n

s : m a n y u s e f u l f u n c t i

  • n

s d e fj n e d i n a g e n e r i c w a y , e . g . , b i n a r y s e a r c h , s

  • r

t i n g , c

  • p

y i n g , s h u ffm i n g , e t c .

slide-14
SLIDE 14

J a v a c

  • l

l e c t i

  • n

s f r a m e w

  • r

k – s u m m a r y

A l a r g e n u m b e r

  • f

d a t a s t r u c t u r e s , b u t

  • r

g a n i s e d i n t

  • r

e l a t i v e l y f e w i n t e r f a c e s ( C

  • l

l e c t i

  • n

, L i s t , e t c . )

  • D

e p e n d i n g

  • n

t h e d a t a s t r u c t u r e , n

  • t

a l l

  • p

e r a t i

  • n

s m a y b e s u p p

  • r

t e d e ffj c i e n t l y – y

  • u

s h

  • u

l d b e a w a r e

  • f

w h i c h

  • p

e r a t i

  • n

s e a c h c l a s s e ffj c i e n t l y s u p p

  • r

t s

I t e r a t

  • r

s a r e v e r y h a n d y , u s e t h e m !

  • L

i s t a n d C

  • l

l e c t i

  • n

b

  • t

h e x t e n d I t e r a b l e

T

  • fj

n d y

  • u

r a w a y a r

  • u

n d a p a r t i c u l a r d a t a s t r u c t u r e c l a s s :

  • L
  • k

a t w h a t i n t e r f a c e s i t i m p l e m e n t s a n d w h a t t h e y a r e f

  • r
  • L
  • k

f

  • r
  • p

e r a t i

  • n

s t h a t r e t u r n i n t e r e s t i n g v i e w s

  • n

t h e d a t a s t r u c t u r e

slide-15
SLIDE 15

Wh a t w e h a v e n ’ t h a d t i me f

  • r

( n

  • t
  • n

e x a m )

slide-16
SLIDE 16

S p l a y t r e e s

S p l a y t r e e s a r e a b a l a n c e d B S T h a v i n g a m

  • r

t i s e d O ( l

  • g

n ) c

  • m

p l e x i t y

  • Tie

m a i n

  • p

e r a t i

  • n

: s p l a y i n g m

  • v

e s a g i v e n n

  • d

e t

  • t

h e r

  • t
  • f

t h e t r e e ( b y d

  • i

n g r

  • t

a t i

  • n

s )

  • I

n s e r t i

  • n

: u s e B S T i n s e r t i

  • n

a n d s p l a y t h e n e w n

  • d

e

  • L
  • k

u p : u s e B S T l

  • k

u p a n d s p l a y t h e c l

  • s

e s t n

  • d

e

  • D

e l e t i

  • n

: t

  • t

a l l y d i fg e r e n t ( a n d m u c h s i m p l e r ) a l g

  • r

i t h m t h a n f

  • r

B S T s !

I t t u r n s

  • u

t t h a t s p l a y i n g a f t e r e v e r y

  • p

e r a t i

  • n

k e e p s t h e t r e e b a l a n c e d e n

  • u

g h B e c a u s e l

  • k

u p d

  • e

s s p l a y i n g , f r e q u e n t l y

  • u

s e d v a l u e s a r e q u i c k e r t

  • a

c c e s s ! C

  • m

p l e x i t y r e q u i r e s a m

  • r

t i s e d c

  • m

p l e x i t y a n a l y s i s

slide-17
SLIDE 17

A m

  • r

t i s e d c

  • m

p l e x i t y a n a l y s i s

A m

  • r

t i s e d c

  • m

p l e x i t y a n a l y s i s : h

  • w

t

  • c

a l c u l a t e t h e a m

  • r

t i s e d c

  • m

p l e x i t y

  • f

a d a t a s t r u c t u r e ?

  • I

n p r i n c i p l e : c a l c u l a t e t h e c

  • s

t

  • f

a n y s e q u e n c e

  • f
  • p

e r a t i

  • n

s

  • W

e d i d t h i s f

  • r

d y n a m i c a r r a y s b u t f

  • r

m

  • r

e c

  • m

p l e x s t r u c t u r e s i t g e t s t

  • h

a r d

O n e a p p r

  • a

c h : t h e b a n k e r ’ s m e t h

  • d
  • I

m a g i n e y

  • u

r d a t a s t r u c t u r e a s a c

  • i

n

  • p

e r a t e d m a c h i n e – e v e r y t i m e y

  • u

p u t i n a c

  • i

n i t e x e c u t e s

  • n

e i n s t r u c t i

  • n
  • I

n “ n

  • r

m a l ” d a t a s t r u c t u r e s , n u m b e r

  • f

c

  • i

n s c h a r g e d f

  • r

e a c h

  • p

e r a t i

  • n

= n u m b e r

  • f

i n s t r u c t i

  • n

s e x e c u t e d = c

  • m

p l e x i t y

  • I

n t h e b a n k e r ’ s m e t h

  • d

, w e c h a r g e e x t r a c

  • i

n s f

  • r

t h e c h e a p

  • p

e r a t i

  • n

s , a n d s a v e t h e m u p f

  • r

l a t e r

  • Wh

e n w e g e t t

  • a

n e x p e n s i v e

  • p

e r a t i

  • n

, w e u s e t h e s a v e d u p c

  • i

n s t

  • p

a y f

  • r

i t

  • F
  • r

d y n a m i c a r r a y s , i f w e c h a r g e a n e x t r a c

  • i

n f

  • r

e a c h a d d

  • p

e r a t i

  • n

, w e s a v e u p e n

  • u

g h c

  • i

n s t

  • p

a y f

  • r

t h e r e s i z e – w e c a n a l w a y s c h a r g e a c

  • n

s t a n t n u m b e r

  • f

c

  • i

n s p e r a d d a n d t h i s m e a n s w e h a v e a m

  • r

t i s e d O ( 1 ) c

  • m

p l e x i t y

I n t e r e s t i n g d a t a s t r u c t u r e s : s p l a y t r e e s , s k e w h e a p s

slide-18
SLIDE 18

P r

  • b

a b i l i s t i c c

  • m

p l e x i t y

S

  • m

e d a t a s t r u c t u r e s r e l y

  • n

r a n d

  • m

c h

  • i

c e

  • S

k i p l i s t s , f

  • r

e x a m p l e

  • W

e s a w t h a t h a s h t a b l e s c a n b e m

  • d

e l l e d t h i s w a y

Tie r e a r e m

  • r

e d a t a s t r u c t u r e s l i k e t h i s :

  • T

r e a p : b i n a r y s e a r c h t r e e w h e r e t h e s h a p e i s r a n d

  • m
  • R

a n d

  • m

i s e d m e l d a b l e h e a p

T y p i c a l l y , y

  • u

c a n g e t f a s t , s i m p l e d a t a s t r u c t u r e s – b u t a n a l y s i n g t h e p e r f

  • r

m a n c e ( i . e . t e l l i n g w h e t h e r i t i s f a s t ) i s d i ffj c u l t !

slide-19
SLIDE 19

R e a l

  • w
  • r

l d p e r f

  • r

m a n c e

C

  • n

s t a n t f a c t

  • r

s a r e i m p

  • r

t a n t ! P e r h a p s t h e m

  • s

t i m p

  • r

t a n t f a c t

  • r

: t h e p r

  • c

e s s

  • r

’ s c a c h e

  • I

t t a k e s a b

  • u

t 2 c l

  • c

k c y c l e s f

  • r

t h e p r

  • c

e s s

  • r

t

  • r

e a d d a t a f r

  • m

m e m

  • r

y

  • B

u t r e c e n t l y

  • a

c c e s s e d p a r t s

  • f

m e m

  • r

y a r e s t

  • r

e d i n t h e p r

  • c

e s s

  • r

’ s c a c h e , a f a s t m e m

  • r

y

  • f

~ 3 2 K B , a n d t a k e

  • n

l y ~ 1 c l

  • c

k c y c l e t

  • r

e a d !

I f y

  • u

r p r

  • g

r a m a c c e s s e s t h e s a m e

  • r

n e a r b y m e m

  • r

y l

  • c

a t i

  • n

s f r e q u e n t l y ( g

  • d

l

  • c

a l i t y ) , i t w i l l r u n m u c h f a s t e r b e c a u s e m u c h

  • f

t h e d a t a i t r e a d s w i l l b e i n t h e c a c h e

  • A

r r a y s h a v e m u c h b e t t e r l

  • c

a l i t y t h a n l i n k e d l i s t s : t h e i r e l e m e n t s a r e s t

  • r

e d c

  • n

t i g u

  • u

s l y i n m e m

  • r

y

  • A

c c e s s i n g t h e e l e m e n t s

  • f

a n a r r a y i n a l i n e a r

  • r

d e r i s e s p e c i a l l y g

  • d

– t h e p r

  • c

e s s

  • r

h a s s p e c i a l c i r c u i t r y t

  • d

e t e c t t h i s , a n d w i l l s t a r t p r e f e t c h i n g t h e a r r a y , r e a d i n g e l e m e n t s i n t

  • t

h e c a c h e b e f

  • r

e y

  • u

r p r

  • g

r a m a s k s f

  • r

t h e m

  • Q

u i c k s

  • r

t a n d m e r g e s

  • r

t h a v e m u c h b e t t e r l

  • c

a l i t y t h a n h e a p s

  • r

t

slide-20
SLIDE 20

R e a l

  • w
  • r

l d p e r f

  • r

m a n c e

“ L a t e n c y n u m b e r s e v e r y p r

  • g

r a m m e r s h

  • u

l d k n

  • w

” :

  • h

t t p s : / / d z

  • n

e . c

  • m

/ a r t i c l e s / e v e r y

  • p

r

  • g

r a m m e r

  • s

h

  • u

l d

  • k

n

  • w
  • L

1 c a c h e r e f e r e n c e ( ~ r e a d i n g

  • n

e v a r i a b l e ) , . 5 n s

  • S

e n d i n g a p a c k e t U S A E u r

  • p

e U S A , 1 5 m s → →

M u l t i p l y t h e t i m e s b y a b i l l i

  • n

t

  • g

e t “ h u m a n s c a l e ” :

  • R

e a d i n g f r

  • m

L 1 c a c h e , . 5 s (

  • n

e h e a r t b e a t )

  • R

e a d i n g f r

  • m

m a i n m e m

  • r

y , 1 s

  • R

e a d i n g f r

  • m

S S D , 1 . 7 d a y s

  • R

e a d i n g f r

  • m

h a r d d r i v e , 4 m

  • n

t h s !

  • S

e n d a p a c k e t U S A E u r

  • p

e U S A , 5 y e a r s ! → →

P r

  • c

e s s

  • r

s a r e f a s t , c

  • m

m u n i c a t i

  • n

( w i t h n e t w

  • r

k s , w i t h h a r d d r i v e s , e v e n w i t h R A M ) i s s l

  • w

!

slide-21
SLIDE 21

S u mmi n g u p

slide-22
SLIDE 22

B a s i c A D T s

M a p s : m a i n t a i n a k e y / v a l u e r e l a t i

  • n

s h i p

  • A

n a r r a y i s a s

  • r

t

  • f

m a p w h e r e t h e k e y s a r e a r r a y i n d i c e s

S e t s : l i k e a m a p b u t w i t h

  • n

l y k e y s , n

  • v

a l u e s Q u e u e : a d d t

  • n

e e n d , r e m

  • v

e f r

  • m

t h e

  • t

h e r S t a c k : a d d a n d r e m

  • v

e f r

  • m

t h e s a m e e n d D e q u e : a d d a n d r e m

  • v

e f r

  • m

e i t h e r e n d P r i

  • r

i t y q u e u e : a d d , r e m

  • v

e m i n i m u m

slide-23
SLIDE 23

I m p l e m e n t i n g m a p s a n d s e t s

A h a s h t a b l e

  • V

e r y f a s t i f y

  • u

c h

  • s

e a g

  • d

h a s h f u n c t i

  • n

: O ( 1 )

  • U

n

  • r

d e r e d : c a n ’ t b e u s e d t

  • ,

e . g . , fj n d a l l v a l u e s i n a g i v e n r a n g e

A b i n a r y s e a r c h t r e e

  • G
  • d

p e r f

  • r

m a n c e i f y

  • u

c a n k e e p i t b a l a n c e d : O ( l

  • g

n )

A s k i p l i s t

  • Q

u i t e s i m p l e t

  • i

m p l e m e n t

  • P

r

  • b

a b i l i s t i c p e r f

  • r

m a n c e

  • n

l y , t y p i c a l l y : O ( l

  • g

n )

A t r i e

  • r

r a d i x t r e e

  • T

r i e s : v e r y s i m p l e t

  • i

m p l e m e n t , O ( w ) p e r f

  • r

m a n c e

  • R

a d i x t r e e s : a b i t h a r d e r , b u t O ( m i n ( w , l

  • g

n ) ) p e r f

  • r

m a n c e

  • B

u t

  • n

l y f

  • r

c e r t a i n t y p e s

  • f

i n p u t d a t a

slide-24
SLIDE 24

I m p l e m e n t i n g q u e u e s , s t a c k s , p r i

  • r

i t y q u e u e s

S t a c k s :

  • a

d y n a m i c a r r a y

  • a

l i n k e d l i s t

Q u e u e s :

  • a

c i r c u l a r a r r a y

  • a

l i n k e d l i s t

P r i

  • r

i t y q u e u e s :

  • a

b i n a r y h e a p

  • a

l e f t i s t h e a p

slide-25
SLIDE 25

G r a p h s

V e r y u s e f u l i n m

  • d

e l l i n g m a n y p r

  • b

l e m s

  • A

w i d e v a r i e t y : d i r e c t e d , u n d i r e c t e d , w e i g h t e d , u n w e i g h t e d , c y c l i c , a c y c l i c

B u t c a n b e a b i t h a r d t

  • p

r

  • g

r a m w i t h

  • P

r

  • b

l e m : h

  • w

t

  • r

e p r e s e n t ? S

  • l

u t i

  • n

( u s u a l l y ) : a d j a c e n c y l i s t

  • P

r

  • b

l e m : w a n t t

  • v

i s i t n

  • d

e s , b u t

  • n

l y v i s i t i n g e a c h n

  • d

e

  • n

c e . S

  • l

u t i

  • n

: D F S

  • P

r

  • b

l e m : w a n t t

  • v

i s i t n

  • d

e s , b u t

  • n

l y v i s i t i n g e a c h n

  • d

e a f t e r i t s n e i g h b

  • u

r s . S

  • l

u t i

  • n

: t

  • p
  • l
  • g

i c a l s

  • r

t

  • P

r

  • b

l e m : g r a p h i s c y c l i c . S

  • l

u t i

  • n

: S C C s t e l l y

  • u

w h e r e t h e c y c l e s a r e

slide-26
SLIDE 26

S

  • r

t i n g

S

  • r

t e d l i s t s a r e a v e r y l

  • w
  • t

e c h d a t a s t r u c t u r e

  • C

r e a t e b y u s i n g a s

  • r

t i n g a l g

  • r

i t h m

  • C

a n u s e b i n a r y s e a r c h t

  • fj

n d v a l u e s e ffj c i e n t l y

B u t t h e y a r e s u r p r i s i n g l y u s e f u l

  • S
  • r

t e d l i s t s c a n i m p l e m e n t r e a d

  • n

l y s e t s

  • r

m a p s

  • O

r a p r i

  • r

i t y q u e u e , i f y

  • u

d

  • n

’ t n e e d t

  • i

n t e r l e a v e a d d s a n d r e m

  • v

e M i n s

  • Y
  • u

c a n a l s

  • d
  • r

a n g e q u e r i e s ( “ a l l v a l u e s b e t w e e n x a n d y ” ) ,

  • r

p r e fj x q u e r i e s l i k e i n t r i e s

slide-27
SLIDE 27

W h a t w e h a v e s t u d i e d

Tie d a t a s t r u c t u r e s a n d A D T s a b

  • v

e + a l g

  • r

i t h m s t h a t w

  • r

k

  • n

t h e s e d a t a s t r u c t u r e s ( s

  • r

t i n g , D i j k s t r a ' s , e t c . ) + c

  • m

p l e x i t y + d a t a s t r u c t u r e d e s i g n ( i n v a r i a n t s , e t c . ) Y

  • u

c a n a p p l y t h e s e i d e a s t

  • y
  • u

r

  • w

n p r

  • g

r a m s , d a t a s t r u c t u r e s , a l g

  • r

i t h m s e t c .

  • U

s i n g a p p r

  • p

r i a t e d a t a s t r u c t u r e s t

  • s

i m p l i f y y

  • u

r p r

  • g

r a m s + m a k e t h e m f a s t e r

  • T

a k i n g i d e a s f r

  • m

e x i s t i n g d a t a s t r u c t u r e s w h e n y

  • u

n e e d t

  • b

u i l d y

  • u

r

  • w

n

slide-28
SLIDE 28

D a t a s t r u c t u r e d e s i g n

H

  • w

t

  • d

e s i g n y

  • u

r

  • w

n d a t a s t r u c t u r e s ?

  • Tii

s t a k e s p r a c t i c e !

S t u d y

  • t

h e r p e

  • p

l e ' s i d e a s :

  • h

t t p : / / e n . w i k i p e d i a .

  • r

g / w i k i / L i s t _

  • f

_ d a t a _ s t r u c t u r e s

  • B
  • k

: P r

  • g

r a m m i n g P e a r l s

  • B
  • k

: Tie A l g

  • r

i t h m D e s i g n M a n u a l

  • S

t u d y y

  • u

r f a v

  • u

r i t e l a n g u a g e ' s s t a n d a r d l i b r a r y

slide-29
SLIDE 29

D a t a s t r u c t u r e d e s i g n

F i r s t , i d e n t i f y w h a t

  • p

e r a t i

  • n

s t h e d a t a s t r u c t u r e m u s t s u p p

  • r

t

  • O

f t e n t h e r e ' s a n e x i s t i n g d a t a s t r u c t u r e y

  • u

c a n u s e

  • O

r p e r h a p s y

  • u

c a n a d a p t a n e x i s t i n g

  • n

e ? Tie n d e c i d e

  • n

:

  • A

r e p r e s e n t a t i

  • n

( t r e e , a r r a y , e t c . )

  • A

n i n v a r i a n t Tie s e h

  • p

e f u l l y d r i v e t h e r e s t

  • f

t h e d e s i g n !

slide-30
SLIDE 30

D a t a s t r u c t u r e d e s i g n

F i n a l l y , r e m e m b e r t h e F i r s t a n d S e c

  • n

d R u l e s

  • f

P r

  • g

r a m O p t i m i s a t i

  • n

:

1 . D

  • n

’ t d

  • i

t . 2 . ( F

  • r

e x p e r t s

  • n

l y ! ) : D

  • n

’ t d

  • i

t y e t .

K e e p t h i n g s s i m p l e !

  • N
  • p
  • i

n t

  • p

t i m i s i n g y

  • u

r a l g

  • r

i t h m s t

  • h

a v e O ( l

  • g

n ) c

  • m

p l e x i t y i f i t t u r n s

  • u

t n ≤ 1

  • P

r

  • fj

l e y

  • u

r p r

  • g

r a m t

  • fj

n d t h e b

  • t

t l e n e c k s a r e

  • U

s e b i g

  • O

c

  • m

p l e x i t y t

  • g

e t a h a n d l e

  • n

p e r f

  • r

m a n c e b e f

  • r

e y

  • u

s t a r t i m p l e m e n t i n g i t

  • D

e c i d e w h i c h

  • p

e r a t i

  • n

s n e e d t

  • b

e e ffj c i e n t a n d u s e t h a t t

  • p

i c k

  • u

t t h e c

  • r

r e c t d a t a s t r u c t u r e

slide-31
SLIDE 31

P r

  • g

r a m m i n g P e a r l s

A c l a s s i c c

  • m

p u t e r s c i e n c e b

  • k

– b e a u t i f u l s

  • l

u t i

  • n

s t

  • v

a r i

  • u

s p r

  • g

r a m m i n g p r

  • b

l e m s , m a n y

  • f

t h e m i n v

  • l

v i n g d a t a s t r u c t u r e s A f u n r e a d , n

  • t

a t e x t b

  • k

I t h e l p e d m e l e a r n h

  • w

t

  • t

h i n k l i k e a c

  • m

p u t e r s c i e n t i s t A g r e a t b

  • k

t

  • r

e a d n e x t !

slide-32
SLIDE 32

T h e A l g

  • r

i t h m D e s i g n M a n u a l

A n e x c e l l e n t b

  • k

a b

  • u

t h

  • w

t

  • u

s e a l g

  • r

i t h m s a n d d a t a s t r u c t u r e s t

  • s
  • l

v e r e a l p r

  • g

r a m m i n g p r

  • b

l e m s L

  • t

s

  • f

g

  • d

t e c h n i q u e s , a n d a d v i c e

  • n

w h e n t

  • u

s e t h e m “ W a r s t

  • r

i e s ” : a c t u a l p r

  • b

l e m s t h a t t h e a u t h

  • r

n e e d e d t

  • s
  • l

v e , f a l s e s t a r t s , h

  • w

t h e y w e r e e v e n t u a l l y s

  • l

v e d E

  • b
  • k

a v a i l a b l e f r

  • m

t h e C h a l m e r s l i b r a r y !

slide-33
SLIDE 33

T h e e x a m

slide-34
SLIDE 34

T h e e x a m T u e s d a y 1 5

t h

  • f

J a n u a r y , 1 4 : – 1 8 : , H ö r s a l s v ä g e n ( r e

  • e

x a m s A p r i l , A u g u s t )

slide-35
SLIDE 35

T h e e x a m

Tie e x a m i s i n E n g l i s h , a n d y

  • u

c a n b r i n g a c h e a t s h e e t ,

  • n

e h a n d w r i t t e n A 4 p i e c e

  • f

p a p e r 6 q u e s t i

  • n

s ; y

  • u

g e t a 3 , 4

  • r

5 (

  • r

U ) f

  • r

e a c h q u e s t i

  • n
  • M

a n y q u e s t i

  • n

s a r e j u s t r i g h t

  • r

w r

  • n

g , w h e r e r i g h t = 5 , w r

  • n

g = U

  • F
  • r

s

  • m

e q u e s t i

  • n

s , y

  • u

m a y h a v e t

  • e

. g . a n s w e r a n e x t r a p a r t t

  • g

e t a 5

  • n

t h a t q u e s t i

  • n

– t h i s w i l l b e c l e a r l y s t a t e d

Tie g r a d e y

  • u

g e t i s d e c i d e d a s f

  • l

l

  • w

s :

  • T
  • g

e t g r a d e 3

  • n

t h e e x a m , y

  • u

m u s t g e t a 3

  • n

3 q u e s t i

  • n

s

  • T
  • g

e t g r a d e 4

  • n

t h e e x a m , y

  • u

m u s t g e t a 4

  • n

4 q u e s t i

  • n

s

  • T
  • g

e t g r a d e 5

  • n

t h e e x a m , y

  • u

m u s t g e t a 5

  • n

5 q u e s t i

  • n

s

B e s t p r e p a r a t i

  • n

: d

  • t

h e e x e r c i s e s , m a k e s u r e y

  • u

u n d e r s t a n d t h e l a b s , l

  • k

a t t h e

  • l

d e x a m s

  • O

l d e x a m s a r e i n S w e d i s h , b u t I w i l l u p l

  • a

d E n g l i s h t r a n s l a t i

  • n

s

  • f

s e l e c t e d q u e s t i

  • n

s – D I T 9 6 / D I T 9 6 1 e x a m q u e s t i

  • n

s a r e i n E n g l i s h a n d u s e f u l t

  • l
  • k

a t ( l i n k e d f r

  • m

t h e c

  • u

r s e w e b s i t e )

Wh a t y

  • u

n e e d t

  • k

n

  • w

: t h e f

  • l

l

  • w

i n g !

slide-36
SLIDE 36

D a t a s t r u c t u r e s

D y n a m i c a r r a y s Q u e u e , s t a c k a n d d e q u e i m p l e m e n t a t i

  • n

s B i n a r y s e a r c h t r e e s , A V L t r e e s , 2

  • 3

t r e e s , A A t r e e s

  • n
  • t

d e l e t i

  • n

f

  • r

A V L , 2

  • 3
  • r

A A t r e e s – b u t s t i l l f

  • r

p l a i n B S T s !

  • n
  • t

B

  • t

r e e s

B i n a r y h e a p s , l e f t i s t h e a p s L i n k e d l i s t s , s k i p l i s t s H a s h t a b l e s

  • R

e h a s h i n g , l i n e a r p r

  • b

i n g , l i n e a r c h a i n i n g

  • H

a s h f u n c t i

  • n

s : t h e g

  • a

l

  • f

a g

  • d

h a s h f u n c t i

  • n

, b u t n

  • t

h

  • w

t

  • c
  • n

s t r u c t t h e m

T r i e s , r a d i x t r e e s , s u ffj x t r e e s G r a p h s ( w e i g h t e d , u n w e i g h t e d , d i r e c t e d , u n d i r e c t e d )

  • R

e p r e s e n t e d u s i n g a d j a c e n c y l i s t s

slide-37
SLIDE 37

A l g

  • r

i t h m s

Tie a l g

  • r

i t h m s t h a t a r e u s e d i n t h e i m p l e m e n t a t i

  • n
  • f

e a c h d a t a s t r u c t u r e G r a p h a l g

  • r

i t h m s :

  • b

r e a d t h

  • fj

r s t a n d d e p t h

  • fj

r s t s e a r c h

  • a

p p l i c a t i

  • n

s

  • f

D F S : t

  • p
  • l
  • g

i c a l s

  • r

t i n g , c h e c k i n g c

  • n

n e c t e d n e s s , S C C s

  • D

i j k s t r a ' s a n d P r i m ' s a l g

  • r

i t h m s

I n s e r t i

  • n

s

  • r

t , q u i c k s

  • r

t , m e r g e s

  • r

t , r a d i x s

  • r

t

  • S

t r a t e g i e s f

  • r

c h

  • s

i n g t h e p i v

  • t

i n q u i c k s

  • r

t – fj r s t e l e m e n t , m e d i a n

  • f
  • t

h r e e , r a n d

  • m

i s e d

slide-38
SLIDE 38

T h e

  • r

y

C

  • m

p l e x i t y a n d b i g

  • O

n

  • t

a t i

  • n
  • F
  • r

i t e r a t i v e a n d r e c u r s i v e f u n c t i

  • n

s – b a s i c a l l y , w h a t ' s i n t h e c

  • m

p l e x i t y h a n d

  • i

n

  • A

l s

  • f
  • r

f u n c t i

  • n

s t h a t u s e s t a n d a r d d a t a s t r u c t u r e s a n d a l g

  • r

i t h m s

D a t a s t r u c t u r e i n v a r i a n t s

slide-39
SLIDE 39

T y p i c a l k i n d s

  • f

q u e s t i

  • n

Wh a t i s t h e c

  • m

p l e x i t y

  • f

t h e f

  • l

l

  • w

i n g p r

  • g

r a m ? P e r f

  • r

m t h e f

  • l

l

  • w

i n g a l g

  • r

i t h m b y h a n d

  • e

. g . : P e r f

  • r

m D i j k s t r a ’ s a l g

  • r

i t h m

  • n

t h i s g r a p h

  • e

. g . i n s e r t a v a l u e i n t

  • t

h i s A A t r e e

D e s i g n a d a t a s t r u c t u r e t h a t s u p p

  • r

t s t h e f

  • l

l

  • w

i n g

  • p

e r a t i

  • n

s …

  • r

: I m p l e m e n t a n a l g

  • r

i t h m f

  • r

p e r f

  • r

m i n g t h e f

  • l

l

  • w

i n g t a s k

  • O

f t e n : g e t t i n g a 5 r e q u i r e s a b e t t e r t i m e c

  • m

p l e x i t y

  • Y
  • u

c a n u s u a l l y a n s w e r i n p s e u d

  • c
  • d

e : d

  • n

’ t n e e d t

  • s

p e l l

  • u

t e v e r y l a s t d e t a i l , b u t a p r

  • g

r a m m e r s h

  • u

l d b e a b l e t

  • t

u r n i t i n t

  • c
  • d

e w i t h

  • u

t h a v i n g t

  • t

h i n k

  • Y
  • u

c a n u s u a l l y u s e e x i s t i n g d a t a s t r u c t u r e s a n d a l g

  • r

i t h m s i n y

  • u

r s

  • l

u t i

  • n

s

S e e

  • l

d e x a m s f

  • r

e x a m p l e s !

slide-40
SLIDE 40

G

  • d

l u c k !