Files
hello-algo/en/codes/ruby/chapter_graph/graph_adjacency_matrix.rb
Yudong Jin 2778a6f9c7 Translate all code to English (#1836)
* 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
2025-12-31 07:44:52 +08:00

117 lines
3.2 KiB
Ruby

=begin
File: graph_adjacency_matrix.rb
Created Time: 2024-04-25
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/print_util'
### Undirected graph class based on adjacency matrix ###
class GraphAdjMat
def initialize(vertices, edges)
### Constructor ###
# Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
@vertices = []
# Adjacency matrix, where the row and column indices correspond to the "vertex index"
@adj_mat = []
# Add vertex
vertices.each { |val| add_vertex(val) }
# Add edge
# Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
edges.each { |e| add_edge(e[0], e[1]) }
end
### Get number of vertices ###
def size
@vertices.length
end
### Add vertex ###
def add_vertex(val)
n = size
# Add the value of the new vertex to the vertex list
@vertices << val
# Add a row to the adjacency matrix
new_row = Array.new(n, 0)
@adj_mat << new_row
# Add a column to the adjacency matrix
@adj_mat.each { |row| row << 0 }
end
### Delete vertex ###
def remove_vertex(index)
raise IndexError if index >= size
# Remove the vertex at index from the vertex list
@vertices.delete_at(index)
# Remove the row at index from the adjacency matrix
@adj_mat.delete_at(index)
# Remove the column at index from the adjacency matrix
@adj_mat.each { |row| row.delete_at(index) }
end
### Add edge ###
def add_edge(i, j)
# Parameters i, j correspond to the vertices element indices
# Handle index out of bounds and equality
if i < 0 || j < 0 || i >= size || j >= size || i == j
raise IndexError
end
# In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
@adj_mat[i][j] = 1
@adj_mat[j][i] = 1
end
### Delete edge ###
def remove_edge(i, j)
# Parameters i, j correspond to the vertices element indices
# Handle index out of bounds and equality
if i < 0 || j < 0 || i >= size || j >= size || i == j
raise IndexError
end
@adj_mat[i][j] = 0
@adj_mat[j][i] = 0
end
### Print adjacency matrix ###
def __print__
puts "Vertex list = #{@vertices}"
puts 'Adjacency matrix ='
print_matrix(@adj_mat)
end
end
### Driver Code ###
if __FILE__ == $0
# Add edge
# 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.new(vertices, edges)
puts "\nAfter initialization, graph is"
graph.__print__
# Add edge
# Add vertex
graph.add_edge(0, 2)
puts "\nAfter adding edge 1-2, graph is"
graph.__print__
# Remove edge
# Vertices 1, 3 have indices 0, 1 respectively
graph.remove_edge(0, 1)
puts "\nAfter removing edge 1-3, graph is"
graph.__print__
# Add vertex
graph.add_vertex(6)
puts "\nAfter adding vertex 6, graph is"
graph.__print__
# Remove vertex
# Vertex 3 has index 1
graph.remove_vertex(1)
puts "\nAfter removing vertex 3, graph is"
graph.__print__
end