CS61A Lecture 26 Amir Kamil and Hamilton Nguyen UC Berkeley March - - PowerPoint PPT Presentation

cs61a lecture 26
SMART_READER_LITE
LIVE PREVIEW

CS61A Lecture 26 Amir Kamil and Hamilton Nguyen UC Berkeley March - - PowerPoint PPT Presentation

CS61A Lecture 26 Amir Kamil and Hamilton Nguyen UC Berkeley March 22, 2013 Announcements HW9 out tonight, due 4/3 Ants extra credit due 4/3 See Piazza for submission instructions Data Structure Applications Data


slide-1
SLIDE 1

CS61A Lecture 26

Amir Kamil and Hamilton Nguyen UC Berkeley March 22, 2013

slide-2
SLIDE 2

 HW9 out tonight, due 4/3  Ants extra credit due 4/3

 See Piazza for submission instructions

Announcements

slide-3
SLIDE 3

   

Data Structure Applications

slide-4
SLIDE 4

The data structures we cover in 61A are used everywhere in CS

  

Data Structure Applications

slide-5
SLIDE 5

The data structures we cover in 61A are used everywhere in CS More about data structures in 61B

 

Data Structure Applications

slide-6
SLIDE 6

The data structures we cover in 61A are used everywhere in CS More about data structures in 61B Example: recursive lists (also called linked lists)

Data Structure Applications

slide-7
SLIDE 7

The data structures we cover in 61A are used everywhere in CS More about data structures in 61B Example: recursive lists (also called linked lists)

  • Operating systems

Data Structure Applications

slide-8
SLIDE 8

The data structures we cover in 61A are used everywhere in CS More about data structures in 61B Example: recursive lists (also called linked lists)

  • Operating systems
  • Interpreters and compilers

Data Structure Applications

slide-9
SLIDE 9

The data structures we cover in 61A are used everywhere in CS More about data structures in 61B Example: recursive lists (also called linked lists)

  • Operating systems
  • Interpreters and compilers
  • Anything that uses a queue

Data Structure Applications

slide-10
SLIDE 10

The data structures we cover in 61A are used everywhere in CS More about data structures in 61B Example: recursive lists (also called linked lists)

  • Operating systems
  • Interpreters and compilers
  • Anything that uses a queue

The Scheme programming language, which we will learn soon, uses recursive lists as its primary data structure

Data Structure Applications

slide-11
SLIDE 11

Example: Environments

Example: http://goo.gl/8DNY1

slide-12
SLIDE 12

Example: Environments

Example: http://goo.gl/8DNY1

Recursive List

slide-13
SLIDE 13

Example: Environments

Example: http://goo.gl/8DNY1

Recursive List

slide-14
SLIDE 14

Example: Environments

Example: http://goo.gl/8DNY1

Recursive List

slide-15
SLIDE 15

Example: Environments

Example: http://goo.gl/8DNY1

Recursive List

slide-16
SLIDE 16

Example: Environments

Example: http://goo.gl/8DNY1

Recursive List

slide-17
SLIDE 17

Example: Environments

Example: http://goo.gl/8DNY1

Recursive List

slide-18
SLIDE 18

Example: Environments

Example: http://goo.gl/8DNY1

Recursive List

slide-19
SLIDE 19

Example: Environments

Example: http://goo.gl/8DNY1

Recursive List

slide-20
SLIDE 20

Example: Environments

Example: http://goo.gl/8DNY1

Recursive List Rest is Empty

slide-21
SLIDE 21

Trees with Internal Node Values

Trees can have values at internal nodes as well as their leaves.

class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left self.right = right def fib_tree(n): if n == 1: return Tree(0) if n == 2: return Tree(1) left = fib_tree(n - 2) right = fib_tree(n - 1) return Tree(left.entry + right.entry, left, right)

slide-22
SLIDE 22

Implementing Sets

