COL 702 : Assignment 1 solutions

Similar documents
Single Source Shortest Path

Shortest Path Problem

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

Single Source Shortest Path (SSSP) Problem

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Single Source Shortest Paths

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Minimum Spanning Trees

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

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Shortest Paths. Nishant Mehta Lectures 10 and 11

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Shortest Paths. Nishant Mehta Lectures 10 and 11

Shortest path problems

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

Lecture 11: Analysis of Algorithms (CS ) 1

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

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

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

Sample Solutions to Homework #4

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

Ma/CS 6a Class 27: Shortest Paths

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

Lecture 22 Tuesday, April 10

W4231: Analysis of Algorithms

Graph Algorithms Using Depth First Search

Artificial Intelligence

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

Directed acyclic graphs

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Lecture I: Shortest Path Algorithms

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.

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

6. Lecture notes on matroid intersection

Solution. violating the triangle inequality. - Initialize U = {s} - Add vertex v to U whenever DIST[v] decreases.

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

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

09 B: Graph Algorithms II

Chapter 25: All-Pairs Shortest-Paths

All Shortest Paths. Questions from exercises and exams

Solutions to Assignment# 4

CMPSCI 311: Introduction to Algorithms Practice Final Exam

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

Solutions to relevant spring 2000 exam problems

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

2. CONNECTIVITY Connectivity

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016

Minimum Spanning Tree

Dr. Alexander Souza. Winter term 11/12

Graph Representation

Konigsberg Bridge Problem

Graph Algorithms (part 3 of CSC 282),

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Introduction to Algorithms. Lecture 11

Algorithms for Data Science

Copyright 2000, Kevin Wayne 1

Dijkstra s Algorithm. Dijkstra s algorithm is a natural, greedy approach towards

MA 252: Data Structures and Algorithms Lecture 36. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Lecture 3: Graphs and flows

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

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

Problem Set 6 Solutions

Section 3.1: Nonseparable Graphs Cut vertex of a connected graph G: A vertex x G such that G x is not connected. Theorem 3.1, p. 57: Every connected

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

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

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

1 Dijkstra s Algorithm

Week 11: Minimum Spanning trees

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Strongly Connected Components

Solutions to Problem Set 3

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

CSE Winter 2015 Quiz 2 Solutions

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

/463 Algorithms - Fall 2013 Solution to Assignment 3

Analysis of Algorithms, I

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

Question 2 (Strongly Connected Components, 15 points). What are the strongly connected components of the graph below?

v V Question: How many edges are there in a graph with 10 vertices each of degree 6?

Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2007 Simonas Šaltenis

All Pairs Shortest Paths

Solution for Homework set 3

Algorithm Design, Anal. & Imp., Homework 4 Solution

Reference Sheet for CO142.2 Discrete Mathematics II

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

Module 5 Graph Algorithms

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Lecture 6 Basic Graph Algorithms

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

Design and Analysis of Algorithms - - Assessment

Dijkstra s Shortest Path Algorithm

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

Transcription:

