I am new to Rust, I am looking for ways to improve my implementation of graphs, can you please suggest any ways to improve it? Thank you.
<code>#![allow(dead_code)]
use std::collections::HashMap;
#[derive(Clone,Debug)]
struct Vertex<T>{
data: T,
edges: HashMap<T,i32>,
}
//Undirected Graph Implementation
struct Graph<T>{
vertices: Vec<Box<Vertex<T>>>,
}
</code>
<code>#![allow(dead_code)]
use std::collections::HashMap;
#[derive(Clone,Debug)]
struct Vertex<T>{
data: T,
edges: HashMap<T,i32>,
}
//Undirected Graph Implementation
struct Graph<T>{
vertices: Vec<Box<Vertex<T>>>,
}
</code>
#![allow(dead_code)]
use std::collections::HashMap;
#[derive(Clone,Debug)]
struct Vertex<T>{
data: T,
edges: HashMap<T,i32>,
}
//Undirected Graph Implementation
struct Graph<T>{
vertices: Vec<Box<Vertex<T>>>,
}
Vertex block:
<code>impl <T> Vertex<T>
where T: Clone + Copy + std::fmt::Debug + Eq + PartialEq + std::hash::Hash{
fn new(val: T)->Vertex<T>{
return Vertex{
data: val,
edges: HashMap::new(),
}
}
fn append(&mut self, other_: T, weight: i32){
self.edges.insert(other_,weight);
}
fn delete(&mut self, other_: T){
if self.edges.contains_key(&other_){ self.edges.remove(&other_); };
}
}
</code>
<code>impl <T> Vertex<T>
where T: Clone + Copy + std::fmt::Debug + Eq + PartialEq + std::hash::Hash{
fn new(val: T)->Vertex<T>{
return Vertex{
data: val,
edges: HashMap::new(),
}
}
fn append(&mut self, other_: T, weight: i32){
self.edges.insert(other_,weight);
}
fn delete(&mut self, other_: T){
if self.edges.contains_key(&other_){ self.edges.remove(&other_); };
}
}
</code>
impl <T> Vertex<T>
where T: Clone + Copy + std::fmt::Debug + Eq + PartialEq + std::hash::Hash{
fn new(val: T)->Vertex<T>{
return Vertex{
data: val,
edges: HashMap::new(),
}
}
fn append(&mut self, other_: T, weight: i32){
self.edges.insert(other_,weight);
}
fn delete(&mut self, other_: T){
if self.edges.contains_key(&other_){ self.edges.remove(&other_); };
}
}
Graph methods: I have doubts regarding my implementation of add_vertex method particularly is there any ways to improve it.
<code>impl <T: Copy + Eq + std::fmt::Debug + std::hash::Hash + PartialEq> Graph<T>{
fn new()->Graph<T>{
return Self { vertices: Vec::new()}
}
//Have to rewrite this fully to implement undirected graph :).
fn add_vertex(&mut self, data: T, connection: HashMap<T,i32>){
//check if the data is already in the vector;
if self.vertices.iter().any(|vertex| vertex.data == data){
let index = self.vertices.iter().position(|vertex| vertex.data == data).unwrap();
let mut node_i = Box::new(Vertex::new(data));
for i in &self.vertices{
if i.data == data{
node_i = i.clone();
}
}
for (key, value) in connection{
self.add_edge(&mut node_i,key, value);
}
self.vertices[index] = node_i;
}
//if there is no vertex with the same name;
else{
let mut new_node = Box::new(Vertex::new(data));
for (key,value) in connection{
self.add_edge(&mut new_node, key, value);
}
self.vertices.push(new_node);
}
}
fn add_edge(&mut self,node_1: &mut Box<Vertex<T>>, node_2: T,value: i32){
node_1.append(node_2, value);
if self.vertices.iter().any(|vertex| vertex.data == node_2){
let index = self.vertices.iter().position(|vertex| vertex.data == node_2).unwrap();
let node_i = self.vertices.get_mut(index).unwrap();
node_i.append(node_1.data,value);
}
}
</code>
<code>impl <T: Copy + Eq + std::fmt::Debug + std::hash::Hash + PartialEq> Graph<T>{
fn new()->Graph<T>{
return Self { vertices: Vec::new()}
}
//Have to rewrite this fully to implement undirected graph :).
fn add_vertex(&mut self, data: T, connection: HashMap<T,i32>){
//check if the data is already in the vector;
if self.vertices.iter().any(|vertex| vertex.data == data){
let index = self.vertices.iter().position(|vertex| vertex.data == data).unwrap();
let mut node_i = Box::new(Vertex::new(data));
for i in &self.vertices{
if i.data == data{
node_i = i.clone();
}
}
for (key, value) in connection{
self.add_edge(&mut node_i,key, value);
}
self.vertices[index] = node_i;
}
//if there is no vertex with the same name;
else{
let mut new_node = Box::new(Vertex::new(data));
for (key,value) in connection{
self.add_edge(&mut new_node, key, value);
}
self.vertices.push(new_node);
}
}
fn add_edge(&mut self,node_1: &mut Box<Vertex<T>>, node_2: T,value: i32){
node_1.append(node_2, value);
if self.vertices.iter().any(|vertex| vertex.data == node_2){
let index = self.vertices.iter().position(|vertex| vertex.data == node_2).unwrap();
let node_i = self.vertices.get_mut(index).unwrap();
node_i.append(node_1.data,value);
}
}
</code>
impl <T: Copy + Eq + std::fmt::Debug + std::hash::Hash + PartialEq> Graph<T>{
fn new()->Graph<T>{
return Self { vertices: Vec::new()}
}
//Have to rewrite this fully to implement undirected graph :).
fn add_vertex(&mut self, data: T, connection: HashMap<T,i32>){
//check if the data is already in the vector;
if self.vertices.iter().any(|vertex| vertex.data == data){
let index = self.vertices.iter().position(|vertex| vertex.data == data).unwrap();
let mut node_i = Box::new(Vertex::new(data));
for i in &self.vertices{
if i.data == data{
node_i = i.clone();
}
}
for (key, value) in connection{
self.add_edge(&mut node_i,key, value);
}
self.vertices[index] = node_i;
}
//if there is no vertex with the same name;
else{
let mut new_node = Box::new(Vertex::new(data));
for (key,value) in connection{
self.add_edge(&mut new_node, key, value);
}
self.vertices.push(new_node);
}
}
fn add_edge(&mut self,node_1: &mut Box<Vertex<T>>, node_2: T,value: i32){
node_1.append(node_2, value);
if self.vertices.iter().any(|vertex| vertex.data == node_2){
let index = self.vertices.iter().position(|vertex| vertex.data == node_2).unwrap();
let node_i = self.vertices.get_mut(index).unwrap();
node_i.append(node_1.data,value);
}
}
This is my implentation
New contributor
Ramanan S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.