This repository contains a Python implementation of the famous adjacency matrix and Graph Ploting using the Graph Theory
(Discrete Mathematics). The implementation includes generating an adjacency matrix for any given graph, which is represented by a set of vertices and edges.
An Adjacency Matrix is a square matrix used to represent a graph, where:
- Rows and columns represent vertices (nodes) of the graph.
- Entries (elements) in the matrix indicate edge connections between vertices.
The graph is represented using Python's namedtuple
for defining the vertices and edges. Here's how the Konigsberg bridge problem is modeled:
Graph = namedtuple('Graph', ['vertices', 'edges'])
vertices = ['A', 'B', 'C', 'D']
edges = [
("A", "B"),
("A", "B"),
("A", "C"),
("A", "C"),
("A", "D"),
("B", "D"),
("C", "D")
]
G = Graph(vertices=vertices, edges=edges)
- Clone the repository:
git clone https://github.com/yourusername/konigsberg-graph.git
- Run the Python script to generate the adjacency matrix:
python graph.py
Libraries: 1) collections 2) tabulate
You can install libraries via pip if needed:
pip install tabulate
pip install collections
Here's an example adjacency matrix for the Konigsberg graph:
vertices = ['A', 'B', 'C', 'D']
edges = [
("A", "B"),
("A", "B"),
("A", "C"),
("A", "C"),
("A", "D"),
("B", "D"),
("C", "D")
]
G = Graph(vertices=vertices, edges=edges)
matrix = adjacency_matrix(G)
╒═══╤═══╤═══╤═══╕
│ 0 │ 2 │ 2 │ 1 │
├───┼───┼───┼───┤
│ 2 │ 0 │ 0 │ 1 │
├───┼───┼───┼───┤
│ 2 │ 0 │ 0 │ 1 │
├───┼───┼───┼───┤
│ 1 │ 1 │ 1 │ 0 │
╘═══╧═══╧═══╧═══╛
If you have any questions or suggestions, feel free to reach out to me at LinkedIn.