1. Representation of a graph
1.1 Adjacency matrix
If there is an edge between two nodes, it is marked as the edge weight in the matrix; if there is no edge, it is marked as 0.
1.2 Incidence matrix
Each node corresponds to an edge set:
1.3 Adjacency list
In order to save space, each node only needs to save a list of nodes connected to it:
Space complexity:
- Directed graph: O(n+e)
- Undirected graph: O(n+2e), because the edge is saved twice
2. Breadth First Search
3. Depth First Search
The depth-first search algorithm is an algorithm used to traverse or search trees or graphs. Traverse the nodes of the tree along the depth of the tree and search the branches of the tree as deep as possible. When the edges of node v have been explored, the search will backtrack to the starting node of the edge where node v is found. This process continues until all nodes reachable from the source node have been found. If there are still undiscovered nodes, select one of them as the source node and repeat the above process. The whole process is repeated until all nodes are visited. It is a blind search.
4. Topological Sort
4.1 Problem
4.2 Strategy: output zero-in-degree nodes sequentially
5. Minimum spanning tree
5.1 Definition
5.2 Prim's Algorithm
example:
6. The shortest path
6.1 Problem description
Each time, the smallest point in the working set is selected, and then the values of all points connected to this point are updated again.
6.2 Examples