Big O Notation and Graphs. CMSC Week 9

Similar documents
Module 11: Additional Topics Graph Theory and Applications

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CSE 101, Winter Discussion Section Week 1. January 8 - January 15

GRAPHS Lecture 17 CS2110 Spring 2014

Algorithm Design and Analysis

CS302 - Data Structures using C++

CS 206 Introduction to Computer Science II

ECE 242 HOMEWORK 5. In the figure below, one can go from node B to A but not from A to B.

IN COLLABORATION WITH IVTB. Diploma in Information Technology

1. Mother vertex using BFS. A mother vertex in a graph G = (V,E) is a vertex v such that all other vertices in G can be reached by a path from v.

CS61BL. Lecture 5: Graphs Sorting

Graphs 3/25/14 15:37 SFO LAX Goodrich, Tamassia, Goldwasser Graphs 2

GRAPHS Lecture 19 CS2110 Spring 2013

CHAPTER 14 GRAPH ALGORITHMS ORD SFO LAX DFW

Algorithm Design and Analysis

CHAPTER 13 GRAPH ALGORITHMS

Graphs ADTs and Implementations

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Lecture 9 Graph Traversal

CS171 Final Practice Exam

Graphs: Graph Data Structure:

Algorithm and Data Structures, Supplemental Solution 2010

Algorithm Design and Analysis

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

CS2 Algorithms and Data Structures Note 9

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

CS171 Final Practice Exam

22.1 Representations of graphs

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

CSE 100: GRAPH ALGORITHMS

: How to Write Fast Numerical Code ETH Computer Science, Spring 2016 Midterm Exam Wednesday, April 20, 2016

CS302 Data Structures using C++

(Re)Introduction to Graphs and Some Algorithms

Graph Algorithms Using Depth First Search

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Graph Traversals BFS & DFS 1 CS S-16

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

For Loop. Variations on Format & Specific Examples

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

Trees. Arash Rafiey. 20 October, 2015

CS/COE

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

CS24 Week 8 Lecture 1

Lecture 16: Directed Graphs

Fundamental Algorithms

Graphs & Digraphs Tuesday, November 06, 2007

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 616 MIDTERM WINTER 2017

CSE 373: Data Structures and Algorithms

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

INSTITUTE OF AERONAUTICAL ENGINEERING

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Introduction to Algorithms. Lecture 11. Prof. Patrick Jaillet

Worksheet for the Final Exam - Part I. Graphs

銘傳大學九十二學年度轉學生招生考試 七月二十六日第三節 資料結構試題

Data Structures and Algorithms

Information Science 2

Exam Datenstrukturen und Algorithmen D-INFK

Basic Graph Definitions

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Graphs: A graph is a data structure that has two types of elements, vertices and edges.

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Portland State University. CS201 Section 5. Midterm Exam. Fall 2018

LECTURE 17 GRAPH TRAVERSALS

Konigsberg Bridge Problem

CS 361 Data Structures & Algs Lecture 11. Prof. Tom Hayes University of New Mexico

Week 9. CS 400 Programming III

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

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

Data Structures and Algorithms Key to Homework Assignment 8

Homework Assignment #3 Graph

Lecture 26: Graphs: Traversal (Part 1)

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

Student number: Datenstrukturen & Algorithmen page 1

Faculty of Science FINAL EXAMINATION

Searching in Graphs (cut points)

Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University

Graphs. Data Structures 1 Graphs

Graph: representation and traversal

22.3 Depth First Search

Encoding/Decoding, Counting graphs


Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CSC263 Week 8. Larry Zhang.

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

CSC 321: Data Structures. Fall 2018

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

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

CS4800: Algorithms & Data Jonathan Ullman

Directed Graphs BOS Goodrich, Tamassia Directed Graphs 1 ORD JFK SFO DFW LAX MIA

Solutions to Midterm 2 - Monday, July 11th, 2009

DESIGN AND ANALYSIS OF ALGORITHMS

Graphs. The ultimate data structure. graphs 1

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

