bellmanford. bellmanford(g,w,s) 1 initializesinglesource(g,s) 2 for i 1 to |v[g]| - 1 3 do for each...

49
BellmanFor d

Post on 20-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord

Page 2: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

Page 3: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

i = 0

6

7

9

2

-4

-3

7

5

-2

8

s

z

y

xt

0

Page 4: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

i = 1

6

7

9

2

-4

-3

7

5

-2

8

s

z

y

xt

0

7

2

Page 5: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

i = 2

6

7

9

2

-4

-3

7

5

-2

8

s

z

y

xt

0

7

2

5

13

Page 6: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

i = 2

6

7

9

2

-4

-3

7

5

-2

8

s

z

y

xt

0

7

2

5

9

Page 7: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

i = 3

6

7

9

2

-4

-3

7

5

-2

8

s

z

y

xt

0

6

2

5

9

Page 8: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

i = 4

6

7

9

2

-4

-3

7

5

-2

8

s

z

y

xt

0

6

2

4

9

Correctness

I) If no negative cycle reachable from s: BF returns true, BF finds shortest paths, BF builds predecessor tree

II) Otherwise: BF returns false

Page 9: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

Page 10: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

O(V)

Page 11: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

O(V)

O(V*E)

Page 12: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

O(V)

O(V*E)

O(E)

Page 13: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

BellmanFord(G,w,s)

1 InitializeSingleSource(G,s)

2 for i 1 to |V[G]| - 1

3 do for each (u,v) E[G]

4 do Relax(u,v,w)

5 for each edge (u,v) E[G]

6 do if d[v] > d[u] + w[u,v]

7 then return false

8 return true

O(V)

O(V*E)

O(E)

O(V*E)

Page 14: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAG Shortest Path

Page 15: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

Page 16: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

6

72

-3

7

-2

8

s

z

y

xt

Page 17: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

6

72

-3

7

-2

8

s

z

y

xt

1/

2/

3/4

Page 18: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

6

72

-3

7

-2

8

s

z

y

xt

1/10

2/7

3/4

5/6 8/9

Page 19: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

8 0

Page 20: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

8 0 7 2

Page 21: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

8 0 7 2 5

Page 22: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

80 7 2 5 9

Page 23: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

80 7 2 5 9

Correct?

Page 24: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

80 7 2 5 9

Correct? Yes, follows directly from L4 and L5

Page 25: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

80 7 2 5 9

Time?

Page 26: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

80 7 2 5 9

O(V+E)

Time?

Page 27: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

80 7 2 5 9

O(V+E)

Time?

O(V)

Page 28: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

80 7 2 5 9

O(V+E)

Time?

O(V)

O(E)

Page 29: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

DAGshortestPaths(G,w,s)

1 topologically sort the vertices of G

2 InitializeSingleSource(G,s)

3 for each vertex u, taken in topological order

4 do for each vertex v adj[u]

5 do Relax(u,v,w)

s

z

y

xt

1/10

2/7

3/4

5/68/97

2 -2

6

7

80 7 2 5 9

O(V+E)

Time: O(V+E) – linear in |adj|

O(V)

O(E)

Page 30: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

Dijkstra’s Algorithm(no negative edges)

Greedy

Page 31: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

Page 32: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

Page 33: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

3

4

Page 34: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

3

4

Could these be optimal?

Page 35: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

3

4

Could these be optimal?I don’t know yet

1?

1 ?

Page 36: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

3

4

optimal?

Page 37: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

3

4

optimal? Yes any path from “3” and “4” will be non-neg, and there is no unexplored paths from “0”

Page 38: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

3

4

Page 39: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

2

4

12

4

3

5

4

Page 40: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

2

4

12

4

3

5

4

Optimal?

Page 41: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

2

4

12

4

3

5

4

Optimal? No, as before there could be frontier edges causing better paths

Page 42: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

2

4

12

4

3

5

4

optimal?

Page 43: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

0

1

34

1

2

4

12

4

3

5

4

optimal? Yes!

As before no better path from frontier,

No better path from explored vertices

Page 44: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

Dijkstra(G,w,s)

1 InitializeSingleSource(G,s)

2 S Ø

3 Q V[G]

4 while Q Ø

5 do u ExtractMin(Q)

6 S S {u}

7 for each vertex v Adj[u]

8 do Relax(u,v,w)

Page 45: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

Dijkstra(G,w,s)

1 InitializeSingleSource(G,s)

2 S Ø

3 Q V[G]

4 while Q Ø

5 do u ExtractMin(Q)

6 S S {u}

7 for each vertex v Adj[u]

8 do Relax(u,v,w)

O(V)

Page 46: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

Dijkstra(G,w,s)

1 InitializeSingleSource(G,s)

2 S Ø

3 Q V[G]

4 while Q Ø

5 do u ExtractMin(Q)

6 S S {u}

7 for each vertex v Adj[u]

8 do Relax(u,v,w)

O(V)

BinHO(V) – Build Heap

Page 47: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

Dijkstra(G,w,s)

1 InitializeSingleSource(G,s)

2 S Ø

3 Q V[G]

4 while Q Ø

5 do u ExtractMin(Q)

6 S S {u}

7 for each vertex v Adj[u]

8 do Relax(u,v,w)

O(V)

BinHO(V) – Build Heap

O(VlgV)

Page 48: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

Dijkstra(G,w,s)

1 InitializeSingleSource(G,s)

2 S Ø

3 Q V[G]

4 while Q Ø

5 do u ExtractMin(Q)

6 S S {u}

7 for each vertex v Adj[u]

8 do Relax(u,v,w)

O(V)

BinHO(V) – Build Heap

O(V*lgV)

O(E*lgV) – Dec.Key

Page 49: BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

Dijkstra(G,w,s)

1 InitializeSingleSource(G,s)

2 S Ø

3 Q V[G]

4 while Q Ø

5 do u ExtractMin(Q)

6 S S {u}

7 for each vertex v Adj[u]

8 do Relax(u,v,w)

O(V)

BinHO(V) – Build Heap

O(V*lgV)

O(E*lgV) – Dec.Key

Time: O( (V+E)*lgV )