implemented weighted adjacency matrix data structure

This commit is contained in:
specCon18 2024-05-28 21:20:14 -04:00
parent 43b45b31fd
commit 81fd2e42c6
3 changed files with 107 additions and 0 deletions

View file

@ -2,4 +2,5 @@ pub mod queue;
pub mod stack;
pub mod min_heap;
pub mod weighted_adj_list;
pub mod weighted_adj_matrix;
pub mod ring_buffer;

View file

@ -0,0 +1,84 @@
pub struct WeightedAdjacencyMatrix {
matrix: Vec<Vec<u64>>,
}
impl WeightedAdjacencyMatrix {
pub fn new(matrix: Vec<Vec<u64>>) -> Self {
WeightedAdjacencyMatrix { matrix }
}
pub fn get_value(&self, row: usize, col: usize) -> Option<u64> {
self.matrix.get(row).and_then(|r| r.get(col).cloned())
}
pub fn set_value(&mut self, row: usize, col: usize, value: u64) -> Result<(), &'static str> {
if let Some(row_vec) = self.matrix.get_mut(row) {
if let Some(cell) = row_vec.get_mut(col) {
*cell = value;
return Ok(());
}
}
Err("Coordinates out of bounds")
}
pub fn transpose(&self) -> WeightedAdjacencyMatrix {
let transposed = (0..self.matrix[0].len())
.map(|i| (0..self.matrix.len()).map(|j| self.matrix[j][i]).collect())
.collect();
WeightedAdjacencyMatrix::new(transposed)
}
pub fn get_row(&self, row_index: usize) -> Option<&Vec<u64>> {
self.matrix.get(row_index)
}
pub fn get_column(&self, col_index: usize) -> Option<Vec<u64>> {
if col_index >= self.matrix[0].len() {
return None;
}
Some(self.matrix.iter().map(|row| row[col_index]).collect())
}
pub fn multiply_scalar(&mut self, scalar: u64) {
for row in self.matrix.iter_mut() {
for cell in row.iter_mut() {
*cell *= scalar;
}
}
}
pub fn add_matrix(&mut self, other: &WeightedAdjacencyMatrix) -> Option<()> {
if self.matrix.len() != other.matrix.len() || self.matrix[0].len() != other.matrix[0].len() {
return None; // Matrices must have the same dimensions to add
}
for i in 0..self.matrix.len() {
for j in 0..self.matrix[i].len() {
self.matrix[i][j] += other.matrix[i][j];
}
}
Some(())
}
pub fn subtract_matrix(&mut self, other: &WeightedAdjacencyMatrix) -> Option<()> {
if self.matrix.len() != other.matrix.len() || self.matrix[0].len() != other.matrix[0].len() {
return None; // Matrices must have the same dimensions to subtract
}
for i in 0..self.matrix.len() {
for j in 0..self.matrix[i].len() {
self.matrix[i][j] -= other.matrix[i][j];
}
}
Some(())
}
pub fn display_matrix(&self) {
// Printing the array with row and column indices
for row in self.matrix.iter() {
for col in row.iter() {
print!("{} ", col);
}
println!();
}
}
}

View file

@ -88,6 +88,27 @@ fn heap_sort_demo(){
heap_sort::heap_sort(&mut arr);
println!("Array after heap sort: {:?}",arr);
}
fn weighted_adj_matrix_demo(){
println!("-------------------");
println!("WEIGHTED ADJACENCY MATRIX DEMO:");
println!("-------------------");
let mut matrix = data_structures::weighted_adj_matrix::WeightedAdjacencyMatrix::new(vec![
vec![1, 2, 3],
vec![4, 5, 6],
vec![7, 8, 9],
]);
// Display the matrix before modification
println!("Matrix before modification:");
matrix.display_matrix();
// Demo of set_value function
match matrix.set_value(1, 1, 10) {
Ok(_) => println!("Successfully set value at row 1, column 1"),
Err(err) => println!("Error: {}", err),
}
// Display the matrix after modification
println!("Matrix after modification:");
matrix.display_matrix();
}
fn main() {
println!("");
println!("|~~~~~~~~~~~~~~~|");
@ -106,4 +127,5 @@ fn main() {
merge_sort_demo();
radix_sort_demo();
heap_sort_demo();
weighted_adj_matrix_demo();
}