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

Similar documents
Outlines: Graphs Part-2

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Chapter 22. Elementary Graph Algorithms

DFS & STRONGLY CONNECTED COMPONENTS

Introduction to Algorithms. Lecture 11

Graph Search. Adnan Aziz

Basic Graph Algorithms

Week 12: Minimum Spanning trees and Shortest Paths

Graph representation

Graph Representation

csci 210: Data Structures Graph Traversals

Elementary Graph Algorithms

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Graph Algorithms. Definition

Minimum Spanning Trees Ch 23 Traversing graphs

Graph: representation and traversal

CSI 604 Elementary Graph Algorithms

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Section authors: Hamilton Clower, David Scott, Ian Dundore.

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

Design and Analysis of Algorithms

Representations of Graphs

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

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

Graph implementations :

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

csci 210: Data Structures Graph Traversals

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Unit 2: Algorithmic Graph Theory

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Three Graph Algorithms

Three Graph Algorithms

Graph: representation and traversal

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

CS521 \ Notes for the Final Exam

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Homework Assignment #3 Graph

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Shortest path problems

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

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

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

Single Source Shortest Paths

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

811312A Data Structures and Algorithms, , Exercise 6, solution

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Dijkstra s Shortest Path Algorithm

Algorithm Design and Analysis

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

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

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

- Logic and Algorithms - Graph Algorithms

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

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

Single Source Shortest Path (SSSP) Problem

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

COT 6405 Introduction to Theory of Algorithms

CHAPTER 13 GRAPH ALGORITHMS

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Lecture 6 Basic Graph Algorithms

Minimum Spanning Trees

Sample Solutions to Homework #4

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

Minimum Spanning Tree

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

Review: Graph Theory and Representation

