Discrete Math 2 Shortest Paths Using Matrix

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
Lecture 19: Parallel Algorithms
13 May 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Introduction.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
1 Representing Graphs. 2 Adjacency Matrix Suppose we have a graph G with n nodes. The adjacency matrix is the n x n matrix A=[a ij ] with: a ij = 1 if.
Floyd’s Algorithm (shortest-path problem) Section 8.2.
CS 261 – Data Structures Graphs. Used in a variety of applications and algorithms Graphs represent relationships or connections Superset of trees (i.e.,
Introduction to Graphs
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.
1 Parallel Algorithms III Topics: graph and sort algorithms.
Midterm 3 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Algorithms All pairs shortest path
Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and.
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
Shortest Path Algorithm This is called “Dijkstra’s Algorithm” …pronounced “Dirk-stra”
1 Network Optimization Chapter 3 Shortest Path Problems.
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
6.1 Hamilton Circuits and Paths: Hamilton Circuits and Paths: Hamilton Path: Travels to each vertex once and only once… Hamilton Path: Travels to each.
Spring 2015 Mathematics in Management Science Traveling Salesman Problem Approximate solutions for TSP NNA, RNN, SEA Greedy Heuristic Algorithms.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
Parallel Programming: All-Pairs Shortest Path CS599 David Monismith Based upon notes from multiple sources.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Chapter 14 Section 3 - Slide 1 Copyright © 2009 Pearson Education, Inc. AND.
Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from.
Shortest Paths.
Graphs Representation, BFS, DFS
Introduction to Graphs
Unit 1: Matrices Day 1 Aug. 7th, 2012.
Introduction to Graphs
Discrete Math 2 Weighted Graph Search Tree
Shortest Path Problems
Unweighted Shortest Path Neil Tang 3/11/2010
Lecture 22: Parallel Algorithms
Graphs Representation, BFS, DFS
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
All-pairs Shortest Path
All-pairs shortest path
CS200: Algorithm Analysis
Graphs.
Linear sequences A linear sequence is a name for a list of numbers where the next number is found by adding or subtracting a constant number. Here is an.
Walks, Paths, and Circuits
Shortest Path Problems
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Floyd-Warshall Algorithm
Shortest Path Algorithms
Floyd’s Algorithm (shortest-path problem)
Discrete Math 2 Shortest Paths Using Matrix
REDUCING MATRICES.
Discrete Math 2 Shortest Path Using Matrix
Algorithms: Design and Analysis
Backtracking and Branch-and-Bound
How to find the nth rule for a linear sequence
Graph Algorithms: Shortest Path
Parallel Graph Algorithms
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Matrices and Determinants
Directed Graphs (Part II)
Introduction to Graphs
All-Pairs Shortest Paths
Introduction to Graphs
Presentation transcript:

Discrete Math 2 Shortest Paths Using Matrix 2001 Discrete Math 2 Shortest Paths Using Matrix CIS112 February 14, 2007 Daniel L. Silver

Overview Previously: In weighted graph . . Shortest path from #7 to all others Search matrix method Now: Problem 8.6.2 Implement Floyd’s Algorithm 2007 Kutztown University

Strategy 3 nested loops i ≔ 1 to 6 j ≔ 1 to 6 k ≔ 1 to 6 Basic rule If d(j,i) + d(i,k) < d(j,k) . . Then d(j,k)  d(j,i) + d(i,k) 2007 Kutztown University

Interpretation of Matrix Row designates from vertices Column designates to vertices Suppose entry [2,3] is 7 From vertex #2 to vertex #3 . . cost of travel = 7 2007 Kutztown University

Seek – best cost route from j to k The Basic Operation Seek – best cost route from j to k Initial entries = cost of direct route, i.e., cost of edge jk No edge  cost = ∞ Guarantees that any route found is better 2007 Kutztown University

Old cost from j to k compared to . . Cost of 2 step hop The Basic Operation Old cost from j to k compared to . . Cost of 2 step hop j to i and then i to k If 2 step hop has better cost . . Then it becomes the new j to k cost 2007 Kutztown University

Initially old cost is edge cost Later . . The Basic Operation Initially old cost is edge cost Later . . Old cost is cost of best route found so far 2007 Kutztown University

Denotations Outer loop – the i loop Middle loop – the j loop Inner loop – the k loop 2007 Kutztown University

Operation Proceed Loops work in tandem 2 outer loops {i & j loops} 2 inner loops {j & k loops} Proceed row by row & column by column Update travel cost . . from vertex j to vertex k 2007 Kutztown University

Matrix for Weighted Graph Vtx A B C D E Z ∞ 2 3 5 1 4 2007 Kutztown University

First Outer Loop i = 1 j = ≔ 1 to 6 k ≔ 1 to 6 2007 Kutztown University

First Inner Loop i = 1 j = 1 k ≔ 1 to 6 2007 Kutztown University

First Middle Loop d(1,1) = ∞ d(1,1) + d(1,k) ≮ d(1,k), ∀k Therefore, no change 2007 Kutztown University

Second Middle Loop i = 1 j = 2 k ≔ 1 to 6 2007 Kutztown University

Second Middle Loop k = 1 d(1,1) = ∞ d(2,1) + d(1,1) = ∞ Therefore, no change 2007 Kutztown University

i=1; j=2; k = 2 (2,1) + (1,2) < (2,2) ≡ 4 < ∞ Vtx A B C D E Z ∞ 2 3 4 5 1 2007 Kutztown University

i=1; j=2; k = 3 (2,1) + (1, 3) < (2,3) ≡ 5 < ∞ Vtx A B C D E Z ∞ 2 3 4 5 1 2007 Kutztown University

i=1; j=2; k ≔ 4,5,6 (2,1) + (1,k) < (2,k) ≡ ∞ ≮ x Vtx A B C D E Z ∞ 2 3 4 5 1 2007 Kutztown University

Third Middle Loop i = 1 j = 3 k ≔ 1 to 6 2007 Kutztown University

Third Middle Loop k = 1 d(1,1) = ∞ d(3,1) + d(1,1) = ∞ Therefore, no change 2007 Kutztown University

i=1; j=3; k = 2 (3,1) + (1, 2) < (3,2) ≡ 5 < ∞ Vtx A B C D E Z ∞ 2 3 4 5 1 2007 Kutztown University

i=1; j=3; k = 3 (3,1) + (1, 3) < (3,3) ≡ 6 < ∞ Vtx A B C D E Z ∞ 2 3 4 5 6 1 2007 Kutztown University

i=1; j=3; k ≔ 4,5,6 (3,1) + (1, k) < (3,k) ≡ ∞ ≮ x Vtx A B C D E Z ∞ 2 3 4 5 6 1 2007 Kutztown University

4th, 5th & 6th Middle Loops i = 1 j ≔ 4 to 6 k ≔ 1 to 6 2007 Kutztown University

4th, 5th & 6th Middle Loops j ≔ 4 to 6 d(j,1) = ∞ d(j,1) + d(i,k) = ∞ d(j,1) + d(i,1) = ∞ ≮ d(j,k) Therefore, no change 2007 Kutztown University

Second Outer Loop i = 2 j = ≔ 1 to 6 k ≔ 1 to 6 2007 Kutztown University

i=2; j=1; k = 1 (1,2) + (2, 1) < (1,1) ≡ 4 < ∞ Vtx A B C D E Z 4 2 3 ∞ 5 6 1 2007 Kutztown University

i=2; j=1; k = 2 (1,2) + (2, 2) < (1,2) ≡ 6 ≮ 2 Vtx A B C D E Z 4 2 3 ∞ 5 6 1 2007 Kutztown University

i=2; j=1; k = 3 (1,2) + (2, 3) < (1,3) ≡ 7 ≮ 3 Vtx A B C D E Z 4 2 3 ∞ 5 6 1 2007 Kutztown University

i=2; j=1; k = 4 (1,2) + (2, 4) < (1,4) ≡ 7 < ∞ Vtx A B C D E Z 4 2 3 7 ∞ 5 6 1 2007 Kutztown University

i=2; j=1; k = 5 (1,2) + (2, 5) < (1,5) ≡ 4 < ∞ Vtx A B C D E Z 4 2 3 7 ∞ 5 6 1 2007 Kutztown University

i=2; j=1; k = 6 (1,2) + (2, 6) < (1,6) ≡ ∞ ≮ ∞ Vtx A B C D E Z 4 2 3 7 ∞ 5 6 1 2007 Kutztown University

Second Middle Loop i = 2 j = 2 k ≔ 1 to 6 2007 Kutztown University

Second Middle Loop k ≔ 1 to3 i j k 2 2 1 (2,2) + (2,1) < (2,1) ? 4 + 2 ≮ 2 2 2 2 (2,2) + (2,2) < (2,2) ? 4 + 4 ≮ 4 2 2 3 (2,2) + (2,3) < (2,3) ? 4 + 5 ≮ 5 2007 Kutztown University

Second Middle Loop k ≔ 4 to6 i j k 2 2 4 (2,2) + (2,4) < (2,4) ? 4 + 5 ≮ 5 2 2 5 (2,2) + (2,5) < (2,5) ? 4 + 2 ≮ 2 2 2 6 (2,2) + (2,6) < (2,6) ? 4 + ∞ ≮ ∞ 2007 Kutztown University

Matrix After 2nd Middle Loop {no change} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 1 2007 Kutztown University

Third Middle Loop k ≔ 1 to3 i j k 2 3 1 2 3 2 2 3 3 2 3 1 (3,2) + (2,1) < (3,1) ? 5 + 2 ≮ 3 2 3 2 (3,2) + (2,2) < (3,2) ? 5 + 4 ≮ 5 2 3 3 (3,2) + (2,3) < (3,3) ? 5 + 5 ≮ 6 2007 Kutztown University

Third Middle Loop k ≔ 4 to6 i j k 2 3 4 2 3 5 2 3 6 2 3 4 (3,2) + (2,4) < (3,4) ? 5 + 5 < ∞ 2 3 5 (3,2) + (2,5) < (3,5) ? 5 + 2 ≮ 5 2 3 6 (3,2) + (2,6) < (3,6) ? 5 + ∞ ≮ ∞ 2007 Kutztown University

Matrix After 3rd Middle Loop {1 cell changed} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 10 1 2007 Kutztown University

Fourth Middle Loop k ≔ 1 to3 i j k 2 4 1 (4,2) + (2,1) < (4,1) ? 5 + 2 < ∞ 2 4 2 (4,2) + (2,2) < (4,2) ? 5 + 4 ≮ 5 2 4 3 (4,2) + (2,3) < (4,3) ? 5 + 5 < ∞ 2007 Kutztown University

Fourth Middle Loop k ≔ 4 to6 i j k 2 4 4 (4,2) + (2,4) < (4,4) ? 5 + 5 < ∞ 2 4 5 (4,2) + (2,5) < (4,5) ? 5 + 2 ≮ 1 2 4 6 (4,2) + (2,6) < (4,6) ? 5 + ∞ ≮ 2 2007 Kutztown University

Matrix After 4th Middle Loop {3 cells changed} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 10 1 2007 Kutztown University

Fifth Middle Loop k ≔ 1 to3 i j k 2 5 1 2 5 2 2 5 3 2 5 1 (5,2) + (2,1) < (5,1) ? 2 + 2 < ∞ 2 5 2 (5,2) + (2,2) < (5,2) ? 2 + 4 ≮ 2 2 5 3 (5,2) + (2,3) < (5,3) ? 2 + 5 ≮ 5 2007 Kutztown University

Fifth Middle Loop k ≔ 4 to6 i j k 2 5 4 2 5 5 2 5 4 (5,2) + (2,4) < (5,4) ? 2 + 5 ≮ 1 2 5 5 (5,2) + (2,5) < (5,5) ? 2 + 2 < ∞ 2 5 6 (5,2) + (2,6) < (5,6) ? 2 + ∞ ≮ 4 2007 Kutztown University

Matrix After 5th Middle Loop {2 cells changed} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 10 1 2007 Kutztown University

Sixth Middle Loop k ≔ 1 to3 i j k 2 6 1 2 6 3 2 6 1 (6,2) + (2,1) < (6,1) ? ∞ + 2 ≮ ∞ 2 6 2 (6,2) + (2,2) < (6,2) ? ∞ + 4 ≮ ∞ 2 6 3 (6,2) + (2,3) < (6,3) ? ∞ + 5 ≮ ∞ 2007 Kutztown University

Sixth Middle Loop k ≔ 4 to6 i j k 2 6 4 2 6 5 2 6 4 (6,2) + (2,4) < (6,4) ? ∞ + 5 ≮ ∞ 2 6 5 (6,2) + (2,5) < (6,5) ? ∞ + 2 ≮ ∞ 2 6 6 (6,2) + (2,6) < (6,6) ? ∞ + ∞ ≮ ∞ 2007 Kutztown University

Matrix After 6th Middle Loop {0 cells changed} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 10 1 2007 Kutztown University

Third Outer Loop i = 3 j = ≔ 1 to 6 k ≔ 1 to 6 Step by step details given here 2007 Kutztown University

Matrix After 3rd Outer Loop {0 cells changed} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 10 1 2007 Kutztown University

Fourth Outer Loop i = 4 j = ≔ 1 to 6 k ≔ 1 to 6 Step by step details given here 2007 Kutztown University

Matrix After 4th Outer Loop {10 cells changed} Vtx A B C D E Z 4 2 3 7 9 5 6 10 12 1 2007 Kutztown University

Fifth Outer Loop i = 5 j = ≔ 1 to 6 k ≔ 1 to 6 Step by step details given here 2007 Kutztown University

Matrix After 5th Outer Loop {12 cells changed} Vtx A B C D E Z 4 2 3 5 7 6 8 1 2007 Kutztown University

Sixth Outer Loop i = 6 j = ≔ 1 to 6 k ≔ 1 to 6 Step by step details given here 2007 Kutztown University

Matrix After 6th Outer Loop {0 cells changed} Vtx A B C D E Z 4 2 3 5 7 6 8 1 2007 Kutztown University

Final Matrix Diagonal values are removed Vtx A B C D E Z - 2 3 5 4 7 6 8 1 2007 Kutztown University

Path Costs from A Given by row entries A  B :: 2 A  C :: 3 A  D :: 5 A  E :: 4 A  Z :: 7 2007 Kutztown University

Path Costs from B Given by row entries B  A :: 2 B  C :: 5 B  D :: 3 B  E :: 2 B  Z :: 5 2007 Kutztown University

Path Costs from C Given by row entries C  A :: 3 C  B :: 5 C  D :: 6 C  E :: 5 C  Z :: 8 2007 Kutztown University

Path Costs from D Given by row entries D  A :: 5 D  B :: 3 D  C :: 6 D  E :: 1 D  Z :: 2 2007 Kutztown University

Path Costs from E Given by row entries E  A :: 4 E  B :: 2 E  C :: 5 E  D :: 1 E  Z :: 3 2007 Kutztown University

Path Costs from Z Given by row entries Z  A :: 7 Z  B :: 5 Z  C :: 8 Z  D :: 2 Z  E :: 3 2007 Kutztown University

Shortest Paths Floyd’s Algorithm gives the path costs Finding the actual paths reuqires additional work We will need two matrices One to hold travel costs Other to hold actual paths 2007 Kutztown University