• No results found

BellmanFord

N/A
N/A
Protected

Academic year: 2020

Share "BellmanFord"

Copied!
39
0
0

Loading.... (view fulltext now)

Full text

(1)

Bellman-Ford

Used to find the shortest path between vertices

in a directed graph

Can be used to detect negative cycles

Can work with negative edges

Does not work with negative cycles

(2)

Algorithm Steps

Loop for all vertices – 1

Loop for all edges

• If source  vertex  destination < source  destination

– Update distance from source to destination

If no changes were made the outer loop can

terminate early

Check for negative cycles

For all edges (u, v), there is a negative cycle if…

• If the distance from the source to vertex u • Plus the weight of the edge (u,v)

(3)

Complexity

Can be done in O(VE) time

(4)

Example

A B

C D

1

3

4

6 -2

(5)

Edge List

(6)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B A-C A-D B-A C-D D-B D-C

A B

C D

1

3

4

6 -2

(7)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 0 + 3 ∞ ∞

A-C A-D B-A C-D D-B D-C

Relax Edge A-B (A

A + |A-B|)

A B

C D

1

3

4

6 -2

(8)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞

A-C A-D B-A C-D D-B D-C

Relax Edge A-B (A

A + |A-B|)

A B

C D

1

3

4

6 -2

(9)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞

A-C 0 3 0 + 1 ∞ A-D

B-A C-D D-B D-C

Relax Edge A-C (A

A + |A-C|)

A B

C D

1

3

4

6 -2

2

(10)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞

A-C 0 3 1 ∞ A-D

B-A C-D D-B D-C

Relax Edge A-C (A

A + |A-C|)

A B

C D

1

3

4

6 -2

(11)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞ A-C 0 3 1 ∞

A-D 0 3 1 0 - 3

B-A C-D D-B D-C

Relax Edge A-D (A

A + |A-D|)

A B

C D

1

3

4

6 -2

(12)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞ A-C 0 3 1 ∞

A-D 0 3 1 -3

B-A C-D D-B D-C

Relax Edge A-D (A

A + |A-D|)

A B

C D

1

3

4

6 -2

(13)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞ A-C 0 3 1 ∞ A-D 0 3 1 -3

B-A 0 3 1 -3 C-D

D-B D-C

Relax Edge B-A (A

B + |B-A|)

A B

C D

1

3

4

6 -2

2 -3

(14)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞ A-C 0 3 1 ∞ A-D 0 3 1 -3 B-A 0 3 1 -3

C-D 0 3 1 -3

D-B D-C

Relax Edge C-D (A

C + |C-D|)

A B

C D

1

3

4

6 -2

2 -3

(15)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞ A-C 0 3 1 ∞ A-D 0 3 1 -3 B-A 0 3 1 -3 C-D 0 3 1 -3

D-B 0 3 1 -3 D-C

Relax Edge D-B (A

D + |D-B|)

A B

C D

1

3

4

6 -2

2 -3

(16)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞ A-C 0 3 1 ∞ A-D 0 3 1 -3 B-A 0 3 1 -3 C-D 0 3 1 -3 D-B 0 3 1 -3 D-C

Relax Edge D-C (A

D + |D-C|)

A B

C D

1

3

4

6 -2

(17)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞ A-C 0 3 1 ∞ A-D 0 3 1 -3 B-A 0 3 1 -3 C-D 0 3 1 -3 D-B 0 3 1 -3

D-C 0 3 -3 + 2 -3

Relax Edge D-C (A

D +|D-C|)

A B

C D

1

3

4

6 -2

(18)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞ A-C 0 3 1 ∞ A-D 0 3 1 -3 B-A 0 3 1 -3 C-D 0 3 1 -3 D-B 0 3 1 -3

D-C 0 3 -1 -3

Relax Edge D-C (A

D + |D-C|)

A B

C D

1

3

4

6 -2

(19)

Iteration 1

A B C D

X 0 ∞ ∞ ∞

A-B 0 3 ∞ ∞ A-C 0 3 1 ∞ A-D 0 3 1 -3 B-A 0 3 1 -3 C-D 0 3 1 -3 D-B 0 3 1 -3 D-C 0 3 -1 -3

Iteration 1 Complete

A B

C D

1

3

4

6 -2

(20)
(21)

Iteration 2

A B C D

X 0 3 -1 -3

A-B A-C A-D B-A C-D D-B D-C

A B

C D

1

3

4

6 -2

(22)

Iteration 2

A B C D

X 0 3 -1 -3

A-B 0 3 -1 -3 A-C

A-D B-A C-D D-B D-C

Relax Edge A-B (A

A + |A-B|)

A B

C D

1

3

4

6 -2

2 -3

(23)

Iteration 2

A B C D

X 0 3 -1 -3

A-B 0 3 -1 -3

A-C 0 3 -1 -3 A-D

B-A C-D D-B D-C

Relax Edge A-C (A

A + |A-C|)

A B

C D

1

3

4

6 -2

2 -3

(24)

Iteration 2

A B C D

X 0 3 -1 -3

A-B 0 3 -1 -3 A-C 0 3 -1 -3

A-D 0 3 -1 -3

B-A C-D D-B D-C

Relax Edge A-D (A

A + |A-D|)

A B

