Skip to content

Commit

Permalink
add length to undirected graph edge
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Dec 8, 2024
1 parent bd5eaf7 commit 8789e02
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 98 deletions.
53 changes: 10 additions & 43 deletions src/graph/directed_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ impl DirectedGraph {
fn dfs_recursive_subroutine(
&self,
start: VertexIndex,
visited: &mut HashSet<usize>,
order: &mut Vec<usize>,
) -> Result<Vec<usize>, GraphError> {
visited: &mut HashSet<VertexIndex>,
order: &mut Vec<VertexIndex>,
) -> Result<Vec<VertexIndex>, GraphError> {
// Ensure the starting vertex exists
let vertex = self.vertices.get(start).ok_or(GraphError::VertexNotFound)?;

Expand Down Expand Up @@ -187,7 +187,7 @@ impl DirectedGraph {
/// for every v ∈ V do
/// if v is unexplored then // in a prior DFS
/// DFS-Topo(G, v)
pub fn topo_sort(&self, reversed: bool) -> Vec<(usize, usize)> {
pub fn topo_sort(&self, reversed: bool) -> Vec<(usize, VertexIndex)> {
let vertices = &self.vertices;
let vertcies_num = vertices.len();
let mut current_label = vertcies_num;
Expand All @@ -206,7 +206,7 @@ impl DirectedGraph {
}
}

let mut sorted_vertices: Vec<(usize, usize)> = topological_sort
let mut sorted_vertices: Vec<(usize, VertexIndex)> = topological_sort
.iter()
.enumerate() // Produces (index, &label)
.map(|(index, &label)| (label, index))
Expand All @@ -232,7 +232,7 @@ impl DirectedGraph {
fn _dfs_topo(
&self,
vertex_index: VertexIndex,
visited: &mut HashSet<usize>,
visited: &mut HashSet<VertexIndex>,
topological_sort: &mut TopologicalSort,
current_label: &mut usize,
reversed: bool,
Expand Down Expand Up @@ -284,7 +284,7 @@ impl DirectedGraph {
fn _dfs_topo_iterative(
&self,
vertex_index: VertexIndex,
visited: &mut HashSet<usize>,
visited: &mut HashSet<VertexIndex>,
topological_sort: &mut TopologicalSort,
current_label: &mut usize,
reversed: bool,
Expand Down Expand Up @@ -384,7 +384,7 @@ impl DirectedGraph {
///
fn _dfs_scc(
&self,
vertex_index: usize,
vertex_index: VertexIndex,
scc_vec: &mut Vec<usize>,
num_scc: usize,
visted_set: &mut HashSet<usize>,
Expand All @@ -403,7 +403,7 @@ impl DirectedGraph {

fn _dfs_scc_iterative(
&self,
vertex_index: usize,
vertex_index: VertexIndex,
scc_vec: &mut [usize],
num_scc: usize,
visited: &mut HashSet<usize>,
Expand Down Expand Up @@ -461,40 +461,7 @@ impl DirectedGraph {
// /// (v*,w*) := such an edge minimizing len(v) + lvw
// /// add w* to X
// /// len(w*) := len(v*) + lv*w*
// pub fn dijkstra(&self, s: &VertexRc, num_scc: &mut usize) {
// s.borrow_mut().explored = true;
// s.borrow_mut().scc = Some(*num_scc);

// for v in &s.borrow().outgoing_edges {
// if !v.destination.borrow().explored {
// self.dfs_topo(&v.destination, num_scc);
// }
// }
// }

// ////////////////// helpers /////////////////////
// fn has_cycle(&self) -> bool {
// for vertex in &self.vertices {
// if self.detect_cycle(vertex, &mut vec![false; self.vertices.len()]) {
// return true;
// }
// }
// false
// }

// fn detect_cycle(&self, vertex: &VertexRc, visited: &mut Vec<bool>) -> bool {
// let vertex_index = self.get_vertex_index(vertex);
// if visited[vertex_index] {
// return true;
// }
// visited[vertex_index] = true;
// for edge in &vertex.borrow().outgoing_edges {
// if self.detect_cycle(&edge.destination, visited) {
// return true;
// }
// }
// visited[vertex_index] = false;
// false
// pub fn dijkstra(&self, s: VertexIndex, ) {
// }

pub fn print_graph(&self) {
Expand Down
10 changes: 9 additions & 1 deletion src/graph/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub type VertexIndex = usize;
pub type Length = usize;

#[derive(Debug, PartialEq)]
pub enum GraphError {
SelfLoop,
Expand All @@ -10,5 +12,11 @@ pub enum GraphError {

pub trait Graph {
fn add_vertex(&mut self);
fn add_edge(&mut self, from: VertexIndex, to: VertexIndex) -> Result<(), GraphError>;
fn add_edge(
&mut self,
from: VertexIndex,
to: VertexIndex,
length: Length,
) -> Result<(), GraphError>;
fn add_unit_edge(&mut self, from: VertexIndex, to: VertexIndex) -> Result<(), GraphError>;
}
Loading

0 comments on commit 8789e02

Please sign in to comment.