Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

Similar documents
2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

Copyright 2000, Kevin Wayne 1

COMP 382: Reasoning about algorithms

Review Asympto&c Bounds

Module 1: Analyzing the Efficiency of Algorithms

Asymptotic Analysis. Slides by Carl Kingsford. Jan. 27, AD Chapter 2

Introduction to Algorithms: Asymptotic Notation

CS473 - Algorithms I

Asymptotic Analysis 1

CSE 421: Intro Algorithms. 2: Analysis. Winter 2012 Larry Ruzzo

2. ALGORITHM ANALYSIS

Copyright 2000, Kevin Wayne 1

Growth of Functions (CLRS 2.3,3)

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

Matching Residents to Hospitals

EECS 477: Introduction to algorithms. Lecture 5

Defining Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 421: Intro Algorithms. Summer 2007 Larry Ruzzo

Analysis of Algorithms

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Define Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 417: Algorithms and Computational Complexity. Winter 2007 Larry Ruzzo

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004

Harvard CS 121 and CSCI E-121 Lecture 20: Polynomial Time

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

CS 4407 Algorithms Lecture 2: Growth Functions

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

CSC Design and Analysis of Algorithms. Lecture 1

Introduction to Algorithms

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

CSE 417: Algorithms and Computational Complexity

Module 1: Analyzing the Efficiency of Algorithms

3.1 Asymptotic notation

Algorithms Design & Analysis. Analysis of Algorithm

CS Non-recursive and Recursive Algorithm Analysis

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Big O Notation. P. Danziger

Approximation and Randomized Algorithms (ARA) Lecture 1, September 3, 2012

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

Running Time Evaluation

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

Lecture 3. Big-O notation, more recurrences!!

1 Substitution method

Principles of Algorithm Analysis

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Asymptotic Analysis and Recurrences

Big O Notation. P. Danziger

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Space Complexity of Algorithms

COMP Analysis of Algorithms & Data Structures

CS Data Structures and Algorithm Analysis

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

COMPUTER ALGORITHMS. Athasit Surarerks.

Combinatorial Optimization

How many hours would you estimate that you spent on this assignment?

COMP Analysis of Algorithms & Data Structures

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Computational Complexity

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

CSE 531 Homework 1 Solution. Jiayi Xian

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

Cpt S 223. School of EECS, WSU

Algorithm efficiency analysis

Data Structure and Algorithm Homework #1 Due: 2:20pm, Tuesday, March 12, 2013 TA === Homework submission instructions ===

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Math 391: Midterm 1.0 Spring 2016

Divide-and-Conquer Algorithms Part Two

Order Notation and the Mathematics for Analysis of Algorithms

with the size of the input in the limit, as the size of the misused.

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

2.1 Computational Tractability. Chapter 2. Basics of Algorithm Analysis. Computational Tractability. Polynomial-Time

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Advanced Algorithmics (6EAP)

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Reading 10 : Asymptotic Analysis

Lecture 2: Asymptotic Notation CSCI Algorithms I

Big , and Definition Definition

Data Structures and Algorithms CSE 465

IS 709/809: Computational Methods in IS Research Fall Exam Review

Data Structures and Algorithms

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

The Growth of Functions and Big-O Notation

Analysis of Algorithms

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

Asymptotic Analysis Cont'd

An analogy from Calculus: limits

Lecture 7: The Subgraph Isomorphism Problem

CS 350 Algorithms and Complexity

Data Structures and Algorithms. Asymptotic notation

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Growth of Functions. As an example for an estimate of computation time, let us consider the sequential search algorithm.

ICS 252 Introduction to Computer Design

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Intro to Theory of Computation

CS 61B Asymptotic Analysis Fall 2017

Lecture 1 - Preliminaries

2 GRAPH AND NETWORK OPTIMIZATION. E. Amaldi Introduction to Operations Research Politecnico Milano 1

P, NP, NP-Complete, and NPhard

Algorithms and Their Complexity

Transcription:

Algorithms and Theory of Computation Lecture 2: Big-O Notation Graph Algorithms Xiaohui Bei MAS 714 August 14, 2018 Nanyang Technological University MAS 714 August 14, 2018 1 / 20

