MST & Shortest-Path Algs. - - PowerPoint PPT Presentation

mst shortest path algs
SMART_READER_LITE
LIVE PREVIEW

MST & Shortest-Path Algs. - - PowerPoint PPT Presentation

MST & Shortest-Path Algs.


slide-1
SLIDE 1

MST & Shortest-Path Algs.

สมชาย ประสิทธิ์จูตระกูล ภาควิชาวิศวกรรมคอมพิวเตอร จุฬาลงกรณมหาวิทยาลัย

(28/10/46, 5/10/47)

slide-2
SLIDE 2

Outline

  • Minimum spanning trees

– Problem definition – Kruskal's algorithm – Prim's algorithm

  • Single-source shortest paths

– Problem definition – Bellman-Ford's algorithm – Dijkstra's algorithm

slide-3
SLIDE 3

Graph Representations

  • adjacency matrix
  • adjacency list

1 4 3 2 4 1

  • 2

2 3 3 2

0 1 2 3 4 0 - 4

  • 1
  • 1 -
  • 3
  • -2

2 -

  • 3

2 3 - 2

  • 4 -
  • 1

2 3 4 < (1,4), (3,1) > < (2,3), (4,-2) > < (3,3), (4,2) > < (1,2) > < >

slide-4
SLIDE 4

Minimum Spanning Trees

  • Given a weighted undirected graph G = (V,E)
  • Find a subset of E of minimum weight

forming a tree on V

1 4 3 2 4 1

  • 2

2 3 3 2 1 4 3 2 1

  • 2

2 2

slide-5
SLIDE 5

Euclidean MST

  • Given a set of points on a Euclidean space
  • Find a set of lines with minimum length

connecting the points in the space

slide-6
SLIDE 6

Applications of MSTs

  • Minimum cost power wiring
  • Computer networks
  • Clustering
  • Approximated solutions to harder problems
  • . . .
slide-7
SLIDE 7

Kruskal's Algorithm

Krusal( G=(V,E) ) { for each v in V create a set {v} put all edges in E in a priority queue Q ordered by weight T = {} while ( |T| < |V|-1 ) { (u,v) = Q.removeMin() if ( findSet(u) != findSet(v) ) { T = T U {(u,v)} union(findSet(u), findSet(v)) } } return T }

slide-8
SLIDE 8

Disjoint Sets

  • Disjoint sets whose elements are numbers from 0 to n-1

1 2 3 4 5 {0}, {1}, {2}, {3}, {4}, {5} 1 2 4 5 3 {0,1}, {2,3}, {4}, {5} 1 2 4 5 3 {0,1}, {2,3,4}, {5} 1 2 4 5 3 {0,1,2,3,4}, {5}

slide-9
SLIDE 9

Disjoint Sets : Representation

1 2 4 5 3 {0,1,2,3,4}, {5} 0 1 2 3 4 5 2 0 2 2 2 5 P 0 1 2 3 4 5

  • 2 -
  • R

