TA: Jade Cheng ICS 241 Recitation Lecture Note #9 October 23, 2009

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

Lecture 19 Shortest Path vs Spanning Tree Max-Flow Problem. October 25, 2009

We will discuss about three different static routing algorithms 1. Shortest Path Routing 2. Flooding 3. Flow Based Routing

CS350: Data Structures Dijkstra s Shortest Path Alg.

Dynamic Programming II

Mon Tue Wed Thurs Fri

Graph Theory: Week 1

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

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

More Dynamic Programming Floyd-Warshall Algorithm (All Pairs Shortest Path Problem)

Lecture 14: Shortest Paths Steven Skiena

Chapter 10. Fundamental Network Algorithms. M. E. J. Newman. May 6, M. E. J. Newman Chapter 10 May 6, / 33

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

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

Van Phuc Pham Abstract

CS 3410 Ch 14 Graphs and Paths

Data Structures in Java. Session 17 Instructor: Bert Huang

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

Introduction to Graph Theory

From Routing to Traffic Engineering

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

All Pairs Shortest Paths

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

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

Basics of Computational Geometry

Graphs Data Structures

Shortest Paths. CSE 373 Data Structures Lecture 21

Jade Yu Cheng ICS 311 Homework 8 Sep 29, 2008

Lecture #7. 1 Introduction. 2 Dijkstra s Correctness. COMPSCI 330: Design and Analysis of Algorithms 9/16/2014

Routing Algorithms. CS158a Chris Pollett Apr 4, 2007.

Today s Outline. CSE 326: Data Structures. Topic #15: Cool Graphs n Pretty Pictures. A Great Mathematician. The Bridges of Königsberg

Shortest path problems

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

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

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

Computer Science and Software Engineering University of Wisconsin - Platteville. 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018

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

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

Student: Yu Cheng (Jade) ICS 412 Homework #3 October 07, Explain why a spin-lock is not a good idea on a single processor, single machine.

Introduction To Graphs and Networks. Fall 2013 Carola Wenk

VERSION Lab 3: Link Layer

Lecture I: Shortest Path Algorithms

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

CS1102: Macros and Recursion

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

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

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

TIE Graph algorithms

22.1 Representations of graphs

Exam 3 Practice Problems

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.

Shortest Path Approximation on Triangulated Meshes. By Annie Luan

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

Artificial Intelligence

The Shortest Path Problem

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Optimization II: Dynamic programming

Unit-5 Dynamic Programming 2016

Adjacency Matrix Undirected Graph. » Matrix [ p, q ] is 1 (True) if < p, q > is an edge in the graph. Graphs Representation & Paths

Minimum Spanning Trees

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

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

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

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

CSE 3101 Design and Analysis of Algorithms Practice Test for Unit 1 Loop Invariants and Iterative Algorithms

Transportation Network Development and Algorithm

Load Balancing and Termination Detection

LEC13: SHORTEST PATH. CSE 373 Analysis of Algorithms Fall 2016 Instructor: Prof. Sael Lee. Lecture slide courtesy of Prof.

Jade Yu Cheng ICS 311 Homework 7 Sep 18, 2008

CS 326: Operating Systems. CPU Scheduling. Lecture 6

CHAPTER 13 GRAPH ALGORITHMS ORD SFO LAX DFW

Chapter 9 Graph Algorithms

Data Structures and Algorithms

Math 1505G, 2013 Graphs and Matrices

Lecture 28: Graphs: Shortest Paths (Part 1)

Computer Networking. Intra-Domain Routing. RIP (Routing Information Protocol) & OSPF (Open Shortest Path First)

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

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

Data Structures and Algorithms

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS

Design and Analysis of Algorithms

Graphs. The ultimate data structure. graphs 1

Today s Outline. Graph ADT? Graph Definitions. Trees as Graphs. Graphs Chapter 9 in Weiss. More Definitions: Simple Paths and Cycles.

How Routing Algorithms Work

8. The Postman Problems

Design and Analysis of Algorithms

Last week: Breadth-First Search

Static Load Balancing

EECS 203 Lecture 20. More Graphs

Theorem: The greedy algorithm for RKP will always output the optimal solution under Selection Criterion 3. Proof sketch: Assume that the objects have