What we should be able to do with a set:

  • Membership testing: Is a value an element of a set?
  • Union: Return a set with all elements in set1 or set2
  • Intersection: Return a set with any elements in set1 and set2
  • Adjunction: Return a set with all elements in s and a value v

Union

1 3 4 2 3 5 1 3 4 2 5

Intersection

1 3 4 2 3 5 3

Adjunction

1 3 4 2 1 3 4 2

slide-23
SLIDE 23

Sets as Unordered Sequences

Proposal 1: A set is represented by a recursive list that contains no duplicate items This is how we implemented dictionaries

def empty(s): return s is Rlist.empty def set_contains(s, v): if empty(s): return False elif s.first == v: return True return set_contains(s.rest, v)

slide-24
SLIDE 24

Sets as Unordered Sequences

slide-25
SLIDE 25

Sets as Unordered Sequences

def adjoin_set(s, v):

slide-26
SLIDE 26

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v):

slide-27
SLIDE 27

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s

slide-28
SLIDE 28

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s)

slide-29
SLIDE 29

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s)

Time order of growth

slide-30
SLIDE 30

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s)

Time order of growth

slide-31
SLIDE 31

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s)

Time order of growth The size of the set

slide-32
SLIDE 32

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2):

Time order of growth The size of the set

slide-33
SLIDE 33

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v)

Time order of growth The size of the set

slide-34
SLIDE 34

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v) return filter_rlist(set1, f)

Time order of growth The size of the set

slide-35
SLIDE 35

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v) return filter_rlist(set1, f)

Time order of growth The size of the set

slide-36
SLIDE 36

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v) return filter_rlist(set1, f)

Time order of growth The size of the set Assume sets are the same size

slide-37
SLIDE 37

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v) return filter_rlist(set1, f) def union_set(set1, set2):

Time order of growth The size of the set Assume sets are the same size

slide-38
SLIDE 38

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v) return filter_rlist(set1, f) def union_set(set1, set2): f = lambda v: not set_contains(set2, v)

Time order of growth The size of the set Assume sets are the same size

slide-39
SLIDE 39

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v) return filter_rlist(set1, f) def union_set(set1, set2): f = lambda v: not set_contains(set2, v) set1_not_set2 = filter_rlist(set1, f)

Time order of growth The size of the set Assume sets are the same size

slide-40
SLIDE 40

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v) return filter_rlist(set1, f) def union_set(set1, set2): f = lambda v: not set_contains(set2, v) set1_not_set2 = filter_rlist(set1, f) return extend_rlist(set1_not_set2, set2)

Time order of growth The size of the set Assume sets are the same size

slide-41
SLIDE 41

Sets as Unordered Sequences

def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v) return filter_rlist(set1, f) def union_set(set1, set2): f = lambda v: not set_contains(set2, v) set1_not_set2 = filter_rlist(set1, f) return extend_rlist(set1_not_set2, set2)

Time order of growth The size of the set Assume sets are the same size

slide-42
SLIDE 42

Sets as Ordered Sequences

Proposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest

slide-43
SLIDE 43

Sets as Ordered Sequences

Proposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest Order of growth?

slide-44
SLIDE 44

Sets as Ordered Sequences

Proposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest Order of growth?

slide-45
SLIDE 45

Sets as Ordered Sequences

Proposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest Order of growth?

def set_contains2(s, v):

slide-46
SLIDE 46

Sets as Ordered Sequences

Proposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest Order of growth?

def set_contains2(s, v): if empty(s) or s.first > v:

slide-47
SLIDE 47

Sets as Ordered Sequences

Proposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest Order of growth?

def set_contains2(s, v): if empty(s) or s.first > v: return False

slide-48
SLIDE 48

Sets as Ordered Sequences

Proposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest Order of growth?

def set_contains2(s, v): if empty(s) or s.first > v: return False elif s.first == v:

slide-49
SLIDE 49

Sets as Ordered Sequences

Proposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest Order of growth?

def set_contains2(s, v): if empty(s) or s.first > v: return False elif s.first == v: return True

slide-50
SLIDE 50

Sets as Ordered Sequences

Proposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest Order of growth?

def set_contains2(s, v): if empty(s) or s.first > v: return False elif s.first == v: return True return set_contains(s.rest, v)

slide-51
SLIDE 51

Set Intersection Using Ordered Sequences

slide-52
SLIDE 52

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order.

slide-53
SLIDE 53

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

slide-54
SLIDE 54

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

slide-55
SLIDE 55

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2):

slide-56
SLIDE 56

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2): if empty(set1) or empty(set2):

slide-57
SLIDE 57

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty

slide-58
SLIDE 58

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first

slide-59
SLIDE 59

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2:

slide-60
SLIDE 60

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest)

slide-61
SLIDE 61

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest)

slide-62
SLIDE 62

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) elif e1 < e2:

slide-63
SLIDE 63

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) elif e1 < e2: return intersect_set2(set1.rest, set2)

slide-64
SLIDE 64

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) elif e1 < e2: return intersect_set2(set1.rest, set2) elif e2 < e1:

slide-65
SLIDE 65

Set Intersection Using Ordered Sequences

This algorithm assumes that elements are in order. Order of growth?

def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) elif e1 < e2: return intersect_set2(set1.rest, set2) elif e2 < e1: return intersect_set2(set1, set2.rest)

slide-66
SLIDE 66

Tree Sets

slide-67
SLIDE 67

Tree Sets

Proposal 3: A set is represented as a Tree. Each entry is:

slide-68
SLIDE 68

Tree Sets

Proposal 3: A set is represented as a Tree. Each entry is:

  • Larger than all entries in its left branch and
slide-69
SLIDE 69

Tree Sets

Proposal 3: A set is represented as a Tree. Each entry is:

  • Larger than all entries in its left branch and
  • Smaller than all entries in its right branch
slide-70
SLIDE 70

Tree Sets

Proposal 3: A set is represented as a Tree. Each entry is:

  • Larger than all entries in its left branch and
  • Smaller than all entries in its right branch

7 3 1 5 9 11

slide-71
SLIDE 71

Tree Sets

Proposal 3: A set is represented as a Tree. Each entry is:

  • Larger than all entries in its left branch and
  • Smaller than all entries in its right branch

7 3 1 5 9 11 7 3 1 5 9 11

slide-72
SLIDE 72

Tree Sets

Proposal 3: A set is represented as a Tree. Each entry is:

  • Larger than all entries in its left branch and
  • Smaller than all entries in its right branch

7 3 1 5 9 11 7 3 1 5 9 11 5 3 1 7 9 11

slide-73
SLIDE 73

Membership in Tree Sets

slide-74
SLIDE 74

Membership in Tree Sets

Set membership tests traverse the tree

slide-75
SLIDE 75

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
slide-76
SLIDE 76

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half
slide-77
SLIDE 77

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

def set_contains3(s, v):

slide-78
SLIDE 78

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

def set_contains3(s, v): if s is None:

slide-79
SLIDE 79

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

def set_contains3(s, v): if s is None: return False

slide-80
SLIDE 80

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

def set_contains3(s, v): if s is None: return False elif s.entry == v:

slide-81
SLIDE 81

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

def set_contains3(s, v): if s is None: return False elif s.entry == v: return True

slide-82
SLIDE 82

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v:

slide-83
SLIDE 83

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v)

slide-84
SLIDE 84

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v) elif s.entry > v:

slide-85
SLIDE 85

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v) elif s.entry > v: return set_contains3(s.left, v)

slide-86
SLIDE 86

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

5 3 1 7 9 11

def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v) elif s.entry > v: return set_contains3(s.left, v)

slide-87
SLIDE 87

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

5 3 1 7 9 11

