CSE 373: Master method, fjnishing sorts, intro to graphs Michael - - PowerPoint PPT Presentation

cse 373 master method fjnishing sorts intro to graphs
SMART_READER_LITE
LIVE PREVIEW

CSE 373: Master method, fjnishing sorts, intro to graphs Michael - - PowerPoint PPT Presentation

CSE 373: Master method, fjnishing sorts, intro to graphs Michael Lee Monday, Feb 12, 2018 1 The tree method: precise analysis Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does


slide-1
SLIDE 1

CSE 373: Master method, fjnishing sorts, intro to graphs

Michael Lee Monday, Feb 12, 2018

1

slide-2
SLIDE 2

The tree method: precise analysis

Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does each recursive level do?

  • 1. How many nodes are there on level i? (i

is “root” level)

  • 2. At some level i, how much work does a single node do?

(Ignoring subtrees)

  • 3. How many recursive levels are there?

How much work does the leaf level (base cases) do?

  • 1. How much work does a single leaf node do?
  • 2. How many leaf nodes are there?

2

slide-3
SLIDE 3

The tree method: precise analysis

Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does each recursive level do?

  • 1. How many nodes are there on level i? (i

is “root” level)

  • 2. At some level i, how much work does a single node do?

(Ignoring subtrees)

  • 3. How many recursive levels are there?

How much work does the leaf level (base cases) do?

  • 1. How much work does a single leaf node do?
  • 2. How many leaf nodes are there?

2

slide-4
SLIDE 4

The tree method: precise analysis

Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does each recursive level do?

  • 1. How many nodes are there on level i? (i

is “root” level)

  • 2. At some level i, how much work does a single node do?

(Ignoring subtrees)

  • 3. How many recursive levels are there?

How much work does the leaf level (base cases) do?

  • 1. How much work does a single leaf node do?
  • 2. How many leaf nodes are there?

2

slide-5
SLIDE 5

The tree method: precise analysis

Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does each recursive level do?

  • 1. How many nodes are there on level i? (i = 0 is “root” level)
  • 2. At some level i, how much work does a single node do?

(Ignoring subtrees)

  • 3. How many recursive levels are there?

How much work does the leaf level (base cases) do?

  • 1. How much work does a single leaf node do?
  • 2. How many leaf nodes are there?

2

slide-6
SLIDE 6

The tree method: precise analysis

Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does each recursive level do?

  • 1. How many nodes are there on level i? (i = 0 is “root” level)
  • 2. At some level i, how much work does a single node do?

(Ignoring subtrees)

  • 3. How many recursive levels are there?

How much work does the leaf level (base cases) do?

  • 1. How much work does a single leaf node do?
  • 2. How many leaf nodes are there?

2

slide-7
SLIDE 7

The tree method: precise analysis

Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does each recursive level do?

  • 1. How many nodes are there on level i? (i = 0 is “root” level)
  • 2. At some level i, how much work does a single node do?

(Ignoring subtrees)

  • 3. How many recursive levels are there?

How much work does the leaf level (base cases) do?

  • 1. How much work does a single leaf node do?
  • 2. How many leaf nodes are there?

2

slide-8
SLIDE 8

The tree method: precise analysis

Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does each recursive level do?

  • 1. How many nodes are there on level i? (i = 0 is “root” level)
  • 2. At some level i, how much work does a single node do?

(Ignoring subtrees)

  • 3. How many recursive levels are there?

How much work does the leaf level (base cases) do?

  • 1. How much work does a single leaf node do?
  • 2. How many leaf nodes are there?

2

slide-9
SLIDE 9

The tree method: precise analysis

Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does each recursive level do?

  • 1. How many nodes are there on level i? (i = 0 is “root” level)
  • 2. At some level i, how much work does a single node do?

(Ignoring subtrees)

  • 3. How many recursive levels are there?

How much work does the leaf level (base cases) do?

  • 1. How much work does a single leaf node do?
  • 2. How many leaf nodes are there?

2

slide-10
SLIDE 10

The tree method: precise analysis

Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does each recursive level do?

  • 1. How many nodes are there on level i? (i = 0 is “root” level)
  • 2. At some level i, how much work does a single node do?

(Ignoring subtrees)

  • 3. How many recursive levels are there?

How much work does the leaf level (base cases) do?

  • 1. How much work does a single leaf node do?
  • 2. How many leaf nodes are there?

2

slide-11
SLIDE 11

The tree method: precise analysis

Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does each recursive level do?

  • 1. How many nodes are there on level i? (i = 0 is “root” level)
  • 2. At some level i, how much work does a single node do?

(Ignoring subtrees)

  • 3. How many recursive levels are there?

How much work does the leaf level (base cases) do?

  • 1. How much work does a single leaf node do?
  • 2. How many leaf nodes are there?

2

slide-12
SLIDE 12

The tree method: precise analysis

n

n 2 n 4

. . .

1 1

. . .

1 1 n 4

. . .

1 1

. . .

1 1 n 2 n 4

. . .

1 1

. . .

1 1 n 4

. . .

1 1

. . .

1 1 1 node, n work per 2 nodes, n work per 4 nodes, n work per

i nodes, n i work per h nodes,

work per

  • 1. numNodes i

=

i

  • 2. workPerNode n i

= n

i

  • 3. numLevels n

= ?

  • 4. workPerLeafNode n

=

  • 5. numLeafNodes n

= ?

3

slide-13
SLIDE 13

The tree method: precise analysis

n

n 2 n 4

. . .

1 1

. . .

1 1 n 4

. . .

1 1

. . .

1 1 n 2 n 4

. . .

1 1

. . .

1 1 n 4

. . .

1 1

. . .

1 1 1 node, n work per 2 nodes, n work per 4 nodes, n work per

i nodes, n i work per h nodes,

work per

  • 1. numNodes(i)

= ?

  • 2. workPerNode(n, i)

= ?

  • 3. numLevels(n)

= ?

  • 4. workPerLeafNode(n) = ?
  • 5. numLeafNodes(n)

= ?

  • 1. numNodes i

=

i

  • 2. workPerNode n i

= n

i

  • 3. numLevels n

= ?

  • 4. workPerLeafNode n

=

  • 5. numLeafNodes n

= ?

3

slide-14
SLIDE 14

The tree method: precise analysis

n

n 2 n 4

. . .

1 1

. . .

1 1 n 4

. . .

1 1

. . .

1 1 n 2 n 4

. . .

1 1

. . .

1 1 n 4

. . .

1 1

. . .

1 1 1 node, n work per 2 nodes, n

2 work per

4 nodes, n

4 work per

2i nodes, n

i work per

2h nodes, 1 work per

  • 1. numNodes(i)

