Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms."— Presentation transcript:

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

2 David Luebke 2 10/16/2015 Problem 1: Recurrences l 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

3 David Luebke 3 10/16/2015 b. T(n) = 2T(n/2) + n lg 2 n n This is a tricky one! What case of the master theorem applies? l Answer: case 2, as generalized in Ex 4.4.2: n if, where k  0, then n Thus T(n) = O(n lg 3 n) Problem 1: Recurrences

4 David Luebke 4 10/16/2015 Problem 1: Recurrences c. T(n) = 9T(n/4) + n 2 n Which case of the master theorem applies? n A: case 3 n What is the answer? n A: O(n 2 )

5 David Luebke 5 10/16/2015 Problem 1: Recurences d. T(n) = 3T(n/2) + n n What case of the master theorem applies? n A: case 1 n What is the answer? n A:

6 David Luebke 6 10/16/2015 Problem 1: Recurrences e. T(n) = T(n/2 +  n) + n n Recognize this one? Remember the solution? n A: O(n) n Proof by substitution: u Assume T(n)  cn u Then T(n)  c(n/2 +  n) + n  cn/2 + c  n + n  cn - cn/2 + c  n + n  cn - (cn/2 - c  n - n)what could n and c be to  cn make the 2nd term positive?

7 David Luebke 7 10/16/2015 Problem 2: Heaps l Implement B UILD -H EAP() as a recursive divide-and-conquer procedure

8 David Luebke 8 10/16/2015 Problem 2: Heaps l Implement B UILD -H EAP() as a recursive divide-and-conquer procedure BuildHeap(A, i) { if (i <= length(A)/2) { BuildHeap(A, 2*i); BuildHeap(A, 2*i + 1); Heapify(A, i); }

9 David Luebke 9 10/16/2015 Problem 2: Heaps l Describe the running time of your algorithm as a recurrence BuildHeap(A, i) { if (i <= length(A)/2) { BuildHeap(A, 2*i); BuildHeap(A, 2*i + 1); Heapify(A, i); }

10 David Luebke 10 10/16/2015 Problem 2: Heaps l 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); }

11 David Luebke 11 10/16/2015 Problem 2: Heaps l 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); }

12 David Luebke 12 10/16/2015 Problem 2: Heaps l Describe the running time of your algorithm as a recurrence: T(n) = 2T(n/2) + O(lg n) l Solve the recurrence: ???

13 David Luebke 13 10/16/2015 Problem 2: Heaps l Describe the running time of your algorithm as a recurrence: T(n) = 2T(n/2) + O(lg n) l Solve the recurrence: T(n) =  (n) by case 1 of the master theorem

14 David Luebke 14 10/16/2015 Problem 3: Short Answer l Prove that (n+1) 2 = O(n 2 ) by giving the constants of n 0 and c used in the definition of O notation l A: c = 2, n 0 = 3 l Need (n+1) 2  cn 2 for all n  n 0 l (n+1) 2  2n 2 if 2n + 1  n 2, which is true for n  3

15 David Luebke 15 10/16/2015 Problem 3: Short Answer l Suppose that you want to sort n numbers, each of which is either 0 or 1. Describe an asymptotically optimal method. l What is one method? l What is another?

16 David Luebke 16 10/16/2015 Problem 3: Short Answer l Briefly describe what we mean by a randomized algorithm, and give two examples. l Necessary: n Behavior determined in part by random-number generator l Nice but not necessary: n Used to ensure no sequence of operations will guarantee worst-case behavior (adversary scenario) l Examples: n r-qsort, r-select, skip lists, universal hashing

17 David Luebke 17 10/16/2015 Problem 4: BST, Red-Black Trees l Label this tree with {6, 22, 9, 14, 13, 1, 8} so that it is a legal binary search tree: 13 6 19 8 14 22 What’s the smallest number? Where’s it go? What’s the next smallest? Where’s it go? etc…

18 David Luebke 18 10/16/2015 Problem 4: BST, Red-Black Trees l Label this tree with R and B so that it is a legal red-black tree: 13 6 19 8 14 22 The root is always black black-height (right subtree) = b.h.(left) For this subtree to work, child must be red Same here

19 David Luebke 19 10/16/2015 Problem 4: BST, Red-Black Trees l Label this tree with R and B so that it is a legal red-black tree: 13 6 19 8 14 22 B Can’t have two reds in a row R R

20 David Luebke 20 10/16/2015 Problem 4: BST, Red-Black Trees l Label this tree with R and B so that it is a legal red-black tree: 13 6 19 8 14 22 B R R B B for this subtree to work, both children must be black