def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v) elif s.entry > v: return set_contains3(s.left, v)

9

slide-88
SLIDE 88

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

5 3 1 7 9 11

def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v) elif s.entry > v: return set_contains3(s.left, v)

9

If 9 is in the set, it is in this branch

slide-89
SLIDE 89

Membership in Tree Sets

Set membership tests traverse the tree

  • The element is either in the left or right sub‐branch
  • By focusing on one branch, we reduce the set by about half

5 3 1 7 9 11

def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v) elif s.entry > v: return set_contains3(s.left, v)

9

If 9 is in the set, it is in this branch Order of growth?

slide-90
SLIDE 90

Adjoining to a Tree Set

slide-91
SLIDE 91

Adjoining to a Tree Set

5 3 1 7 9 11 8

slide-92
SLIDE 92

Adjoining to a Tree Set

5 3 1 7 9 11 8

Right!

slide-93
SLIDE 93

Adjoining to a Tree Set

5 3 1 7 9 11 8

Right!

slide-94
SLIDE 94

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8

Right!

slide-95
SLIDE 95

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8

Right! Left!

slide-96
SLIDE 96

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8 7 8

Right! Left!

slide-97
SLIDE 97

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8 7 8

Right! Left!

None None

slide-98
SLIDE 98

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8 7 8

Right! Left! Right!

None None

slide-99
SLIDE 99

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8 7 8

Right! Left! Right!

None None 8 None

slide-100
SLIDE 100

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8 7 8

Right! Left! Right!

None None 8 None

Stop!

slide-101
SLIDE 101

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8 7 8

Right! Left! Right!

None None 8 None

Stop!

slide-102
SLIDE 102

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8 7 8

Right! Left! Right!

None None 8 None

Stop!

8

slide-103
SLIDE 103

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8 7 8

Right! Left! Right!

None None 8 None

Stop!

8 7 8

slide-104
SLIDE 104

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8 7 8

Right! Left! Right!

None None 8 None

Stop!

8 7 8 7 9 11 8

slide-105
SLIDE 105

Adjoining to a Tree Set

5 3 1 7 9 11 8 7 9 11 8 7 8

Right! Left! Right!

None None 8 None

Stop!

8 7 8 7 9 11 8 5 3 1 7 9 11 8

slide-106
SLIDE 106

What Did I Leave Out?

slide-107
SLIDE 107

What Did I Leave Out?

Sets as ordered sequences:

slide-108
SLIDE 108

What Did I Leave Out?

Sets as ordered sequences:

  • Adjoining an element to a set
slide-109
SLIDE 109

What Did I Leave Out?

Sets as ordered sequences:

  • Adjoining an element to a set
  • Union of two sets
slide-110
SLIDE 110

What Did I Leave Out?

Sets as ordered sequences:

  • Adjoining an element to a set
  • Union of two sets

Sets as binary trees:

slide-111
SLIDE 111

What Did I Leave Out?

Sets as ordered sequences:

  • Adjoining an element to a set
  • Union of two sets

Sets as binary trees:

  • Intersection of two sets
slide-112
SLIDE 112

What Did I Leave Out?

Sets as ordered sequences:

  • Adjoining an element to a set
  • Union of two sets

Sets as binary trees:

  • Intersection of two sets
  • Union of two sets
slide-113
SLIDE 113

What Did I Leave Out?

Sets as ordered sequences:

  • Adjoining an element to a set
  • Union of two sets

Sets as binary trees:

  • Intersection of two sets
  • Union of two sets

That's homework 9!

slide-114
SLIDE 114

 Why things go wrong  What can we do about this

Social Implications / Programming Practices

slide-115
SLIDE 115

Therac‐25 Case Study

slide-116
SLIDE 116

Therac‐25 Case Study

slide-117
SLIDE 117

 Medical imaging

device

Therac‐25 Case Study

slide-118
SLIDE 118

   

Therac‐25 Case Study