= ?

  • 2. workPerNode(n, i)

= ?

  • 3. numLevels(n)

= ?

  • 4. workPerLeafNode(n) = ?
  • 5. numLeafNodes(n)

= ?

  • 1. numNodes i

=

i

  • 2. workPerNode n i

= n

i

  • 3. numLevels n

= ?

  • 4. workPerLeafNode n

=

  • 5. numLeafNodes n

= ?

3

slide-15
SLIDE 15

The tree method: precise analysis

n

n 2 n 4

. . .

1 1

. . .

1 1 n 4

. . .

1 1

. . .

1 1 n 2 n 4

. . .

1 1

. . .

1 1 n 4

. . .

1 1

. . .

1 1 1 node, n work per 2 nodes, n

2 work per

4 nodes, n

4 work per

2i nodes, n

i work per

2h nodes, 1 work per

  • 1. numNodes(i)

= 2i

  • 2. workPerNode(n, i)

= n

2i

  • 3. numLevels(n)

= ?

  • 4. workPerLeafNode(n) = 1
  • 5. numLeafNodes(n)

= ?

3

slide-16
SLIDE 16

The tree method: precise analysis

How many levels are there, exactly? Is it log2(n)? Let’s try an example. Suppose we have T . What happens? T 4 2 1 1 2 1 1 Height is log . For this recursive function, num recursive levels is same as height. Important: total levels, counting base case, is height . Important: for other recursive functions, where base case doesn’t happen at n , num recursive levels might be difgerent then height.

4

slide-17
SLIDE 17

The tree method: precise analysis

How many levels are there, exactly? Is it log2(n)? Let’s try an example. Suppose we have T(4). What happens? 4 T T 4 2 1 1 2 1 1 Height is log . For this recursive function, num recursive levels is same as height. Important: total levels, counting base case, is height . Important: for other recursive functions, where base case doesn’t happen at n , num recursive levels might be difgerent then height.

4

slide-18
SLIDE 18

The tree method: precise analysis

How many levels are there, exactly? Is it log2(n)? Let’s try an example. Suppose we have T(4). What happens? 4 2 T(1) T(1) 2 T(1) T(1) 4 2 1 1 2 1 1 Height is log . For this recursive function, num recursive levels is same as height. Important: total levels, counting base case, is height . Important: for other recursive functions, where base case doesn’t happen at n , num recursive levels might be difgerent then height.

4

slide-19
SLIDE 19

The tree method: precise analysis

How many levels are there, exactly? Is it log2(n)? Let’s try an example. Suppose we have T(4). What happens? 4 2 1 1 2 1 1 Height is log . For this recursive function, num recursive levels is same as height. Important: total levels, counting base case, is height . Important: for other recursive functions, where base case doesn’t happen at n , num recursive levels might be difgerent then height.

4

slide-20
SLIDE 20

The tree method: precise analysis

How many levels are there, exactly? Is it log2(n)? Let’s try an example. Suppose we have T(4). What happens? 4 2 1 1 2 1 1 Height is log2(4) = 2. For this recursive function, num recursive levels is same as height. Important: total levels, counting base case, is height . Important: for other recursive functions, where base case doesn’t happen at n , num recursive levels might be difgerent then height.

4

slide-21
SLIDE 21

The tree method: precise analysis

How many levels are there, exactly? Is it log2(n)? Let’s try an example. Suppose we have T(4). What happens? 4 2 1 1 2 1 1 Height is log2(4) = 2. For this recursive function, num recursive levels is same as height. Important: total levels, counting base case, is height + 1. Important: for other recursive functions, where base case doesn’t happen at n , num recursive levels might be difgerent then height.

4

slide-22
SLIDE 22

The tree method: precise analysis

How many levels are there, exactly? Is it log2(n)? Let’s try an example. Suppose we have T(4). What happens? 4 2 1 1 2 1 1 Height is log2(4) = 2. For this recursive function, num recursive levels is same as height. Important: total levels, counting base case, is height + 1. Important: for other recursive functions, where base case doesn’t happen at n ≤ 1, num recursive levels might be difgerent then height.

4

slide-23
SLIDE 23

The tree method: precise analysis

We discovered:

  • 1. numNodes(i)

= 2i

  • 2. workPerNode(n, i)

= n

2i

  • 3. numLevels(n)

= log2(n)

  • 4. workPerLeafNode(n) = 1
  • 5. numLeafNodes(n)

= 2numLevels(n) = 2log2(n) = n Our formulas: recursiveWork

numLevels(n) i

numNodes i workPerNode n i baseCaseWork numLeafNodes n workPerLeafNode n totalWork recursiveWork baseCaseWork

5

slide-24
SLIDE 24

The tree method: precise analysis

We discovered:

  • 1. numNodes(i)

= 2i

  • 2. workPerNode(n, i)

= n

2i

  • 3. numLevels(n)

= log2(n)

  • 4. workPerLeafNode(n) = 1
  • 5. numLeafNodes(n)

= 2numLevels(n) = 2log2(n) = n Our formulas: recursiveWork =

numLevels(n)

  • i=0

numNodes(i) · workPerNode(n, i) baseCaseWork numLeafNodes n workPerLeafNode n totalWork recursiveWork baseCaseWork

5

slide-25
SLIDE 25

The tree method: precise analysis

We discovered:

  • 1. numNodes(i)

= 2i

  • 2. workPerNode(n, i)

= n

2i

  • 3. numLevels(n)

= log2(n)

  • 4. workPerLeafNode(n) = 1
  • 5. numLeafNodes(n)

= 2numLevels(n) = 2log2(n) = n Our formulas: recursiveWork =

numLevels(n)

  • i=0

numNodes(i) · workPerNode(n, i) baseCaseWork = numLeafNodes(n) · workPerLeafNode(n) totalWork recursiveWork baseCaseWork

5

slide-26
SLIDE 26

The tree method: precise analysis

We discovered:

  • 1. numNodes(i)

= 2i

  • 2. workPerNode(n, i)

= n

2i

  • 3. numLevels(n)

= log2(n)

  • 4. workPerLeafNode(n) = 1
  • 5. numLeafNodes(n)

= 2numLevels(n) = 2log2(n) = n Our formulas: recursiveWork =

numLevels(n)

  • i=0

numNodes(i) · workPerNode(n, i) baseCaseWork = numLeafNodes(n) · workPerLeafNode(n) totalWork = recursiveWork + baseCaseWork

5

slide-27
SLIDE 27

The tree method: precise analysis

Solve for recursive case: recursiveWork =

log2(n)

  • i=0

2i · n 2i

log n i

