Crates.io | luka |
lib.rs | luka |
version | 0.4.0 |
source | src |
created_at | 2021-08-01 09:24:43.729594 |
updated_at | 2021-08-14 12:32:52.098143 |
description | Library for working with graphs |
homepage | |
repository | https://github.com/amazing-hash/luka |
max_upload_size | |
id | 429963 |
size | 111,471 |
use luka::Graph;
use luka::algorithms;
use luka::utils;
fn main() {
let mut graph = Graph::new(100);
graph.add_edge(1, 2, 0).unwrap();
graph.add_edge(1, 3, 0).unwrap();
graph.add_edge(2, 4, 0).unwrap();
graph.add_edge(3, 4, 0).unwrap();
graph.add_edge(4, 5, 0).unwrap();
let start = graph.get_vertex(1).unwrap();
let target = graph.get_vertex(5).unwrap();
let parents = algorithms::bfs(&graph, &start).unwrap();
match utils::find_path(&graph, &target, &parents).unwrap() {
Some(path) => {
assert_eq!(path.iter().map(|vertex|vertex.id()).collect::<Vec<usize>>(), vec![1, 2, 5]);
}
None => {
println!("Path not found !!!")
}
}
}
use luka::Graph;
use luka::algorithms;
use luka::utils;
fn main() {
struct CustomVisitor {
visiting_order: Vec<usize>
}
impl <T> VisitorBFS<T> for CustomVisitor {
fn queue_extract_event(&mut self, vertex: &Vertex<T>) -> Result<VisitorBFSAction, GraphError> {
self.visiting_order.push(vertex.id());
Ok(VisitorBFSAction::Nothing)
}
}
let mut graph = Graph::new(100);
graph.add_edge(1, 2, 0.0).unwrap();
graph.add_edge(2, 3, 0.0).unwrap();
graph.add_edge(2, 4, 0.0).unwrap();
graph.add_edge(2, 5, 0.0).unwrap();
graph.add_edge(4, 8, 0.0).unwrap();
graph.add_edge(8, 17, 0.0).unwrap();
let mut visitor = CustomVisitor{ visiting_order: vec![] };
let start = graph.get_vertex(1).unwrap();
bfs_with_visitor(&graph, &start, &mut visitor).unwrap();
assert_eq!(vec![1, 2, 3, 4, 5, 8, 17], visitor.visiting_order);
}
Breadth-first search (BFS) is one of the simplest graph traversal algorithms and is the basis for many important graph algorithms. Algorithmic complexity - O(|V| + |E|), where |V| is the number of vertexs in the graph and |E| is the number of edges in the graph.
Depth-first search (DFS) - one of the methods of graph traversal Algorithmic complexity - O(|V| + |E|), where |V| is the number of vertexs in the graph and |E| is the number of edges in the graph.
Search for connectivity components. The algorithm works with an undirected graph. The algorithm divides the vertices of the graph into several groups, so that within one group one can reach from one vertex to any other, but between different groups there is no path. Algorithmic complexity - O(|V| + |E|), where |V| is the number of vertexs in the graph and |E| is the number of edges in the graph.
A strong connectivity component is a subset of vertices (maximal inclusion) such that any two vertices of this subset are reachable from each other. The graph is oriented. Algorithmic complexity - O(|V| + |E|), where |V| is the number of vertexs in the graph and |E| is the number of edges in the graph.
Deikstra's Algorithm is an algorithm on graphs. It finds shortest paths from one of the vertices of a graph to all other vertices. The algorithm works only for graphs without edges of negative weight. Algorithmic complexity - O(|E| log |V|), where |V| is the number of vertexs in the graph and |E| is the number of edges in the graph.
The problem of topological sorting of a graph is as follows: specify such a linear order on its vertices that any edge leads from a vertex with a smaller number to a vertex with a larger number. Obviously, if the graph has cycles, there is no such order. Algorithmic complexity - O(|V| + |E|), where |V| is the number of vertexs in the graph and |E| is the number of edges in the graph.
Algorithmic complexity - O(|V| + |E|), where |V| is the number of vertexs in the graph and |E| is the number of edges in the graph.
The algorithm finds the size of each subtree. The graph must be a tree, i.e. a connected acyclic graph. Algorithmic complexity - O(|E|), where |E| is the number of edges in the graph.
The algorithm finds the depth of each vertex. The graph must be a tree, i.e. a connected acyclic graph. Algorithmic complexity - O(|E|), where |E| is the number of edges in the graph.
Kruskal's algorithm is an efficient algorithm for constructing the minimum spanning tree of a weighted connected undirected graph. Algorithmic complexity - O(|E| * log(|E|)), where |E| is the number of edges in the graph.
This algorithm for finding shortest paths in a weighted graph with positive or negative weights of edges (but no negative cycles). Algorithmic complexity - O(|V|^3), where |V| is the number of vertexs in the graph.
The Bellman-Ford algorithm is an algorithm for finding the shortest path in a weighted graph. Unlike Dijkstra's algorithm, Bellman-Ford's algorithm allows edges with negative weight, but negative weight loops are still forbidden. Algorithmic complexity - O(|E| * |V|), where |E| is the number of edges in the graph and |V| is the number of vertexs in the graph.
Algorithmic complexity of construction - O(n). Algorithmic complexity of the query response -O(log n) where n is number nodes of tree.
Finding bridges in an undirected graph. Algorithmic complexity - O(|E| + |V|), where |E| is the number of edges in the graph and |V| is the number of vertexs in the graph.