Homework 5: Graphs, Minimum Spanning Trees, and Dijkstra Shortest-Path

Similar documents
Homework Assignment #3 Graph

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Chapter 9 Graph Algorithms

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

Chapter 9 Graph Algorithms

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Chapter 9 Graph Algorithms

Data Structures and Algorithms Key to Homework Assignment 8

Graph Algorithms (part 3 of CSC 282),

Unweighted Graphs & Algorithms

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Minimum Spanning Trees My T. UF

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

Outline. Graphs. Divide and Conquer.

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Minimum Spanning Trees

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

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Graph. Vertex. edge. Directed Graph. Undirected Graph

Undirected Graphs. Hwansoo Han

CS4800: Algorithms & Data Jonathan Ullman

Lecture 9 Graph Traversal

PATH FINDING AND GRAPH TRAVERSAL

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Math 15 - Spring Homework 5.2 Solutions

Week 12: Minimum Spanning trees and Shortest Paths

Exam 3 Practice Problems

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

Graph Algorithms (part 3 of CSC 282),

DS UNIT 4. Matoshri College of Engineering and Research Center Nasik Department of Computer Engineering Discrete Structutre UNIT - IV

Minimum Spanning Trees

Eulerian Paths and Cycles

Minimum spanning trees

MATH 363 Final Wednesday, April 28. Final exam. You may use lemmas and theorems that were proven in class and on assignments unless stated otherwise.

L22-23: Graph Algorithms

Graphs & Digraphs Tuesday, November 06, 2007

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Konigsberg Bridge Problem

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

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Inf 2B: Graphs II - Applications of DFS

CSE 521: Design and Analysis of Algorithms I

COMP 355 Advanced Algorithms

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

The Konigsberg Bridge Problem

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.

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Spanning trees. Suppose you have a connected undirected graph

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs

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

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

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

Minimum Spanning Trees Ch 23 Traversing graphs

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

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

ECE 242 HOMEWORK 5. In the figure below, one can go from node B to A but not from A to B.

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

Minimum Spanning Trees COSC 594: Graph Algorithms Spring By Kevin Chiang and Parker Tooley

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

Lecture 6 Basic Graph Algorithms

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

TA: Jade Cheng ICS 241 Recitation Lecture Notes #12 November 13, 2009

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Chapter 23. Minimum Spanning Trees

(Re)Introduction to Graphs and Some Algorithms

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Solving problems on graph algorithms

Module 2: NETWORKS AND DECISION MATHEMATICS

CSE 100: GRAPH ALGORITHMS

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Reference Sheet for CO142.2 Discrete Mathematics II

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Announcements Problem Set 5 is out (today)!

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

Further Mathematics 2016 Module 2: NETWORKS AND DECISION MATHEMATICS Chapter 9 Undirected Graphs and Networks

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

Graphs and Genetics. Outline. Computational Biology IST. Ana Teresa Freitas 2015/2016. Slides source: AED (MEEC/IST); Jones and Pevzner (book)

Number Theory and Graph Theory

Design and Analysis of Algorithms - - Assessment

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

Student Name and ID Number. MATH 3012 Final Exam, December 11, 2014, WTT

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

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

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Homework 4 Even numbered problem solutions cs161 Summer 2009

CSC 505, Fall 2000: Week 7. In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion.

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

Introduction to Computer Science and Programming for Astronomers

Artificial Intelligence

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

CSE 21: Mathematics for Algorithms and Systems Analysis

S.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear

The minimum spanning tree problem

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Minimum-Cost Spanning Tree. Example

Transcription:

Homework 5: Graphs, Minimum Spanning Trees, and Dijkstra Shortest-Path 1. (4 points) A graph is Hamiltonian if there is a cycle in the graph visiting each vertex exactly once. Give an example of an Eulerian graph that is not Hamiltonian, and a Hamiltonian graph that is not Eulerian. Answer: A Hamiltonian graph that is not Eulerian is K2, the complete graph on n vertices, for any n 2, since each vertex has odd degree. An Eulerian graph that is not Hamiltonian is, for example two simple cycles C[n], C[m] sharing a single vertex. 2. (2 points) For an unweighted graph, DFS traversal of the graph produces: a. minimum spanning tree b. longest spanning tree c. all pair shortest path tree *d. both a and c Explanation: Minimum spanning tree is actually all pair shortest path. It visits all the nodes once. 3.(4 points) We want to assign classrooms for each class. There are n classes that meet in a day. For each class, we know the start and end-time of the class. No two classes whose meeting times overlap can be assigned the same classroom. Further assume that each classroom is large enough, so that any class can be assigned any classroom. Suppose we want to fewest number of classrooms required to accommodate all classes. How can we model this problem as a graph problem? Give an example of a graph and how you ll use a graph algorithm to solve this problem. Explanation: Model the classes as vertices of a graph, and an edge between two vertices if the corresponding classes overlap. Let the colors correspond to the classrooms. Then, the fewest number of colors required to color the vertices so that no adjacent vertices share the same color is the fewest number of classrooms required so that no two overlapping classes have to share the same classroom.

5. (4 points) Write pseudo-code to detect a cycle in a graph. Answers: def iscyclicutil(self, v, visited, recstack): # Mark current node as visited and # adds to recursion stack visited[v] = True recstack[v] = True # Recur for all neighbours # if any neighbour is visited and in # recstack then graph is cyclic for neighbour in self.graph[v]: if visited[neighbour] == False: if self.iscyclicutil(neighbour, visited, recstack) == True: return True elif recstack[neighbour] == True: return True # The node needs to be popped from # recursion stack before function ends recstack[v] = False return False # Returns true if graph is cyclic else false def iscyclic(self): visited = [False] * self.v recstack = [False] * self.v for node in range(self.v): if visited[node] == False: if self.iscyclicutil(node,visited,recstack) == True: return True return False