n n log n Solve for base case: baseCaseWork numLeafNodes n workDonePerLeafNode n n n So exact closed form is n log n n.

6

slide-28
SLIDE 28

The tree method: precise analysis

Solve for recursive case: recursiveWork =

log2(n)

  • i=0

2i · n 2i =

log2(n)

  • i=0

n n log n Solve for base case: baseCaseWork numLeafNodes n workDonePerLeafNode n n n So exact closed form is n log n n.

6

slide-29
SLIDE 29

The tree method: precise analysis

Solve for recursive case: recursiveWork =

log2(n)

  • i=0

2i · n 2i =

log2(n)

  • i=0

n = n log2(n) Solve for base case: baseCaseWork = numLeafNodes(n) · workDonePerLeafNode(n) = n · 1 = n So exact closed form is n log n n.

6

slide-30
SLIDE 30

The tree method: precise analysis

Solve for recursive case: recursiveWork =

log2(n)

  • i=0

2i · n 2i =

log2(n)

  • i=0

n = n log2(n) Solve for base case: baseCaseWork = numLeafNodes(n) · workDonePerLeafNode(n) = n · 1 = n So exact closed form is n log2(n) + n.

6

slide-31
SLIDE 31

The tree method: practice

Practice: Let’s go back to our old recurrence... S(n) =    2 if n ≤ 1 2S (n/3) + n2

  • therwise

7

slide-32
SLIDE 32

The tree method: practice

n2

n2 9 n4 81

. . .

2 2

. . .

2 2 n2 81

. . .

2 2

. . .

2 2 n2 9 n2 81

. . .

2 2

. . .

2 2 n2 81

. . .

2 2

. . .

2 2 1 node, n work per 2 nodes, n work per 4 nodes, n work per

i nodes, n

i work per

h nodes,

work per

  • 1. numNodes i

=

i

  • 2. workPerNode n i

= n

i

  • 3. numLevels n

= log n

  • 4. workPerLeafNode n

=

  • 5. numLeafNodes n

=

numLevels n log n

nlog

8

slide-33
SLIDE 33

The tree method: practice

n2

n2 9 n4 81

. . .

2 2

. . .

2 2 n2 81

. . .

2 2

. . .

2 2 n2 9 n2 81

. . .

2 2

. . .

2 2 n2 81

. . .

2 2

. . .

2 2 1 node, n work per 2 nodes, n work per 4 nodes, n work per

i nodes, n

i work per

h nodes,

work per

  • 1. numNodes(i)

= ?

  • 2. workPerNode(n, i)

= ?

  • 3. numLevels(n)

= ?

  • 4. workPerLeafNode(n) = ?
  • 5. numLeafNodes(n)

= ?

  • 1. numNodes i

=

i

  • 2. workPerNode n i

= n

i

  • 3. numLevels n

= log n

  • 4. workPerLeafNode n

=

  • 5. numLeafNodes n

=

numLevels n log n

nlog

8

slide-34
SLIDE 34

The tree method: practice

n2

n2 9 n4 81

. . .

2 2

. . .

2 2 n2 81

. . .

2 2

. . .

2 2 n2 9 n2 81

. . .

2 2

. . .

2 2 n2 81

. . .

2 2

. . .

2 2 1 node, n2 work per 2 nodes, n2

32 work per

4 nodes, n2

34 work per

2i nodes,

n2 32i work per

2h nodes, 1 work per

  • 1. numNodes(i)

= ?

  • 2. workPerNode(n, i)

= ?

  • 3. numLevels(n)

= ?

  • 4. workPerLeafNode(n) = ?
  • 5. numLeafNodes(n)

= ?

  • 1. numNodes i

=

i

  • 2. workPerNode n i

= n

i

  • 3. numLevels n

= log n

  • 4. workPerLeafNode n

=

  • 5. numLeafNodes n

=

numLevels n log n

nlog

8

slide-35
SLIDE 35

The tree method: practice

n2

n2 9 n4 81

. . .

2 2

. . .

2 2 n2 81

. . .

2 2

. . .

2 2 n2 9 n2 81

. . .

2 2

. . .

2 2 n2 81

. . .

2 2

. . .

2 2 1 node, n2 work per 2 nodes, n2

32 work per

4 nodes, n2

34 work per

2i nodes,

n2 32i work per

2h nodes, 1 work per

  • 1. numNodes(i)

= 2i

  • 2. workPerNode(n, i)

= n2

9i

  • 3. numLevels(n)

= log3(n)

  • 4. workPerLeafNode(n) = 2
  • 5. numLeafNodes(n)

= 2numLevels(n) = 2log3(n) = nlog3(2)

8

slide-36
SLIDE 36

The tree method: practice

  • 1. numNodes(i)

= 2i

  • 2. workPerNode(n, i)

= n2

9i

  • 3. numLevels(n)

= log3(n)

  • 4. workPerLeafNode(n) = 2
  • 5. numLeafNodes(n)

= 2numLevels(n) = 2log3(n) = nlog3(2) Combine into a single expression representing the total runtime. totalWork

log n i i

n

i

nlog n

log n i i i

nlog n

log n i i

nlog

9

slide-37
SLIDE 37

The tree method: practice

  • 1. numNodes(i)

= 2i

  • 2. workPerNode(n, i)

= n2

9i

  • 3. numLevels(n)

= log3(n)

  • 4. workPerLeafNode(n) = 2
  • 5. numLeafNodes(n)

= 2numLevels(n) = 2log3(n) = nlog3(2) Combine into a single expression representing the total runtime. totalWork =  

log3(n)

  • i=0

2i · n2 9i   + 2nlog3(2) n

log n i i i

nlog n

log n i i

nlog

9

slide-38
SLIDE 38

The tree method: practice

  • 1. numNodes(i)

= 2i

  • 2. workPerNode(n, i)

= n2

9i

  • 3. numLevels(n)

= log3(n)

  • 4. workPerLeafNode(n) = 2
  • 5. numLeafNodes(n)

= 2numLevels(n) = 2log3(n) = nlog3(2) Combine into a single expression representing the total runtime. totalWork =  

log3(n)

  • i=0

2i · n2 9i   + 2nlog3(2) = n2

log3(n)

  • i=0

2i 9i + 2nlog3(2) n

log n i i

nlog

9

slide-39
SLIDE 39

The tree method: practice

  • 1. numNodes(i)

= 2i

  • 2. workPerNode(n, i)

= n2

9i

  • 3. numLevels(n)

= log3(n)

  • 4. workPerLeafNode(n) = 2
  • 5. numLeafNodes(n)

= 2numLevels(n) = 2log3(n) = nlog3(2) Combine into a single expression representing the total runtime. totalWork =  