O, Ω, and Θ Nanyang Technological University MAS 714 August 14, 2018 2 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Upper Bounds We say T(n) = O(f(n)) if there exists constants c > 0 and n 0 0, such that for all n n 0, we have T(n) c f(n). Nanyang Technological University MAS 714 August 14, 2018 2 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Upper Bounds We say T(n) = O(f(n)) if there exists constants c > 0 and n 0 0, such that for all n n 0, we have T(n) c f(n). Examples T(n) = 1000n + 100 Nanyang Technological University MAS 714 August 14, 2018 2 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Upper Bounds We say T(n) = O(f(n)) if there exists constants c > 0 and n 0 0, such that for all n n 0, we have T(n) c f(n). Examples T(n) = 1000n + 100 = T(n) = O(n) Nanyang Technological University MAS 714 August 14, 2018 2 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Upper Bounds We say T(n) = O(f(n)) if there exists constants c > 0 and n 0 0, such that for all n n 0, we have T(n) c f(n). Examples T(n) = 1000n + 100 = T(n) = O(n) set c = 1001 and n0 = 100 Nanyang Technological University MAS 714 August 14, 2018 2 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Upper Bounds We say T(n) = O(f(n)) if there exists constants c > 0 and n 0 0, such that for all n n 0, we have T(n) c f(n). Examples T(n) = 1000n + 100 = T(n) = O(n) set c = 1001 and n0 = 100 T(n) = pn 2 + qn + r for constants p, q, r > 0 Nanyang Technological University MAS 714 August 14, 2018 2 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Upper Bounds We say T(n) = O(f(n)) if there exists constants c > 0 and n 0 0, such that for all n n 0, we have T(n) c f(n). Examples T(n) = 1000n + 100 = T(n) = O(n) set c = 1001 and n0 = 100 T(n) = pn 2 + qn + r for constants p, q, r > 0 = T(n) = O(n 2 ) Nanyang Technological University MAS 714 August 14, 2018 2 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Upper Bounds We say T(n) = O(f(n)) if there exists constants c > 0 and n 0 0, such that for all n n 0, we have T(n) c f(n). Examples T(n) = 1000n + 100 = T(n) = O(n) set c = 1001 and n0 = 100 T(n) = pn 2 + qn + r for constants p, q, r > 0 = T(n) = O(n 2 ) set c = p + q + r and n0 = 1 also correct to say T(n) = O(n 3 ) Nanyang Technological University MAS 714 August 14, 2018 2 / 20

