CSE 101- Winter 18 Discussion Section Week 2

Similar documents
Design and Analysis of Algorithms

Design and Analysis of Algorithms

Graphs: Connectivity. A graph. Transportation systems. Social networks

CSE Winter 2015 Quiz 2 Solutions

We can use a max-heap to sort data.

Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

Depth First Search (DFS)

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

Lecture 22 Tuesday, April 10

Analyzing Complexity of Lists

Lecture 3: Graphs and flows

Depth First Search (DFS)

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

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

CS473 - Algorithms I

Graph Representations and Traversal

1 Digraphs. Definition 1

CS4800: Algorithms & Data Jonathan Ullman

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 14 Component-I (A) - Personal Details

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Spanning Trees. CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Motivation. Observations. Spanning tree via DFS

CSE 101. Algorithm Design and Analysis Miles Jones and Russell Impagliazzo Miles Office 4208 CSE Building

Practice Problems for the Final

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

3.1 Basic Definitions and Applications

FINAL EXAM SOLUTIONS

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

DESIGN AND ANALYSIS OF ALGORITHMS

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

CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

COL 702 : Assignment 1 solutions

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

Directed acyclic graphs

Unit-2 Divide and conquer 2016

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

CS170 Discussion Section 4: 9/18

Analysis of Algorithms Prof. Karen Daniels

Copyright 2000, Kevin Wayne 1

Solving Linear Recurrence Relations (8.2)

CS 473: Algorithms. Chandra Chekuri 3228 Siebel Center. Fall University of Illinois, Urbana-Champaign

Algorithm Design and Analysis

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Trees. Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck. April 29, 2016

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

Graphs: Connectivity. Jordi Cortadella and Jordi Petit Department of Computer Science

Directed Graphs (II) Hwansoo Han

CSE 331: Introduction to Algorithm Analysis and Design Graphs

GRAPHS Lecture 19 CS2110 Spring 2013

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

Randomized Algorithms: Selection

Shortest Path Problem

A loose end: binary search

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

22 Elementary Graph Algorithms. There are two standard ways to represent a

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

Solutions to relevant spring 2000 exam problems

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

CSE 373 Final Exam 3/14/06 Sample Solution

Lecture 9: Sorting Algorithms

ICS 311, Fall 2017, Problem Set 04, Topics 7 & 8

The guaranteed existence of a source suggests an alternative approach to linearization:

CPE702 Algorithm Analysis and Design Week 7 Algorithm Design Patterns

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

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.

DEPTH-FIRST SEARCH A B C D E F G H I J K L M N O P. Graph Traversals. Depth-First Search

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

2. Instance simplification Solve a problem s instance by transforming it into another simpler/easier instance of the same problem

3.4 Testing Bipartiteness

DFS on Directed Graphs BOS. Outline and Reading ( 6.4) Digraphs. Reachability ( 6.4.1) Directed Acyclic Graphs (DAG s) ( 6.4.4)

Strongly Connected Components. Andreas Klappenecker

Shortest path problems

Exercise 1. D-ary Tree

Divide and Conquer 4-0

Inf 2B: Graphs II - Applications of DFS

COMP 251 Winter 2017 Online quizzes with answers

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

Graph Algorithms Using Depth First Search

Discrete Structures CISC 2315 FALL Graphs & Trees

Question Bank Subject: Advanced Data Structures Class: SE Computer

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

The Konigsberg Bridge Problem

Digraphs ( 12.4) Directed Graphs. Digraph Application. Digraph Properties. A digraph is a graph whose edges are all directed.

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Algorithms (IX) Guoqiang Li. School of Software, Shanghai Jiao Tong University

Strongly Connected Components

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Data Structures and Algorithms CMPSC 465

Module 5 Graph Algorithms

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

CS Algorithms and Complexity

CPS 616 TRANSFORM-AND-CONQUER 7-1

TCOM 501: Networking Theory & Fundamentals. Lecture 11 April 16, 2003 Prof. Yannis A. Korilis

Lecture 7. Transform-and-Conquer

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

CSI 604 Elementary Graph Algorithms

Transcription:

S 101- Winter 18 iscussion Section Week 2

Topics Topological ordering Strongly connected components Binary search ntroduction to ivide and onquer algorithms

Topological ordering

Topological ordering => (only forward edges if vertices arranged in increasing order of labels) B

Topological ordering

Topological ordering inductive approach ow would you approach this problem?

Topological ordering inductive approach ow would you approach this problem? ind a vertex which you know can be labelled 1 What are the properties of such a starting vertex?

Topological ordering inductive approach ow would you approach this problem? ind a vertex which you know can be labelled 1 What are the properties of such a starting vertex? laim: always has some vertex with indegree = 0 Take an arbitrary vertex v. f v doesn t have indegree = 0, traverse any incoming edge to reach a predecessor of v. f this vertex doesn t have indegree = 0, traverse any incoming edge to reach a predecessor, etc. ventually, this process will either dentify a vertex with indegree = 0, or else reach a vertex that has been reached previously (a contradiction, given that is acyclic)

Topological ordering inductive approach ow would you approach this problem? ind a vertex which you know can be labelled 1 What are the properties of such a starting vertex? nductive (or recursive) approach ind a vertex v with indegree(v) = 0; give it lowest available label; elete v (and incident edges, update degrees of remaining edges) Repeat (Bonus) can you do the same, beginning with an ending vertex?

Topological ordering using S laim: n a, every edge leads to a vertex with lower post number. Why?

Topological ordering using S laim: n a, every edge leads to a vertex with lower post number. Why? ny edge (u,v) for which post(v) > post(u) is a back edge. But a, being acyclic, has no back edges.