log3(n)

  • i=0

2i · n2 9i   + 2nlog3(2) = n2

log3(n)

  • i=0

2i 9i + 2nlog3(2) = n2

log3(n)

  • i=0

2 9 i + 2nlog3(2)

9

slide-40
SLIDE 40

The fjnite geometric series

We have: n2

log3(n)

  • i=0

2 9 i + 2nlog3(2) The fjnite geometric series identity:

n i

ri rn r Plug and chug: totalWork n

log n i i

nlog n

log n i i

nlog n

log n

nlog

10

slide-41
SLIDE 41

The fjnite geometric series

We have: n2

log3(n)

  • i=0

2 9 i + 2nlog3(2) The fjnite geometric series identity:

n−1

  • i=0

ri = 1 − rn 1 − r Plug and chug: totalWork n

log n i i

nlog n

log n i i

nlog n

log n

nlog

10

slide-42
SLIDE 42

The fjnite geometric series

We have: n2

log3(n)

  • i=0

2 9 i + 2nlog3(2) The fjnite geometric series identity:

n−1

  • i=0

ri = 1 − rn 1 − r Plug and chug: totalWork = n2

log3(n)

  • i=0

2 9 i + 2nlog3(2) = n2

log3(n)+1−1

  • i=0

2 9 i + 2nlog3(2) = n2 1 − 2

9

log3(n)+1 1 − 2

9

+ 2nlog3(2)

10

slide-43
SLIDE 43

Applying the fjnite geometric series

With a bunch of efgort... totalWork = n2 1 − 2

9

log3(n)+1 1 − 2

9

+ 2nlog3(2) = 9 7n2

  • 1 − 2

9 2 9 log3(n) + 2nlog3(2) = 9 7n2 − 2 7n2 2 9 log3(n) + 2nlog3(2) = 9 7n2 − 2 7n2nlog3(2/9) + 2nlog3(2) = 9 7n2 − 2 7n2nlog3(2)−2 + 2nlog3(2) = 9 7n2 − 2 7nlog3(2) + 2nlog3(2) = 9 7n2 + 12 7 nlog3(2)

11

slide-44
SLIDE 44

The master theorem

Is there an easier way? If we want to fjnd an exact closed form, no. Must use either the unfolding technique or the tree technique. If we want to fjnd a big- bound, yes.

12

slide-45
SLIDE 45

The master theorem

Is there an easier way? If we want to fjnd an exact closed form, no. Must use either the unfolding technique or the tree technique. If we want to fjnd a big- bound, yes.

12

slide-46
SLIDE 46

The master theorem

Is there an easier way? If we want to fjnd an exact closed form, no. Must use either the unfolding technique or the tree technique. If we want to fjnd a big-Θ bound, yes.

12

slide-47
SLIDE 47

The master theorem

The master theorem Suppose we have a recurrence of the following form: T(n) =    d if n = 1 aT n

b

  • + nc
  • therwise

Then... If logb a c, then T n nc If logb a c, then T n nc log n If logb a c, then T n nlogb a

13

slide-48
SLIDE 48

The master theorem

The master theorem Suppose we have a recurrence of the following form: T(n) =    d if n = 1 aT n

b

  • + nc
  • therwise

Then... ◮ If logb(a) < c, then T(n) ∈ Θ (nc) ◮ If logb(a) = c, then T(n) ∈ Θ (nc log(n)) ◮ If logb(a) > c, then T(n) ∈ Θ

  • nlogb(a)

13

slide-49
SLIDE 49

The master theorem

Given: T(n) =    d aT n

b

  • + nc

Then... If logb(a) < c, then T(n) ∈ Θ (nc) If logb(a) = c, then T(n) ∈ Θ (nc log(n)) If logb(a) > c, then T(n) ∈ Θ

  • nlogb(a)

Sanity check: try checking merge sort. We have a , b , and c . We know logb a log c, therefore merge sort is n log n . Sanity check: try checking S n S n n . We have a , b , and c . We know log c, therefore S n n .

14

slide-50
SLIDE 50

The master theorem

Given: T(n) =    d aT n

b

  • + nc

Then... If logb(a) < c, then T(n) ∈ Θ (nc) If logb(a) = c, then T(n) ∈ Θ (nc log(n)) If logb(a) > c, then T(n) ∈ Θ

  • nlogb(a)

Sanity check: try checking merge sort. We have a , b , and c . We know logb a log c, therefore merge sort is n log n . Sanity check: try checking S n S n n . We have a , b , and c . We know log c, therefore S n n .

14

slide-51
SLIDE 51

The master theorem

Given: T(n) =    d aT n

b

  • + nc

Then... If logb(a) < c, then T(n) ∈ Θ (nc) If logb(a) = c, then T(n) ∈ Θ (nc log(n)) If logb(a) > c, then T(n) ∈ Θ

  • nlogb(a)

Sanity check: try checking merge sort. We have a = 2, b = 2, and c = 1. We know logb(a) = log2(2) = 1 = c, therefore merge sort is Θ (n log(n)). Sanity check: try checking S n S n n . We have a , b , and c . We know log c, therefore S n n .

14

slide-52
SLIDE 52

The master theorem

Given: T(n) =    d aT n

b

  • + nc

Then... If logb(a) < c, then T(n) ∈ Θ (nc) If logb(a) = c, then T(n) ∈ Θ (nc log(n)) If logb(a) > c, then T(n) ∈ Θ

  • nlogb(a)

Sanity check: try checking merge sort. We have a = 2, b = 2, and c = 1. We know logb(a) = log2(2) = 1 = c, therefore merge sort is Θ (n log(n)). Sanity check: try checking S(n) = 2S(n/3) + n2. We have a , b , and c . We know log c, therefore S n n .

14

slide-53
SLIDE 53

The master theorem

Given: T(n) =    d aT n

b

  • + nc

Then... If logb(a) < c, then T(n) ∈ Θ (nc) If logb(a) = c, then T(n) ∈ Θ (nc log(n)) If logb(a) > c, then T(n) ∈ Θ

  • nlogb(a)

Sanity check: try checking merge sort. We have a = 2, b = 2, and c = 1. We know logb(a) = log2(2) = 1 = c, therefore merge sort is Θ (n log(n)). Sanity check: try checking S(n) = 2S(n/3) + n2. We have a = 2, b = 3, and c = 2. We know log3(2) ≤ 1 < 2 = c, therefore S(n) ∈ Θ

  • n2

.

14

slide-54
SLIDE 54

The master theorem: intuition

Intuition, the logb(a) < c case:

  • 1. We do work more rapidly then we divide.
  • 2. So, more of the work happens near the “top”, which means