Some Remarks Equals sign. O(f(n)) is a set of functions, but computer scientists often write T(n) = O(f(n)) instead of T(n) O(f(n)). Consider f(n) = 5n 3 and g(n) = 3n 2, we write f(n) = O(n 3 ) = g(n), but it does not mean f(n) = g(n). Domain. The domain of f(n) is typically the natural numbers {0, 1, 2,...}. Sometimes we restrict to a subset of the natural numbers. Other times we extend to the reals. Nonnegative functions. When using big-o notation, we assume that the functions involved are (asymptotically) nonnegative. Nanyang Technological University MAS 714 August 14, 2018 3 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Lower Bounds We say T(n) = Ω(f(n)) if there exists constants ɛ > 0 and n 0 0, such that for all n n 0, we have T(n) ɛ f(n). Nanyang Technological University MAS 714 August 14, 2018 4 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Lower Bounds We say T(n) = Ω(f(n)) if there exists constants ɛ > 0 and n 0 0, such that for all n n 0, we have T(n) ɛ f(n). Examples T(n) = pn 2 + qn + r for constants p, q, r > 0 Nanyang Technological University MAS 714 August 14, 2018 4 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Lower Bounds We say T(n) = Ω(f(n)) if there exists constants ɛ > 0 and n 0 0, such that for all n n 0, we have T(n) ɛ f(n). Examples T(n) = pn 2 + qn + r for constants p, q, r > 0 = T(n) = Ω(n 2 ) Nanyang Technological University MAS 714 August 14, 2018 4 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Lower Bounds We say T(n) = Ω(f(n)) if there exists constants ɛ > 0 and n 0 0, such that for all n n 0, we have T(n) ɛ f(n). Examples T(n) = pn 2 + qn + r for constants p, q, r > 0 = T(n) = Ω(n 2 ) set ɛ = p and n0 = 1 also correct to say T(n) = Ω(n) Nanyang Technological University MAS 714 August 14, 2018 4 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Lower Bounds We say T(n) = Ω(f(n)) if there exists constants ɛ > 0 and n 0 0, such that for all n n 0, we have T(n) ɛ f(n). Examples T(n) = pn 2 + qn + r for constants p, q, r > 0 = T(n) = Ω(n 2 ) set ɛ = p and n0 = 1 also correct to say T(n) = Ω(n) Meaningful Statement. Any compare-based sorting algorithm requires Ω(n log n) compares in the worst case. Nanyang Technological University MAS 714 August 14, 2018 4 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Lower Bounds We say T(n) = Ω(f(n)) if there exists constants ɛ > 0 and n 0 0, such that for all n n 0, we have T(n) ɛ f(n). Examples T(n) = pn 2 + qn + r for constants p, q, r > 0 = T(n) = Ω(n 2 ) set ɛ = p and n0 = 1 also correct to say T(n) = Ω(n) Meaningful Statement. Any compare-based sorting algorithm requires Ω(n log n) compares in the worst case. Meaningless Statement. Any compare-based sorting algorithm requires O(n log n) compares in the worst case. Nanyang Technological University MAS 714 August 14, 2018 4 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Tight Bounds We say T(n) = Θ(f(n)) if there exists constants c 1, c 2 > 0 and n 0 0, such that for all n n 0, we have c 1 f(n) T(n) c 2 f(n). Nanyang Technological University MAS 714 August 14, 2018 5 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Tight Bounds We say T(n) = Θ(f(n)) if there exists constants c 1, c 2 > 0 and n 0 0, such that for all n n 0, we have c 1 f(n) T(n) c 2 f(n). Alternative definition T(n) = Θ(f(n)) if T(n) is both O(f(n)) and also Ω(f(n)). Nanyang Technological University MAS 714 August 14, 2018 5 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Tight Bounds We say T(n) = Θ(f(n)) if there exists constants c 1, c 2 > 0 and n 0 0, such that for all n n 0, we have c 1 f(n) T(n) c 2 f(n). Alternative definition T(n) = Θ(f(n)) if T(n) is both O(f(n)) and also Ω(f(n)). Examples T(n) = pn 2 + qn + r for constants p, q, r > 0 Nanyang Technological University MAS 714 August 14, 2018 5 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Tight Bounds We say T(n) = Θ(f(n)) if there exists constants c 1, c 2 > 0 and n 0 0, such that for all n n 0, we have c 1 f(n) T(n) c 2 f(n). Alternative definition T(n) = Θ(f(n)) if T(n) is both O(f(n)) and also Ω(f(n)). Examples T(n) = pn 2 + qn + r for constants p, q, r > 0 = T(n) = Θ(n 2 ) Nanyang Technological University MAS 714 August 14, 2018 5 / 20

O, Ω, and Θ Let T, f are two monotone increasing functions that maps from N to R +. Asymptotic Tight Bounds We say T(n) = Θ(f(n)) if there exists constants c 1, c 2 > 0 and n 0 0, such that for all n n 0, we have c 1 f(n) T(n) c 2 f(n). Alternative definition T(n) = Θ(f(n)) if T(n) is both O(f(n)) and also Ω(f(n)). Examples T(n) = pn 2 + qn + r for constants p, q, r > 0 = T(n) = Θ(n 2 ) set c1 = p, c 2 = p + q + r and n 0 = 1 T(n) is neither Θ(n) nor Θ(n 3 ) Nanyang Technological University MAS 714 August 14, 2018 5 / 20

Properties of Asymptotic Growth Rates Nanyang Technological University MAS 714 August 14, 2018 6 / 20

Properties of Asymptotic Growth Rates 1 If f = O(g) and g = O(h), then f = O(h). Nanyang Technological University MAS 714 August 14, 2018 6 / 20

Properties of Asymptotic Growth Rates 1 If f = O(g) and g = O(h), then f = O(h). 2 If f = Ω(g) and g = Ω(h), then f = Ω(h). Nanyang Technological University MAS 714 August 14, 2018 6 / 20

Properties of Asymptotic Growth Rates 1 If f = O(g) and g = O(h), then f = O(h). 2 If f = Ω(g) and g = Ω(h), then f = Ω(h). 3 If f = Θ(g) and g = Θ(h), then f = Θ(h). Nanyang Technological University MAS 714 August 14, 2018 6 / 20

Properties of Asymptotic Growth Rates 1 If f = O(g) and g = O(h), then f = O(h). 2 If f = Ω(g) and g = Ω(h), then f = Ω(h). 3 If f = Θ(g) and g = Θ(h), then f = Θ(h). 4 If f = O(h) and g = O(h), then f + g = O(h). Nanyang Technological University MAS 714 August 14, 2018 6 / 20

