fn minimum_spanning_tree_impl(links: &[Link], node_count: usize) -> Graph {
		let mut roots = Vec::with_capacity(node_count);
		let mut mst = Vec::new();

		// initial root of every node is itself
		for i in 0..node_count {
			roots.push(i as ID);
		}

		// find root of node
		fn root(roots: &mut [ID], i: ID) -> usize {
			let mut i = i as usize;
			while roots[i] != i as ID {
				// Path halving optimization
				let tmp = roots[roots[i] as usize];
				roots[i] = tmp;
				i = tmp as usize;
			}
			i
		}

		for link in links {
			let x = root(&mut roots, link.from);
			let y = root(&mut roots, link.to);
			if x != y {
				mst.push(link.clone());
				roots[x] = roots[y];
			}
		}

		// count roots of all minimum spanning trees
		let roots_count = roots.iter().enumerate().fold(0,
			|acc, (i, j)| acc + ((i as u32) == *j) as usize
		);

		Graph{ node_count: (mst.len() + roots_count), links: mst }
	}
// Implementation of the Kruskal minimum spanning tree algorithm
Implementation of the Kruskal minimum spanning tree algorithm
Implementation of the Kruskal minimum spanning tree algorithm
fn minimum_spanning_tree_impl(links: &[Link], node_count: usize) -> Graph {
		let mut roots = Vec::with_capacity(node_count);
		let mut mst = Vec::new();
		// initial root of every node is itself
		for i in 0..node_count {
			roots.push(i as ID);
		}
		// find root of node
		fn root(roots: &mut [ID], i: ID) -> usize {
			let mut i = i as usize;
			while roots[i] != i as ID {
				// Path halving optimization
				let tmp = roots[roots[i] as usize];
				roots[i] = tmp;
				i = tmp as usize;
			}
			i
		}
		for link in links {
			let x = root(&mut roots, link.from);
			let y = root(&mut roots, link.to);
			if x != y {
				mst.push(link.clone());
				roots[x] = roots[y];
			}
		}
		// count roots of all minimum spanning trees
		let roots_count = roots.iter().enumerate().fold(0,
			|acc, (i, j)| acc + ((i as u32) == *j) as usize
		);
		Graph{ node_count: (mst.len() + roots_count), links: mst }
	}
fn minimum_spanning_tree_impl ( links : & [ Link ] , node_count : usize ) -> Graph { let mut roots = Vec :: with_capacity ( node_count ) ; let mut mst = Vec :: new ( ) ; // initial root of every node is itself for i in 0 .. node_count { roots . push ( i as ID ) ; } // find root of node fn root ( roots : & mut [ ID ] , i : ID ) -> usize { let mut i = i as usize ; while roots [ i ] != i as ID { // Path halving optimization let tmp = roots [ roots [ i ] as usize ] ; roots [ i ] = tmp ; i = tmp as usize ; } i } for link in links { let x = root ( & mut roots , link . from ) ; let y = root ( & mut roots , link . to ) ; if x != y { mst . push ( link . clone ( ) ) ; roots [ x ] = roots [ y ] ; } } // count roots of all minimum spanning trees let roots_count = roots . iter ( ) . enumerate ( ) . fold ( 0 , | acc , ( i , j ) | acc + ( ( i as u32 ) == * j ) as usize ) ; Graph { node_count : ( mst . len ( ) + roots_count ) , links : mst } }
Implementation of the Kruskal minimum spanning tree algorithm
Implementation of the Kruskal minimum spanning tree algorithm
links
&[Link]
node_count
usize
links
&[Link]
node_count
usize