Graphs

Adjacency Matrix Representation

In [6]:
%pprint
Pretty printing has been turned OFF
In [3]:
from pythonds9.graphs.utils import viz_graph
In [4]:
num_verts = 30
x = [0] * num_verts
In [7]:
x
Out[7]:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
In [8]:
num_verts = 6
adj = [[0]*num_verts]
In [9]:
adj
Out[9]:
[[0, 0, 0, 0, 0, 0]]
In [13]:
%pprint
Pretty printing has been turned ON
In [14]:
adj = [[0]*num_verts] * num_verts
In [15]:
adj
Out[15]:
[[0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0]]
In [16]:
adj[0]
Out[16]:
[0, 0, 0, 0, 0, 0]
In [17]:
adj[0][3]
Out[17]:
0

Graphs with Adjacency Matrix

This will be a workshop exercise for the students.