COL 70 : Assignment 1 solutions 1(a ( n T (n = T + bn log n ( n = T + b n ( n log + bn log n ( n = T + b n ( n 8 log + b n log ( n + bn log n = bn log n + b n log n + bn log n +... log n terms = nb [(log n + 1 log n + 1 ( log 1 log n +... + log 1 bn [log n 1] = O(n log n + log ] +... 1(b T (n = at (n 1 + bn c = a [at (n + b(n 1 c ] + bn c = a T (n + ab(n 1 c + bn c = a [at (n 3 + b(n c ] + ab(n 1 c + bn c n = a i b(n i c i=0 = bn c n i=0 a i Thus, if 0 a < 1, T (n = O(n c. if a = 1, T (n = O(n c+1. if a > 1, T (n = O(a n. 7 Assume that there exists a linear time algorithm Select(S, r that partitions the array S into two parts around the rth smallest element and returns this element with rank r. Input: Set S = {x 1,..., x n }, k= number of partitions (1 < k < n Output: A set P = {y 0, y 1,..., y k } (y i S and y i < y i+1 that partitions S into k partitions S i such that S i = {x j y i 1 < x j y i }, (1 i k, S i = n/k or n/k + 1 1

Algorithm Divide(S, x /* x is a power of */ m Select(S, S /; S 1 {y y S, y m}; S {y y S, y > m}; if x = then /* Concatenate partitions S 1 and S */ return S 1. S ; else S L Divide(S 1, x/; S R Divide(S, x/; return S L. S R ; Algorithm 1: Divide k l such that l 1 < k l ; S Divide(S, k ; /* if k = k then return points of each S i */ P ; size 0; r = n%k; splitcount n k + 1; for i in 1... k do if size + S i splitcount then P P Select(S i, splitcount size; if r < 0 then splitcount splitcount + n k ; else splitcount splitcount + n k + 1; r = r 1; size size + S i ; return P ; Running Time: Select takes time linear in the size of its input. Let T (n, x be the time taken by the Divide algorithm on an input of n elements and x partitions. Then and T (n, x = T (n 1, x/ + T (n n 1, x/ + cn T (n, = cn Solving this recurrence using the Recurrence Tree, we have O(log k levels where each level has an overall cost of O(n. Therefore, the total time for the Divide algorithm is O(n log k = O(n log k. In the for loop, we have at most k calls to Select with an overall cost of O(n. Therefore, the total cost is O(n log k. Proof of correctness: Since k > k n/k < n/k. Therefore, each of the k points returned must be in exactly one of the k partitions obtained from the Divide algorithm. The rest of the proof follows from the correctness of the Select algorithm. We also ensure that there are r partitions with size n/k + 1 and k r partitions of size n/k where r = n%k. 11 To prove that the subgraph returned by the Djikstra or (Bellman Ford algorithm is a directed tree, rooted at source with n 1 edges. Directed : Since both Djikstra and Bellman Ford return single source shortest paths, the resulting graph must be directed, with all directed paths starting from the source. Tree rooted at source s

1. indegree(s = 0 : The weight of the shortest path to source s is initialized to 0 in both Djikstra and Bellman Ford algorithms. In a graph with non-negative edges, there is no path with weight less than 0. Therefore, in both algorithms, no relaxation can reduce the label of s, and indegree(s = 0 in the resulting graph.. v V \{s} such that v is reachable from s, indegree(v = 1 : Say there is a node u such that indegree(u =. This implies there are two paths from s to u, say p 1 and p. Let the predecessors of u in p 1 and p be y 1 and y, respectively. Since both p 1 and p are shortest paths, it must be that weight(p 1 = weight(p = w. Say that p 1 is discovered first, then the label of u is δ(u = w. When the edge (y, u is subsequently relaxed, the new label is w, which is not less than δ(u, hence the edge (y, u cannot be a part of the resulting graph, resulting in a contradiction. 3. Output graph has no cycles: Let there be cycle in the graph, and let u be a node in this cycle. We have two cases, (i s is not a part of this cycle, and (ii s is a part of the cycle. In case (i, indegree(u =, and in case (ii indegree(s = 1, both resulting in contradictions. Therefore, there are no cycles in the output graph. n 1 edges : Since the output graph is a directed graph with n nodes where the indegree of all nodes but one is equal to 1, the total number of edges is n 1. 1 Input: A directed acyclic graph, G(V, E, source s. Output : Single source shortest paths from s. Algorithm Topological Sort of the vertices in G. Initialization v V \{s}, δ(v = δ(s = 0 For u V in topological order, relax edges outgoing from u. v V, (u, v E, δ(v = min(δ(v, δ(u + w(u, v Proof of Correctness Claim: When the edges outgoing from u are relaxed, the shortest path to u has already been discovered. We prove the above using induction. Base case: For all nodes u with topological order less than that of s, there is no path from u to s, and for s, weight of the shortest path is equal to 0. Inductive step: Let the topological order of u be k. Assume that the shortest paths to all nodes having topological order less than k has been discovered. Let v be the predecessor of u in the shortest path from s to u. Topological-order(v < Topological-order(u = k. Therefore, shortest path from s to v has been discovered before relaxing edge (v, u and when edge (v, u is relaxed, the shortest path to u is discovered. Analysis. Topological sort involves DFS, which is O( V + E. Initialization of δ(u u V is O( V. Relaxing each edge at most once requires O( E. Therefore, total running time is O( V + E. 13 (i A is the n n adjacency matrix of a graph. Suppose that B i,j is more than the path weight of the shortest path of at most edges between vertex i and vertex j. Definition of B i.j is as follows B i,j = min 1 k n {a i,k + a k,j } The shortest path from i to j must go via some vertex k such that 1 k n. So we have a contradiction since the definition of B i,j covers all such intermediate vertices. Therefore B i,j is the path weight of the shortest path of at most edges between vertex i and vertex j. (ii We will prove by induction. Base Case: For l = 1, A is the adjacency matrix itself and thus gives the shortest paths of length at most 1 edge. 3

Induction Hypothesis: Suppose B = A A...l times stores the shortest path distance with at most l edges between i and j, be true for all l < k. We have to prove that the hypothesis is true for l = k. B = (A A...l 1times A The first part is the matrix that represents the shortest path distances between vertices with at most l 1 edges. By definition of of the operation B is the matrix representing the shortest path distances between vertices with at most l edges.

(iii A fast algotithm to compute A l Input: The matrix A and l Output: A l Algorithm: temp = A P =n n empty matrix while l > 0 : if l mod == 1 : P = P temp temp =temp temp l = l/ return P Running Time: O(n 3 logl since each operation takes O(n 3 time and there are O(logl such operations. 5