Outline. Graphs. Divide and Conquer.

CS 206 Introduction to Computer Science II

Transcription:

Big O Notation and Graphs CMSC 132 - Week 9

Graphs API A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs of vertices, called edges Vertices and edges can be objects that store some information. Example: A vertex represents an airport and stores the 3-letter airport code An edge represents a flight route between 2 airports, and stores the route mileage Figure from Dr. Noha Adly s lectures, Alexandria University

Graphs API Edge Types: Directed edge: ordered pair of vertices (u, v), first vertex is the source Undirected edge: unordered pair of vertices (u, v) Directed Graph Undirected Graph

Terminology End vertices or endpoints of an edge (u, v) Degree of a vertex in an undirected graph (in-degree and out-degree for directed graphs) Parallel edges: h and i are parallel edges Self-loops: j is a self-loop Figure from Dr. Noha Adly s lectures, Alexandria University

Graph Representation Adjacency List Adjacency Matrix List of Edges (A,B) (A,C) (A,D) (B,E) (B,F) (C,F)

Graph Representation

Depth-first Search

Depth-first Search Recursive

Depth-first Search Figure from Dr. Noha Adly s lectures, Alexandria University

Depth-first Search Figure from Dr. Noha Adly s lectures, Alexandria University

Depth-first Nonrecursive Pseudocode

Example What is the output of DFS traversal if we start at A and use the nonrecursive version?

Example Output: A D C F B E

1. What is the traversal order of this graph using DFS-recursive version? The start point is 6. A.6, 4, 3, 2, 1, 5 B.6, 4, 5, 2, 3, 1 C.6, 4, 5, 1, 2, 3 D.6, 4, 3, 2, 5, 1

1. What is the traversal order of this graph using DFS-recursive version? The start point is 6. A.6, 4, 3, 2, 1, 5 B.6, 4, 5, 2, 3, 1 C.6, 4, 5, 1, 2, 3 D.6, 4, 3, 2, 5, 1

2. What is the traversal order of this graph using DFS-nonrecursive version? The start point is 6. A.6, 4, 3, 2, 5, 1 B.6, 4, 5, 1, 2, 3 C.6, 4, 5, 2, 1, 3 D.6, 4, 3, 2, 1, 5

2. What is the traversal order of this graph using DFS-nonrecursive version? The start point is 6. A.6, 4, 3, 2, 5, 1 B.6, 4, 5, 1, 2, 3 C.6, 4, 5, 2, 1, 3 D.6, 4, 3, 2, 1, 5

3. What is the traversal order of this graph using DFS-recursive version? The start point is 1. A.1, 3, 5, 4, 2 B.1, 2, 4, 5, 3 C.1, 2, 5, 4, 3 D.1, 2, 3, 4, 5

3. What is the traversal order of this graph using DFS-recursive version? The start point is 1. A.1, 3, 5, 4, 2 B.1, 2, 4, 5, 3 C.1, 2, 5, 4, 3 D.1, 2, 3, 4, 5

4. What is the traversal order of this graph using DFS-nonrecursive version? The start point is 1. A.1, 2, 3, 4, 5 B.1, 2, 5, 4, 3 C.1, 2, 4, 5, 3 D.1, 4, 3, 5, 2

4. What is the traversal order of this graph using DFS-nonrecursive version? The start point is 1. A.1, 2, 3, 4, 5 B.1, 2, 5, 4, 3 C.1, 2, 4, 5, 3 D.1, 4, 3, 5, 2

