Shortest paths and transitive closure Data structure 2002/12/4.

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
CS203 Lecture 15.
Every edge is in a red ellipse (the bags). The bags are connected in a tree. The bags an original vertex is part of are connected.
1 Slides based on those of Kenneth H. Rosen Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Graphs.
Graph Theory.
Reachability as Transitive Closure
Chapter 6. Konigsberg Bridge Problem A river Pregel flows around the island Keniphof and then divides into two. Four land areas A, B, C, D have this river.
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
CS138A Single Source Shortest Paths Peter Schröder.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
1 Review of some graph algorithms Graph G(V,E) (Chapter 22) –Directed, undirected –Representation Adjacency-list, adjacency-matrix Breadth-first search.
Chapter 25: All-Pairs Shortest-Paths
CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
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.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.
1 Pertemuan 24 Shortest Path Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
All-Pairs Shortest Paths
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
Using Dijkstra’s Algorithm to Find a Shortest Path from a to z 1.
5/27/03CSE Shortest Paths CSE Algorithms Shortest Paths Problems.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
All-Pairs Bottleneck Paths in Vertex Weighted graphs Asaf Shapira Microsoft Research Raphael Yuster University of Haifa Uri Zwick Tel-Aviv University.
Chapter 5 Dynamic Programming 2001 년 5 월 24 일 충북대학교 알고리즘연구실.
Chapter 2 Graph Algorithms.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
CHAPTER 6 GRAPHS All the programs in this file are selected from
Week 4 Single Source Shortest Paths All Pairs Shortest Path Problem.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
All-pairs Shortest Paths. p2. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i.
Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths.
Minimum weight spanning trees
Introduction to Algorithms Jiafen Liu Sept
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Parallel Programming: All-Pairs Shortest Path CS599 David Monismith Based upon notes from multiple sources.
Shortest Path Graph Theory Basics Anil Kishore.
Algorithms LECTURE 14 Shortest Paths II Bellman-Ford algorithm
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
CS223 Advanced Data Structures and Algorithms 1 Review for Final Neil Tang 04/27/2010.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
1 Chapter Equivalence, Order, and Inductive Proof.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
1 Section 4.1 Properties of Binary Relations A binary relation R over a set A is a subset of A  A. If (x, y)  R we also write x R y. Example. Some sample.
All-Pairs Shortest Paths
Graph Theory. undirected graph node: a, b, c, d, e, f edge: (a, b), (a, c), (b, c), (b, e), (c, d), (c, f), (d, e), (d, f), (e, f) subgraph.
Graphs 2015, Fall Pusan National University Ki-Joune Li.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
IS 2610: Data Structures Graph April 12, Graph Weighted graph – call it networks Shortest path between nodes s and t in a network  Directed simple.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
Minimum Spanning Tree Chapter 13.6.
Spanning Tree A spanning tree is any tree that consists solely of edges in G and that includes all the vertices in G. Example.
Analysis and design of algorithm
2018, Fall Pusan National University Ki-Joune Li
Chapter 11 Graphs.
2017, Fall Pusan National University Ki-Joune Li
CHAPTER 6 GRAPHS All the programs in this file are selected from
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Lecture 10 Graph Algorithms
Presentation transcript:

Shortest paths and transitive closure Data structure 2002/12/4

Is there a path? How short it can be?  Single source/ All destinations –Nonnegative edge costs –General weights  All-pairs shortest path  Transitive closure

Single source all destinations Dijkstra’s algorithm  A spanning tree again  For nonnegative edge costs (Why?)  Start from a vertex v, greedy method  dist[w]: the shortest length to w through S v u w length( ) dist[w] dist[u]

shortest() Void shortestpath(int v, int cost[][MAX_VERTICES], int dist [], int n, int found[]) { int i,u,w; for (i=0;i<n;i++) { found[i]=FALSE; dist [i] = cost[v][i]; } found[v]=TRUE; dist [v]=0; for(i=0;i<n-2;i++){ u=choose(dist,n,found); found[u]=TRUE; for(w=0;w<n;w++) if(dist [u]+cost[u][w] < dist [w]) dist [w] = dist [u]+cost[u][w]; } O(n^2)

An example San Francisco Los Angeles Denver Chicago Boston New York Miami New Orleans

Single source all destinations BellmanFord algorithm  For general weights  Path has at most n-1 edges, otherwise…  dist k [u]: from v to u, has at most k edges 

BellmanFord() Void BellmanFord(int n, int v) { int i,k; for (i =0;i<n;i++) dist[i] = length[v][i]; for(k=2;k<=n-1;k++) for(each u s.t. u!=v and u has at least one incoming edge) for(each in the graph) if(dist([u]>dist[i]+length[i][u]) dist[u]=dist[i]+length[i][u]; } O(n^3)

All-Pairs shortest paths  執行 n 次 single source all destinations algo.  Dynamic programming strategy – 利用 recursive formula 來表示 – 好好地 implement recursive formula, 用 table 輔助  A k [i][j] ≡ shortest length from i to j through no intermediate vertex greater than k  A -1 [i][j] = length[i][j]  A k [i][j] = min{A k-1 [i][j], A k-1 [i][k]+ A k-1 [k][j], k≥0

AllLengths() Void AllLength(int n) { int i,j,k; for(i=0;i<n;i++) for(j=0;j<n;j++) a[i][j] = length[i][j]; for(k=0;k<n;k++) for(i=0;i<n;i++) for(j=0;j<n;j++) if((a[i][k]+a[k][j])<a[i][j]) a[i][j] = a[i][k]+a[k][j]; } O(n^3)

Transitive closure  Definition: transitive closure matrix, A + –A + [i][j] = 1, if there’s a path of length > 0 from i to j –A + [i][j] = 0, otherwise  Definition: reflexive transitive closure matrix, A* –A*[i][j] = 1, if there’s a path of length >= 0 from i to j –A*[i][j] = 0, otherwise  O(n^3), by AllLengths()  O(n^2), an undirected graph, by connected check