that the nc term dominates.

15

slide-55
SLIDE 55

The master theorem: intuition

Intuition, the logb(a) > c case:

  • 1. We divide more rapidly then we do work.
  • 2. So, most of the work happens near the “bottom”, which

means the work done in the leaves dominates.

  • 3. Note: Work in leaves is about

d · aheight = d · alogb(n) = d · nlogb(a).

16

slide-56
SLIDE 56

The master theorem: intuition

Intuition, the logb(a) = c case:

  • 1. Work is done roughly equally throughout tree.
  • 2. Each level does about the same amount of work, so we

approximate by just multiplying work done on fjrst level by the height: nc logb(n).

17

slide-57
SLIDE 57

Parting thoughts on sorting

A few fjnal thoughts about sorting...

18

slide-58
SLIDE 58

Hybrid sorts

Problem: Quick sort, in the best case, is pretty fast – often a constant factor faster then merge sort. But in the worst case, it’s O

  • n2

! Idea: If things start looking bad, stop using quicksort! Switch to a difgerent sorting algorithm. Hybrid sort A sorting algorithm which combines two or more other sorting algorithms.

19

slide-59
SLIDE 59

Hybrid sorts

Problem: Quick sort, in the best case, is pretty fast – often a constant factor faster then merge sort. But in the worst case, it’s O

  • n2

! Idea: If things start looking bad, stop using quicksort! Switch to a difgerent sorting algorithm. Hybrid sort A sorting algorithm which combines two or more other sorting algorithms.

19

slide-60
SLIDE 60

Hybrid sorts

Problem: Quick sort, in the best case, is pretty fast – often a constant factor faster then merge sort. But in the worst case, it’s O

  • n2

! Idea: If things start looking bad, stop using quicksort! Switch to a difgerent sorting algorithm. Hybrid sort A sorting algorithm which combines two or more other sorting algorithms.

19

slide-61
SLIDE 61

Hybrid sort example: introsort

Example: Introsort

Core idea: Combine quick sort with heap sort (Why heap sort? It’s also O (n log(n)), and can be implemented in-place.)

  • 1. Run quicksort, but keep track of how many recursive calls

we’ve made

  • 2. If we pass some threshold (usually,

lg n )

2.1 Assume we’ve hit our worst case and switch to heapsort 2.2 Else continue using quick sort

Punchline: worst-case runtime is now n log n , not n . Used by various C++ implementations, used by Microsoft’s .NET framework.

20

slide-62
SLIDE 62

Hybrid sort example: introsort

Example: Introsort

Core idea: Combine quick sort with heap sort (Why heap sort? It’s also O (n log(n)), and can be implemented in-place.)

  • 1. Run quicksort, but keep track of how many recursive calls

we’ve made

  • 2. If we pass some threshold (usually, 2⌊lg(n)⌋)

2.1 Assume we’ve hit our worst case and switch to heapsort 2.2 Else continue using quick sort

Punchline: worst-case runtime is now n log n , not n . Used by various C++ implementations, used by Microsoft’s .NET framework.

20

slide-63
SLIDE 63

Hybrid sort example: introsort

Example: Introsort

Core idea: Combine quick sort with heap sort (Why heap sort? It’s also O (n log(n)), and can be implemented in-place.)

  • 1. Run quicksort, but keep track of how many recursive calls

we’ve made

  • 2. If we pass some threshold (usually, 2⌊lg(n)⌋)

2.1 Assume we’ve hit our worst case and switch to heapsort 2.2 Else continue using quick sort

Punchline: worst-case runtime is now n log n , not n . Used by various C++ implementations, used by Microsoft’s .NET framework.

20

slide-64
SLIDE 64

Hybrid sort example: introsort

Example: Introsort

Core idea: Combine quick sort with heap sort (Why heap sort? It’s also O (n log(n)), and can be implemented in-place.)

  • 1. Run quicksort, but keep track of how many recursive calls

we’ve made

  • 2. If we pass some threshold (usually, 2⌊lg(n)⌋)

2.1 Assume we’ve hit our worst case and switch to heapsort 2.2 Else continue using quick sort

Punchline: worst-case runtime is now O (n log(n)), not O

  • n2

. Used by various C++ implementations, used by Microsoft’s .NET framework.

20

slide-65
SLIDE 65

Hybrid sort example: introsort

Example: Introsort

Core idea: Combine quick sort with heap sort (Why heap sort? It’s also O (n log(n)), and can be implemented in-place.)

  • 1. Run quicksort, but keep track of how many recursive calls

we’ve made

  • 2. If we pass some threshold (usually, 2⌊lg(n)⌋)

2.1 Assume we’ve hit our worst case and switch to heapsort 2.2 Else continue using quick sort

Punchline: worst-case runtime is now O (n log(n)), not O

  • n2

. Used by various C++ implementations, used by Microsoft’s .NET framework.

20

slide-66
SLIDE 66

Adaptive sorts

Observation: Most real-world data is contains “mostly-sorted runs” – chunks of data that are already sorted. Idea: Modify our strategy based on what the input data actually looks like! Adaptive sort A sorting algorithm that adapts to patterns and pre-existing order in the input. Most adaptive sorts take advantage of how real-world data is

  • ften partially sorted.

21

slide-67
SLIDE 67

Adaptive sorts

Observation: Most real-world data is contains “mostly-sorted runs” – chunks of data that are already sorted. Idea: Modify our strategy based on what the input data actually looks like! Adaptive sort A sorting algorithm that adapts to patterns and pre-existing order in the input. Most adaptive sorts take advantage of how real-world data is

  • ften partially sorted.

21

slide-68
SLIDE 68

Adaptive sorts

Observation: Most real-world data is contains “mostly-sorted runs” – chunks of data that are already sorted. Idea: Modify our strategy based on what the input data actually looks like! Adaptive sort A sorting algorithm that adapts to patterns and pre-existing order in the input. Most adaptive sorts take advantage of how real-world data is

  • ften partially sorted.

21

slide-69
SLIDE 69

Adaptive sorts: Timsort

Example: Timsort

Core idea: Combine merge sort with insertion sort (Who’s Tim? A Python core developer)

  • 1. Works by looking for “sorted runs”.
  • 2. Will use insertion sort to merge two small runs and merge sort

to merge two large runs

  • 3. Implementation is very complex – lots of nuances, lots of

clever tricks (e.g. detecting runs that are in reverse sorted

  • rder, sometimes skipping elements)

Punchline: worst-case runtime is still n log n , but best case runtime is n . Used by Python and Java.

22

slide-70
SLIDE 70

