How to Represent a Graph Data Structure in Python

Python representation of graph.

Fahadul Shadhin
Geek Culture

--

Photo by Clint Adair on Unsplash

Graph is an important data structure studied in Computer Science. Graph theory is an equally important topic in both mathematics and Computer Science.

Representing a graph in a program means finding a way to store the graph in a computer’s memory. Which is a prerequisite to working with graphs in Computer Science.

In this article, I will discuss how to represent a graph using Python.

Table of Contents:· The Basics of Graph
· Two Types of Graph Representation
· Adjacency Matrix Representation
· Adjacency List Representation
Adjacency list of a directed graph:
Adjacency list of an undirected graph:
Adjacency list of a graph with path costs:
· The Graph Class
· Take Input From File
· Conclusion
· Helpful Resources

The Basics of Graph

A graph is a non-linear data structure that consists of a set of nodes and edges. Nodes are also referred to as vertices. An edge is a path that connects two nodes.

If we consider the following graph:

--

--