DSP-FPGA Ch04-Retiming HK192
DSP-FPGA Ch04-Retiming HK192
DSP-FPGA Ch04-Retiming HK192
BMĐT
GV: Hồ Trung Mỹ
Ch.04
Retiming (Tái định thì)
TLTK:
1. Các slide từ sách của Prof. Parhi
2. Slide của Prof. Viktor Öwall
3. Slide của Prof. Lan-Da Van
1
Outline
4.1 Introduction
4.2 Definitions and Properties
Quantitative Description of Retiming
Properties of Retiming
4.3 Retiming Techniques
Cutset Retiming and Pipelining
Retiming for Clock Period Minimization
Retiming for Register Minimization
4.4 Conclusions
2
4.1 INTRODUCTION
Retiming is a transformation technique used to
change the locations of delay elements in a circuit
without affecting the input/output characteristics of
the circuit.
Retiming is a transformation technique to change the
locations of delay element such that
The input/output characteristics are kept
The latency of the system is not changed
The critical path of the system is reduced
The number of registers is reduced
The power consumption is reduced
3
Example
33% reduction
The filter in Fig. 4.1(a) is described by The filter in Fig. 4.1(b) is described by
4
Example
Example 1: Retiming by moving delays from all inputs
to all outputs
D retiming
D D
5
4.2 DEFINITIONS AND PROPERTIES
4.2.1 Quantitative Description of Retiming
Retiming maps a circuit (graph) G to a retimed circuit (graph) Gr.
A retiming solution is characterized by a value r(V) for each node V in the
graph G.
r(V): the number of delays moved from the output edge of the node V
to each of its input edges
Let w(e) denote the weight of the edge e in the original graph G, and let
wr(e) denote the weight of the edge e in the retimed graph Gr. The weight
of the edge UV in the retimed graph is computed by:
wr(e) = w(e) + r(V) – r(U)
A retiming solution is feasible if wr(e) ≥ 0 holds for all edges
The IIR example:
(G) D
4 (2) (Gr) 4 (2)
6
4.2.2 Properties of Retiming
Paths: a sequence of nodes and edges
V0 V1 Vk 1 Vk
e0 e1 ek 2 ek 1
k 1
Weight of the path p: w( p) w(e )
i 0
i
k
Delay of the path p: t ( p ) t (V )
i 0
i
k 1
Weight of the path p: w( p) w(ei )
k i 0
Delay of the path p: t ( p) t (Vi )
i 0
Properties of Retiming
Property 1: The weight of the retimed path p=V0->V1->…->Vk is given by
wr(p)=w(p)+r(Vk)-r(V0).
k 1 k 1
wr ( p) wr (ei ) ( w(ei ) r (Vi 1 ) r (Vi ))
i 0 i 0
k 1
k 1 k 1
w(ei ) r (Vi 1 ) r (Vi ) w( p) r (Vk ) r (V0 )
i 0 i 0 i 0
Property 2: Retiming does not change the number of delays in a
cycle.
Property 3: Retiming does not alter the iteration bound in a DFG.
Property 4: Adding the constant value j to the retiming value of
each node does not change the mapping from G to Gr.
8
DFG Illustration of the Example
9
4.3 RETIMING TECHNIQUES
10
4.3.1 Cutset Retiming and Pipelining
Cutset Retiming
11
Node retiming
Cutset retiming – If one of the subgraph G2 is a single node
and the other subgraph is the rest of the graph minus the
edges going into and out of the chosen node. We call this
node retiming.
G1
(1) (1)
1 1 2D 1 2D
2D
D D D
D
D (2)
(1) (2) 2 3 (1) 2 3
2 3
G2 D
Node
4 4 retiming 4 (2)
(2)
Subtracting one delay from each
edge outgoing from the node
Adding one delay to each edge
incident into the node
12
Node retiming
To recap the feasibility:
Feasibility constraints: for each edge U->V of the retimed
graph, the number of delay elements, i.e. the weight of
the edge must be non-negative, we have
wr (e) w(e) r (V ) r (U ) 0
r (U ) r (V ) w(e)
where wr(e) and w(e) are the number of delay elements on
the edge U->V after and before retiming, respectively.
For a circuit graph, there is a retiming design space
defined by the system of inequalities (each edge forms an
inequality).
This requires solving systems of inequalities.
13
Pipelining – Feedforward Cutset
Pipelining – special case of cutset retiming, where
there are not edges in the cutset from the subgraph
G2 to the subgraph G1, termed as feedforward cutset.
In D D D
In D G1
a b c cutset a b c
G2
a b c
2D 2D
2D
14
Example-Lattice Filter
Critical path
D D
D D
D D ……D
2D
2D 2D ……
2D
17
Another example- A 100-stage lattice filter
Stage 1 Stage 2 Critical path Stage 100
……
2D
2D 2D ……
2D
2D
……
D D D
Tcritical = 2Tmult+2Tadd
19
Retiming for Clock Period Minimization (1/2)
20
Retiming for Clock Period Minimization (2/2)
21
Find W(U,V) and D(U,V) by manual calculation
Find W(U,V): the minimum delay out of all paths from U to V
W(1,1) = W(2,2) = W(3,3) = W(4,4) =0
Between 1 and 2, there are two paths
P1: 1 – 3 – 2
P2: 1 – 4 – 2
W(1, 2) = Min [(W (13) + W (32)) , (W (14) + W (42))]
= Min [(1 + 0), (2 + 0)]
= Min (1, 2)
=1
There is only one path from 1 to 3: W (1, 3) = 1
Similarly,
W (1, 4) = 2
W (2, 1) = 1
W (2, 3) = 1 + 1 = 2 (path 2 -1 -3)
W (2, 4) = 1 + 2 = 3 (path 2 -1 -4)
W (3, 1) = 0 + 1 = 1 (path 3 -2 -1)
W (3, 2) = 0
W (3, 4) = 0 + 1 + 2 = 3 (path 3 -2 -1 -4)
W (4, 1) = 0 + 1 = 1 (path 4 - 2 -1)
W (4, 2) = 0
W (4, 3) = 0 + 1 + 1 = 2 (path 4 - 2 -1 -3)
22
Find W(U,V) and D(U,V) by manual calculation
Find D(U,V): the maximum computation time among all paths having
path delay = W (U, V)
D (1, 1) = t1 = 1
D (1, 2) = t1 + t3 + t2 = 1 + 2 + 1 = 4
D (1, 3) = t1 + t3 = 1 + 2 = 3
D(U,V)
D (1, 4) = t1 +t4 = 1 + 2 = 3
D (2, 1) = t2 + t1 = 1 + 1 = 2
D (2, 2) = t2 = 1
D (2, 3) = t2 + t1 + t3 = 1 + 1 + 2 = 4
D (2, 4) = t2 + t1 +t4 = 1 + 1 + 2 = 4
D (3, 1) = t3 + t2 + t1 = 2 + 1 + 1 = 4
D (3, 2) = t3 + t2 = 2 + 1 = 3
D (3, 3) = t3 = 1
D (3, 4) = t3 + t2 + t1 +t4 = 2 + 1 + 1 + 2 = 6
D (4, 1) = t4 + t2 + t1 = 2 + 1 + 1 = 4
D (4, 2) = t4 + t2 = 2 + 1 = 3
D (4, 3) = t4+ t2 + t1 + t3 = 2 + 1 + 1 + 2 = 6
D (4, 4) = t4 = 1 23
Find W(U,V) and D(U,V) by Algorithm
24
Find W(U,V) and D(U,V) by Algorithm
Step 3: Find S’(U,V): the shortest distance matrix
r(2) (U, V)
25
Find W(U,V) and D(U,V) by Algorithm
Step 3: Find S’(U,V): the shortest distance matrix
r(3) (U, V)
G’
r(4) (U, V)
26
Find W(U,V) and D(U,V) by Algorithm
Step 4: Find W(U,V) and D(U,V) :
𝑆 ′ 𝑈,𝑉
If U≠V, then 𝑊 𝑈, 𝑉 = and D(U,V)= M.W(U,V)−S’UV+t(V)
𝑀
If U=V, then W(U,V)=0 and D(U,V)=t(U).
𝑀 = 𝑡𝑚𝑎𝑥 ∙ 𝑛, where tmax is the maximum computation time of the
nodes in G and 𝑛 is the number of nodes in G.
𝑫 𝑼, 𝑽
𝑾 𝑼, 𝑽
V
V U
U
Some sample calculations for W(U,V) Some sample calculations for D(U,V)
5 15
𝑊 1,2 = =1 𝑊 1,4 = =2
8 8
7 20
𝑊 1,3 = =1 𝑊 3,4 = =3
8 8 27
Example with c=3
𝒓 𝑼 −𝒓 𝑽 ≤𝒘 𝒆
𝒓 𝑼 − 𝒓 𝑽 ≤ 𝑾 𝑼, 𝑽 − 𝟏
𝑫 𝑼, 𝑽 > 𝒄
28
Example with c=3
(without changing)
30
Example with c=2
32
Bellman-Ford algorithm
Find shortest path from origin = 5 to every other node.
Initial value of r(1)(V) = (0, 0, 0, 0)
𝑘 = 2; 𝑉 = 1, 2, 3, 4 and r(1)(V) = (0, 0, 0, 0) first iteration
V=1
Incoming (2 1): r(1) (2) + W(2 1) = 0 + 0 = 0
Incoming (3 1): r(1) (3) + W(3 1) = 0 + 0 = 0
Incoming (4 1): r(1) (4) + W(4 1) = 0 + 1 = 1
No change in r(2) (1) ⇒ r(2) (1) = 0
V=2
Incoming (1 2): r(1) (1) + W(1 2) = 0 + 1 = 1
Incoming (3 2): r(1) (3) + W(3 2) = 0 + 1 = 1
Incoming (4 2): r(1) (4) + W(4 2) = 0 + 2 = 2
No change in r(2) (2) ⇒ r(2) (2) = 0
V=3
Incoming (1 3): r(1) (1) + W(1 3) = 0 + 0 = 0
Incoming (2 3): r(1) (2) + W(2 3) = 0 + (-1) = -1
Incoming (4 3): r(1) (4) + W(4 3) = 0 + 2 = 2
r(2) (3) is changed from 0 to -1
V=4
Incoming (1 4): r(1) (1) + W(1 4) = 0 + 0 = 0
Incoming (2 4): r(1) (2) + W(2 4) = 0 + (-1) = -1
Incoming (3 4): r(1) (3) + W(3 4) = 0 + 1 = 1
r(2) (4) is changed from 0 to -1
So r(2) (V) = (0, 0, -1, -1)
33
Bellman-Ford algorithm
𝑘 = 3; 𝑉 = 1, 2, 3, 4 and r(2)(V) = (0, 0, -1, -1) second iteration
V=1
Incoming (2 1): r(2) (2) + W(2 1) = 0 + 0 = 0
Incoming (3 1): r(2) (3) + W(3 1) = -1 + 0 = -1
Incoming (4 1): r(2) (4) + W(4 1) = -1 + 1 = 0
r(3) (1) is changed from 0 to -1
V=2
Incoming (1 2): r(2) (1) + W(1 2) = 0 + 1 = 1
Incoming (3 2): r(2) (3) + W(3 2) = -1 + 1 = 0
Incoming (4 2): r(2) (4) + W(4 2) = -1 + 2 = 1
No change in r(3) (2) ⇒ r(3) (2) = 0
V=3
Incoming (1 3): r(2) (1) + W(1 3) = 0 + 0 = 0
Incoming (2 3): r(2) (2) + W(2 3) = 0 + (-1) = -1
Incoming (4 3): r(2) (4) + W(4 3) = -1 + 2 = 1
No change in r(3) (3) ⇒ r(3) (3) = -1
V=4
Incoming (1 4): r(2) (1) + W(1 4) = 0 + 0 = 0
Incoming (2 4): r(2) (2) + W(2 4) = 0 + (-1) = -1
Incoming (3 4): r(2) (3) + W(3 4) = -1 + 1 = 0
No change in r(3) (4) ⇒ r(3) (4) = -1
So r(3) (V) = (-1, 0, -1, -1)
34
Bellman-Ford algorithm
𝑘 = 4; 𝑉 = 1, 2, 3, 4 and r(3)(V) = (-1, 0, -1, -1) third iteration
V=1
Incoming (2 1): r(3) (2) + W(2 1) = 0 + 0 = 0
Incoming (3 1): r(3) (3) + W(3 1) = -1 + 0 = -1
Incoming (4 1): r(3) (4) + W(4 1) = -1 + 1 = 0
No change in r(4) (1) ⇒ r(1) (3) = -1
V=2
Incoming (1 2): r(3) (1) + W(1 2) = -1 + 1 = 0
Incoming (3 2): r(3) (3) + W(3 2) = -1 + 1 = 0
Incoming (4 2): r(3) (4) + W(4 2) = -1 + 2 = 1
No change in r(4) (2) ⇒ r(4) (2) = 0
V=3
Incoming (1 3): r(3) (1) + W(1 3) = -1 + 0 = -1
Incoming (2 3): r(3) (2) + W(2 3) = 0 + (-1) = -1
Incoming (4 3): r(3) (4) + W(4 3) = -1 + 2 = 1
No change in r(4) (3) ⇒ r(4) (3) = -1
V=4
Incoming (1 4): r(3) (1) + W(1 4) = -1 + 0 = 0
Incoming (2 4): r(3) (2) + W(2 4) = 0 + (-1) = -1
Incoming (3 4): r(3) (3) + W(3 4) = -1 + 1 = 0
No change in r(4) (4) ⇒ r(4) (4) = -1
So r(4) (V) = (-1, 0, -1, -1)
35
Example with c=2
Final retiming vector is (-1, 0, -1, -1). i.e. nodes 1, 3 and 4
are advanced by 1 clock period.
For more convenience, we can use the fourth property of
retiming (see on Slide 8) and add 1 to all entries of r(V).
wr (U
e
V ) w(U
e
V ) r (V ) r (U ) 0
36
4.3.3 Retiming for Register Minimization
D
D
Delay
reduction
D
38
Other Applications of Retiming
Retiming for Folding (Chapter 6)
Retiming for Power Reduction (Chap. 17)
Retiming for Logic Synthesis (Beyond Scope of
This Class)
Multi-Rate/Multi-Dimensional Retiming
(Denk/Parhi, Trans. VLSI, Dec. 98, Jun.99)
39
4.4 Conclusion
Reduction of clock period, number of registers,
and power consumption by retiming for
synchronous systems
41
Bellman-Ford algorithm finds shortest distance
This is a single point shortest path algorithm.
If there are no negative cycles (i.e. cycles having negative
weight or w(ei) 0) then it finds shortest distance from any
arbitrarily chosen node U (called origin) to each node in the
graph.
If there are negative cycles, then the algorithm goes in an
infinite loop.
But after one pass of the algorithm, we can check for
presence of negative cycles.
We initialize the distance vector r(1) (V) to initial values of the
edge weights from U to other nodes. If edge does not exist,
we will take the weight as
In each iteration, we try to find whether there is a shorter way
to reach node i from node U by going via another node W.
If the edge W V has negative weight, then it will provide a
shorter way to V from U via W.
42
Bellman-Ford algorithm finds shortest distance
Example 1: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
From 2 to V directly
Some steps are described below
1 r(1) (V) = [W(2 1), W(2 2), W(2 3), W(2 4)]
-3 = (, 0, 1, 2)
1 2 is chosen as an original node.
First iteration:
2 4 V=1
1 2 There is one incoming edge into node 1, i.e.
4 1 weight 1.
3 New weight of r(2) (1) = r(1) (4) + W (4 1)
= 2+1
2
= 3 < r(1) (1) =
So we will change r(2) (1) to 3 for next iteration.
43
Bellman-Ford algorithm finds shortest distance
Example 1: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
1 r(1) (V) = [W(2 1), W(2 2), W(2 3), W(2 4)]
-3 = (, 0, 1, 2)
1
First iteration:
2 4 V=2
1 2 There is one incoming edge into node 2, i.e.
1 2 weight -3.
3 New weight of r(2) (2) = r(1) (1) + W (1 2)
= + (-3)
2
= > r(1) (2) = 0
It will retain same value for r(2) (2).
44
Bellman-Ford algorithm finds shortest distance
Example 1: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
45
Bellman-Ford algorithm finds shortest distance
Example 1: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
r(1) (V) = [W(2 1), W(2 2), W(2 3), W(2 4)]
= (, 0, 1, 2)
1 First iteration:
-3 1 V=4
It has 2 incoming edges 3 4, 2 4
2 4 3 4 : r(2)(4) = r(1) (3) + W (3 4)
1 2 =1+2
=3
3 2 4 : r(2)(4) = r(1) (2) + W (2 4)
=0 + 2
2
=2
⟹ r(2)(4) = min {3,2} = 2
Hence there is no change for r(2) (4).
At the end of the first iteration:
r(2) (V) = ( 3, 0, 1, 2 ) 46
Bellman-Ford algorithm finds shortest distance
Example 1: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the first iteration:
r(2) (V) = ( 3, 0, 1, 2 )
1
-3 1
Second iteration:
2 4 V=1
1 2 There is one incoming edge into node 1, i.e.
4 1 weight 1.
3 New weight of r(3) (1) = r(2) (4) + W (4 1)
= 2+1
2
=3
It will retain same value for r(3) (1).
47
Bellman-Ford algorithm finds shortest distance
Example 1: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the first iteration:
r(2) (V) = ( 3, 0, 1, 2 )
1
-3 1
Second iteration:
2 4 V=2
1 2 There is one incoming edge into node 2, i.e.
1 2 weight -3.
3 New weight of r(3) (2) = r(2) (1) + W (1 2)
= 3 + (-3)
2
=0
It will retain same value for r(3) (2).
48
Bellman-Ford algorithm finds shortest distance
Example 1: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the first iteration:
r(2) (V) = ( 3, 0, 1, 2 )
1
-3 1
Second iteration:
2 4 V=3
1 2 There is one incoming edge into node 3, i.e.
2 3 weight 1.
3 New weight of r(3) (3) = r(2) (2) + W (2 3)
=0+1
2
=1
It will retain same value for r(3) (3).
49
Bellman-Ford algorithm finds shortest distance
Example 1: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the first iteration:
r(2) (V) = ( 3, 0, 1, 2 )
1 second iteration:
-3 1 V=4
It has 2 incoming edges 3 4, 2 4
2 4 3 4 : r(3)(4) = r(2) (3) + W (3 4)
1 2 =1+2
=3
3 2 4 : r(3)(4) = r(2) (2) + W (2 4)
=0 + 2
2 =2
We conclude that there ⟹ r(3)(4) = min {3,2} = 2
are no negative cycles Hence there is no change for r(3) (4).
and (3, 0, 1, 2) is a At the end of the second iteration:
shortest path solution. r(3) (V) = ( 3, 0, 1, 2 ) 50
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
From 2 to V directly
This graph has a Some steps are described below
negative cycle.
1 r(1) (V) = [W(2 1), W(2 2), W(2 3), W(2 4)]
-3 = (, 0, 1, 1)
1 2 is chosen as an original node.
First iteration:
2 4 V=1
1 2 There is one incoming edge into node 1, i.e.
4 1 weight 1.
3 New weight of r(2) (1) = r(1) (4) + W (4 1)
= 1+1
1
= 2 < r(1) (1) =
So we will change r(2) (1) to 2 for next iteration.
51
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
1 r(1) (V) = [W(2 1), W(2 2), W(2 3), W(2 4)]
-3 = (, 0, 1, 1)
1
First iteration:
2 4 V=2
1 2 There is one incoming edge into node 2, i.e.
1 2 weight -3.
3 New weight of r(2) (2) = r(1) (1) + W (1 2)
= + (-3)
1
= > r(1) (2) = 0
It will retain same value for r(2) (2).
52
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
53
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
r(1) (V) = [W(2 1), W(2 2), W(2 3), W(2 4)]
= (, 0, 1, 1)
1 First iteration:
-3 1 V=4
It has 2 incoming edges 3 4, 2 4
2 4 3 4 : r(2)(4) = r(1) (3) + W (3 4)
1 2 =1+2
=3
3 2 4 : r(2)(4) = r(1) (2) + W (2 4)
=0 + 1
1
=1
⟹ r(2)(4) = min {3,1} = 1
Hence there is no change for r(2) (4).
At the end of the first iteration:
r(2) (V) = ( 2, 0, 1, 1 ) 54
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the first iteration:
r(2) (V) = ( 2, 0, 1, 1 )
1
-3 1
Second iteration:
2 4 V=1
1 2 There is one incoming edge into node 1, i.e.
4 1 weight 1.
3 New weight of r(3) (1) = r(2) (4) + W (4 1)
= 1+1
1
=2
It will retain same value for r(3) (1).
55
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the first iteration:
r(2) (V) = ( 2, 0, 1, 1 )
1
-3 1
Second iteration:
2 4 V=2
1 2 There is one incoming edge into node 2, i.e.
1 2 weight -3.
3 New weight of r(3) (2) = r(2) (1) + W (1 2)
= 2 + (-3)
1
= -1
r(3) (2) is changed to -1
56
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the first iteration:
r(2) (V) = ( 2, 0, 1, 1 )
1
-3 1
Second iteration:
2 4 V=3
1 2 There is one incoming edge into node 3, i.e.
2 3 weight 1.
3 New weight of r(3) (3) = r(2) (2) + W (2 3)
=0+1
1
=1
It will retain same value for r(3) (3).
57
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the second iteration:
r(2) (V) = ( 2, 0, 1, 1 )
1 Second iteration:
-3 1 V=4
It has 2 incoming edges 3 4, 2 4
2 4 3 4 : r(3)(4) = r(2) (3) + W (3 4)
1 2 =1+2
=3
3 2 4 : r(3)(4) = r(2) (2) + W (2 4)
=0 + 1
1 =1
⟹ r(3)(4) = min {3,1} = 1
⟹ retain same value for r(3) (4).
At the end of the second iteration:
r(3) (V) = ( 2, -1, 1, 1 ) 58
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the second iteration:
r(3) (V) = ( 2, -1, 1, 1 )
1
-3 1
Third iteration:
2 4 V=1
1 2 There is one incoming edge into node 1, i.e.
4 1 weight 1.
3 New weight of r(4) (1) = r(3) (4) + W (4 1)
= 1+1
1
=2
It will retain same value for r(4) (1).
59
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the second iteration:
r(3) (V) = ( 2, -1, 1, 1 )
1
-3 1
Third iteration:
2 4 V=2
1 2 There is one incoming edge into node 2, i.e.
1 2 weight -3.
3 New weight of r(4) (2) = r(3) (1) + W (1 2)
= 2 + (-3)
1
= -1
It will retain same value for r(4) (2).
60
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the second iteration:
r(3) (V) = ( 2, -1, 1, 1 )
1
-3 1
Third iteration:
2 4 V=3
1 2 There is one incoming edge into node 3, i.e.
2 3 weight 1.
3 New weight of r(4) (3) = r(3) (2) + W (2 3)
= -1 + 1
1
= 0 < r(3) (3) = 1
r(4) (3) is changed to 0
61
Bellman-Ford algorithm finds shortest distance
Example 2: In the graph below, let node 2 be origin. Find
the shortest path from 2 to 1, 3, 4 (or V = 1, 3, 4).
Some steps are described below
At the end of the second iteration:
r(3) (V) = ( 2, -1, 1, 1 )
1 Third iteration:
-3 1 V=4
It has 2 incoming edges 3 4, 2 4
2 4 3 4 : r(4)(4) = r(3) (3) + W (3 4)
1 2 =1+2
=3
3 2 4 : r(4)(4) = r(3) (2) + W (2 4)
= -1 + 1
1 =0
⟹ r(4)(4) = min {3,0} = 0
⟹ r(4) (4) is changed to 0
Exist negative cycle. At the end of the third iteration:
(2412) r(4) (V) = ( 2, -1, 0, 0 ) 62
Floy-Warshall algorithm finds shortest distance
Consider a directed graph having 𝑛 nodes. The distance
between every pair of nodes (𝑖, 𝑗) is represented in a matrix
𝑅, which is of order 𝑛 × 𝑛
We initialize the matrix such that element (𝑖, 𝑗) represents
distance from node 𝑖 to node 𝑗 if the edge (𝑖 𝑗) exists. It is
otherwise. This is the first version of the matrix and we will
call this version R(1).
The algorithm builds successive versions R(2), R(3)…R(n+1)
such that there is improvement in each iteration.
63
Floy-Warshall algorithm finds shortest distance
The distance from node U to node V (U V ) is stored
in R(1) (U, V).
In each iteration we try to find a better alternative by
taking a hop via another node K. We compare direct
distance U V with one-hop distance (U K + K
V).
If one of these branches is negative, it is possible that
the one-hop path is a better alternative.
64
Floy-Warshall algorithm finds shortest distance
Example: In the graph below, find the shortest
distance between every pair of nodes by using
Floy-Warshall algorithm.
65
Floy-Warshall algorithm finds shortest distance
R(1)
U V
Let K = 1
V = 1: W (11) =
V = 2: W (12) = -3
• Shortest Distance W (42)
= W (41) + W (12) = 1 – 3 = -2
⟹W (42) is changed from to -2
V = 3: W (13) =
V = 4: W (14) =
66
Floy-Warshall algorithm finds shortest distance
R(1)
U V
Let K = 1
V = 1: W (11) =
V = 2: W (12) = -3 R(2)
U V
• Shortest Distance W (42)
= W (41) + W (12) = 1 – 3 = -2
⟹W (42) is changed from to -2
V = 3: W (13) =
V = 4: W (14) =
67
Floy-Warshall algorithm finds shortest distance
R(2)
U V
Let K = 2
V = 1: W (21) =
V = 2: W (22) =
V = 3: W (23) = 1
• W (13) = W(12) + W(23)
= -3 + 1 = -2
68
Floy-Warshall algorithm finds shortest distance
R(2)
U V
Let K = 2
V = 4: W (24) = 2
• W (14) = W(12) + W(24)
= -3 + 2 = -1
69
Floy-Warshall algorithm finds shortest distance
R(2)
U V
Let K = 2
V = 4: W (24) = 2 R(3)
U V
• W (14) = W(12) + W(24)
= -3 + 2 = -1
70
Floy-Warshall algorithm finds shortest distance
R(3)
U V
Let K = 3
W(U⟶3)
V = 1: W (31) =
V = 2: W (32) =
V = 3: W (33) =
V = 4: W (34) = 2
• W (14) = W(13) + W(34)
= -2 + 2 = 0 (unchanged)
• W (24) = W(23) + W(34)
= 1 + 2 = 3 (unchanged)
• W (44) = W(43) + W(34)
= -1 + 2 = 1 (unchanged) 71
Floy-Warshall algorithm finds shortest distance
R(3)
U V
Let K = 3
unchanged
V = 1: W (31) = R(4)
V = 2: W (32) = U V
V = 3: W (33) =
V = 4: W (34) = 2
• W (14) = W(13) + W(34)
= -2 + 2 = 0 (unchanged)
• W (24) = W(23) + W(34)
= 1 + 2 = 3 (unchanged)
• W (44) = W(43) + W(34)
= -1 + 2 = 1 (unchanged) 72
Floy-Warshall algorithm finds shortest distance
R(4)
U V
Let K = 4
V = 1: W (41) = 1
U = 1,2,3,4
• W (11) = W(14) + W(41)
= -1+ 1 = 0 1 -1
• W (21) = W(24) + W(41) K=4
2 V=1
= 2+1=3 2 1
• W (31) = W(34) + W(41) 4 1
= 2+1=3
2
3
• W (41) = W(44) + W(41) 0
= 0 + 1 = 1 (unchanged)
4
73
Floy-Warshall algorithm finds shortest distance
R(4)
U V
Let K = 4
V = 2: W (42) = 1
U = 1,2,3,4
• W (12) = W(14) + W(42)
= -1+ (-2) = -3 (unchanged) 1 -1
• W (22) = W(24) + W(42) K=4
2 V=2
= 2 + (-2) = 0 2 -2
• W (32) = W(34) + W(42) 4 2
= 2 + (-2) = 0
2
3
• W (42) = W(44) + W(42) 0
= 0 + (-2) = -2 (unchanged)
4
74
Floy-Warshall algorithm finds shortest distance
R(4)
U V
Let K = 4
V = 3: W (43) = -1
U = 1,2,3,4
• W (13) = W(14) + W(43)
= -1+ (-1) = -2 (unchanged) 1 -1
• W (23) = W(24) + W(43) K=4
2 V=3
= 2 + (-1) = 1 (unchanged) 2 -1
• W (33) = W(34) + W(43) 4 3
= 2 + (-1) = 1
2
3
• W (43) = W(44) + W(43) 0
= 0 + (-1) = -1 (unchanged)
4
75
Floy-Warshall algorithm finds shortest distance
R(4)
U V
Let K = 4
V = 4: W (44) = 0
U = 1,2,3,4
⟹ unchanged 1 -1
K=4
2 V=4
2 0
4 4
2
3
0
4
76
Floy-Warshall algorithm finds shortest distance
R(4)
U V
V = 4: W (44) = 0
R(5)
⟹ unchanged U V
78
Solving Systems of Inequalities
79
Example: M=5, N=4
Bellman-Ford algorithm finds shortest distance from node N + 1 to every other node.
Floyd-Warshall algorithm finds shortest distance to every node from every other node..
80
Bellman-Ford algorithm
Find shortest path from origin = 5 to every other node.
Initial value of r(1)(V) = (0, 0, 0, 0)
𝑘 = 2; 𝑉 = 1, 2, 3, 4 and r(1)(V) = (0, 0, 0, 0) first iteration
V=1
Incoming (2 1): r(1) (2) + W(2 1) = 0 + 0 = 0
Incoming (5 1): Ignore links from home node
No change in r(2) (1) ⇒ r(2) (1) = 0
V=2
No incoming Edges
No change in r(2) (2) ⇒ r(2) (2) = 0
V=3
Incoming (2 3): r(1) (2) + W(2 3) = 0 + 2 = 2 > 0
Incoming (1 3): r(1) (1) + W(1 3) = 0 + 5 = 5 > 0
No Change r(2) (3) ⇒ r(2) (3) = 0
V=4
Incoming (1 4): r(1) (1) + W(1 4) = 0 + 4 = 4 > 0
Incoming (3 4): r(1) (3) + W(3 4) = 0 + (-1) = -1 < 0
r(2) (4) is changed from 0 to -1
So r(2) (V) = (0, 0, 0, -1)
81
Bellman-Ford algorithm
𝑘 = 3; 𝑉 = 1, 2, 3, 4 and r(2)(V) = (0, 0, 0, -1) second iteration
V=1
Incoming (2 1): r(2) (2) + W(2 1) = 0 + 0 = 0
Incoming (5 1): Ignore links from home node
No change in r(3) (1) ⇒ r(3) (1) = 0
V=2
No incoming Edges
No change in r(3) (2) ⇒ r(3) (2) = 0
V=3
Incoming (2 3): r(2) (2) + W(2 3) = 0 + 2 = 2 > 0
Incoming (1 3): r(2) (1) + W(1 3) = 0 + 5 = 5 > 0
No Change in r(3) (3) ⇒ r(3) (3) = 0
V=4
Incoming (1 4): r(2) (1) + W(1 4) = 0 + 4 = 4 > 0
Incoming (3 4): r(2) (3) + W(3 4) = 0 + (-1) = -1
No Change in r(3) (4) ⇒ r(3) (4) = -1
So r(3) (V) = (0, 0, 0, -1)
In entire second iteration, there was no change. Hence in third iteration
also, there will be no change and in negative cycle test also, there will
be no change. So the final solution is r(2) (V) = (0, 0, 0, -1).
82
Example: M=5, N=4
Bellman-Ford algorithm finds shortest distance from node N + 1 to every other node.
Floyd-Warshall algorithm finds shortest distance to every node from every other node..
83
Example 4.3.1 with M=5, N=4 (2/2)
Floyd-Warshall algorithm finds shortest distance to every node from every other node.
84