Finish assignment for course 1 week 4
This commit is contained in:
24
src/util.rs
24
src/util.rs
@@ -1,16 +1,10 @@
|
||||
use crate::min_cut::Graph;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::{BufRead, BufReader, Error, ErrorKind};
|
||||
use std::vec::Vec;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Graph {
|
||||
nodes: Vec<u32>,
|
||||
edges: Vec<(u32, u32)>,
|
||||
}
|
||||
|
||||
|
||||
pub fn read_to_vector(path: &str) -> Result<Vec<i64>, io::Error> {
|
||||
let file = File::open(path)?;
|
||||
let br = BufReader::new(file);
|
||||
@@ -34,8 +28,20 @@ pub fn read_to_graph(path: &str) -> Result<Graph, io::Error> {
|
||||
|
||||
for line in br.lines() {
|
||||
let line = line?;
|
||||
println!("{:?}", line);
|
||||
}
|
||||
let mut nodes = line.split_whitespace();
|
||||
let current_node = nodes.next().unwrap().parse().unwrap();
|
||||
g.nodes.push(current_node);
|
||||
|
||||
for neightbor in nodes {
|
||||
let neightbor: u32 = neightbor.parse().unwrap();
|
||||
// If the neighbor is smaller than the current node that means we
|
||||
// have already added it when we iterated over that smaller node.
|
||||
// Hence, we do not add the edge again.
|
||||
if neightbor > current_node {
|
||||
let edge = (current_node, neightbor);
|
||||
g.edges.push(edge);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(g)
|
||||
}
|
||||
Reference in New Issue
Block a user