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

Slides:



Advertisements
Similar presentations
David Luebke 1 6/7/2014 CS 332: Algorithms Skip Lists Introduction to Hashing.
Advertisements

David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
1 7/2/2015 ITCS 6114 Graph Algorithms. 2 7/2/2015 Graphs ● A graph G = (V, E) ■ V = set of vertices ■ E = set of edges = subset of V  V ■ Thus |E| =
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Data Structure – Final Review
Graph partition in PCB and VLSI physical synthesis Lin Zhong ELEC424, Fall 2010.
Graphs Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Subchap 22.1 – Representation of Graphs.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Mathematics Review and Asymptotic Notation
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Data Structures Week 9 Introduction to Graphs Consider the following problem. A river with an island and bridges. The problem is to see if there is a way.
Red-Black Trees Red-black trees: –Binary search trees augmented with node color –Operations designed to guarantee that the height h = O(lg n)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
1/24 Introduction to Graphs. 2/24 Graph Definition Graph : consists of vertices and edges. Each edge must start and end at a vertex. Graph G = (V, E)
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
CSC 413/513: Intro to Algorithms Graph Algorithms.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
David Luebke 1 12/23/2015 Heaps & Priority Queues.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
Great Theoretical Ideas in Computer Science for Some.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
CS 201: Design and Analysis of Algorithms
Algorithm Analysis Fall 2017 CS 4306/03
Heaps, Heapsort, and Priority Queues
Chapter 5 : Trees.
Graphs Algorithm Design and Analysis Bibliography:
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Data Structures Review Session 2
CS 3343: Analysis of Algorithms
CS120 Graphs.
CS200: Algorithm Analysis
Elementary Graph Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
Heaps, Heapsort, and Priority Queues
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Heapsort Sorting in place
Solving Recurrences Continued The Master Theorem
Introduction to Algorithms: Greedy Algorithms (and Graphs)
GRAPHS Lecture 17 CS 2110 — Spring 2019.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Graphs Algorithm Design and Analysis Bibliography:
Presentation transcript:

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

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 = O(n) O(n) terms

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

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 )

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:

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?

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

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

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

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

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

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

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

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

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?

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

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: What’s the smallest number? Where’s it go? What’s the next smallest? Where’s it go? etc…

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: The root is always black black-height (right subtree) = b.h.(left) For this subtree to work, child must be red Same here

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: B Can’t have two reds in a row R R

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: B R R B B for this subtree to work, both children must be black

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: B R R B B B R

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?

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? R B RB B B R

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 )

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

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)

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

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

David Luebke 29 10/16/2015 Graphs: Adjacency Matrix l Example: a d bc A ?? 4

David Luebke 30 10/16/2015 Graphs: Adjacency Matrix l Example: a d bc A

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

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

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

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

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

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?

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)  …