Properties of Asymptotic Growth Rates 1 If f = O(g) and g = O(h), then f = O(h). 2 If f = Ω(g) and g = Ω(h), then f = Ω(h). 3 If f = Θ(g) and g = Θ(h), then f = Θ(h). 4 If f = O(h) and g = O(h), then f + g = O(h). 5 If g = O(f), then f + g = Θ(f). Nanyang Technological University MAS 714 August 14, 2018 6 / 20

Properties of Asymptotic Growth Rates 1 If f = O(g) and g = O(h), then f = O(h). 2 If f = Ω(g) and g = Ω(h), then f = Ω(h). 3 If f = Θ(g) and g = Θ(h), then f = Θ(h). 4 If f = O(h) and g = O(h), then f + g = O(h). 5 If g = O(f), then f + g = Θ(f). f(n) 6 If lim = c > 0, then f = Θ(g). n g(n) Nanyang Technological University MAS 714 August 14, 2018 6 / 20

Some Common Functions 1 Polynomials. Let f be a polynomial of degree d, in which the coefficient a d is positive. Then f = O(n d ). Asymptotic rate of growth is determined by their high-order term. Nanyang Technological University MAS 714 August 14, 2018 7 / 20

Some Common Functions 1 Polynomials. Let f be a polynomial of degree d, in which the coefficient a d is positive. Then f = O(n d ). Asymptotic rate of growth is determined by their high-order term. Polynomial-Time Algorithm A polynomial-time algorithm is one with running time O(n d ) for some constant d. Nanyang Technological University MAS 714 August 14, 2018 7 / 20

Some Common Functions 1 Polynomials. Let f be a polynomial of degree d, in which the coefficient a d is positive. Then f = O(n d ). Asymptotic rate of growth is determined by their high-order term. Polynomial-Time Algorithm A polynomial-time algorithm is one with running time O(n d ) for some constant d. 2 Logarithms. For every a, b > 1 and every x > 0, we have log a n = Θ(log b n) = O(n x ). Nanyang Technological University MAS 714 August 14, 2018 7 / 20

Some Common Functions 1 Polynomials. Let f be a polynomial of degree d, in which the coefficient a d is positive. Then f = O(n d ). Asymptotic rate of growth is determined by their high-order term. Polynomial-Time Algorithm A polynomial-time algorithm is one with running time O(n d ) for some constant d. 2 Logarithms. For every a, b > 1 and every x > 0, we have log a n = Θ(log b n) = O(n x ). No need to specify base (assuming it s a constant). Logarithms are always better than polynomials. Nanyang Technological University MAS 714 August 14, 2018 7 / 20

Some Common Functions 1 Polynomials. Let f be a polynomial of degree d, in which the coefficient a d is positive. Then f = O(n d ). Asymptotic rate of growth is determined by their high-order term. Polynomial-Time Algorithm A polynomial-time algorithm is one with running time O(n d ) for some constant d. 2 Logarithms. For every a, b > 1 and every x > 0, we have log a n = Θ(log b n) = O(n x ). No need to specify base (assuming it s a constant). Logarithms are always better than polynomials. 3 Exponentials. For every r > 1 and every d > 0, we have n d = O(r n ). Nanyang Technological University MAS 714 August 14, 2018 7 / 20

Some Common Functions 1 Polynomials. Let f be a polynomial of degree d, in which the coefficient a d is positive. Then f = O(n d ). Asymptotic rate of growth is determined by their high-order term. Polynomial-Time Algorithm A polynomial-time algorithm is one with running time O(n d ) for some constant d. 2 Logarithms. For every a, b > 1 and every x > 0, we have log a n = Θ(log b n) = O(n x ). No need to specify base (assuming it s a constant). Logarithms are always better than polynomials. 3 Exponentials. For every r > 1 and every d > 0, we have n d = O(r n ). Polynomials are always better than exponentials. Nanyang Technological University MAS 714 August 14, 2018 7 / 20

More Notations Asymptotic Smaller T(n) We say T(n) = o(f(n)) if lim n f(n) = 0. Asymptotic Larger f(n) We say T(n) = ω(f(n)) if lim n T(n) = 0. Nanyang Technological University MAS 714 August 14, 2018 8 / 20

O, Ω, and Θ with multiple variables Asymptotic Upper Bounds We say T(m, n) = O(f(m, n)) if there exists constants c > 0, m 0 0 and n 0 0, such that for all m m 0 and n n 0, we have T(m, n) c f(m, n). Similar definitions for Ω and Θ. Nanyang Technological University MAS 714 August 14, 2018 9 / 20

