Ideal Flow Network is a new concept to analyze transportation and communication networks. In this tutorial, we will use a simple network (visualized using graphiz).

First, you need to download and install **graphiz** and **numpy**. The code of **IdealFlowNetwork** module is available in GitHub.

In [1]:

```
import graphviz as dg
import numpy as np
import IdealFlowNetwork as ifn
```

Suppose we have the following strongly connected network

In [2]:

```
d=dg.Digraph(engine='circo')
d.edge("a","b")
d.edge("a","c")
d.edge("a","d")
d.edge("d","e")
d.edge("e","a")
d.edge("e","d")
d.edge("c","b")
d.edge("b","d")
d.edge("e","c")
d
```

Out[2]:

In [3]:

```
# a b c d e
C=[[0, 1, 1, 1, 0], # a
[0, 0, 0, 1, 0], # b
[0, 1, 1, 0, 0], # c
[0, 0, 0, 0, 2], # d
[1, 0, 1, 2, 0]
]
C
```

Out[3]:

In [4]:

```
A=ifn.capacity2adj(C)
ifn.isIrreducible(A)
```

Out[4]:

We compute the stochastic matrix as follow

In [5]:

```
S=ifn.capacity2stochastic(C)
print('stochastic matrix=\n',S)
```

In [6]:

```
print('Entropy=', ifn.networkEntropy(S))
print('Entropy ratio=', ifn.entropyRatio(S))
```

Now we can compute flow matrix F that represents the flow in each link.

In [7]:

```
F=ifn.capacity2idealFlow(C)
print('Ideal Flow matrix=\n', F)
```

Notice that the sum of rows and sum of columns of an ideal flow matrix are always the same.

In [8]:

```
sR=ifn.sumOfRow(F)
sC=ifn.sumOfCol(F)
print('sum of row=',sR,'\n')
print('sum of columns=',sC,'\n')
```

Premagic matrix means the matrix has exactly the same sum of rows as the sum of columns.

In [9]:

```
print('isPremagic(F)=',ifn.isPremagic(F),'\n')
```

In [10]:

```
print('isIrreducible(F)=', ifn.isIrreducible(F))
print('isNonNegative(F)=', ifn.isNonNegative(F))
print('is Ideal Flow(F)=', ifn.isIdealFlow(F))
```

In the following example, we will examine an efficient network. Let us define an adjacency matrix.

In [11]:

```
# a b c d
A=[[0, 0, 1, 1], # a
[1, 0, 0, 0], # b
[0, 1, 0, 1], # c
[0, 1, 0, 0], # d
]
A=np.asarray(A)
A
```

Out[11]:

We can draw this network

In [12]:

```
d1=dg.Digraph(engine='circo')
d1.edge("a","c")
d1.edge("a","d")
d1.edge("b","a")
d1.edge("c","b")
d1.edge("c","d")
d1.edge("d","b")
d1
```

Out[12]:

In [13]:

```
F=ifn.adj2idealFlow(A)
print('F=',F,'\n')
```

*premagic* property.

In [14]:

```
sR=ifn.sumOfRow(F)
sC=ifn.sumOfCol(F)
print('sum of row=',sR,'\n')
print('sum of columns=',sC,'\n')
print('isPremagic(F)=',ifn.isPremagic(F),'\n')
```

In [15]:

```
kappa=1000
F=ifn.adj2idealFlow(A, kappa)
print('F=',F,'\n')
```

We can get back the stochastic matrix based on ideal flow matrix.

In [16]:

```
S=ifn.idealFlow2stochastic(F)
print('S=',S,'\n')
```

In [17]:

```
print('Entropy=', ifn.networkEntropy(S))
print('Entropy ratio=', ifn.entropyRatio(S))
```

last update: Sept 2017

*Cite this tutorial as*:

Teknomo,K. (2017) **Ideal Flow Network Anaysis using Python** (http://people.revoledu.com/kardi/tutorial/Python/Ideal+Flow.html)

See Also: Python for Data Science, Resources on Ideal Flow Network

Visit www.Revoledu.com for more tutorials in Data Science.

Copyright © 2017 Kardi Teknomo

Permission is granted to share this notebook as long as the copyright notice is intact.