From 722ef3502d2ad180684380fbde0532fbc1a80ff9 Mon Sep 17 00:00:00 2001 From: specCon18 Date: Tue, 28 May 2024 22:19:12 -0400 Subject: [PATCH] Revert "Revert "implemented weighted adjacency matrix data structure"" This reverts commit 84da9b21b5812c89da04e232ef09d63cfc0717f5. --- src/data_structures/mod.rs | 1 + src/data_structures/weighted_adj_matrix.rs | 84 ++++++++++++++++++++++ src/main.rs | 22 ++++++ 3 files changed, 107 insertions(+) create mode 100644 src/data_structures/weighted_adj_matrix.rs diff --git a/src/data_structures/mod.rs b/src/data_structures/mod.rs index 06f904a..9826a9e 100644 --- a/src/data_structures/mod.rs +++ b/src/data_structures/mod.rs @@ -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; diff --git a/src/data_structures/weighted_adj_matrix.rs b/src/data_structures/weighted_adj_matrix.rs new file mode 100644 index 0000000..5edbeb0 --- /dev/null +++ b/src/data_structures/weighted_adj_matrix.rs @@ -0,0 +1,84 @@ +pub struct WeightedAdjacencyMatrix { + matrix: Vec>, +} + +impl WeightedAdjacencyMatrix { + pub fn new(matrix: Vec>) -> Self { + WeightedAdjacencyMatrix { matrix } + } + + pub fn get_value(&self, row: usize, col: usize) -> Option { + 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> { + self.matrix.get(row_index) + } + + pub fn get_column(&self, col_index: usize) -> Option> { + 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!(); + } + } +} + diff --git a/src/main.rs b/src/main.rs index d2b17ec..404eb58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); }