Topological ordering using S laim: n a, every edge leads to a vertex with lower post number. Why? ny edge (u,v) for which post(v) > post(u) is a back edge. But a, being acyclic, has no back edges. Obvious solution: Run S, then perform tasks in decreasing order of post numbers Linear running time!

Topological ordering example B

Topological ordering example 1, B

Topological ordering example 1, B 2,3

Topological ordering example 1, 4,5 B 2,3

Topological ordering example 1,6 4,5 B 2,3

Topological ordering example 1,6 4,5 B 2,3 Vertices explored in alphabetical order Topological order: --B

Topological ordering example 1,6 4,5 B 2,3 Vertices explored in alphabetical order Topological order: --B B Vertices explored in order: B--

Topological ordering example 1,6 4,5 B 2,3 Vertices explored in alphabetical order Topological order: --B B 1, Vertices explored in order: B--

Topological ordering example 1,6 4,5 B 2,3 Vertices explored in alphabetical order Topological order: --B B 1,2 Vertices explored in order: B--

Topological ordering example 1,6 4,5 B 2,3 Vertices explored in alphabetical order Topological order: --B 3, B 1,2 Vertices explored in order: B--

Topological ordering example 1,6 4,5 B 2,3 Vertices explored in alphabetical order Topological order: --B 3,4 B 1,2 Vertices explored in order: B--

Topological ordering example 1,6 4,5 B 2,3 Vertices explored in alphabetical order Topological order: --B 5, 3,4 B 1,2 Vertices explored in order: B--

Topological ordering example 1,6 4,5 B 2,3 Vertices explored in alphabetical order Topological order: --B 5,6 3,4 B 1,2 Vertices explored in order: B-- Topological order: --B

Strongly onnected omponents Two vertices u and v of a directed graph are connected if there is a path from u to v and a path from v to u.

Strongly onnected omponents Two vertices u and v of a directed graph are connected if there is a path from u to v and a path from v to u. This definition leads to a partition of a directed graph into disjoint sets of vertices -> strongly connected components B

Meta graph Meta raph The graph obtained by shrinking each strongly connected component down to a single meta-node and drawing an edge from one meta-node to another if there is an edge between their respective components. Meta raph is a. (Why?) n other words, every directed graph is a dag of its strongly connected components. B

Meta graph Meta raph The graph obtained by shrinking each strongly connected component down to a single meta-node and drawing an edge from one meta-node to another if there is an edge between their respective components. Meta raph is a. (Why?) n other words, every directed graph is a dag of its strongly connected components. B

Meta graph Meta raph The graph obtained by shrinking each strongly connected component down to a single meta-node and drawing an edge from one meta-node to another if there is an edge between their respective components. Meta raph is a. (Why?) n other words, every directed graph is a dag of its strongly connected components., B,,, B

Binary search Looking up a word in the dictionary Problem: ind x in a sorted array [1 n] lgorithm: ompare x with middle element of array Recursively find x in left subarray or right subarray

Binary search pseudocode Binary_search([], key, first, last) if (first > last) return not_found mid = (first + last)/2 if (key == [mid]) return mid if (key < [mid]) return Binary_search([],key,first,mid-1) if (key > [mid]) return Binary_search([],key,mid+1,last)

ivide and onquer ivide the problem (instance) into one or more subproblems onquer each subproblem recursively ombine separate solutions

Binary search pseudocode Binary_search([], key, first, last) if (first > last) return not_found //base case mid = (first + last)/2 if (key == [mid]) return mid //divide & conquer if (key < [mid]) return Binary_search([],key,first,mid-1) if (key > [mid]) return Binary_search([],key,mid+1,last)

Binary search revisited ivide: compare x with middle element onquer: recurse in one subarray ombine: trivial Running time T(n) = T(n/2) + O(1) Master theorem: T(n) a T(n/b) + O(n d ) 1. T(n) = O(n d ) if a < b d 2. T(n) = O(n d log n) if a = b d 3. T(n) = O(nlogba) if a > b d Using master theorem, T(n) = O(log n) (ase 2)

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1,

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2,

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 3, 2,

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2, 3, 4,

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2, 3, 4, 5,

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2, 3, 4, 5,6

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2, 3, 4, 5,6 7,

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2, 3, 4, 5,6 7,8

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2, 3, 4,9 5,6 7,8

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2, 3,10 4,9 5,6 7,8

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2, 3,10 11, 4,9 5,6 7,8

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2, 3,10 11,12 4,9 5,6 7,8

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2,13 3,10 11,12 4,9 5,6 7,8

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 2,13 3,10 11,12 4,9 5,6 7,8

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 14, 2,13 3,10 11,12 4,9 5,6 7,8

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. a) ndicate the pre and post numbers of the nodes. Solution 1, 14,15 2,13 3,10 11,12 4,9 5,6 7,8

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. B a) ndicate the pre and post numbers of the nodes. Solution 1,16 14,15 2,13 3,10 11,12 4,9 5,6 7,8

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. b) What are the sources and sinks of the graph?

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. b) What are the sources and sinks of the graph? Solution rom the S performed in the previous step we are aware that vertex is a source node since it has the highest post number.

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. b) What are the sources and sinks of the graph? Solution rom the S performed in the previous step, we are aware that vertex has the lowest post number. Thus, vertex is a sink.

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. b) What are the sources and sinks of the graph? Solution rom the S performed in the previous step, we are aware that vertex has the lowest post number. Thus, vertex is a sink. lso, in the above S while visiting the vertices in the adjacency list of vertex we could have visited vertex before vertex. This would have lead to vertex having the least post number. Thus, vertex can be a sink as well.

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. c) What topological ordering is found by the algorithm? Solution

Solved xercise Run the S-based topological ordering algorithm on the following graph. Whenever you have a choice of vertices to explore, always pick the one that is alphabetically first. c) What topological ordering is found by the algorithm? Solution