Junior Circle Meeting 3 Circuits and Paths. April 18, 2010

CS 4349 Lecture October 23rd, 2017

UNIVERSITY OF TORONTO Department of Computer Science April 2014 Final Exam CSC373H1S Robert Robere Duration - 3 hours No Aids Allowed.

Fishnet Assignment 2: Routing Due: Wednesday, February 2 at the beginning of class CSE/EE 461: Winter 2005


Shortest Path Problem

2. CONNECTIVITY Connectivity

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 7 Lecturer: David Wagner February 13, Notes 7 for CS 170

Transcription:

TA: Jade Cheng ICS 241 Recitation Lecture Note #9 October 23, 2009 Recitation #9 Question: For each of these problems about a subway system, describe a weighted graph model that can be used to solve the problem. [Chapter 9.6 Review] a. What is the least amount of time required to travel between two stops? Answer: A simple graph with weighted edges will do it: Vertices: The stops that are presented on the subway map. Edges: The adjacent stops are connected with edges. Weights: The times required to travel between adjacent stops. b. What is the minimum distance that can be traveled to reach a stop from another stop? Answer: A simple graph with weighted edges will do it: Vertices: Same as above Edges: The adjacent stops are connected with edges. Weights: The distances between adjacent stops. c. What is the least fare required to travel between two stops if fares between stops are added to give the total fare? Answer: A simple graph with weighted edges will do it: Vertices: The stops that are presented on the subway map. Edges: The adjacent stops are connected with edges. Weights: The fares between adjacent stops. Question: Find the length of the shortest path in the following edge weighted graphs by tracing Dijkstra s Algorithm. [Chapter 9.6 Review]

Review: The pseudo code for Dijkstra s algorithm: // has vertices,,, _ and weights, // where, if, is not an edge in procedure Dijkstra (: weighted connected simple graph, with all weights positive) // Initialize All labels are to be infinite. for 1 to // Initialize the label of, the starting vertex, to be 0. 0 // Initialize set to be an empty set. // At the end of the main loop, length of a shortest path from to. while begin // Find the vertex with the minimal label, and add it to set. vertex not in with minimal endwhile end // Update the labels of vertices that are not in. for all vertices not in if, then, a. Find the shortest path between and in the given graph, use vertex as the source.

Answer: Let s start from the original graph: Step 1: Step 2: 0 0 2 2 0 5 5 Step 3: Step 4:,,, 0 0 2 2 2 2 4 4 2 6 8 8 4 6 10 Step 5: Step 6:,,,,,,, 0 0 2 2 4 4 8 8 10 10

At this point, vertex, the destination, in this case is added to set, therefore the main loop condition,, can t be satisfied any more. Program exit the loop and terminate. Now let s take a look at what we have obtained from the algorithm execution. Obviously we have a set of labels, which represent the lengths of the shortest paths from the starting vertex, in this case, to reach all other vertices of the input graph. This conclusion is clear by looking at the graph as well: We learned from this output graph that all pairs of shortest paths starting from vertex. Namely, the minimal cost of traveling from vertex to is 2, from to is 8 going through, from to is 10 going through and, and from to is 4 going through. You might wonder, is it necessary to compute them all, as the question asks for only the shortest path from to. The answer to this question is, yes! Resolving the whole world in order to find one optimal path turned out to be the best way to do it. b. Find the shortest path between and in the given graph, use vertex as the source Answer: Let s start from the original graph: Step 1: Step 2: 0 0 3 3 0 7 7

Step 3: Step 4:,,, 0 0 3 3 3 2 5 5 7 7 3 5 8 5 2 7 At this point, vertex, the destination, in this case is added to set, therefore the main loop condition,, can t be satisfied any more. Program exit the loop and terminate. Now let s take a look at what we have obtained from the algorithm execution. Obviously we have a set of labels, which represent the lengths of the shortest paths from the starting vertex, in this case, to reach all other vertices in set. They are vertices and. And we don t know about the shortest paths to the rest of the vertices in this case. This conclusion is clear by looking at the graph as well:

