16a: Immutability / Graphs

ICS211, Spring 2013
Dr. Zach

(switch view)

Status

Exam 3 Results

Dangers of Mutability

public void setValue(int value) {
  if (value < 1 || value > 13) {
    throw new IllegalArgumentException("Value " + value +
        " out of valid 1 - 13 range.");
  }
  this.value = value;
}

public void setSuit(Suit suit) {
  this.suit = suit;
}

Mutable PlayingCard

Immutability and Lists/Arrays/Objects

Unchangeable CardProtector...

Unchangeable CardProtector...

Unchangeable CardProtector!

Immutability

Summary (last lecture + a half)

Graphs

Graphs

As a data structure

Adjacency Matrix

Digraph of 6 vertices

   A  B  C  D  E  F
A  F  F  T  F  F  F
B  T  F  T  F  T  F
C  F  T  F  F  F  F
D  F  T  F  F  F  F
E  T  T  F  F  T  F
F  F  F  F  F  F  F

Adjacency Matrix Options

Uni graph of 6 vertices

Adjacency List

A| -> C
B| -> A -> C -> E
C| -> B
D| -> B
E| -> B -> E
F|

Adjacency List Options

Graphs as ADTs

Graphs in your future

Job Interview Question #5

You own 25 horses but no watch, clock, or way of measuring time.
You want to know which of your horses is the fastest.
You have a track wide enough to run only 5 horses at a time in a race.
What is the fewest number of races you'd need to run to find the fastest horse?  6
What if you want to find the top two fastest horses? (min # of races?)  7
What if you want to find the top three fastest horses? (min # of races?)  7

Job Interview Question #6

How would you implement an LRU cache?

For next time...