Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
CS138A Single Source Shortest Paths Peter Schröder.
Introduction to Algorithms 6.046J/18.401J/SMA5503
Chapter 25: All-Pairs Shortest-Paths
Midterm <  70 3.
Introduction To Algorithms CS 445 Discussion Session 8 Instructor: Dr Alon Efrat TA : Pooja Vaswani 04/04/2005.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Shortest Paths1 C B A E D F
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
CS420 lecture twelve Shortest Paths wim bohm cs csu.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
1 Graphs: shortest paths & Minimum Spanning Tree(MST) Fundamental Data Structures and Algorithms Ananda Guna April 8, 2003.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Tree tree = connected graph with no cycle tree = connected graph with |V|-1 edges tree = graph with |V|-1 edges and no cycles.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graph Algorithms Shortest path problems [Adapted from K.Wayne]
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
All-Pairs Shortest Paths
Shortest Paths1 C B A E D F
Assignment 4. (Due on Dec 2. 2:30 p.m.) This time, Prof. Yao and I can explain the questions, but we will NOT tell you how to solve the problems. Question.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Shortest Paths C B A E D F
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Lecture 16. Shortest Path Algorithms
Introduction to Algorithms Jiafen Liu Sept
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
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.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
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 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Minimum Spanning Trees
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Introduction to Graphs
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Minimum-Cost Spanning Tree
Chapter 23 Minimum Spanning Tree
Minimum-Cost Spanning Tree
CS 583 Analysis of Algorithms
Minimum Spanning Tree Algorithms
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Minimum-Cost Spanning Tree
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

Minimum weight spanning trees

Prim’s algorithm 1) S  {1} 2) add the cheapest edge {u,v} such that u  S and v  S C S  S  {v} (until S=V)

Minimum weight spanning trees 1) S  {1} 2) add the cheapest edge {u,v} such that u  S and v  S C S  S  {v} (until S=V) P = MST output by Prim T = optimal MST Is P = T ? assume all the edgeweights different

Minimum weight spanning trees P = MST output by Prim T = optimal MST P = T assuming all the edgeweights different v 1,v 2,...,v n order added to S by Prim smallest i such that the edge e  E used by Prim to connect v i+1 is not in T.

Minimum weight spanning trees S smallest i such that the edge e  E used by Prim to connect v i+1 is not in T. S={v 1,...,v i } look at T v i+1 f

Minimum weight spanning trees S smallest i such that the edge e  E used by Prim to connect v i+1 is not in T. S={v 1,...,v i } look at T+e e f w(f) > w(e) v i+1

Minimum weight spanning trees S smallest i such that the edge e  E used by Prim to connect v i+1 is not in T. S={v 1,...,v i } look at T+e-f e f w(f) > w(e) v i+1

Prim’s algorithm for i  1 to n do C[i]   C[0]  0 S  {} while S  V do j  with minimal C[j] over j  S C S  S  { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]  w[{j,u}]; P[u]  j

O(E+V log V) for i  1 to n do C[i]   C[0]  0 S  {} while S  V do j  with minimal C[j] over j  S C S  S  { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]  w[{j,u}]; P[u]  j Extract-Min O(log n) time Decrease-Key O(1) time (amortized) Prim’s algorithm

Shortest path problem A B

A B

Longest path problem ? A B

path from u to v = sequence v 1,...,v n such that v 1 = u, v 2 = v, {v i,v i+1 }  E no repeated vertices tour from u to v = sequence v1,...,vn such that v1 = u, v2 = v, {vi,vi+1}  E no repeated edges walk from u to v = sequence v 1,...,v n such that v 1 = u, v 2 = v, {v i,v i+1 }  E  walk from u to v  exists path from u to v shortest path = shortest walk longest path  longest walk

Shortest path problem A B 0

A B 01

A B 01 2

A B 01 23

A B

A B 6

Dijkstra’s algorithm for i  1 to n do T[i]   T[0]  0 S  {} while S  V do j  with minimal T[j] over j  S C S  S  { j } for u neighbors of j do if T[j]+w[{j,u}] < T[u] then T[u]  T[j]+w[{j,u}]; P[u]  j running time ?

Dijkstra’s algorithm (for s.p.) T[0]  0; S  {}; for i  1 to n do T[i]   while S  V do j  with minimal T[j] over j  S C S  S  { j } for u neighbors of j do if T[j]+w[{j,u}] < T[u] then T[u]  T[j]+w[{j,u}]; P[u]  j Prim’s algorithm (for MST) C[0]  0; S  {}; for i  1 to n do C[i]   while S  V do j  with minimal C[j] over j  S C S  S  { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]  w[{j,u}]; P[u]  j

Dijkstra’s algorithm - correctness T[0]  0; S  {}; for i  1 to n do T[i]   while S  V do j  with minimal T[j] over j  S C S  S  { j } for u neighbors of j do if T[j]+w[{j,u}] < T[u] then T[u]  T[j]+w[{j,u}]; P[u]  j Claim: After t steps we have 1. d(0,u) = T[u] for all u  S 2. T[u] = d S (0,u) for all u, where d S (0,u) is the length of shortest path from 0 to u, such that all vertices (except possibly u) of the path are in S Proof: induction on t.

Negative edge-weights? A B

Negative edge-weights? A 2 B

A B Shortest path problem

A B Shortest path problem A B Longest path problem

A B Shortest path problem A B Longest path problem shortest path  shortest walk (if negative)

Negative edge weights allowed but no negative cycles shortest path  shortest walk (if negative)

