CSCE f(n) = Θ(g(n)), if f(n) = O(g(n)) and f(n) = Ω(g(n)).

Similar documents
Review for Midterm Exam

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Data Structures Question Bank Multiple Choice

DATA STRUCTURES AND ALGORITHMS

Solutions to relevant spring 2000 exam problems

Algorithms and Data Structures

CSci 231 Final Review

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

Introduction to Data Structure

Lecture 3: Graphs and flows

Info 2950, Lecture 16

COMP 251 Winter 2017 Online quizzes with answers

CS521 \ Notes for the Final Exam

CISC 320 Midterm Exam

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Course Review for Finals. Cpt S 223 Fall 2008

CMSC th Lecture: Graph Theory: Trees.

Assignment 1 (concept): Solutions

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Algorithm Design and Analysis

Priority Queue: Heap Structures

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

COMP251: Background. Jérôme Waldispühl School of Computer Science McGill University

Design and Analysis of Algorithms - - Assessment

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Lecture 9 Graph Traversal

The divide and conquer strategy has three basic parts. For a given problem of size n,

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graph Algorithms Using Depth First Search

Definition of Graphs and Trees. Representation of Trees.

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Course Review. Cpt S 223 Fall 2009

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Graph Algorithms. Chromatic Polynomials. Graph Algorithms

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Goals! CSE 417: Algorithms and Computational Complexity!

CSE 421 DFS / Topological Ordering

Notation Index. Probability notation. (there exists) (such that) Fn-4 B n (Bell numbers) CL-27 s t (equivalence relation) GT-5.

Brute Force: Selection Sort

Suggested Study Strategy

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

Shortest Path Problem

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Trees. Arash Rafiey. 20 October, 2015

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Priority queues. Priority queues. Priority queue operations

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Algorithm Design and Analysis

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

CS8391-DATA STRUCTURES

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Graphs & Digraphs Tuesday, November 06, 2007

W4231: Analysis of Algorithms

Copyright 2000, Kevin Wayne 1

Algorithms and Data Structures

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

CS:3330 (22c:31) Algorithms

Design and Analysis of Algorithms

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Midterm solutions. n f 3 (n) = 3

Data Structure. IBPS SO (IT- Officer) Exam 2017

Analysis of Algorithms

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Assignment 8 CSCE 156/156H/RAIK 184H Spring 2017

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

CSE5311 Design and Analysis of Algorithms. Administrivia Introduction Review of Basics IMPORTANT

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination T09:00

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Module 2: Classical Algorithm Design Techniques

DESIGN AND ANALYSIS OF ALGORITHMS

CSI 604 Elementary Graph Algorithms

Transcription:

CSCE 3110 Asymptotic Notations Let f and g be functions on real numbers. Then: f(n) = O(g(n)), if there are constants c and n 0 so that f(n) cg(n)), for n n 0. f(n) = Ω(g(n)), if there are constants c > 0 and n 0 so that f(n) cg(n)) for n n 0. f(n) = Θ(g(n)), if f(n) = O(g(n)) and f(n) = Ω(g(n)).

Growth of Functions Prove that n 10,000 = O(2 n ). Theorem Let f(n) = k i=0 a in i (so f(n) is polynomial of degree k). Prove f(n) = O(n k ). Proof. Note that for n 1, we have f(n) k a i n i i=0 k k a i n k n k a i, i=0 i=0 Now take n 0 = 1, and c = k i=0 a i. Then, f(n) cn k, for n n 0, and consequently, f(n) = O(n k ).

Prove n! = O(n n ). Is it true that 2 n = O(n 1000 )? Prove n n = Ω(2 n ).

Sums Show k i=0 ai = ak+1 1 a 1. Calculate n i=1 i Calculate n i=1 i2 Can we calculate n i=1 i5? Estimate f(n) = n i=1 1/i

Measuring time For i 1 to n do For j 1 to n do S EndFor EndFor End. How many times S is executed? n 2 For i 1 to n do For j 1 to i do S EndFor EndFor End. How many times S is executed? ( n j=1j) = n(n+1)/2

Measuring time For i 1 to n do For j 1 to i do For k 1 to j do S EndFor EndFor EndFor End. How many times S is executed? n i=1i(i +1)/2 (what is the value of this sum?)