findSet( e ) { if ( P[e] == e ) return e; else return findSet(P[e]); unionSet( s, t ) { if ( R[s] > R[t] ) P[t] = s; else { P[s] = t; if (R[s] == R[t]) R[t]++; } }

O(1) O(log n)

slide-10
SLIDE 10

Kruskal's Algorithm

Krusal( G=(V,E) ) { for each v in V create a set {v} put all edges in E in a priority queue Q ordered by weight // binary heap T = {} while ( |T| < |V|-1 ) { (u,v) = Q.removeMin() if ( findSet(u) != findSet(v) ) { T = T U {(u,v)} union(findSet(u), findSet(v)) } } return T } Θ(v) O(e) O(e log e) O(e log v) note : e = O(v2) for simple graphs O(e log v)

slide-11
SLIDE 11

Prim's Algorithm

Prim( G=(V,E) ) { select an arbitrary vertex v S = {v}, T = {} while( |S| < n-1 ) { select the edge (u,v) of minimum weight where u is in T and v is not add v into S add the edge into T } }

slide-12
SLIDE 12

Prim's Algorithm

T T

v v for each u adjacent to v { if( w(v,u) < d[u] ) d[u] = w(v,u); }

slide-13
SLIDE 13

Prim's Algorithm

Prim( G=(V,E) ) { for each vertex u in V { d[u] = INFINITY; p[u] = null; } select an arbitrary vertex v and let's d[v] = 0 put all vertices in V in a priority queue Q ordered by d[] // use binary heap T = {} while( !Q.isEmpty() ) { v = Q.removeMin() T = T U ((p[v], v)} for each u in Q which is adjacent to v { if( w(v,u) < d[u] ) { Q.decreaseKey(u, w(v,u)); p[u] = v; } } } return T; }

Θ(v) O(v) O(v log v) O(e log v) O(e log v)

slide-14
SLIDE 14

Prim's Algorithm

Prim( g[][] ) { for each vertex u in V { d[u] = INFINITY; p[u] = null; } select an arbitrary vertex v and let's d[v] = 0 T = {} for ( i = 1 to |V| ) { v = minIndex(d); d[v] = INFINITY; T = T U ((p[v], v)} for ( u = 1 to |V| ) { if( d[u] < INFINITY AND w(v,u) < d[u] ) { d[u] = w(v,u); p[u] = v; } } } return T; }

Θ(v) O( v2 ) O( v2 ) O( v2 )

slide-15
SLIDE 15

Kruskal vs. Prim

  • Kruskal

– good for sparse graph O(e log v) – still O(e log v), if we use path compression

  • Prim

– using simple list gives O(v2) which is optimal for dense graphs – using binary heap takes O(e log v)

slide-16
SLIDE 16

Single-Source Shortest Paths

  • Given a weighted directed graph G and a

source vertex s

  • Find a shortest path from s to every vertex

in G

  • No negative-weight cycle
  • the problem has optimal substructures

s

slide-17
SLIDE 17

Dijkstra's Algorithm

s s s s s s

slide-18
SLIDE 18

Relaxation

2 3 4

S

10 6 7 8 2 2 3 8 2 3 5 3 3 ∞ 5 2 3 4

S

9 6 7 8 2 2 3 2 3 5 3 11 5

v v

for each u adjacent to v { if( d[v]+w(v,u) < d[u] ) d[u] = d[v]+w(v,u); }

slide-19
SLIDE 19

Prim's Algorithm

Prim( G=(V,E) ) { for each vertex u in V { d[u] = INFINITY; p[u] = null; } select an arbitrary vertex v and let's d[v] = 0 put all vertices in V in a priority queue Q ordered by d[] // binary heap T = {} while( !Q.isEmpty() ) { v = Q.removeMin() T = T U ((p[v], v)} for each u in Q which is adjacent to v { if( w(v,u) < d[u] ) { Q.decreaseKey(u, w(v,u)); p[u] = v; } } } return T; }

slide-20
SLIDE 20

Dijkstra's Algorithm

Dijkstra( G=(V,E), s ) { for each vertex u in V { d[u] = INFINITY; p[u] = null; } d[s] = 0 put all vertices in V in a priority queue Q ordered by d[] // binary heap while( !Q.isEmpty() ) { v = Q.removeMin() for each u in Q which is adjacent to v { if( d[v]+w(v,u) < d[u] ) { Q.decreaseKey(u, d[v]+w(v,u)); p[u] = v; } } } return (p[], d[]); }

O(e log v)

slide-21
SLIDE 21

Negative Weight

2 3 4

S

10 6 7 8 2 2 3 8 2 3 5 3 − 2 ∞ 5 2 3 4

S

9 6 7 8 2 2 3 2 3 5 3 11 5

v v

3

Dijkstra's algorithm gives an optimal solution if there is no negative-weight edges in the graph.

slide-22
SLIDE 22

Bellman-Ford Algorithm

  • work with negative-weight edge
  • can detect negative-weight cycle
slide-23
SLIDE 23

Bellman-Ford's Algorithm

Bellman_Ford( G=(V,E), s ) { for each vertex u in V { d[u] = INFINITY; } d[s] = 0; p[s] = null; for ( i = 1 to |V| ) { for each edge (u,v) in E { if( d[v]+w(v,u) < d[u] ) { p[u] = v; d[u] = d[v]+w(v,u); } } } for each edge (u,v) in E { // if true -> negative-weight cycle if( d[v]+w(v,u) < d[u] ) return (null, null); } return (p[], d[]); }

O(ve)

slide-24
SLIDE 24

Etc.

  • Shortest path on DAG
  • Using MST to approx. TSP