Revert "implemented weighted adjacency matrix data structure"
This reverts commit 81fd2e42c6.
This commit is contained in:
parent
81fd2e42c6
commit
84da9b21b5
3 changed files with 0 additions and 107 deletions
|
|
@ -2,5 +2,4 @@ pub mod queue;
|
|||
pub mod stack;
|
||||
pub mod min_heap;
|
||||
pub mod weighted_adj_list;
|
||||
pub mod weighted_adj_matrix;
|
||||
pub mod ring_buffer;
|
||||
|
|
|
|||
|
|
@ -1,84 +0,0 @@
|
|||
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!();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
22
src/main.rs
22
src/main.rs
|
|
@ -88,27 +88,6 @@ 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!("|~~~~~~~~~~~~~~~|");
|
||||
|
|
@ -127,5 +106,4 @@ fn main() {
|
|||
merge_sort_demo();
|
||||
radix_sort_demo();
|
||||
heap_sort_demo();
|
||||
weighted_adj_matrix_demo();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue