-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
rust/src/topological_sort.rs → rust/graphs/dfs/topological_sort.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
fn find_path(graph: &Vec<Vec<usize>>, u1: usize, matching: &mut Vec<usize>, vis: &mut Vec<bool>) -> bool { | ||
vis[u1] = true; | ||
for v in &graph[u1] { | ||
let u2 = matching[*v]; | ||
if u2 == usize::MAX || (!vis[u2] && find_path(graph, u2, matching, vis)) { | ||
matching[*v] = u1; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
pub fn max_matching(graph: &Vec<Vec<usize>>, n2: usize) -> (usize, Vec<usize>) { | ||
let n1 = graph.len(); | ||
let mut matching = vec![usize::MAX; n2]; | ||
let mut matches = 0; | ||
for u in 0..n1 { | ||
let mut vis = vec![false; n1]; | ||
if find_path(graph, u, &mut matching, &mut vis) { | ||
matches += 1; | ||
} | ||
} | ||
return (matches, matching); | ||
} | ||
|
||
fn main() { | ||
let g = vec![vec![0, 1], vec![0]]; | ||
let (cardinality, matching) = max_matching(&g, 2); | ||
assert_eq!(cardinality, 2); | ||
assert_eq!(matching, vec![1, 0]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use priority_queue::PriorityQueue; | ||
|
||
// https://cp-algorithms.com/graph/dijkstra_sparse.html | ||
// O(E*log(V)) time and O(V) memory | ||
pub fn dijkstra_heap(graph: &Vec<Vec<(usize, i32)>>, s: usize) -> (Vec<i32>, Vec<usize>) { | ||
let n = graph.len(); | ||
let mut prio = vec![i32::MAX; n]; | ||
let mut pred = vec![usize::MAX; n]; | ||
let mut q = PriorityQueue::<usize, i32>::new(); | ||
prio[s] = 0; | ||
q.push(0, 0); | ||
loop { | ||
match q.pop() { | ||
None => break, | ||
Some((u, _d)) => { | ||
for (v, len) in &graph[u] { | ||
let nprio = prio[u] + len; | ||
if prio[*v] > nprio { | ||
prio[*v] = nprio; | ||
pred[*v] = u; | ||
q.push(*v, nprio); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return (prio, pred); | ||
} | ||
|
||
fn main() { | ||
let g = vec![vec![(1, 10), (2, 8)], vec![(2, -5)], vec![]]; | ||
let (prio, pred) = dijkstra_heap(&g, 0); | ||
assert_eq!(prio, vec![0, 10, 5]); | ||
assert_eq!(pred, vec![usize::MAX, 0, 1]); | ||
} |
File renamed without changes.