6. Why can't Prim's or Kruskal's algorithms be used on a directed graph? Prim's and Kruskal's algorithm output a minimum spanning tree for connected and "undirected" graph. If it is not connected, we can tweak them to output minimum spanning forests. In Prim's algorithm, we divide the graph in two sets of vertices. One set of the explored vertices which have already formed MST (Set1) and another set of unexplored vertices which will eventually join the first set to complete "spanning"(set2). At each instant, we select a minimum weighted edge in the cut joining the two disjoint sets. If there is no directed edge from explored nodes of MST to remaining unexplored, the algorithm gets stuck even though there are edges from unexplored nodes to explored nodes in MST. In Kruskal's algorithm, the idea is to sort the edges in ascending order by their weight and pick them up in order and include them in MST explored nodes/edges if they do not already form a cycle with explored nodes. This is done by Union-Find DS. But detection of cycle for directed graphs fails with this method. For ex: Graph containing edges [1->2] [2->3] [1->3] will be reported to contain a cycle with the Union-Find method. So Prim's fails because it assumes, every node is reachable from every node which though valid for undirected graphs may not be true for digraphs. Kruskal fails because of failure to detect cycles and sometimes it is essential to add edges making cycles to satisfy "minimum" weighted property of MST. Also, in case of digraphs, MST doesn't make complete sense. Its equivalent for digraphs is "minimum spanning arborescence" which will produce a tree where every vertex can be reached from a single vertex. Answer from: https://stackoverflow.com/questions/22649416/why-cant-prims-or-kruskals-algorithms-b e-used-on-a-directed-graph/35685918#35685918

7. (2 points) If my code represents a graph by {a: (b, c, d), b: (a), c: (a, d), d: (a, c)} then I am using a to represent it? *a. adjacency list b. adjacency matrix c. incidence matrix d. adjacency incident 8. (2 points) Consider the following adjacency matrix: a b c d e f g a 0 0 1 0 1 0 1 b 0 1 0 0 0 1 0 c 1 0 0 0 0 1 1 d 0 0 0 0 1 0 0 e 1 0 0 1 0 0 0 f 0 1 1 0 0 0 1 g 1 0 1 0 0 1 0 In the graph so described, there is a loop at vertex? a. a *b. b c. d d. f 9. (2 points) Consider the graph with following adjacency matrix: a b c d a 0 1 1 0 b 1 0 0 1 c 1 0 0 1 d 0 1 1 0 Which of the following is an Eulerian circuit through that graph? a. a-c, c-b, b-d, d-a b. a-d, d-b, b-c, c-a *c. a-b, b-d, d-c, c-a d. a-b, b-c, c-d, d-a 10. (2 points) Consider the graph described by the adjacency list {a: (b, c), b: (a, e), c: (b), d: (f, g) f: (d), g: (d)}. What are the connected components of that graph?

a. a-b-c-d and e-f-g *b. a-b-c-e and d-f-g c. a-b-c-g and d-f-g d. the whole graph is connected 11. (2 points) Consider the graph described by the adjacency list {a: (b, c, d), b: (a, c), c: (a, b, d), d: (a, c)}. Does it contain an Eulerian path? *a. Yes b. No c. Not enough information 12. (2 points) Consider the following matrix for a weighted graph; in what order will Kruskal's add edges to the MST? a b c d e f g h a 0 8 0 0 0 0 0 0 b 8 0 4 2 0 0 0 0 c 0 4 0 11 0 3 0 0 d 0 2 11 0 5 6 10 0 e 0 0 0 5 0 0 0 0 f 0 0 3 6 0 0 0 7 g 0 0 0 10 0 0 0 9 h 0 0 0 0 0 7 9 0 a. b-d, b-c, d-e, c-f, f-h, a-b, g-h b. c-f, b-d, b-c, d-e, f-h, a-b, g-h *c. b-d, c-f, b-c, d-e, f-h, a-b, g-h d. b-d, c-f, b-c, d-e, g-h, f-h, a-b 13. (2 points) Consider the following matrix for a weighted graph; in what order will Prim's add edges to the MST, if it starts at vertex a? a b c d e f g h a 0 8 0 0 0 0 0 0 b 8 0 4 2 0 0 0 0 c 0 4 0 11 0 3 0 0 d 0 2 11 0 5 6 10 0

e 0 0 0 5 0 0 0 0 f 0 0 3 6 0 0 0 7 g 0 0 0 10 0 0 0 9 h 0 0 0 0 0 7 9 0 a. a-b, c-f, b-c, d-e, f-h, b-d, g-h *b. a-b, b-d, b-c, c-f, d-e, f-h, h-g c. a-b, b-c, c-f, d-e, b-d, f-h, h-g d. a-b, c-f, b-d, b-c, d-e, f-h, h-g 14. (2 points) Consider the graph described by the adjacency list {a: (b, c, d), b: (a, c), c: (a, b, d), d: (a, c)}. Does it contain an Eulerian circuit? a. Yes *b. No c. Not enough information 15. (2 points) Consider the following matrix for a weighted graph; at what point will Dijkstra's shortest-path algorithm, looking for the shortest path from a to h, overwrite the previously recorded shortest path to an intermediate node? a b c d e f g h a 0 8 0 0 0 0 0 0 b 8 0 4 2 0 0 0 0 c 0 4 0 11 0 3 0 0 d 0 2 11 0 5 6 10 0 e 0 0 0 5 0 0 0 0 f 0 0 3 6 0 0 0 7 g 0 0 0 10 0 0 0 9 h 0 0 0 0 0 7 9 0 a. when it finds a new path to b b. when it finds a new path to e c. when it finds a new path to d *d. when it finds a new path to f