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

Similar documents
CSE Winter 2015 Quiz 1 Solutions

Lecture 5: Graphs & their Representation

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

CHAPTER 14 GRAPH ALGORITHMS ORD SFO LAX DFW

GRAPH THEORY - FUNDAMENTALS

Complexity of Algorithms

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Quiz 1 Solutions. (a) If f(n) = Θ(g(n)) and g(n) = Θ(h(n)), then h(n) = Θ(f(n)) Solution: True. Θ is transitive.

Introduction to Graphs

Graphs. Introduction To Graphs: Exercises. Definitions:

Paths. Path is a sequence of edges that begins at a vertex of a graph and travels from vertex to vertex along edges of the graph.

Introduction to Graph Theory

CPCS Discrete Structures 1

INTRODUCTION. An easy way to express the idea of an algorithm (very much like C/C++, Java, Pascal, Ada, )

Introduction to Data Structure

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 2: Asymptotics

CMSC 380. Graph Terminology and Representation

Complexity of Algorithms. Andreas Klappenecker

Announcements. CSEP 521 Applied Algorithms. Announcements. Polynomial time efficiency. Definitions of efficiency 1/14/2013

Data Structures and Algorithms CSE 465

Solving Linear Recurrence Relations (8.2)

Graphs Definitions. Gunnar Gotshalks. GraphDefinitions 1

Chapter 2: Complexity Analysis

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

1 Digraphs. Definition 1

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

Adjacent: Two distinct vertices u, v are adjacent if there is an edge with ends u, v. In this case we let uv denote such an edge.

Graphs: Introduction. Ali Shokoufandeh, Department of Computer Science, Drexel University

Discrete mathematics , Fall Instructor: prof. János Pach

UCSD CSE 101 MIDTERM 1, Winter 2008

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

Quiz 1 Practice Problems

Choice of C++ as Language

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Graphs (MTAT , 6 EAP) Lectures: Mon 14-16, hall 404 Exercises: Wed 14-16, hall 402

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Chapter 9 Graph Algorithms

Lecture 22 Tuesday, April 10

CS 561, Lecture 9. Jared Saia University of New Mexico

CSE 421: Introduction to Algorithms Complexity

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Quiz 1 Practice Problems

DEFINITION OF GRAPH GRAPH THEORY GRAPHS ACCORDING TO THEIR VERTICES AND EDGES EXAMPLE GRAPHS ACCORDING TO THEIR VERTICES AND EDGES

Graph and Digraph Glossary

Master Theorem, Introduction to Graphs

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

Graphs. Terminology. Graphs & Breadth First Search (BFS) Extremely useful tool in modeling problems. Consist of: Vertices Edges

Math Summer 2012

EECS 1028 M: Discrete Mathematics for Engineers

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

CISC 320 Midterm Exam

Solving problems on graph algorithms

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

EECS Sample Midterm Exam

Lecture 4: Graph Algorithms

Number Theory and Graph Theory

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

An Introduction to Graph Theory

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

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming

GRAPHS Lecture 17 CS2110 Spring 2014

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

GRAPHS Lecture 19 CS2110 Spring 2013

EECS 477. HOMEWORK 4 SOLUTIONS.

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 9 Notes. Class URL:

Strongly Connected Components

CS141: Intermediate Data Structures and Algorithms Analysis of Algorithms

UNIT 1 ANALYSIS OF ALGORITHMS

Discrete Structures CISC 2315 FALL Graphs & Trees

Graph Theory: Introduction

DATA STRUCTURES AND ALGORITHMS

Section 8.2 Graph Terminology. Undirected Graphs. Definition: Two vertices u, v in V are adjacent or neighbors if there is an edge e between u and v.

Theory and Algorithms Introduction: insertion sort, merge sort

CS141: Intermediate Data Structures and Algorithms Analysis of Algorithms

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

Characterizing Graphs (3) Characterizing Graphs (1) Characterizing Graphs (2) Characterizing Graphs (4)

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

Week 11: Minimum Spanning trees

Graph Theory. Part of Texas Counties.

Graph Algorithms. Definition

Directed Graph and Binary Trees

Graphs ADTs and Implementations

Graph Theory CS/Math231 Discrete Mathematics Spring2015

1 Matchings in Graphs

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 4 Notes. Class URL:

Parallel Graph Algorithms

Graphs and Network Flows IE411. Lecture 21. Dr. Ted Ralphs

END-TERM EXAMINATION

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Basics of Graph Theory

