37
David Luebke 1 03/25/22 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

Embed Size (px)

Citation preview

Page 1: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 1 04/20/23

CS 332: Algorithms

Go Over Midterm

Intro to Graph Algorithms

Page 2: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 2 04/20/23

Problem 1: Recurrences

Give asymptotic bounds for the following recurrences. Justify by naming the case of the master theorem, iterating, or substitution

a. T(n) = T(n-2) + 1

What is the solution?

How would you show it?

T(n) = 1 + 1 + 1 + 1 + … + 1 = O(n)

O(n) terms

Page 3: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 3 04/20/23

b. T(n) = 2T(n/2) + n lg2 n This is a tricky one! What case of the master

theorem applies? Answer: case 2, as generalized in Ex 4.4.2:

if , where k 0, then

Thus T(n) = O(n lg3n)

Problem 1: Recurrences

nnnf kab lg)( log

nnnT kab 1log lg)(

Page 4: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 4 04/20/23

Problem 1: Recurrences

c. T(n) = 9T(n/4) + n2

Which case of the master theorem applies? A: case 3 What is the answer? A: O(n2)

Page 5: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 5 04/20/23

Problem 1: Recurences

d. T(n) = 3T(n/2) + n What case of the master theorem applies? A: case 1 What is the answer? A: 3loglog 2)( nnnT ab

Page 6: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 6 04/20/23

Problem 1: Recurrences

e. T(n) = T(n/2 + n) + n Recognize this one? Remember the solution? A: O(n) Proof by substitution:

Assume T(n) cn Then T(n) c(n/2 + n) + n

cn/2 + cn + n

cn - cn/2 + cn + n

cn - (cn/2 - cn - n)what could n and c be to cn make the 2nd term positive?

Page 7: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 7 04/20/23

Problem 2: Heaps

Implement BUILD-HEAP() as a recursive divide-and-conquer procedure

Page 8: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 8 04/20/23

Problem 2: Heaps

Implement BUILD-HEAP() as a recursive divide-and-conquer procedureBuildHeap(A, i)

{

if (i <= length(A)/2)

{

BuildHeap(A, 2*i);

BuildHeap(A, 2*i + 1);

Heapify(A, i);

}

}

Page 9: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 9 04/20/23

Problem 2: Heaps

Describe the running time of your algorithm as a recurrenceBuildHeap(A, i)

{

if (i <= length(A)/2)

{

BuildHeap(A, 2*i);

BuildHeap(A, 2*i + 1);

Heapify(A, i);

}

}

Page 10: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 10 04/20/23

Problem 2: Heaps

Describe the running time of your algorithm as a recurrence: T(n) = ???BuildHeap(A, i)

{

if (i <= length(A)/2)

{

BuildHeap(A, 2*i);

BuildHeap(A, 2*i + 1);

Heapify(A, i);

}

}

Page 11: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 11 04/20/23

Problem 2: Heaps

Describe the running time of your algorithm as a recurrence: T(n) = 2T(n/2) + O(lg n)BuildHeap(A, i)

{

if (i <= length(A)/2)

{

BuildHeap(A, 2*i);

BuildHeap(A, 2*i + 1);

Heapify(A, i);

}

}

Page 12: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 12 04/20/23

Problem 2: Heaps

Describe the running time of your algorithm as a recurrence: T(n) = 2T(n/2) + O(lg n)

Solve the recurrence: ???

Page 13: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 13 04/20/23

Problem 2: Heaps

Describe the running time of your algorithm as a recurrence: T(n) = 2T(n/2) + O(lg n)

Solve the recurrence: T(n) = (n) by case 1 of the master theorem

Page 14: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 14 04/20/23

Problem 3: Short Answer

Prove that (n+1)2 = O(n2) by giving the constants of n0 and c used in the definition of O notation

A: c = 2, n0 = 3

Need (n+1)2 cn2 for all n n0

(n+1)2 2n2 if 2n + 1 n2, which is true for n 3

Page 15: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 15 04/20/23

Problem 3: Short Answer

Suppose that you want to sort n numbers, each of which is either 0 or 1. Describe an asymptotically optimal method.

What is one method? What is another?

Page 16: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 16 04/20/23

Problem 3: Short Answer

Briefly describe what we mean by a randomized algorithm, and give two examples.

Necessary: Behavior determined in part by random-number generator

Nice but not necessary: Used to ensure no sequence of operations will guarantee

worst-case behavior (adversary scenario) Examples:

r-qsort, r-select, skip lists, universal hashing

Page 17: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 17 04/20/23

Problem 4: BST, Red-Black Trees

Label this tree with {6, 22, 9, 14, 13, 1, 8} so that it is a legal binary search tree:

13

6

1 9

8

14

22

What’s the smallest number? Where’s it go?What’s the next smallest? Where’s it go?etc…

Page 18: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 18 04/20/23

Problem 4: BST, Red-Black Trees

Label this tree with R and B so that it is a legal red-black tree:

13

6

1 9

8

14

22

The root is always blackblack-height (right subtree) = b.h.(left)

For this subtree to work,child must be red

Same here

Page 19: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 19 04/20/23

Problem 4: BST, Red-Black Trees

Label this tree with R and B so that it is a legal red-black tree:

13

6

1 9

8

14

22

B

Can’t have tworeds in a row R

R

Page 20: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 20 04/20/23

Problem 4: BST, Red-Black Trees

Label this tree with R and B so that it is a legal red-black tree:

13

6

1 9

8

14

22

B

R

R

B

B

for this subtree towork, both childrenmust be black

Page 21: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 21 04/20/23

Problem 4: BST, Red-Black Trees

Label this tree with R and B so that it is a legal red-black tree:

13

6

1 9

8

14

22

B

R

R

B

B

B

R

Page 22: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 22 04/20/23

Problem 4: BST, Red-Black Trees

Rotate so the left child of the root becomes the new root. Can it be labeled as red-black tree?

6

1

22

9

8

13

14

Page 23: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 23 04/20/23

Problem 4: BST, Red-Black Trees

Rotate so the left child of the root becomes the new root. Can it be labeled as red-black tree?

6

1

22

9

8

13

14

R

B

RB

B

B

R

Page 24: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 24 04/20/23

Graphs

A graph G = (V, E) V = set of vertices E = set of edges = subset of V V Thus |E| = O(|V|2)

Page 25: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 25 04/20/23

Graph Variations

Variations: A connected graph has a path from every vertex to

every other In an undirected graph:

Edge (u,v) = edge (v,u) No self-loops

In a directed graph: Edge (u,v) goes from vertex u to vertex v, notated uv

Page 26: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 26 04/20/23

Graph Variations

More variations: A weighted graph associates weights with either

the edges or the vertices E.g., a road map: edges might be weighted w/ distance

A multigraph allows multiple edges between the same vertices

E.g., the call graph in a program (a function can get called from multiple other functions)

Page 27: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 27 04/20/23

Graphs

We will typically express running times in terms of |E| and |V| (often dropping the |’s) If |E| |V|2 the graph is dense If |E| |V| the graph is sparse

If you know you are dealing with dense or sparse graphs, different data structures may make sense

Page 28: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 28 04/20/23

Representing Graphs

Assume V = {1, 2, …, n} An adjacency matrix represents the graph as a

n x n matrix A: A[i, j] = 1 if edge (i, j) E (or weight of

edge)= 0 if edge (i, j) E

Page 29: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 29 04/20/23

Graphs: Adjacency Matrix

Example:

1

2 4

3

a

d

b c

A 1 2 3 4

1

2

3 ??4

Page 30: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 30 04/20/23

Graphs: Adjacency Matrix

Example:

1

2 4

3

a

d

b c

A 1 2 3 4

1 0 1 1 0

2 0 0 1 0

3 0 0 0 0

4 0 0 1 0

Page 31: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 31 04/20/23

Graphs: Adjacency Matrix

How much storage does the adjacency matrix require?

A: O(V2) What is the minimum amount of storage needed by

an adjacency matrix representation of an undirected graph with 4 vertices?

A: 6 bits Undirected graph matrix is symmetric No self-loops don’t need diagonal

Page 32: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 32 04/20/23

Graphs: Adjacency Matrix

The adjacency matrix is a dense representation Usually too much storage for large graphs But can be very efficient for small graphs

Most large interesting graphs are sparse E.g., planar graphs, in which no edges cross, have |

E| = O(|V|) by Euler’s formula For this reason the adjacency list is often a more

appropriate respresentation

Page 33: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 33 04/20/23

Graphs: Adjacency List

Adjacency list: for each vertex v V, store a list of vertices adjacent to v

Example: Adj[1] = {2,3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3}

Variation: can also keep a list of edges coming into vertex

1

2 4

3

Page 34: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 34 04/20/23

Graphs: Adjacency List

How much storage is required? The degree of a vertex v = # incident edges

Directed graphs have in-degree, out-degree For directed graphs, # of items in adjacency lists is

out-degree(v) = |E|takes (V + E) storage (Why?)

For undirected graphs, # items in adj lists is degree(v) = 2 |E| (handshaking lemma)

also (V + E) storage So: Adjacency lists take O(V+E) storage

Page 35: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 35 04/20/23

The End

Coming up: actually doing something with graphs

Page 36: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 36 04/20/23

Exercise 1 Feedback

First, my apologies… Harder than I thought Too late to help with midterm

Proof by substitution: T(n) = T(n/2 + n) + n Most people assumed it was O(n lg n)…why?

Resembled proof from class: T(n) = 2T(n/2 + 17) + n The correct intuition: n/2 dominates n term, so it resembles

T(n) = T(n/2) + n, which is O(n) by m.t. Still, if it’s O(n) it’s O(n lg n), right?

Page 37: David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms

David Luebke 37 04/20/23

Exercise 1: Feedback

So, prove by substitution thatT(n) = T(n/2 + n) + n = O(n lg n)

Assume T(n) cn lg n Then T(n) c(n/2 + n) lg (n/2 + n)

c(n/2 + n) lg (n/2 + n) c(n/2 + n) lg (3n/2)