Adaptive sorts: Timsort

Example: Timsort

Core idea: Combine merge sort with insertion sort (Who’s Tim? A Python core developer)

  • 1. Works by looking for “sorted runs”.
  • 2. Will use insertion sort to merge two small runs and merge sort

to merge two large runs

  • 3. Implementation is very complex – lots of nuances, lots of

clever tricks (e.g. detecting runs that are in reverse sorted

  • rder, sometimes skipping elements)

Punchline: worst-case runtime is still n log n , but best case runtime is n . Used by Python and Java.

22

slide-71
SLIDE 71

Adaptive sorts: Timsort

Example: Timsort

Core idea: Combine merge sort with insertion sort (Who’s Tim? A Python core developer)

  • 1. Works by looking for “sorted runs”.
  • 2. Will use insertion sort to merge two small runs and merge sort

to merge two large runs

  • 3. Implementation is very complex – lots of nuances, lots of

clever tricks (e.g. detecting runs that are in reverse sorted

  • rder, sometimes skipping elements)

Punchline: worst-case runtime is still n log n , but best case runtime is n . Used by Python and Java.

22

slide-72
SLIDE 72

Adaptive sorts: Timsort

Example: Timsort

Core idea: Combine merge sort with insertion sort (Who’s Tim? A Python core developer)

  • 1. Works by looking for “sorted runs”.
  • 2. Will use insertion sort to merge two small runs and merge sort

to merge two large runs

  • 3. Implementation is very complex – lots of nuances, lots of

clever tricks (e.g. detecting runs that are in reverse sorted

  • rder, sometimes skipping elements)

Punchline: worst-case runtime is still O (n log(n)), but best case runtime is O (n). Used by Python and Java.

22

slide-73
SLIDE 73

Adaptive sorts: Timsort

Example: Timsort

Core idea: Combine merge sort with insertion sort (Who’s Tim? A Python core developer)

  • 1. Works by looking for “sorted runs”.
  • 2. Will use insertion sort to merge two small runs and merge sort

to merge two large runs

  • 3. Implementation is very complex – lots of nuances, lots of

clever tricks (e.g. detecting runs that are in reverse sorted

  • rder, sometimes skipping elements)

Punchline: worst-case runtime is still O (n log(n)), but best case runtime is O (n). Used by Python and Java.

22

slide-74
SLIDE 74

Linear sorts

Linear sorts (aka ‘Niche’ sorts)

Basic idea: Can we do better then O (n log(n)) if we assume more things about the input list?

23

slide-75
SLIDE 75

Counting sort

Counting sort ◮ Assumption: Input is a list of ints where every item is between 0 and k ◮ Worst-case runtime: n k How would you implement this? Hint: start by creating a temp array of length k. Take inspiration from how hash tables work. The algorithm:

  • 1. Create a temp array named arr of length k.
  • 2. Loop through the input array. For each number num, run

arr[num] += 1

  • 3. The temp array will now contain how many times we say each

number in the input list.

  • 4. Iterate through it to create the output list.

24

slide-76
SLIDE 76

Counting sort

Counting sort ◮ Assumption: Input is a list of ints where every item is between 0 and k ◮ Worst-case runtime: n k How would you implement this? Hint: start by creating a temp array of length k. Take inspiration from how hash tables work. The algorithm:

  • 1. Create a temp array named arr of length k.
  • 2. Loop through the input array. For each number num, run

arr[num] += 1

  • 3. The temp array will now contain how many times we say each

number in the input list.

  • 4. Iterate through it to create the output list.

24

slide-77
SLIDE 77

Counting sort

Counting sort ◮ Assumption: Input is a list of ints where every item is between 0 and k ◮ Worst-case runtime: O (n + k) How would you implement this? Hint: start by creating a temp array of length k. Take inspiration from how hash tables work. The algorithm:

  • 1. Create a temp array named arr of length k.
  • 2. Loop through the input array. For each number num, run

arr[num] += 1

  • 3. The temp array will now contain how many times we say each

number in the input list.

  • 4. Iterate through it to create the output list.

24

slide-78
SLIDE 78

Other linear sorts

Other interesting linear sorts: ◮ Radix sort

◮ Assumes items in list are some kind of sequence-like thing such as strings or ints (which is a sequence of digits) ◮ Assumes each “digit” is also sortable ◮ Sorts all the digits in the 1s place, then the 2s place...

Bucket sort

A generalization of counting sort Assumes items are randomly and uniformly distributed across a range of possibilities

25

slide-79
SLIDE 79

Other linear sorts

Other interesting linear sorts: ◮ Radix sort

◮ Assumes items in list are some kind of sequence-like thing such as strings or ints (which is a sequence of digits) ◮ Assumes each “digit” is also sortable ◮ Sorts all the digits in the 1s place, then the 2s place...

◮ Bucket sort

◮ A generalization of counting sort ◮ Assumes items are randomly and uniformly distributed across a range of possibilities

25

slide-80
SLIDE 80

Graphs

Introduction to graphs

26

slide-81
SLIDE 81

What is a graph?

What is a graph? This is a graph: A B C D E F G H This is also a graph: x y In this class, by “graph”, we mean the graph data structure.

27

slide-82
SLIDE 82

What is a graph?

What is a graph? This is a graph: A B C D E F G H This is also a graph: x y In this class, by “graph”, we mean the graph data structure.

27

slide-83
SLIDE 83

What is a graph?

What is a graph? This is a graph: A B C D E F G H This is also a graph: 2 4 6 8 2 4 6 8 x y In this class, by “graph”, we mean the graph data structure.

27

slide-84
SLIDE 84

What is a graph?

What is a graph? This is a graph: A B C D E F G H This is also a graph: 2 4 6 8 2 4 6 8 x y In this class, by “graph”, we mean the graph data structure.

27

slide-85
SLIDE 85

Graph: formal defjnition

Graph A graph is a pair G = (V , E), where... ◮ V is a set of vertices ◮ E is a set of edges (pairs of vertices) Notes: ◮ Vertices are the circle things, edges are the lines ◮ The words “node” and “vertex” are synonyms

28

slide-86
SLIDE 86

Graph: formal defjnition examples

Examples:

a

V = {a} E = {}

b c d

V = {b, c, d} E = {(b, c), (c, d)}

e f g h

V = {e, f , g, h} E = {(e, f ), (f , g), (g, h), (h, e), (e, g), (f , h)}

29

slide-87
SLIDE 87

Applications of graphs

In a nutshell: Graphs let us model the “relationship” between items. If that seems like a very general defjnition, that’s because graphs are a very general concept! Core insight: Graphs are an abstract concept that appear in many difgerent ways Many problems can be modeled as a graph problem Some examples...