Overview. CSE 101: Design and Analysis of Algorithms Lecture 1

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

Binary Relations McGraw-Hill Education

Copyright 2000, Kevin Wayne 1

Approximation slides 1. An optimal polynomial algorithm for the Vertex Cover and matching in Bipartite graphs

Transcription:

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

Important Annotations were added (post-lecture) to the tablet slides, to fill in a few gaps (Lecture 1) Look through Additional Resources (more practice problems and reading material) at the end of the website: http://vlsicad.ucsd.edu/courses/cse101-w18/ Closed-form - is a solution to a recurrence relation

Review of Graphs Graph is a pair of finite sets G = (V, E), where: V is a set of vertices, V = {v 1, v 2,,v n } E is a set of edges, E = {(v, u) v, u belong to V} If edges (v, u) are unordered, then graph G is called an undirected graph, otherwise G is called a directed graph.

Review of Graphs Undirected Graph V = {A, B, C, D, E} E = {(A,B), (A,D), (A,E), (B,D), (B, E), (C, D)} Directed Graph V = {A, B, C, D, E, F} E = {(A,B), (B, C), (C, E), (E,D), (D, B), (E, F)}

Review of Graphs The number of vertices in a graph G = (V, E) is usually denoted by n, i.e. V = n. The number of edges in a graph G = (V, E) is usually denoted by m, i.e. E = m The vertices and edges of a graph may be labelled in different ways: Vertices with numbers: V = {1, 2,, n} Vertices with letters: V = {a, b, } or V = {A, B, } Edges with letters: E = {e 1, e 2,, e m }, where e i is a pair of vertices (v i, u j ). General labelling: V = {v 1, v 2,, v n }, E = {(v i, v j ) v i, v j are both in V}

Review of Graphs If e = (u, v) is an edge of graph G = (u, v), i.e. (u, v) belongs to E, then: Vertices u and v are called endpoints of the edge e = (u, v) Vertices u and v are adjacent (in other words, any two vertices connected with an edge are adjacent) Vertices u, v and edge e = (u, v) are incident If u = v, then edge e = (u, u) is called a self-loop Undirected graphs without self-loops are called simple graphs (we will assume simple graphs when we say graphs in the future) If two edges e 1 and e 2 share endpoints, then these edges are also adjacent.

Review of Graphs Endpoints of e=(a, B): A and B A, B are adjacent A, B are incident with e=(a, B) There are not self-loops A is incident to e=(a, B), e=(a, B) is incident from A B is incident from e=(a, B), e=(a, B) is incident to B (A, B) and (B, D) are adjacent (B,C) and (C, E) are adjacent

Review of Graphs If G is an undirected graph (all pair of edges (u, v) are unordered). Then: The number of edges incident to vertex v, where v belongs to V, is called a degree of vertex v and denoted as deg(v). Self-loops are counted twice. If G is a directed graph (all pair of edges (u, v) are ordered). Then: The number of all edges (v, x) is an outdegree of vertex v and the number of all edges (x, v) is an indegree of vertex v (outdegree - the number of edges that point out from a vertex, indegree - the number of edges that point to a vertex)

Review of Graphs Question: What would be the degree of each node if this was an undirected graph (i.e. the vertices and the edges remain the same, only the arrows are removed)? Ans: Sum of indegree and outdegree in the original graph. Outdegree shown in blue. Indegree Shown in red.

Review of Graphs Q: What are the indegree and outdegree of each vertex? Vertex Indegree Outdegree a 3 2 b 1 2 c 3 1 d 1 3 e 3 3 f 0 0

Review of Graphs A sequence of vertices {v 1, v 2,, v k } is called a path, if all pairs (v i, v i+1 ) belong to E for all i: 1 <= i <= k - 1. A path is called a simple path if all vertices in this path are unique. Vertex v is said to be reachable from vertex u if there is a path from vertex v to vertex u. A cycle is a path starts and ends at the same vertex. The length of a path is the number of edges in this path. A graph without cycles called an acyclic graph

Review of Graphs Path: (A, E, B, A, D, C) Simple path: (A, E, B, D, C) Path: (A, B, C, E, D, B, C) Simple path: (A, B, C, E, D)

Review of Graphs An undirected graph G = (V, E) is called connected if there is a path between any two vertices in this graph. A maximal subset of nodes S in an undirected graph G = (V, E) is called a connected component if a subgraph of graph G that consists of vertices from S and all edges that have both endpoints in S is connected. If graph G is connected then V = S (S needs to be maximal) Two distinct connected components of a graph can t intersect and there can t be edges that have endpoints in different connected components. If graph G is not connected then V can be represented as a union of several connected components.