O, Ω, and Θ with multiple variables Asymptotic Upper Bounds We say T(m, n) = O(f(m, n)) if there exists constants c > 0, m 0 0 and n 0 0, such that for all m m 0 and n n 0, we have T(m, n) c f(m, n). Similar definitions for Ω and Θ. Examples T(m, n) = 32mn 2 + 17mn + 32n 3 Nanyang Technological University MAS 714 August 14, 2018 9 / 20

O, Ω, and Θ with multiple variables Asymptotic Upper Bounds We say T(m, n) = O(f(m, n)) if there exists constants c > 0, m 0 0 and n 0 0, such that for all m m 0 and n n 0, we have T(m, n) c f(m, n). Similar definitions for Ω and Θ. Examples T(m, n) = 32mn 2 + 17mn + 32n 3 T(m, n) is both O(mn 2 + n 3 ) and O(mn 3 ). T(m, n) is neither O(n 3 ) nor O(mn 2 ). Nanyang Technological University MAS 714 August 14, 2018 9 / 20

Data Structures We discussed algorithms in peusdocode, but how the data will be represented in an actual implementation of the algorithm? Nanyang Technological University MAS 714 August 14, 2018 10 / 20

Data Structures We discussed algorithms in peusdocode, but how the data will be represented in an actual implementation of the algorithm? Data Structures: a particular way of organizing data such that it can be used efficiently in an algorithm. Appropriately designed data structures can help to give more efficient algorithms. Data structure in Number Addition : arrays. Nanyang Technological University MAS 714 August 14, 2018 10 / 20

Data Structures We discussed algorithms in peusdocode, but how the data will be represented in an actual implementation of the algorithm? Data Structures: a particular way of organizing data such that it can be used efficiently in an algorithm. Appropriately designed data structures can help to give more efficient algorithms. Data structure in Number Addition : arrays. Program = Algorithm + Data Structure Nanyang Technological University MAS 714 August 14, 2018 10 / 20

Common Data Structures Elementary Data Structures Arrays Linked Lists Stacks and Queues Priority Queues Hash Tables Binary Search Trees many others Nanyang Technological University MAS 714 August 14, 2018 11 / 20

Graph Algorithms Nanyang Technological University MAS 714 August 14, 2018 12 / 20

Graphs One of the most important combinatoric object in Computer Science, Optimization, Combinatorics. Figure: Seven Bridges of Königsberg Nanyang Technological University MAS 714 August 14, 2018 13 / 20

Graphs Graph A graph G consists of a set V of vertices and a set E of edges Directed Graph: Each edge e E is an ordered pair (u, v) for some u, v V. Undirected Graph: Each edge e E is an unordered pair {u, v} for some u, v V. Nanyang Technological University MAS 714 August 14, 2018 14 / 20

Graphs Graph 1 A graph G consists of a set V of vertices and a set E of edges Directed Graph: Each edge e E is an ordered pair (u, v) for some u, v V. 2 3 4 5 Undirected Graph: Each edge e E is an unordered pair {u, v} for some u, v V. 6 Example In the above undirected graph G = (V, E): V = {1, 2, 3, 4, 5, 6} E = {{1, 2}, {1, 3}, {2, 3}, {2, 4}, {3, 4}, {3, 5}, {4, 5}, {4, 6}} Nanyang Technological University MAS 714 August 14, 2018 14 / 20

Applications Transportation Networks The map of routes served by an airline carrier/railway company. vertices are airports/train stations; an edges from u to v if there is a flight/railway track between them. Nanyang Technological University MAS 714 August 14, 2018 15 / 20

Applications Transportation Networks The map of routes served by an airline carrier/railway company. vertices are airports/train stations; an edges from u to v if there is a flight/railway track between them. Information Networks The World Wide Web can be viewed as a directed graph. vertices are Web pages; an edge from u to v if u has a hyperlink to v. Nanyang Technological University MAS 714 August 14, 2018 15 / 20

Applications Transportation Networks The map of routes served by an airline carrier/railway company. vertices are airports/train stations; an edges from u to v if there is a flight/railway track between them. Information Networks The World Wide Web can be viewed as a directed graph. vertices are Web pages; an edge from u to v if u has a hyperlink to v. Social Networks A collection of people who interact with each other forms an undirected graph. vertices are people an edge joining u and v if they are friends. Nanyang Technological University MAS 714 August 14, 2018 15 / 20

