mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-11 06:35:39 +08:00
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
117 lines
3.8 KiB
Python
117 lines
3.8 KiB
Python
"""
|
|
File: graph_adjacency_matrix.py
|
|
Created Time: 2023-02-23
|
|
Author: krahets (krahets@163.com)
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.append(str(Path(__file__).parent.parent))
|
|
from modules import Vertex, print_matrix
|
|
|
|
|
|
class GraphAdjMat:
|
|
"""Undirected graph class based on adjacency matrix"""
|
|
|
|
def __init__(self, vertices: list[int], edges: list[list[int]]):
|
|
"""Constructor"""
|
|
# Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
|
|
self.vertices: list[int] = []
|
|
# Adjacency matrix, where the row and column indices correspond to the "vertex index"
|
|
self.adj_mat: list[list[int]] = []
|
|
# Add vertices
|
|
for val in vertices:
|
|
self.add_vertex(val)
|
|
# Add edges
|
|
# Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
|
for e in edges:
|
|
self.add_edge(e[0], e[1])
|
|
|
|
def size(self) -> int:
|
|
"""Get the number of vertices"""
|
|
return len(self.vertices)
|
|
|
|
def add_vertex(self, val: int):
|
|
"""Add vertex"""
|
|
n = self.size()
|
|
# Add the value of the new vertex to the vertex list
|
|
self.vertices.append(val)
|
|
# Add a row to the adjacency matrix
|
|
new_row = [0] * n
|
|
self.adj_mat.append(new_row)
|
|
# Add a column to the adjacency matrix
|
|
for row in self.adj_mat:
|
|
row.append(0)
|
|
|
|
def remove_vertex(self, index: int):
|
|
"""Remove vertex"""
|
|
if index >= self.size():
|
|
raise IndexError()
|
|
# Remove the vertex at index from the vertex list
|
|
self.vertices.pop(index)
|
|
# Remove the row at index from the adjacency matrix
|
|
self.adj_mat.pop(index)
|
|
# Remove the column at index from the adjacency matrix
|
|
for row in self.adj_mat:
|
|
row.pop(index)
|
|
|
|
def add_edge(self, i: int, j: int):
|
|
"""Add edge"""
|
|
# Parameters i, j correspond to the vertices element indices
|
|
# Handle index out of bounds and equality
|
|
if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:
|
|
raise IndexError()
|
|
# In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
|
|
self.adj_mat[i][j] = 1
|
|
self.adj_mat[j][i] = 1
|
|
|
|
def remove_edge(self, i: int, j: int):
|
|
"""Remove edge"""
|
|
# Parameters i, j correspond to the vertices element indices
|
|
# Handle index out of bounds and equality
|
|
if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:
|
|
raise IndexError()
|
|
self.adj_mat[i][j] = 0
|
|
self.adj_mat[j][i] = 0
|
|
|
|
def print(self):
|
|
"""Print adjacency matrix"""
|
|
print("Vertex list =", self.vertices)
|
|
print("Adjacency matrix =")
|
|
print_matrix(self.adj_mat)
|
|
|
|
|
|
"""Driver Code"""
|
|
if __name__ == "__main__":
|
|
# Initialize undirected graph
|
|
# Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
|
vertices = [1, 3, 2, 5, 4]
|
|
edges = [[0, 1], [0, 3], [1, 2], [2, 3], [2, 4], [3, 4]]
|
|
graph = GraphAdjMat(vertices, edges)
|
|
print("\nAfter initialization, the graph is")
|
|
graph.print()
|
|
|
|
# Add edge
|
|
# Vertices 1, 2 have indices 0, 2 respectively
|
|
graph.add_edge(0, 2)
|
|
print("\nAfter adding edge 1-2, the graph is")
|
|
graph.print()
|
|
|
|
# Remove edge
|
|
# Vertices 1, 3 have indices 0, 1 respectively
|
|
graph.remove_edge(0, 1)
|
|
print("\nAfter removing edge 1-3, the graph is")
|
|
graph.print()
|
|
|
|
# Add vertex
|
|
graph.add_vertex(6)
|
|
print("\nAfter adding vertex 6, the graph is")
|
|
graph.print()
|
|
|
|
# Remove vertex
|
|
# Vertex 3 has index 1
|
|
graph.remove_vertex(1)
|
|
print("\nAfter removing vertex 3, the graph is")
|
|
graph.print()
|