algorithms bellman-ford and floyd

18
Algorithms Bellman-Ford and Floyd Two basic algorithms for path searching in a graph Evlogi Hristov Telerik Corporation Student at Telerik Academy

Upload: adamma

Post on 23-Feb-2016

82 views

Category:

Documents


0 download

DESCRIPTION

Algorithms Bellman-Ford and Floyd. Two basic algorithms for path searching in a graph. Evlogi Hristov. Telerik Corporation. Student at Telerik Academy. Table of Contents. Relaxing of edges and paths Negative Edge Weights Negative Cycles Bellman-Ford algorithm Description Pseudo code - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Algorithms Bellman-Ford and Floyd

AlgorithmsBellman-Ford and

FloydTwo basic algorithms for path searching in a

graph

Evlogi Hristov

Telerik Corporation

Student at Telerik Academy

Page 2: Algorithms Bellman-Ford and Floyd

Table of Contents1. Relaxing of edges and paths2. Negative Edge Weights3. Negative Cycles4. Bellman-Ford algorithm

Description Pseudo code

5. Floyd-Warshall algorithm Description Pseudo code

2

Page 3: Algorithms Bellman-Ford and Floyd

Relaxing Edges

Page 4: Algorithms Bellman-Ford and Floyd

Relaxing Edges

if dist[v] > dist[u] + w dist[v] = dist[u] + w

-1

2

52 74

2 + 2 = 4

2 + 5 = 7

4 -1 = 33

Edge relaxation : Test whether traveling along a given edge gives a new shortest path to its destination vertex

Path relaxation: Test whether traveling through a given vertex gives a new shortest path connecting two other given vertices

Page 5: Algorithms Bellman-Ford and Floyd

Negative Edges and Cycles

Page 6: Algorithms Bellman-Ford and Floyd

Negative Edges and Cycles

Negative edges & Negative cycles A cycle whose edges sum to a

negative value Cannot produce a correct "shortest

path" answer if a negative cycle is reachable from the source

S 2-3

-2 1

32 5

2

0

0

3

Page 7: Algorithms Bellman-Ford and Floyd

Bellman-FordAlgorithm

Page 8: Algorithms Bellman-Ford and Floyd

Bellman-Ford Based on dynamic programming approach

Shortest paths from a single source vertex to all other vertices in a weighted graph Slower than other algorithms but

more flexible, can work with negative weight edges

Finds negative weight cycles in a graph

Worst case performance O(V·E)

8

Page 9: Algorithms Bellman-Ford and Floyd

Bellman-Ford (2)

9

//Step 1: initialize graphfor each vertex v in vertices if v is source then dist[v] = 0 else dist[v] = infinity

//Step 2: relax edges repeatedlyfor i = 1 to i = vertices-1 for each edge (u, v, w) in edges if dist[v] > dist[u] + w dist[v] = dist[u] + w

//Step 3: check for negative-weight cyclesfor each edge (u, v, w) in edges if dist[v] > dist[u] + w error "Graph contains a negative-weight cycle"

Page 10: Algorithms Bellman-Ford and Floyd

10

Bellman-Ford Algorithm

Live Demo

A-1

1

0

5

B

C D

23E

-3

2

4

∞∞

∞∞

Page 11: Algorithms Bellman-Ford and Floyd

Bellman-Ford (3) Flexibility

Optimizations http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm

Disadvantages Does not scale well Count to infinity (if node is

unreachabel) Changes of network topology are not

reflected quickly (node-by-node)11

for i = 1 to i = n for each edge (u, v, w) in edges if dist[v] > dist[u] + w dist[v] = dist[u] + w

Page 12: Algorithms Bellman-Ford and Floyd

Floyd-WarshallAlgorithm

Page 13: Algorithms Bellman-Ford and Floyd

Floyd-Warshall Based on dynamic programming approach

All shortest paths through the graph between each pair of vertices Positive and negative edges Only lengths No details about the path

Worst case performance: O(V 3)13

Page 14: Algorithms Bellman-Ford and Floyd

Floyd-Warshall (2)

14

let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity)

for each vertex v dist[v][v] == 0

for each edge (u,v) dist[u][v] = w(u,v) // the weight of the edge (u,v)

for k = 1 to k = |V| for i = 1 to i = |V| for j = 1 to j = |V| if (dist[i][j] > dist[i][k] + dist[k][j]) dist[i][j] = dist[i][k] + dist[k][j]

Page 15: Algorithms Bellman-Ford and Floyd

15

Floyd-Warshall Algorithm

Live Demo

A

-4

11

4

B

D C

12

Page 16: Algorithms Bellman-Ford and Floyd

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезанияASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET

курсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGapfree C# book, безплатна книга C#, книга Java, книга C# Дончо Минков - сайт за програмиране

Николай Костов - блог за програмиранеC# курс, програмиране, безплатно

?? ? ?

??? ?

?

? ?

??

?

?

? ?

Questions?

?

Bellman-Ford

http://algoacademy.telerik.com

Page 17: Algorithms Bellman-Ford and Floyd

Links for more information

Negative weights http://www.informit.com/articles/article.

aspx?p=169575&seqNum=8

MIT Lecture and Proof http://videolectures.net/mit6046jf05_de

maine_lec18/

Optimizations fhttp://en.wikipedia.org/wiki/Bellman%E

2%80%93Ford_algorithmy

Page 18: Algorithms Bellman-Ford and Floyd

Free Trainings @ Telerik Academy

“C# Programming @ Telerik Academy csharpfundamentals.telerik.com

Telerik Software Academy academy.telerik.com

Telerik Academy @ Facebook facebook.com/TelerikAcademy

Telerik Software Academy Forums forums.academy.telerik.com