Applications Transportation Networks The map of routes served by an airline carrier/railway company. vertices are airports/train stations; an edges from u to v if there is a flight/railway track between them. Social Networks A collection of people who interact with each other forms an undirected graph. vertices are people an edge joining u and v if they are friends. Information Networks The World Wide Web can be viewed as a directed graph. vertices are Web pages; an edge from u to v if u has a hyperlink to v. Dependency Networks A directed graph that captures the interdependencies among a collection of objects, e.g., in an university: vertices are courses offered an edge from u to v if u is a prerequisite for v. Nanyang Technological University MAS 714 August 14, 2018 15 / 20

Notation and Convention usually use n = V and m = E u and v are the end points of an edge e = {u, v} multi-graphs may have loops which are edges (u, u) multi-edges which are different edges between same pair of vertices in most of this class we only consider simple graphs Nanyang Technological University MAS 714 August 14, 2018 16 / 20

Graph Representation Nanyang Technological University MAS 714 August 14, 2018 17 / 20

Graph Representation Adjacency Matrix Represent a graph G = (V, E) by an n n adjacency matrix A, where A[u, v] = 1 if edge (u, v) E, and A[u, v] = 0 otherwise. The matrix is symmetric for undirected graphs. Nanyang Technological University MAS 714 August 14, 2018 17 / 20

Graph Representation Adjacency Matrix Represent a graph G = (V, E) by an n n adjacency matrix A, where A[u, v] = 1 if edge (u, v) E, and A[u, v] = 0 otherwise. The matrix is symmetric for undirected graphs. Advantages: can check if an edge (u, v) E in O(1) time can do linear algebra on the matrix Nanyang Technological University MAS 714 August 14, 2018 17 / 20

Graph Representation Adjacency Matrix Represent a graph G = (V, E) by an n n adjacency matrix A, where A[u, v] = 1 if edge (u, v) E, and A[u, v] = 0 otherwise. The matrix is symmetric for undirected graphs. Advantages: can check if an edge (u, v) E in O(1) time can do linear algebra on the matrix Disadvantages: require Ω(n 2 ) space even when m = o(n 2 ) cannot examine all edges incident to a given node efficiently Nanyang Technological University MAS 714 August 14, 2018 17 / 20

Graph Representation Adjacency List Represent a graph G = (V, E) by adjacency lists, which is an array Adj of length n, where each entry Adj[v] is a list containing all vertices adjacent to vertex v Nanyang Technological University MAS 714 August 14, 2018 18 / 20

Graph Representation Adjacency List Represent a graph G = (V, E) by adjacency lists, which is an array Adj of length n, where each entry Adj[v] is a list containing all vertices adjacent to vertex v Advantages: take O(n + m) space, which is close to optimal can browse all edges incident to a given vertex efficiently Nanyang Technological University MAS 714 August 14, 2018 18 / 20

Graph Representation Adjacency List Represent a graph G = (V, E) by adjacency lists, which is an array Adj of length n, where each entry Adj[v] is a list containing all vertices adjacent to vertex v Advantages: take O(n + m) space, which is close to optimal can browse all edges incident to a given vertex efficiently Disadvantages: hard to find a specific edge Standard representation for graphs. Nanyang Technological University MAS 714 August 14, 2018 18 / 20

A Concrete Representation Linked List Linked lists are a data structure to represent sequence whose length is arbitrary and changeable. each entry in a linked list consists of a cell for data and a pointer that points to the next entry there is a pointer to the first element the last entry points to NIL Nanyang Technological University MAS 714 August 14, 2018 19 / 20

A Concrete Representation Linked List Linked lists are a data structure to represent sequence whose length is arbitrary and changeable. each entry in a linked list consists of a cell for data and a pointer that points to the next entry there is a pointer to the first element the last entry points to NIL Advantages: easy to add an element into a linked list, and to sequentially read the list. Disadvantages: no random access. Nanyang Technological University MAS 714 August 14, 2018 19 / 20

Adjacency List Example Example of a Linked List 12 99 37 Nanyang Technological University MAS 714 August 14, 2018 20 / 20

Adjacency List Example Example of a Linked List 12 99 37 Example of an Adjacency List 1 2 3 4 3 2 3 4 1 4 1 1 2 4 1 2 Nanyang Technological University MAS 714 August 14, 2018 20 / 20