A molecular graph represents a molecule as a network: atoms are nodes and chemical bonds are edges. It gives a machine learning model the molecule’s connectivity directly, rather than requiring it to recover that connectivity from a character sequence such as SMILES. section 1.2.1 section 11.1.2
Water as a graph
Water, H₂O, has three nodes: one oxygen and two hydrogen atoms. Two edges connect the oxygen node to the hydrogen nodes, representing the two O–H bonds. section 1.2.1
Nodes and edges can also carry attributes. Atom features can describe element type, charge, attached hydrogens, or aromaticity; edge features can distinguish single, double, triple, and aromatic bonds. These attributes add chemical information to the connectivity. section 1.2.1 section 11.2.1
Connectivity can be stored in an adjacency matrix, whose entries indicate whether pairs of atoms are connected, or in an edge list of atom pairs. Edge lists are useful for sparse molecular graphs, where each atom bonds to relatively few others. section 11.2.1
Figure 11.3 Graphs contain multitudes. Aspirin represented as a graph across multiple formats: 1) 3D ball-and-stick model, 2) Graph representation with nodes and edges, 3) Adjacency matrix representation, 4) Edge list representation.

Compare aspirin’s molecular structure with its graph, adjacency matrix, and edge list. section 11.2.1
SMILES and molecular graphs are complementary representations. For example, aspirin’s SMILES string, CC(=O)Oc1ccccc1C(=O)O, uses parentheses for branches and matching digits for ring closure. A graph makes those atom-to-atom connections explicit. section 11.2.2 section 11.1.2
To prepare a molecular graph for a graph neural network:
- Parse and validate the SMILES string with RDKit, then extract its atoms and bonds. section 11.2.2
- Build a feature vector for each atom and an edge list from the bonds. One implementation uses atom symbol, degree, hydrogen count, implicit valence, and aromaticity. section 11.2.2
- Represent each undirected bond with edges in both directions, and add a self-loop for each atom so aggregation can retain the atom’s own features. section 11.2.2
Listing 11.1 Convert SMILES String to Graph Representation
def smile_to_graph(smiles: str) -> Tuple[int, List, List]:
mol = Chem.MolFromSmiles(smiles)
if mol is None:
raise ValueError(f"Invalid SMILES: {smiles}")
num_atoms = mol.GetNumAtoms()
features = []
for atom in mol.GetAtoms(): #A
feature = atom_features(atom)
features.append(feature / (sum(feature) + 1e-10))
# Extract bonds
edges = []
for bond in mol.GetBonds(): #B
edges.append([bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()])
g = nx.Graph(edges).to_directed()
edge_index = []
mol_adj = np.zeros((num_atoms, num_atoms)) #C
for e1, e2 in g.edges:
mol_adj[e1, e2] = 1
mol_adj += np.matrix( #D
np.eye(mol_adj.shape[0]) #D
) #D
index_row, index_col = np.where(mol_adj >= 0.5)
for i, j in zip(index_row, index_col): #E
edge_index.append([i, j])
return num_atoms, features, edge_indexInspect the RDKit-based conversion, including validation, feature extraction, and edge construction. section 11.2.2