Measuring time Binary Search We are searching for X in a sorted array A. l 0; H n While L H do Mid (L+H Div2) if A[Mid] < X then L Mid +1 else if A[Mid] > X then H Mid 1 else return Mid EndWhile Return NOTFOUND End. Time complexity is O(log(n)): at each loop iteration (H L) is divided by 2.

Measuring time What is time complexity of the following program Read integer N X N While X > 0 do For i 1 to X do S EndFor X (X div 2) Endwhile End.

Recurrence relations Merge Sort Mergesort(L, H) If L < H then Mid (L+H) div 2 Mergesort(L,Mid) Mergesort(Mid + 1, H) Merge(L,Mid + 1,H) EndIf End. Merging is done in linear time What is the time complexity of Mergesort? Recurrence relation is T(n) = 2T(n/2)+n:Now Solve this!

Elementary Data Structures Lists Lists are ordered multi sets. Major Operations Insert O(1) time, delete O(1) time, traverse O(n) time. Implementations Arrays, and Linked lists Syntax?

Elementary Data Structures Stacks, and Queues Stack is a list so that O(1) operations insert:push and delete:pop are always done on one end. Queue is a list so that O(1) time operations insert:inq and delete:deq are always done on opposite ends. Syntax?

Data Structures Trees What is a tree? What is a binary tree? Major Operations on Trees Tree Traversals:Preorder, Inorder, Postorder, O(n) time, on a tree with n vertices. Priority Queues A Priority Queue is a ADT that supports 2 major operations: Insert, and DelMin. DelMin Finds minimum element, and deletes it, whereas, Insert is a typical Insertion. In a List implementation Insert (at the beginning of list) takes O(1) time, and DelMin takes O(n) time.

Heaps A famous model or implementation for priority queues is a heap: A Near Complete Binary Tree so that the values stored at the children are the value stored at parent. A nice property of near complete binary tree on n nodes is that its depth is O(logn). Heaps (or Priority Queues) are one of simplest and most effective data structures ever invented. Note that a heap of size n can be stored in an array A of size n. particularly, leftchild and right child of A[i] are stored in A[2i] and A[2i + 1], respectively. Conversely, in this simple implementation, the parent of A[i] is at A[n/2].

Insert and DelMin in a Heap Insert Operation Assume that we want to insert x in a heap. A new leaf in the heap is created, where x is placed. We then, repeatedly swap (or exchange), the content of the parent of the node, where x is stored, and content of node where x is stored, until, the heap property is satisfied, or we hit the root of the heap. Note that Insert takes O(logn) time, since the depth of a heap is O(log(n)). DelMin We first delete the last leaf in the heap and place its content, or x, at the root of the heap. We then, repeatedly swap (or exchange) the content of node containing x, and the content of a child of this node, where the smaller data is stored, until the heap property is satisfied, or x is stored at a leaf. Note that DelMin also takes O(log(n)) time.

A Nice Application:Heap Sort To sort n numbers we first place them in a heap, in O(nlog(n)) time. (or even O(n) time.) We then execute DelMin, n times, and each time store the content of the deleted root. This process takes O(nlog(n)) time. The interesting part of this process is that the same array that stores the heap can also be used to store sorted numbers. So sorting is done inplace.

More General Operations on a Heap Change Operation:Making the value smaller Assume we make the content of a node smaller. To reconstruct the heap, we follow a process similar to Insert, with the exception that, we start the process, at the node where the change is made. So Change operation is done in O(log(n)) time. Change Operation:Making the value larger Assume we make the content of a node larger. To reconstruct the heap, we follow a process similar to DelMin, by moving down, with the exception that, we start the process, at the node where the change is made. So this operation is also done in O(log(n)) time. General Delete Operation We first delete the last leaf in the heap and place its content in the node to be deleted. We now a process similar to Change operation. It is important to note that we do not get to a cyclic situation.

Graphs Basic Definitions Let G = (V,E), be a graph, V = n, E = m. For any x V, the degree of x, or deg(x), is the number of vertices adjacent to x. Maximum and minimum degree in G, are denoted, respectively, by (G) and δ(g). Data Structures for Graphs Adjacency Matrix: an n n matrix M, so that M[i,j] = 1 if and only if ij E, requiring O(n 2 ) storage. Adjacency List:A Collection of n linked lists, so that list i contains all vertices adjacent to vertex i, requiring O(n+m) storage. Adjacency List: In the implementation, a one dimension array L, can be used, so that, L[i] is list containing vertices adjacent to i. Alternatively, L can be linked list itself.