30

slide-88
SLIDE 88

Applications of graphs

In a nutshell: Graphs let us model the “relationship” between items. If that seems like a very general defjnition, that’s because graphs are a very general concept! Core insight: ◮ Graphs are an abstract concept that appear in many difgerent ways ◮ Many problems can be modeled as a graph problem Some examples...

30

slide-89
SLIDE 89

Application: Airline fmight graph

Questions: What is the cheapest/shortest/etc fmight from A to B? Is the route the airline ofgering me actually the cheapest route? What happens if a city is snowed in – how can we reroute fmights?

http://allthingsgraphed.com/public/images/airline-google-earth.png

31

slide-90
SLIDE 90

Application: Airline fmight graph

Questions: What is the cheapest/shortest/etc fmight from A to B? Is the route the airline ofgering me actually the cheapest route? What happens if a city is snowed in – how can we reroute fmights?

http://allthingsgraphed.com/public/images/airline-google-earth.png

31

slide-91
SLIDE 91

Application: Social media graph

Questions: Why does this graph look clustered? Why are all my friends more popular then me? Who do my friends know? If I want to hire somebody to promote my product, who do I pick?

http://allthingsgraphed.com/public/images/facebook/facebook-friend-graph.png

32

slide-92
SLIDE 92

Application: Social media graph

Questions: Why does this graph look clustered? Why are all my friends more popular then me? Who do my friends know? If I want to hire somebody to promote my product, who do I pick?

http://allthingsgraphed.com/public/images/facebook/facebook-friend-graph.png

32

slide-93
SLIDE 93

Application: Social media polarization

Questions: how to ideas fmow between bloggers? Right now? Over time? Who’s the most infmuential within a given party? In general?

http://allthingsgraphed.com/public/images/political-blogs-2004/left-right.png

33

slide-94
SLIDE 94

Application: Social media polarization

Questions: how to ideas fmow between bloggers? Right now? Over time? Who’s the most infmuential within a given party? In general?

http://allthingsgraphed.com/public/images/political-blogs-2004/left-right.png

33

slide-95
SLIDE 95

Application: Analyzing code

Questions: which fjles import which ones? Which fjles are most used and should be optimized? What if two fjles import each

  • ther? If a fjle has a security vulnerability, how might it propagate?

http://allthingsgraphed.com/public/images/java-call-graph/cassandra-import-graph.png

34

slide-96
SLIDE 96

Application: Analyzing code

Questions: which fjles import which ones? Which fjles are most used and should be optimized? What if two fjles import each

  • ther? If a fjle has a security vulnerability, how might it propagate?

http://allthingsgraphed.com/public/images/java-call-graph/cassandra-import-graph.png

34

slide-97
SLIDE 97

Application of graphs

Other interesting questions: ◮ How does Google work? How do I solve Sudoku effjciently? How do genes in my DNA infmuence and regulate each other? How can I allocate registers to variables in a program? How similar/dissimilar are words? Based on spelling? Based

  • n meaning?

35

slide-98
SLIDE 98

Application of graphs

Other interesting questions: ◮ How does Google work? ◮ How do I solve Sudoku effjciently? How do genes in my DNA infmuence and regulate each other? How can I allocate registers to variables in a program? How similar/dissimilar are words? Based on spelling? Based

  • n meaning?

35

slide-99
SLIDE 99

Application of graphs

Other interesting questions: ◮ How does Google work? ◮ How do I solve Sudoku effjciently? ◮ How do genes in my DNA infmuence and regulate each other? How can I allocate registers to variables in a program? How similar/dissimilar are words? Based on spelling? Based

  • n meaning?

35

slide-100
SLIDE 100

Application of graphs

Other interesting questions: ◮ How does Google work? ◮ How do I solve Sudoku effjciently? ◮ How do genes in my DNA infmuence and regulate each other? ◮ How can I allocate registers to variables in a program? How similar/dissimilar are words? Based on spelling? Based

  • n meaning?

35

slide-101
SLIDE 101

Application of graphs

Other interesting questions: ◮ How does Google work? ◮ How do I solve Sudoku effjciently? ◮ How do genes in my DNA infmuence and regulate each other? ◮ How can I allocate registers to variables in a program? ◮ How similar/dissimilar are words? Based on spelling? Based

  • n meaning?

35

slide-102
SLIDE 102

Modeling problems with graph

How would you model the following using graphs? Decide what you think the vertices are and what the edges are: ◮ Maps (e.g. Google Maps)

Idea: vertices are intersections, edges are roads. How do we model traffjc? Paths for cyclists vs cars? One-way roads? ◮ Web pages Idea: vertices are webpages, links are edges. ◮ A running program Idea: model each statement as vertices, and the next lines it can execute as edges. ◮ Courses at UW Idea: model courses as vertices, and pre-requisites as edges. ◮ A family tree Idea: model people as vertices, and relations as edges. Or the other way around: model events like birth or divorce as vertices, and make people the edges connecting events?

36

slide-103
SLIDE 103

Modeling problems with graph

How would you model the following using graphs? Decide what you think the vertices are and what the edges are: ◮ Maps (e.g. Google Maps)

Idea: vertices are intersections, edges are roads. How do we model traffjc? Paths for cyclists vs cars? One-way roads? ◮ Web pages Idea: vertices are webpages, links are edges. ◮ A running program Idea: model each statement as vertices, and the next lines it can execute as edges. ◮ Courses at UW Idea: model courses as vertices, and pre-requisites as edges. ◮ A family tree Idea: model people as vertices, and relations as edges. Or the other way around: model events like birth or divorce as vertices, and make people the edges connecting events?

36

slide-104
SLIDE 104

Modeling problems with graph

How would you model the following using graphs? Decide what you think the vertices are and what the edges are: ◮ Maps (e.g. Google Maps)

Idea: vertices are intersections, edges are roads. How do we model traffjc? Paths for cyclists vs cars? One-way roads? ◮ Web pages Idea: vertices are webpages, links are edges. ◮ A running program Idea: model each statement as vertices, and the next lines it can execute as edges. ◮ Courses at UW Idea: model courses as vertices, and pre-requisites as edges. ◮ A family tree Idea: model people as vertices, and relations as edges. Or the other way around: model events like birth or divorce as vertices, and make people the edges connecting events?

36

slide-105
SLIDE 105

Modeling problems with graph

How would you model the following using graphs? Decide what you think the vertices are and what the edges are: ◮ Maps (e.g. Google Maps)