slide-119
SLIDE 119

 What happened?   

Therac‐25 Case Study

slide-120
SLIDE 120

 What happened?  6 serious injuries  

Therac‐25 Case Study

slide-121
SLIDE 121

 What happened?  6 serious injuries  4 deaths 

Therac‐25 Case Study

slide-122
SLIDE 122

 What happened?  6 serious injuries  4 deaths  Otherwise effective – saved hundreds of lives

Therac‐25 Case Study

slide-123
SLIDE 123

  

 

Lesson to be learned

slide-124
SLIDE 124

 Social responsibility in engineering  

 

Lesson to be learned

slide-125
SLIDE 125

 Social responsibility in engineering  First real incident of fatal software failure 

 

Lesson to be learned

slide-126
SLIDE 126

 Social responsibility in engineering  First real incident of fatal software failure  Bigger issue

 No bad guys  Honestly believed there was nothing wrong

Lesson to be learned

slide-127
SLIDE 127

 Other engineering fields: clear sense of

degradation and decay

 Can software become brittle or fractured?

“Software Rot”

slide-128
SLIDE 128

  

A bigger picture

slide-129
SLIDE 129

All software is part of a bigger system

  

A bigger picture

slide-130
SLIDE 130

All software is part of a bigger system

 Software degrades because:

 Other piece of software changes  Hardware changes  Environment changes

A bigger picture

slide-131
SLIDE 131

Ex: Compatibility Issues

slide-132
SLIDE 132

 The makers of the Therac did not fully

understand the complexity of their software

 Complexity of constructs in other fields more

apparent

A bigger issue

slide-133
SLIDE 133

A “simple” program

slide-134
SLIDE 134

A “simple” program

slide-135
SLIDE 135

 This program can delete any file you can

A “simple” program

slide-136
SLIDE 136

   

Complexity in the Therac‐25

slide-137
SLIDE 137

 Abundant user interface issues   

Complexity in the Therac‐25

slide-138
SLIDE 138

 Abundant user interface issues  Cursor position and field entry  

Complexity in the Therac‐25

slide-139
SLIDE 139

 Abundant user interface issues  Cursor position and field entry  Default values 

Complexity in the Therac‐25

slide-140
SLIDE 140

 Abundant user interface issues  Cursor position and field entry  Default values  Too many error messages

Complexity in the Therac‐25

slide-141
SLIDE 141

Too many error messages

slide-142
SLIDE 142

Too many error messages

slide-143
SLIDE 143

 No atomic test‐and‐set  No hardware interlocks

(More) Complexity in the Therac‐25

slide-144
SLIDE 144

 Know your user  Fail‐Soft (or Fail‐Safe)  Audit Trail  Correctness from the start  Redundancy

How can we solve these things?

slide-145
SLIDE 145

Fail‐Soft (or Fail‐Safe)

def mutable_rlist(): def dispatch(message, value=None): nonlocal contents if message == 'first': return first(contents) if message == 'rest': return rest(contents) if message == 'len': return len_rlist(contents) ... return dispatch

slide-146
SLIDE 146

Fail‐Soft (or Fail‐Safe)

def mutable_rlist(): def dispatch(message, value=None): nonlocal contents if message == 'first': return first(contents) if message == 'rest': return rest(contents) if message == 'len': return len_rlist(contents) ... else: print('Unknown message') return dispatch

slide-147
SLIDE 147

 Edsger Dijkstra: “On the Cruelty of Really

Teaching Computing Sciences”

 CS students shouldn’t use computers  Rigorously prove correctness of their programs  Correctness proofs  Compilation (pre‐execution) analysis

Correctness from the start

slide-148
SLIDE 148

 Black box debugging  Glass box debugging  Don’t break what works  Golden rule of debugging…

On debugging

slide-149
SLIDE 149

“Debug by subtraction, not by

addition”

Prof. Brian Harvey

Golden rule of debugging