Quickly introduce 10 basic graph algorithms, with examples and visualization
Graphs have become a powerful means of modeling and capturing data in real-world scenarios, such as social media networks, web pages and links, as well as locations and routes in GPS. If you have a set of objects that are related to each other, you can use graphs to represent them.
Simple Application Server
USD1.00 New User Coupon
* Only 3,000 coupons available.
* Each new user can only get one coupon(except users from distributors).
* The coupon is valid for 30 days from the date of receipt.
In this article, I will briefly explain 10 basic graphics algorithms that are very useful for analysis and application.
First, let us introduce the graph.
What is a graph?
A graph consists of a limited set of vertices or nodes and a set of edges connecting these vertices. If two vertices are connected to each other by the same edge, they are called adjacent.
Some basic definitions related to graphs are given below. You can refer to the example in Figure 1.
Order: the number of vertices in the graph
Number of edges Size: the number of edges in the graph
Vertex degree: the number of edges associated with a vertex
Isolated vertex: Vertex that is not connected to other vertices in the graph
Self-loop: an edge from a vertex to itself
Directed graph: All edges have a direction to represent the starting point and ending point of the graph
Undirected graph: A graph with edges without directions
Weighted graph: The edges of the graph have weights
Unweighted graph: The edges of the graph have no weights
Breadth-first search
Traverse or search is one of the basic operations that can be performed on the graph. In breadth first search (BFS), we start from a specific vertex and explore all its neighbors at the current depth before entering the vertex of the next layer. Unlike trees, graphs can contain cycles (the first and last vertices are the same path). Therefore, we must keep track of the vertices visited. When implementing BFS, we use the queue data structure.
Figure 2 shows an animation of BFS traversal of an example graph. Notice how the vertices are found (yellow) and visited (red).
application
Used to determine the shortest path and minimum spanning tree.
Used by search engine crawlers to build an index of web pages.
Used to search on social networks.
Used to find available adjacent nodes in a peer-to-peer network, such as BitTorrent.
Depth-first search
In depth-first search (DFS), we start from a specific vertex and search as far as possible along each branch before backtracking. In DFS, we also need to track the vertices visited. When implementing DFS, we use a stack data structure to support backtracking.
FIG. 3 shows an animation of DFS traversal of the same example graph used in FIG. 2. Notice how it traverses to depth and backtracking.
application
Used to find the path between two vertices.
Used to detect cycles in the graph.
Used for topological sorting.
Used to solve puzzles with only one solution (such as a maze)
Shortest path
The shortest path from one vertex to another is the path with the smallest sum of weights of the edges that should be moved in the graph.
Figure 4 shows an animation in which the shortest path from vertex 1 to vertex 6 in the figure is determined.
algorithm
Dijkstra's shortest path algorithm, bellman algorithm
application
It is used to find a route from one place to another in map software such as Google Maps or Apple maps.
It is used to solve the minimum delay path problem in the network.
In an abstract machine, the choice to reach a certain goal state is determined by switching between different states (for example, it can be used to determine the smallest possible number of moves to win a game).
Cycle Detection Cycle Detection
A cycle is a path where the first vertex and the last vertex in the graph are the same. If we start from a vertex, follow a path, and finally reach the starting point, then the path is a cycle. Cycle detection is the process of detecting these cycles. Figure 5 shows the animation traversing a loop.
algorithm
Floyd period detection algorithm, Brent algorithm
application
Used for message-based distributed algorithms.
Used to process large-scale graphics using the distributed processing system on the cluster.
Used to detect deadlocks in concurrent systems.
Used in encryption applications to determine the key that can map a message to a message with the same encryption value.
Minimum spanning tree
The minimum spanning tree is a subset of the edges of the graph. It connects all the vertices with the smallest sum of edge weights and does not contain any cycles.
Figure 6 is an animation showing the process of obtaining the minimum spanning tree.
algorithm
Prim algorithm, Kruskal algorithm
application
Used to construct a broadcast tree in a computer network.
Used for graph-based cluster analysis.
Used for image segmentation.
Used for the regionalization of social geographic areas, dividing the area into adjacent areas.
Strongly connected components
If every vertex in the graph can be reached from every other vertex, then the graph is strongly connected.
Figure 7 shows an example graph with three strongly connected components, with vertices in red, green, and yellow.
algorithm
Kosaraju's algorithm, Tarjan's strongly connected component algorithm
application
It is used to calculate Dulmage-Mendelsohn decomposition, which is a classification of complete bipartite graphs.
In social networks, it is used to find a group of close people and make suggestions based on common interests.
Topological sort
The topological sort of a graph is to sort its vertices linearly, so for each directed edge (u, v) in the sort, the vertex u is before v.
Figure 8 shows an example of topological sorting of vertices (1, 2, 3, 5, 4, 6, 7, 8). You can see that vertex 5 should be after vertices 2 and 3. Similarly, vertex 6 should be after vertex 4 and 5.
algorithm
Kahn algorithm
Algorithm based on depth first search
application
Used for instruction scheduling.
Used for data serialization.
Used to determine the order of compilation tasks executed in the makefile.
Used to resolve symbol dependencies in the linker.
Graph coloring
The graph coloring assigns colors to the elements of the graph under certain conditions. Vertex shading is the most commonly used graphics shading technique. In vertex coloring, we try to color the vertices of the graph with k colors, and no two adjacent vertices should have the same color. Other coloring techniques include edge coloring and face coloring.
The color number of a graph is the minimum number of colors required to color the graph.
Figure 9 shows the vertex coloring of an example graph using 4 colors.
algorithm
Use breadth-first search or depth-first search algorithms, greedy coloring
application
Used to make a schedule.
Used to allocate mobile radio frequencies.
Used to simulate and solve games such as Sudoku.
Used to check whether the graph is a bipartite graph.
Used to paint different colors on geographic maps of neighboring countries or states.
Maximum Flow (Maximum Flow)
We can model a graph as a flow network with edge weights as flow capacity. In the maximum flow problem, we must find a flow path that can obtain the maximum possible flow.
Figure 10 shows an example of an animation that determines the maximum and final flow values of the network.
algorithm
Ford-Fulkerson algorithm, Edmonds-Karp algorithm, Dinic algorithm
application
It is used for airline dispatching and arranging flight crew.
Used for image segmentation to find the background and foreground in the image.
Used to eliminate teams that cannot win enough games to catch up with the current division.
match
Matching in a graph refers to a set of edges that do not have a common vertex (that is, no two edges share a common vertex). If a match contains the maximum number of edges matched by as many vertices as possible, then this match is called a maximum match.
Figure 11 shows an animation of obtaining a complete match of a bipartite graph, which has two sets of vertices, represented by orange and blue.
algorithm
Hopcroft-Karp algorithm, Hungarian algorithm, Blossom algorithm
application
Used to match the bride and groom (marital stability issues).
Used to determine the vertex cover.
Used in transportation theory to solve travel resource allocation and optimization problems.
At last
I hope this article's brief introduction to graphics algorithms will be helpful to you