Idea: vertices are intersections, edges are roads. How do we model traffjc? Paths for cyclists vs cars? One-way roads? ◮ Web pages Idea: vertices are webpages, links are edges. ◮ A running program Idea: model each statement as vertices, and the next lines it can execute as edges. ◮ Courses at UW Idea: model courses as vertices, and pre-requisites as edges. ◮ A family tree Idea: model people as vertices, and relations as edges. Or the other way around: model events like birth or divorce as vertices, and make people the edges connecting events?

36

slide-106
SLIDE 106

Modeling problems with graph

How would you model the following using graphs? Decide what you think the vertices are and what the edges are: ◮ Maps (e.g. Google Maps)

Idea: vertices are intersections, edges are roads. How do we model traffjc? Paths for cyclists vs cars? One-way roads? ◮ Web pages Idea: vertices are webpages, links are edges. ◮ A running program Idea: model each statement as vertices, and the next lines it can execute as edges. ◮ Courses at UW Idea: model courses as vertices, and pre-requisites as edges. ◮ A family tree Idea: model people as vertices, and relations as edges. Or the other way around: model events like birth or divorce as vertices, and make people the edges connecting events?

36

slide-107
SLIDE 107

Is there a “graph” ADT?

Question: is there a graph ADT? Well, what operations belong to the ADT? Hmm, lots of ideas (getEdge(v), reachableFrom(...), centrality(...), etc..) Observation: It’s very unclear what the “standard operations” are Instead, what we do is think about graphs abstractly, and think about which algorithms are relevant to the problems at hand.

37

slide-108
SLIDE 108

Is there a “graph” ADT?

Question: is there a graph ADT? Well, what operations belong to the ADT? Hmm, lots of ideas (getEdge(v), reachableFrom(...), centrality(...), etc..) Observation: It’s very unclear what the “standard operations” are Instead, what we do is think about graphs abstractly, and think about which algorithms are relevant to the problems at hand.

37

slide-109
SLIDE 109

Is there a “graph” ADT?

Question: is there a graph ADT? Well, what operations belong to the ADT? Hmm, lots of ideas (getEdge(v), reachableFrom(...), centrality(...), etc..) Observation: It’s very unclear what the “standard operations” are Instead, what we do is think about graphs abstractly, and think about which algorithms are relevant to the problems at hand.

37

slide-110
SLIDE 110

Undirected graphs

Undirected graph In a undirected graph, edges have no direction: are two-way

d a c b

This means that x y E implies that y x

  • E. (Often, we

treat these two pairs as equivalent and only include one). Degree of a vertex The degree of some vertex v is the number of edges containing that vertex. So, the degree is the number of adjacent vertices.

38

slide-111
SLIDE 111

Undirected graphs

Undirected graph In a undirected graph, edges have no direction: are two-way

d a c b

This means that x y E implies that y x

  • E. (Often, we

treat these two pairs as equivalent and only include one). Degree of a vertex The degree of some vertex v is the number of edges containing that vertex. So, the degree is the number of adjacent vertices.

38

slide-112
SLIDE 112

Undirected graphs

Undirected graph In a undirected graph, edges have no direction: are two-way

d a c b

This means that (x, y) ∈ E implies that (y, x) ∈ E. (Often, we treat these two pairs as equivalent and only include one). Degree of a vertex The degree of some vertex v is the number of edges containing that vertex. So, the degree is the number of adjacent vertices.

38

slide-113
SLIDE 113

Undirected graphs

Undirected graph In a undirected graph, edges have no direction: are two-way

d a c b

This means that (x, y) ∈ E implies that (y, x) ∈ E. (Often, we treat these two pairs as equivalent and only include one). Degree of a vertex The degree of some vertex v is the number of edges containing that vertex. So, the degree is the number of adjacent vertices.

38

slide-114
SLIDE 114

Directed graphs

Directed graph In a directed graph, edges do have a direction: are one-way

d a c b

Now, x y and y x mean difgerent things. In-degree of a vertex The in-degree of v is the number of edges that point to v. Out-degree of a vertex The out-degree of v is the number of edges that start at v.

39

slide-115
SLIDE 115

Directed graphs

Directed graph In a directed graph, edges do have a direction: are one-way

d a c b

Now, x y and y x mean difgerent things. In-degree of a vertex The in-degree of v is the number of edges that point to v. Out-degree of a vertex The out-degree of v is the number of edges that start at v.

39

slide-116
SLIDE 116

Directed graphs

Directed graph In a directed graph, edges do have a direction: are one-way

d a c b

Now, (x, y) and (y, x) mean difgerent things. In-degree of a vertex The in-degree of v is the number of edges that point to v. Out-degree of a vertex The out-degree of v is the number of edges that start at v.

39

slide-117
SLIDE 117

Directed graphs

Directed graph In a directed graph, edges do have a direction: are one-way

d a c b

Now, (x, y) and (y, x) mean difgerent things. In-degree of a vertex The in-degree of v is the number of edges that point to v. Out-degree of a vertex The out-degree of v is the number of edges that start at v.

39

slide-118
SLIDE 118

Self-loops and parallel edges

Self-loop A self-loop is an edge that starts and ends at the same vertex.

a

Parallel edges Two edges are parallel if they both start and end at the same vertices.

a b

Whether we allow or disallow self-loops and parallel edges depends

  • n what we’re trying to model.

Simple graph A graph with no self-loops and no parallel edges.

40

slide-119
SLIDE 119

Self-loops and parallel edges

Self-loop A self-loop is an edge that starts and ends at the same vertex.

a

Parallel edges Two edges are parallel if they both start and end at the same vertices.

a b

Whether we allow or disallow self-loops and parallel edges depends

  • n what we’re trying to model.

Simple graph A graph with no self-loops and no parallel edges.

40

slide-120
SLIDE 120

Self-loops and parallel edges

Self-loop A self-loop is an edge that starts and ends at the same vertex.

a

Parallel edges Two edges are parallel if they both start and end at the same vertices.

a b

Whether we allow or disallow self-loops and parallel edges depends

  • n what we’re trying to model.

Simple graph A graph with no self-loops and no parallel edges.

40

slide-121
SLIDE 121

Self-loops and parallel edges

Self-loop A self-loop is an edge that starts and ends at the same vertex.

a

Parallel edges Two edges are parallel if they both start and end at the same vertices.

a b

Whether we allow or disallow self-loops and parallel edges depends

  • n what we’re trying to model.

Simple graph A graph with no self-loops and no parallel edges.

40

slide-122
SLIDE 122

Some questions

Questions: ◮ In an undirected graph, is it possible to have a vertex with a degree of zero? Yes. ◮ In a directed graph, a vertex with an in-degree and out-degree

  • f both zero?

Yes.

41