C D

1

3

4

6 -2

2 -3

(25)

Iteration 2

A B C D

X 0 3 -1 -3

A-B 0 3 -1 -3 A-C 0 3 -1 -3 A-D 0 3 -1 -3

B-A 0 3 -1 -3 C-D

D-B D-C

Relax Edge B-A (A

B + |B-A|)

A B

C D

1

3

4

6 -2

2 -3

(26)

Iteration 2

A B C D

X 0 3 -1 -3

A-B 0 3 -1 -3 A-C 0 3 -1 -3 A-D 0 3 -1 -3 B-A 0 3 -1 -3

C-D 0 3 -1 -3

D-B D-C

Relax Edge C-D (A

C + |C-D|)

A B

C D

1

3

4

6 -2

2 -3

(27)

Iteration 2

A B C D

X 0 3 -1 -3

A-B 0 3 -1 -3 A-C 0 3 -1 -3 A-D 0 3 -1 -3 B-A 0 3 -1 -3 C-D 0 3 -1 -3 D-B 0 3 -1 -3 D-C

Relax Edge D-B (A

D + |D-B|)

A B

C D

1

3

4

6 -2

2 -3

(28)

Iteration 2

A B C D

X 0 3 -1 -3

A-B 0 3 -1 -3 A-C 0 3 -1 -3 A-D 0 3 -1 -3 B-A 0 3 -1 -3 C-D 0 3 -1 -3 D-B 0 3 -1 -3 D-C 0 3 -1 -3

Relax Edge D-C (A

D + |D-C|)

A B

C D

1

3

4

6 -2

2 -3

(29)

Iteration 2

A B C D

X 0 3 -1 -3

A-B 0 3 -1 -3 A-C 0 3 -1 -3 A-D 0 3 -1 -3 B-A 0 3 -1 -3 C-D 0 3 -1 -3 D-B 0 3 -1 -3 D-C 0 3 -1 -3

Iteration 2 Complete

A B

C D

1

3

4

6 -2

(30)

Iteration 2

A B C D

X 0 3 -1 -3

A-B 0 3 -1 -3 A-C 0 3 -1 -3 A-D 0 3 -1 -3 B-A 0 3 -1 -3 C-D 0 3 -1 -3 D-B 0 3 -1 -3 D-C 0 3 -1 -3

No Changes in Iteration 2

Algorithm Part 1 Complete

A B

C D

1

3

4

6 -2

(31)

A B C D

X 0 3 -1 -3

A-B 0 3 -1 -3 A-C 0 3 -1 -3 A-D 0 3 -1 -3 B-A 0 3 -1 -3 C-D 0 3 -1 -3 D-B 0 3 -1 -3 D-C 0 3 -1 -3

A B

C D

1

3

4

6 -2

(32)

A B C D

A-B 0 3 -1 -3

A B

C D

1

3

4

6 -2

2 -3

Check For Negative Cycles

A

A

+

|A-B|

>

A

B

(33)

A B C D

A-C 0 3 -1 -3

A B

C D

1

3

4

6 -2

2 -3

Check For Negative Cycles

A

A

+

|A-C|

>

A

C

(34)

A B C D

A-D 0 3 -1 -3

A B

C D

1

3

4

6 -2

2 -3

Check For Negative Cycles

A

A

+

|A-D|

>

A

D

(35)

A B C D

B-A 0 3 -1 -3

A B

C D

1

3

4

6 -2

2 -3

Check For Negative Cycles

A

B

+

|B-A|

>

A

A

(36)

A B C D

C-D 1 3 -1 -3

A B

C D

1

3

4

6 -2

2 -3

Check For Negative Cycles

A

C

+

|C-D|

>

A

D

(37)

A B C D

D-B 1 3 -1 -3

A B

C D

1

3

4

6 -2

2 -3

Check For Negative Cycles

A

D

+

|D-B|

>

A

B

(38)

A B C D

D-C 1 3 -1 -3

A B

C D

1

3

4

6 -2

2 -3

Check For Negative Cycles

A

D

+

|D-C|

>

A

C

(39)

No Negative Cycles

For all edges (u, v)

References

Related documents

We evaluated the clusters using both intrinsic and extrinsic methods. For intrinsic evaluation, we used thesaurus information for two novel metrics: 1) the number of negative

In this work,a shoeprint image has been studied for detecting edges using various types of edge detection methods: Canny, Sobel and Prewitt have been tested to detect the

the potential to improve classification as the signed edges introduce what can be seen as agreements (positive edges) or disagreements (negative edges) between nodes, allowing to

is that the proposed CLPD method uses Vertical Edge Detection Algorithm (VEDA) to extract the vertical edges of plates.. The proposed CLPD method can work to detect the region of

In [12,13], after enhance­ ment of original X-ray images, they used canny edge detec­ tion to detect all the edges, then they applied the iterative thresholding approach by

Work study is being used to analyze method of work, gain insight in how work is performed, improve method of work or detect potential for improvements enable

Then, among all graphs with q edges we will identify graphs (containing many cycles) and (containing no cycles) whose chromatic coefficients form bounds for the chromatic

One of the most common and effective method to detect edges in an image is a wavelet transform. It has been widely used in feature extraction, especially in pattern recognition