Big-O Notation Examples void foo(int n) { int i,j,k; for(i = 1; i <= n; i++) i = 1 i = 2 i = 3 i = n j = 1 j = 2 j = 3 => j = n k = 100 k = 200 k = 300 k = n * 100 total = 100 + 200 + 300 + 400 + 500 = 100 (1+2+3+..+n) = 100( n(n-1)/2) = O(n^2) for(j = 1; j <= i; j++) for(k=1; k <= 100; k++){ print("good"); }

Big-O Notation Examples void foo(int n) { int i,j,k; for(i = 1; i <= n; i++) i = 1 i = 2 i = 3 i = n j = 1 j = 4 j = 9 => j = n^2 k = n/2 k = n/2 * 4 k = n/2 * 9 k = n/2 * n ^2 total = n/2 + n/2 * 4 + n/2 * 9 + + n/2 * n^2 = n/2 (1 + 4 + 9 + n^2) = n/2 * (n(n+1)(2n + 1)/6) = O (n^4) for(j = 1; j <= i*i; j++) for(k=1; k <= n/2; k++){ print("good"); }

Big-O Notation Examples void foo(){ O(n/2 * n/2 * log_2 n) = O(n^2 log_2 n) int i,j,k; times for(i= n/2, i <= n; i++) //n/2 j++) //n/2 times for(j = 1; j <= n/2, log_2^n times for(k=1; k <=n; k=k*2) // print("yes");

5. What is the Big-O of the following code? A.O (nlog_2 n) B.O (n) void foo(int n) { for(int i = n; i > 0; i = i / 2) print("good"); } C.O (log_2 n) D.O (log_2 n^2)

5. What is the Big-O of the following code? A.O (nlog_2 n) B.O (n) void foo(int n) { for(int i = n; i > 0; i = i / 2) print("good"); } C.O (log_2 n) D.O (log_2 n^2)

6. What is the Big-O of the following code? A.O (n) B.O (log_2 n) C.O (n^2 log_2 n) D.O (log_2 n^2) void foo() { j++) k <=n; k=k*2) } int i,j,k; for(i= n/2, i <= n; i++) for(j = 1; j <= n/2, print("yes"); for(k=1;

6. What is the Big-O of the following code? A.O (n) B.O (log_2 n) C.O (n^2 log_2 n) D.O (log_2 n^2) void foo() { j++) k <=n; k=k*2) } int i,j,k; for(i= n/2, i <= n; i++) for(j = 1; j <= n/2, print("yes"); for(k=1;

7. What is the Big-O of the following code? A.O (n^2 log_2 n) B.O (n (log_2 n)^2) C.O (n) D.O (log_2 n^2) void foo() { k=k*2) } int i,j,k; for(i = n/2; i <=n; i++) for(j = i; j <=n;j=j*2) for(k=1; k <=n; print("good");

7. What is the Big-O of the following code? A.O (n^2 log_2 n) B.O (n (log_2 n)^2) C.O (n) D.O (log_2 n^2) void foo() { k=k*2) } int i,j,k; for(i = n/2; i <=n; i++) for(j = i; j <=n;j=j*2) for(k=1; k <=n; print("good");

8. What is the Big-O of the following code? A.O (n) B.O (n^2) C.O (1) void (int n) { for(i = 1; i<=n; i++) } for(j = 1; j <= i; j = j+i) print("good"); D.O (log_2 n)

8. What is the Big-O of the following code? A.O (n) B.O (n^2) C.O (1) void (int n) { for(i = 1; i<=n; i++) } for(j = 1; j <= i; j = j+i) print("good"); D.O (log_2 n)

9. What is the Big-O of the following code? A.O (log_2 n) B.O (1) void foo(int n) { for(int i = 1; i < n; i = i * 2) print("good"); } C.O (n^2) D.O (n)

9. What is the Big-O of the following code? A.O (log_2 n) B.O (1) void foo(int n) { for(int i = 1; i < n; i = i * 2) print("good"); } C.O (n^2) D.O (n)

10. What is the Big-O of the following code? A.O (1) B.O (log_2 n) C.O (n) D.O (n^2) void (int n) { } for(i = 1; i<=n; i++) for(j = 1; j <= n; j++) print("good");

10. What is the Big-O of the following code? A.O (1) B.O (log_2 n) C.O (n) D.O (n^2) void (int n) { } for(i = 1; i<=n; i++) for(j = 1; j <= n; j++) print("good");