Which Data Structure is Better? How fast δ(g) and (G) can be computed? How fast G can be updated, if a node x is deleted? Given x,y V, how fast one can determine, if xy E? How effective we can simulate a dynamic network?

α(g) or Chromatic Number of a Graph α(g) is the smallest number of colors assigned to vertices, so that endpoints of any edge are assigned different colors. Theorem α(g) (G)+1 Proof. We use induction on number of vertices. If n = 1, then the claim is true. No assume that claim holds for all graphs with n = k vertices, and let G be graph with n = k +1. Remove any vertex x from G, to obtain a graph G = G x on k vertices. Then by inductive hypothesis G can be colored with (G )+1 (G)+1 colors. Therefore there exists one color, among (G)+1 colors, that is not used to color vertices in G that are adjacent to x. To compete the proof, we assign this color to x.

Turning the Proof into an Algorithm for Coloring We convert our proof to an algorithm for the assignment of colors. The input to the algorithm is the adjacency matrix of graph G = (V,E), V = n. The main data structure is a one dimensional array color. Algorithm COLOR Order the vertices of G as x 1,x 2,...,x n, and set color[x 1 ] 1. For i 2 to n Do Select the smallest color j that is not used to color any vertices adjacent to x i in {x 1,x 2,...,x i 1 }. color[x i ] j. EndFor End

Turning the Proof into an Algorithm for Coloring Theorem Algorithm COLOR has time complexity of O(n 2 ) and uses (G)+1 colors. Proof. Clearly the algorithm uses (G)+1 colors, since it is based on the our inductive proof. To prove the claim concerning time complexity it is sufficient to show that we can find in O(n) time the smallest color that is NOT used for coloring the vertices adjacent to x i. To do this at iteration i of the For Loop, we construct, using the adjacency matrix and Color array, an auxiliary array A[1..n] so that A[j] is marked 1 if color j used to color any vertices adjacent to x i which is indexed lower than i. We then scan A in O(n) time to find the smallest color which is not used to color any vertices adjacent to x i in O(n) time.

Graph Traversals Breadth First Search (BFS) BFS(s) 1 InQ(s); visit[s] 1 2 While Q do 3 x DeQ() 4 For all y G[x] do 5 If visit[y] = 0 then visit[y] 1;parent[y] x; InQ(y) endif 6 EndWhile

Graph Traversals BFS Theorem BFS can be implemented in O( V + E ) time and requires O( V + E ) storage, provided that G is stored in the adjacency list format. Applications of BFS Finding a spanning tree, finding shortest paths from a source to all other vertices, finding connected components, determining if G is acyclic, determining if G is bipartite, can be done in O( V + E ) time.

Graph Traversals Depth First Search (DFS) DFS(s) 1 visit[s] 1 2 For all y G[s] do 3 If visit[y] = 0 then DFS(y) Theorem DFS can be implemented in O( V + E ) time and requires O( V + E ) storage, provided that G is stored in the adjacency list format. Moreover using DFS, finding a spanning tree, finding connected components, finding biconnected components, determining if G is acyclic, and determining if G is bipartite, can be done in O( V + E ) time.

Topological Ordering Directed Acyclic Graphs:DAG Let G = (V,E) be a directed graph, G is a DAG, if it does not contain any cycles. A topological ordering of a DAG is an ordering v 1,v 2,...v n of vertices so that for any v i v j E, we have i < j. The following simple algorithm that uses a Queue finds a topological ordering in a DAG. TOPO(G) 1 For each x V do if indeg(x) = 0 then InQ(x) 2 i 0 3 While Q do 4 v DeQ();i i +1;Num[i] v 5 For all vertices y adjacent from v do indeg(y) = indeg(y) 1; If indeg(y) = 0 then InQ(y) Endfor 6 EndWhile

Topological Ordering Theorem Let G = (V,E) be a DAG, then the previous algorithm finds a topological ordering of G in O( V + E ) time.