Review of Graphs Q: How many connected components are there in this graph? Ans: 3

Review of Graphs A directed graph is called strongly connected if for any pair of vertices (u, v) there is a path from vertex u to vertex v and there is a path from vertex v to vertex u. A maximal subset of nodes S in a directed graph G = (V, E) is called a strongly connected component if a subgraph of graph G that consists of vertices from S and all edges that have both endpoints in S is strongly connected. If graph G is strongly connected then V = S (S needs to be maximal) Two distinct SCC can t intersect, but there can be edges that have endpoints in different SCC. If graph G is not connected then V can be represented as a union of several SCC. *SCC: Strongly Connected Components

Review of Graphs SCC: 1. A, B, E, D, G 2. C 3. F, I, H

Review of Asymptotic Notation Asymptotic Analysis: Value or a curve a function f(n) approaches or becomes almost equal to when value of n is sufficiently large. How fast does a function grow? Describes long-term behaviour of functions. Application in Algorithms: Analysing runtimes of the algorithms. Interested in how time taken grows as the size of input grows. Use asymptotic analysis to compare two algorithms.

Review of Asymptotic notation We are given two functions f:n R, g:n R: We say that f = O(g(n)) if there exists constants c > 0 and n 0 > 0, such that f(n) <= c * g(n) for all n > n 0 We say that f = Ω(g(n)) if there exists constants c > 0 and n 0 > 0, such that f(n) >= c * g(n) for all n > n 0 We say that f = Θ(g(n)) if f = O(g(n)) and f = Ω(g(n)) Note that: f = O(g(n)) if and only if g = Ω(f(n))

Review of Asymptotic notation Let lim n f(n) / g(n) = C If C <, then f(n) = O(g(n)) If C > 0, then f(n) = Ω(g(n)) If 0 < C <, then f(n) = Θ(g(n))

Example 2.1 for(int i = 0; i < n; i++) What is the time complexity of the algorithm? for(int k = n - 1; k >= 0; k--) cout << CSE 101\n ;

Example 2.1 for(int i = 0; i < n; i++) for(int k = n - 1; k >= 0; k--) cout << CSE 101\n ; There are n iterations in the outer loop and there are n iterations in the inner loop. Thus, the time complexity is Θ(n 2 )

Example 2.2 void f(n) { What is the time complexity of the algorithm? if(n == 0) return; f(n - 1); f(n - 1); }

Example 2.2 void f(n) { if(n == 0) return; Let T(n) be the number of operations. Then } f(n - 1); f(n - 1); T(0) = c T(n) = 2 * T(n - 1), n > 0 T(n) = 2 * T(n - 1) = 2 * 2 * T(n - 2) = = 2 k * T(n - k) = = 2 n * T(0) = 2 n Thus T(n) = Θ(2 n )

Example 2.3 for(int l = 0, r = 0, sum = 0; r < n; r++) { What is the time complexity of the algorithm? sum += a[r]; while(sum > k && l <= r) { sum -= a[l]; l++; } } // k is given

Example 2.3 for(int l = 0, r = 0, sum = 0; r < n; r++) { sum += a[r]; while(sum > k && l <= r) { sum -= a[l]; l++; } } // k is given Let T(n) be the number of operations. Let m i be the number of steps the inner loop makes when r = i. At each step of the inner loop variable l increases by 1 and l can t be larger than r, which can t be larger than n. Then: T(n) = Θ(m 1 ) + Θ(m 2 ) + + Θ(m n ) + Θ(n) = Θ (m 1 + m 2 + + m n ) + Θ(n) = Θ(n) + Θ(n) = Θ(n)

Example 2.4 for(int i = 1; i <= n; i++) What is the time complexity of the algorithm? for(int k = i; k <= n; k += i) cout << CSE 101\n ;

Example 2.4 for(int i = 1; i <= n; i++) for(int k = i; k <= n; k += i) cout << CSE 101\n ; Let T(n) be the number of operations. There are n steps in the outer loop. There are n / i steps in the inner loop. Then: T(n) = n + n / 2 + n / 3 + + n / (n-1) + n / n = n * (1 + 1 / 2 + 1 / 3 + + 1 / (n-1) + 1 / n) = = n * Θ(ln(n)) [Proof on the next slide] T(n) = Θ(nlog(n))