( D. Θ n. ( ) f n ( ) D. Ο%

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

( ) n 3. n 2 ( ) D. Ο

Week 11: Minimum Spanning trees

Data Structures and Algorithms. Werner Nutt

Shortest Path Algorithms

Basic Graph Algorithms (CLRS B.4-B.5, )

Undirected Graphs. Hwansoo Han

n 2 ( ) ( ) + n is in Θ n logn

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

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

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

W4231: Analysis of Algorithms

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

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

Announcements Problem Set 4 is out!

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.

CSC263 Week 8. Larry Zhang.

Shortest Paths. Nishant Mehta Lectures 10 and 11

Final Exam. EECS 2011 Prof. J. Elder - 1 -

Shortest Paths. Nishant Mehta Lectures 10 and 11

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


Single Source Shortest Path

All Shortest Paths. Questions from exercises and exams

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

Transcription:

Tutorial Question 1 A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first tree can also be used to classify the edges reachable from the source of the search into the same four categories. Prove that in a breadth-first search of a directed graph, the following properties hold: 1. There are no forward edges. 2. For each tree edge u, v, we have d[v] = d[u] + 1. 3. For each cross edge u, v, we have d[v] d[u] + 1. 4. For each back edge u, v, we have 0 d[v] d[u]. Background: Breadth First Search Breadth First Search starts at a source s. Each vertex, u, has a distance, d[u], from s. Intuitively, d[s] = 0. If the graph is not a connected graph (there is a path from each vertex to every other vertex), then vertices not reachable from s will have an infinite distance from s. That is, d[u] =, where u is not reachable from s. 1

Figure 1: Example BFS Tree. Bold directed edges indicate tree edges that point from parent to child. Numbers indicate the distance from source s. def bfs(v, adj, s): for each u in V: color[u] = WHITE d[u] = INFINITY p[u] = NIL color[s] = GRAY d[s] = 0 Q = {s} while Q is not empty: u = head of Q for each v in adj[u]: if color[v] == WHITE: color[v] = GRAY d[v] = d[u] + 1 p[v] = u append v to end of Q remove u from head of Q color[u] = BLACK return (d, p) Background: Edge Classification 1. Tree Edge A tree edge is an edge that is in the output tree of some graph search algorithm, such as the breadth first search. 2. Forward Edge An edge not in the output tree that connects a vertex to a descendent in the output tree. 3. Back Edge An edge of the original graph that connects a vertex to its ancestor in the output tree. 4. Cross Edge All other edges. Edges 2 not in the output tree that is neither a forward edge nor a back edge. Answer

Figure 2: For a forward edge u, v to exist, the path from u to v in the BFS tree must contain at least one intermediate node. Thus the following condition must hold d[u] + 1 < d[v]. 1. There are no forward edges. For a forward edge u, v to exist, u, v must not be in the BFS tree and u is an ancestor of v in the BFS tree. Thus, d[u] + 1 < d[v] must hold. That is, there must be at least one vertex in the predecessor path from v to u. Otherwise, it would imply that u, v was in the BFS tree and would not be considered a forward edge. Statement 1: When u is the head of the GRAY queue of discovered vertices, then it is made the ancestor of all adjacent vertices that are WHITE. Each is also made GRAY and added to the end of the queue of discovered vertices. For u, v to exist in the graph but not be included in the BFS tree, v could not have been WHITE when u was the head of the queue. Which means that v was either BLACK or GRAY. Statement 2: If v was BLACK, then v has been visited before u. Thus, d[v] d[u]. So u cannot be an ancestor of v. Statement 3: If v was GRAY, then v has been discovered before u became the head of the queue. In fact, v is in the queue behind u when u was the head of the queue. Any new vertices discovered while u is the head of the queue will have a distance of d[u] + 1. But since v was in the queue before any of these newly discovered nodes, d[v] d[u] + 1. Thus, whether v was BLACK or GRAY when u was the head of the queue, none of the conditions allow forward edges to exist. Proving that there are no forward edges in a BFS tree. 2. For each tree edge u, v, we have d[v] = d[u] + 1. Following the BFS tree construction, if u, v is a tree edge, then d[v] = d[u] + 1, because u is a parent of v in the BFS tree. 3. For each cross edge u, v, we have d[v] d[u] + 1. 3

From statement 3, any edge u, v excluded from the BFS tree must have d[v] d[u] + 1. 4. For each back edge u, v, we have 0 d[v] d[u]. If u, v is a back edge, then v must be an ancestor of u. So d[v] < d[u]. Since v may very well be the source s, 0 d[v]. Question 2 There are two well-known algorithms for finding minimum spanning trees. Point out the difference between Prim s algorithm and Kruskal s algorithm in terms of the construction of the MST tree. The construction of an MST using Prim s algorithm begins from a single vertex. From here, the tree is expanded until it covers all the vertices in the graph. In other words, there is only one partial tree during the construction of the MST from the very beginning to the end. However, Kruskal s algorithm starts from a forest of V single vertex trees. At each step, it merges two trees in the forest, continuing until there is only one tree left; the MST. Question 3 Assume that the weight assigned to each edge in graph G = (V, E) is distinct. Let e be a maximum-weighted edge on a cycle of G = (V, E). Show that a minimum spanning tree in G = (V, E {e}) is also a minimum spanning tree in G. Let T be a minimal spanning tree of G. We claim that T is also a minimal spanning tree of G. The only difference between G and G is that G has the additional edge e. Thus, our claim will only be wrong if the minimal spanning tree of G contains the edge e. Let T be such a tree. Removing e = u, v from T disconnects the tree into two subtrees, one containing u and the other containing v. If there exist an edge of lower weight than e that connects the two subtrees, then T was not a MST! Since e is part of a cycle, then by including e in T, at least one of the other edges in the cycle must have been excluded from T (because there are no cycles in trees). And we know that any edge of the cycle not used in T has a lower weight than e (because the weights are distinct). In other words, there exist an edge of lower weight than e that connects the two subtrees. Thus, any tree containing e cannot be a MST of G. Removing e from consideration, gives us G. Hence, any MST of G is also a MST of G. Question 4 Given a directed weighted graph G = (V, E) with source s, under what circumstances should the Bellman-Ford algorithm be used, instead of Dijkstra s 4

Figure 3: Example Graph algorithm, to solve the single-source shortest paths problem with source s. When there exist a negative edge in G; because Dijkstra s algorithm doesn t work for graphs with negative edges. Question 5 Given a directed weighted graph G = (V, E), let d(v i, v j ) be the distance in G from v i to v j in terms of the minimum number of edges used in the path, v i V and v j V. Define the diameter D of G as D = max vi V,v j V {d(v i, v j )}. What is the time complexity for finding all pairs of shortest paths in G if the repeated squaring approach is applied? Background: Matrix Multiplication for Graphs Using the graph depicted in Figure 3, let W be a weighted adjacency matrix of the graph G. Let the matrix M (k) contain the all pairs shortest paths of G of at most k edges. W = 0 1 8 0 2 0 3 0 = M (1) Multiplying M (1) W will give us M (2). This is the shortest path between any pair of vertices using at most two edges. 0 1 8 3 M (2) = 0 5 2 0 3 0 Thus, in order to find all pairs of shortest paths in G, we need to find M (n 1) ; because that would give us all pairs shortest paths of G of at most n 1 edges. Hence, we calculate: M (1), M (2), M (3),..., M (n 1), where M (i) = M (i 1) W. With repeated squaring, we only need to calculate: 5

M (1), M (2), M (4),..., M (2 log 2 (n 1) ), where M (2i) = M (i) M (i). Each multiplication is O(n 3 ) time. Since the number of multiplication is O(log 2 n), the runtime of repeated squaring is O(n 3 log 2 n). If D is given, then the runtime is O(n 3 log 2 D). 6