Question: Answer: Extend Dijkstra s algorithm for finding the length of a shortest path between two vertices in a weighted simple connected graph so that the length of a shortest path between the vertex and every other vertex of the graph is found. [Chapter 9.6 Review] As you might notice, we ve already done this once. In part a of the previous question, we trace through the algorithm and resolved the entire graph starting from the specified vertex. This is because the destination vertex happen to be the last vertex that was added in the set. Before adding the destination, we ve already learned the shortest paths from the source to all other vertices. Follow the same trend of thoughts, if we let the algorithm continue until it reaches the point that all vertices are in set, we would end up with a graph connecting all other vertices with the source in the shortest paths. Modified Dijkstra s algorithm that finds the shortest paths between and every other vertiex: // has vertices,,, _ and weights, // where, if, is not an edge in procedure Dijkstra (: weighted connected simple graph, with all weights positive) // Initialize All labels are to be infinite. for 1 to // Initialize the label of, the starting vertex, to be 0. 0 // Initialize set to be an empty set. // Main loop terminates when contains all vertices in the input graph. while begin // Find the vertex with the minimal label, and add it to set. vertex not in with minimal endwhile end // Update the labels of vertices that are not in. for all vertices not in if, then, Let s look at an example, if we apply the modified Dijkstra s algorithm on part b of the previous question, we would have two more steps:

Step 5: Step 6:,,,,,,, 0 0 3 3 5 5 7 7 7 7 Now let s take a look at what we have. We have a set of labels consisting all vertices, which represent the lengths of the shortest paths from the starting vertex, in this case, to reach all other vertices of the input graph. This conclusion is clear by looking at the graph as well: Question: Review: Use Warshall s Algorithm to find the transitive closures of the relation defined on,,,, :,,,,,,,,,. [Chapter 8.4 Review] Procedure Warshall ( zero-one matrix) for 1 to begin for 1 to begin end end is for 1 to

Answer: We talked about Warshall s Algorithm tracing by going through the loop counters. This is a time-consuming procedure, although we understand that it takes the computer no time to accomplish the task. Here we ll introduce a simpler graphical way to trace this algorithm. Maybe we ll understand the code better by looking at it from a different perspective. 0 0 1 0 0 0 0 0 1 0 The given relation can be represented as an adjacent matrix: 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0, or as a directed graph:, tells us that the algorithm goes through the 1 s and 0 s in the matrix and turns on some of the 0 bits according to the conditions. This can also be seen as extra edges added to the directed graph, of course. We are examining edge 0, if holds, we turn on 1, which is saying if vertices, and vertices, are both connected, we connect vertices,. This is the transitivity property by definition. If two vertices are connected through the vertex, this edge would be connected after this round execution. Step 0: Step 1: done with 1 The original input matrix and graph Any new nodes connected through? Yes,, so, 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0

Step 2: done with 2 Step 3: done with 3 Any new nodes connected through Any new nodes connected through? Yes,, so, Yes,, so, 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 Step 4: done with 4 Step 5: done with 5 Any new nodes connected through Any new nodes connected through? Yes,, so, No and, so, 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 At this point we ve looped from 1 to 5 for the outer loop counter. So we are done, and we have the final adjacent matrix and the graph representation. They both demonstrate the transitive closure property of the original relation on,,,,. Question: Use Warshall s Algorithm to find the transitive closure of the relations on the relation 2,1, 2,3, 3,1, 3,4, 4,1, 4,3 on 1, 2, 3, 4. [Chapter 8.4 Review]

Answer: Let s follow the same steps as we did for the previous question. The given relation can be 0 0 0 0 represented as an adjacent matrix:, or as a directed graph: 1 0 0 1 Step 1: done with 1 Step 2: done with 2 Any new nodes connected through 1? Any new nodes connected through 2? No No 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 Step 3: done with 3 Step 4: done with 4 Any new nodes connected through 3? Any new nodes connected through 4? Yes, 4 3 4, so 4, 4 Yes, 3 4 3, so 3, 3 and 2 3 4, so 2, 4 0 0 0 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 0 1 1

At this point we ve looped from 1 to 4 for the outer loop counter. So we are done, and we have the final adjacent matrix and the graph representation. They both demonstrate the transitive closure property of the original relation on 1, 2, 3, 4.