The task is to implement Graph data structure and basic algorithms.
add_edge take two vertexes and create an edge with the given weight-value.
delete_edge take two vertexes and delete an edge between them.
add_vertex add a nonadjacent vertex to the graph.
delete_vertex delete last vertex with all edges incident to it.
get_edge_weight take two vertexes and return weight of the edge between them.
find_shortest_path_djkstra take an vertex index and find the shortest path from all another vertexes.
get_adjacency_matrix return the adjency matrix of the current graph.
find_all_shortest_path_wallsher return the shortest paths matrix.
find_minimal_spanning_tree return sequence of vertexes (incoming - outgoing vertex) - it's like a sequence of graph edges. This sequence of graph edges is minimal spanning tree for this graph.
breadth_first_search return lengths of the smallest paths sequence for the given vertex in unweighted graph.
depth_first_search traverse graph and mark all visited vertexes in boolean sequence.
find_connected_components return sequence of sequences(sequence of connected components) with marked vertexes.
topological_sort return the ordered sequence of vertexes( topological order).
The data organization type of data structure Graph is an adjacency matrix.
The basic value of this matrix is the weight of the vertex in graph.
I encapsulated the analog of the vector into Graph that I had already developed(Sequence).
The are 3 basic types for graphs: directed graph, undirected graph, metagraph.
V - finite set of vertexes, U - finite set of edges. Graph G = <V, U>.
Undirected graph
G = <V, U>
Directed graph
G = <V, U>
Adjacency matrix
Adjacency matrix directed graph
Adjacency list
It's one way to represent a graph as a collection of vertex lists.
Each vertex of the graph corrensponds to a list consisting of the "neighbors" of this vertex.
Vertex degree is quantity of edges incident to this vertex.
Route is a vertex sequence:
Route is a chain if:
Path is the shortest distance between two vertexes.
Edge deletion
Vertex deletion
The problem is to find path with the smallest length in unweighted graph( path with the least number of edges).
Algorithm asymptotic is
We use queue (adapter on array_sequence with push(append) and pop(get_first->erase(0))) to mark vertexes which are already burning. Also we use boolean sequence to mark already
used vertexes and sequence of shortest lengths - our result.
How to apply
1. Finding the shortest path in an unweighted graph
2. Search for connectivity components in a graph
The problem is to traverse graph.
Algorithm asymptotic is
1. Finding connected components
2. Topological sorting
3. Finding the bridges
Bridge is an edge, whose removal increase quantity of connected components.
The problem is to find all connected components.
Algorithm asymptotic is
In this algorithm we use depth first search by launching it from first vertex and set all true values in the boolean sequence, which we have obtained, are connected component.
Then we start dfs with the next remaining vertex.
Connected component
is the maximal connected subgraph of the graph G.
Сonnected graph
is a graph containing exactly one connected component. This means, that there is at least one chain between any pair of vertexes.
The problem is to change vertexes numbering according to the topoligical order in the directed graph.
Algorithm asymptotic is
This sequence is topologically ordered set of vertexes - solution of the problem.
If our graph have a cycle, vertex sequence will remain in the original order.
Topological sort
is renumbering of the vertexes set so that each edge leads from a vertex with a smaller number to a vertex with a larger one.
Topological sorting may not exist at all — if the graph contains cycles.
The problem is to find shortest paths from a given vertex to all another vertexes.
Algorithm asymptotic is
The vertex chosen by us in this iteration is marked as visited in boolean visited_sequence[vertex].
Then on this iteration we consider all edges coming from the vertex named vertex and for every vertex named j algorithm tries to make better value path_sequence[j].
It is worth noting that if not all vertexes of the graph are reachable from the vertex s, then the values path_sequence[vertex] will remain infinity( SIZE_MAX_LOCAL)
The problem is to find shortest path from every vertex to all another vertexes.
Algorithm asymptotic is
Let the iteration be at k phase. We need to rebuild the matrix in matching (k+1) phase and make better every path_matrix[i][j].
As in Dijkstra's algorithm, the formula on pseudocode is path_matrix[i][j] = min(path_matrix[i][j], path_matrix[i][k] + path_matrix[k][j]).
The problem is to find minimal spanning tree for the inputed graph.
Algorithm asymptotic is
Desired spanning tree is building by adding one edge with the minimal weight at a time.
The minimal spanning tree is a tree without cycles based on vertexes of the main graph. It's easy to understand, that if our current graph has n vertexes, then our spanning tree will contain n vertexes and n - 1 edges. If the main graph has more than 1 connected components, we can't build spanning tree.
.
┈┈╭━╱▔▔▔▔╲━╮┈┈┈
┈┈╰╱╭▅╮╭▅╮╲╯┈┈┈
╳┈┈▏╰┈▅▅┈╯▕┈┈┈┈
┈┈┈╲┈╰━━╯┈╱┈┈╳┈
┈┈┈╱╱▔╲╱▔╲╲┈┈┈┈
┈╭━╮▔▏┊┊▕▔╭━╮┈╳
┈┃┊┣▔╲┊┊╱▔┫┊┃┈┈
┈╰━━━━╲╱━━━━╯┈╳