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

Preview:

Citation preview

David Luebke 1 04/20/23

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

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)(

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)

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

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?

David Luebke 7 04/20/23

Problem 2: Heaps

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

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);

}

}

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);

}

}

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);

}

}

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);

}

}

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: ???

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

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

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?

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

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…

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

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

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

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

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

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

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)

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

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)

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

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

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

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

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

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

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

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

David Luebke 35 04/20/23

The End

Coming up: actually doing something with graphs

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?

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)

Recommended