21 David Luebke 21 10/16/2015 Problem 4: BST, Red-Black Trees l Label this tree with R and B so that it is a legal red-black tree: 13 6 19 8 14 22 B R R B B B R

22 David Luebke 22 10/16/2015 Problem 4: BST, Red-Black Trees l 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

23 David Luebke 23 10/16/2015 Problem 4: BST, Red-Black Trees l 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

24 David Luebke 24 10/16/2015 Graphs l A graph G = (V, E) n V = set of vertices n E = set of edges = subset of V  V n Thus |E| = O(|V| 2 )

25 David Luebke 25 10/16/2015 Graph Variations l Variations: n A connected graph has a path from every vertex to every other n In an undirected graph: u Edge (u,v) = edge (v,u) u No self-loops n In a directed graph: u Edge (u,v) goes from vertex u to vertex v, notated u  v

26 David Luebke 26 10/16/2015 Graph Variations l More variations: n A weighted graph associates weights with either the edges or the vertices u E.g., a road map: edges might be weighted w/ distance n A multigraph allows multiple edges between the same vertices u E.g., the call graph in a program (a function can get called from multiple other functions)

27 David Luebke 27 10/16/2015 Graphs l We will typically express running times in terms of |E| and |V| (often dropping the |’s) n If |E|  |V| 2 the graph is dense n If |E|  |V| the graph is sparse l If you know you are dealing with dense or sparse graphs, different data structures may make sense

28 David Luebke 28 10/16/2015 Representing Graphs l Assume V = {1, 2, …, n} l An adjacency matrix represents the graph as a n x n matrix A: n A[i, j] = 1 if edge (i, j)  E (or weight of edge) = 0 if edge (i, j)  E

29 David Luebke 29 10/16/2015 Graphs: Adjacency Matrix l Example: 1 24 3 a d bc A1234 1 2 3 ?? 4

30 David Luebke 30 10/16/2015 Graphs: Adjacency Matrix l Example: 1 24 3 a d bc A1234 10110 20010 30000 40010

31 David Luebke 31 10/16/2015 Graphs: Adjacency Matrix l How much storage does the adjacency matrix require? l A: O(V 2 ) l What is the minimum amount of storage needed by an adjacency matrix representation of an undirected graph with 4 vertices? l A: 6 bits n Undirected graph  matrix is symmetric n No self-loops  don’t need diagonal

32 David Luebke 32 10/16/2015 Graphs: Adjacency Matrix l The adjacency matrix is a dense representation n Usually too much storage for large graphs n But can be very efficient for small graphs l Most large interesting graphs are sparse n E.g., planar graphs, in which no edges cross, have |E| = O(|V|) by Euler’s formula n For this reason the adjacency list is often a more appropriate respresentation

33 David Luebke 33 10/16/2015 Graphs: Adjacency List l Adjacency list: for each vertex v  V, store a list of vertices adjacent to v l Example: n Adj[1] = {2,3} n Adj[2] = {3} n Adj[3] = {} n Adj[4] = {3} l Variation: can also keep a list of edges coming into vertex 1 24 3

34 David Luebke 34 10/16/2015 Graphs: Adjacency List l How much storage is required? n The degree of a vertex v = # incident edges u Directed graphs have in-degree, out-degree n For directed graphs, # of items in adjacency lists is  out-degree(v) = |E| takes  (V + E) storage (Why?) n For undirected graphs, # items in adj lists is  degree(v) = 2 |E| (handshaking lemma) also  (V + E) storage l So: Adjacency lists take O(V+E) storage

35 David Luebke 35 10/16/2015 The End l Coming up: actually doing something with graphs

36 David Luebke 36 10/16/2015 Exercise 1 Feedback l First, my apologies… n Harder than I thought n Too late to help with midterm l Proof by substitution: n T(n) = T(n/2 +  n) + n n Most people assumed it was O(n lg n)…why? u Resembled proof from class: T(n) = 2T(n/2 + 17) + n u The correct intuition: n/2 dominates  n term, so it resembles T(n) = T(n/2) + n, which is O(n) by m.t. n Still, if it’s O(n) it’s O(n lg n), right?

37 David Luebke 37 10/16/2015 Exercise 1: Feedback l So, prove by substitution that T(n) = T(n/2 +  n) + n = O(n lg n) l Assume T(n)  cn lg n l 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)  …


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

Similar presentations


Ads by Google