The Bellman-Ford algorithm D[v] = estimate on the distance from 0 Initially: D[0] = 0 D[i] =  for all other vertices Relaxation of an edge: if D[u] + w(u,v) < D[v] then D[v]  D[u] + w(u,v)

The Bellman-Ford algorithm D[v] = estimate on the distance from 0 Initially: D[0] = 0 D[i] =  for all other vertices Repeat |V| times relax every edge e running time = O( V.E )

The Bellman-Ford algorithm Let S[u] be the number of edges in the shortest path from 0 to u Claim: After t steps of the algorithm S[u]  t  D[u] = d(0,u)

All-pairs shortest path problem Dijkstra  O( V 2 log V + V.E ) (negative edges not allowed)

The Floyd-Warshall algorithm dynamic programming algorithm negative edges allowed, no negative cycles d ij k = length of the shortest path from i to j which only uses vertices 1...k d ij k = min {d ij k-1,d ik k-1 + d kj k-1 }

The Floyd-Warshall algorithm d ij 0  w ij d ij k = min {d ij k-1,d ik k-1 + d kj k-1 } for k from 1 to n do for i from 1 to n do for j from 1 to n do for all i,j  V Time = ? Space = ?

The Floyd-Warshall algorithm d ij 0  w ij d ij k = min {d ij k-1,d ik k-1 + d kj k-1 } for k from 1 to n do for i from 1 to n do for j from 1 to n do for all i,j  V Time = O(n 3 ) Space = O(n 3 )

The Floyd-Warshall algorithm d ij  w ij d ij = min {d ij,d ik + d kj } for k from 1 to n do for i from 1 to n do for j from 1 to n do for all i,j  V Time = O(n 3 ) Space = O(n 2 )

Single source shortest paths Dijkstra = O(E+V log V) Bellman-Ford = O(E.V) allows negative edge-weights (but not negative cycles) All-pairs shortest paths Dijkstra = O(EV+V 2 log V) Floyd-Warshall = O(V 3 ) allows negative edge-weights (but not negative cycles) Johnson = O(EV+V 2 log V) allows negative edge-weights (but not negative cycles)

Johnson’s algorithm Dijkstra = O(E+V log V) Bellman-Ford = O(E.V) allows negative edge-weights (but not negative cycles) w’(u,v) = w(u,v) + h(u) – h(v) doesn’t change the shortest paths use Bellman-Ford to compute h such that w’ is non-negative

 M[i-1,j]  d-1 M[i,j]  d  M[i,j-1]  d-1  M[i-1,j-1]  d-1 M[i-1,j]  d-1  and M[i,j-1]  d-1 and M[i-1,j-1]  d-1 and A[i,j]=1  M[i,j]  d

if A[i,j] = 1 then M[i,j]  1+min(M[i-1,j],M[i,j-1],M[i-1,j-1]) else M[i,j]  0

If there exists a subset of size k then the k largest numbers have average at least k

Find(A[l..r], n, s, B) if r  l+1 then try all possibilities else m  median(A[l..r]); p  partition(A[l..r],m) s’  sum in A[p+1..r]; n’  (r-p) if (s+s’)/(n+n’) < B then Find(A[p+1..r], n, s,B) else Find(A[l..p],n+n’,s+s’,B) T(n)=T(n/2)+O(n)

b[k] = the largest subset of a 1,...,a k no three consecutive selected b[k-1] b[k-2] + a k b[k-3] + a k + a k-1 last not included in OPT last included in OPT, second to last not last 2 in OPT

b[0]  0; b[1]  a 1 ; b[2]  a 1 +a 2 ; c[0]  1; c[1]  2; c[2]  3; for k from 3 to n do b[k]  b[k-1]; c[k]  1; if a k +b[k-2] > b[k] then b[k]  a k +b[k-1]; c[k]  2; if a k +a k-1 +b[k-3] > b[k] then b[k]  a k +a k-1 +b[k-3]; c[k]  3;\ k  n while (k  0) do if c[k]=3 then print(a k,a k-1 ); k  k-3; if c[k]=2 then print(a k ); k  k-2; if c[k]=1 then k  k-1;

P[0...M,0..k] P[S,i] = can obtain sum S using a 1,...,a i P[0,0]  true; for i from to M do P[i,0]  false; for i from 1 to n do for S from 0 to n do P[S,i]  P[S,i-1] if a i  S and P[S-a i,i-1] then P[S,i]  true return P[M,n]

P[0...M,0..k] P[S,i] = k means can obtain sum S using k numbers from a 1,...,a i P[0,0]  0; for i from to M do P[i,0]   ; for i from 1 to n do for S from 0 to n do P[S,i]  P[S,i-1] if a i  S then P[S,i]  min(P[S,i-1],P[S-a i,i-1]+1) return P[M,n]

rev(G) H = empty graph on |V| vertices for v  V do for each out-neighbor u of v do append(H,u,v) return H sort(G) = rev(rev(G)) undirected = compare lists rev(rev(rev(G))) and rev(rev(G))

M[0]  0 for i from 1 to n do for k from 1 to  i 1/2  do M[i]=min(M[i],M[i-k 2 ]) Lagrange’s theorem: for all n we have M[n]  4 Legendre: if n  4 k (8m + 7)  M[n]  3

M[i] = sum of the largest increasing subsequence which ends with the i-th element for i from 1 to n do M[i]  A[i] for j from 0 to n do if A[j]<A[i] then M[i]  max(M[i],M[j]+A[i]) Output(n,u) if n>0 then i  index with maximum M[i] subject to A[i]<u print(A[i]); Output(i-1,A[i])