mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 04:16:29 +08:00
Compare commits
3 commits
cae8ae5c25
...
d4cc9913ed
Author | SHA1 | Date | |
---|---|---|---|
|
d4cc9913ed | ||
|
4db5c19011 | ||
|
235f4e9fa4 |
2 changed files with 20 additions and 20 deletions
|
@ -2,30 +2,30 @@
|
|||
|
||||
### Key review
|
||||
|
||||
- A graph consists of vertices and edges and can be represented as a set comprising a group of vertices and a group of edges.
|
||||
- Compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) have a higher degree of freedom and are therefore more complex.
|
||||
- The edges of a directed graph have directionality, any vertex in a connected graph is reachable, and each edge in a weighted graph contains a weight variable.
|
||||
- Adjacency matrices use matrices to represent graphs, with each row (column) representing a vertex and matrix elements representing edges, using $1$ or $0$ to indicate the presence or absence of an edge between two vertices. Adjacency matrices are highly efficient for add, delete, find, and modify operations, but they consume more space.
|
||||
- Adjacency lists use multiple linked lists to represent graphs, with the $i^{th}$ list corresponding to vertex $i$, containing all its adjacent vertices. Adjacency lists save more space compared to adjacency matrices, but since it is necessary to traverse the list to find edges, their time efficiency is lower.
|
||||
- When the linked lists in the adjacency list are too long, they can be converted into red-black trees or hash tables to improve query efficiency.
|
||||
- From the perspective of algorithmic thinking, adjacency matrices embody the principle of "space for time," while adjacency lists embody "time for space."
|
||||
- Graphs can be used to model various real systems, such as social networks, subway routes, etc.
|
||||
- A graph is made up of vertices and edges. It can be described as a set of vertices and a set of edges.
|
||||
- Compared to linear relationships (like linked lists) and hierarchical relationships (like trees), network relationships (graphs) offer greater flexibility, making them more complex.
|
||||
- In a directed graph, edges have directions. In a connected graph, any vertex can be reached from any other vertex. In a weighted graph, each edge has an associated weight variable.
|
||||
- An adjacency matrix is a way to represent a graph using matrix (2D array). The rows and columns represent the vertices. The matrix element value indicates whether there is an edge between two vertices, using $1$ for an edge or $0$ for no edge. Adjacency matrices are highly efficient for operations like adding, deleting, or checking edges, but they require more space.
|
||||
- An adjacency list is another common way to represent a graph using a collection of linked lists. Each vertex in the graph has a list that contains all its adjacent vertices. The $i^{th}$ list represents vertex $i$. Adjacency lists use less space compared to adjacency matrices. However, since it requires traversing the list to find edges, the time efficiency is lower.
|
||||
- When the linked lists in an adjacency list are long enough, they can be converted into red-black trees or hash tables to improve lookup efficiency.
|
||||
- From the perspective of algorithmic design, an adjacency matrix reflects the concept of "trading space for time", whereas an adjacency list reflects "trading time for space".
|
||||
- Graphs can be used to model various real-world systems, such as social networks, subway routes.
|
||||
- A tree is a special case of a graph, and tree traversal is also a special case of graph traversal.
|
||||
- Breadth-first traversal of a graph is a search method that expands layer by layer from near to far, usually implemented with a queue.
|
||||
- Depth-first traversal of a graph is a search method that prefers to go as deep as possible and backtracks when no further paths are available, often based on recursion.
|
||||
- Breadth-first traversal of a graph is a search method that expands layer by layer from near to far, typically using a queue.
|
||||
- Depth-first traversal of a graph is a search method that prioritizes reaching the end before backtracking when no further path is available. It is often implemented using recursion.
|
||||
|
||||
### Q & A
|
||||
|
||||
**Q**: Is a path defined as a sequence of vertices or a sequence of edges?
|
||||
|
||||
Definitions vary between different language versions on Wikipedia: the English version defines a path as "a sequence of edges," while the Chinese version defines it as "a sequence of vertices." Here is the original text from the English version: In graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices.
|
||||
In graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices.
|
||||
|
||||
In this document, a path is considered a sequence of edges, rather than a sequence of vertices. This is because there might be multiple edges connecting two vertices, in which case each edge corresponds to a path.
|
||||
|
||||
**Q**: In a disconnected graph, are there points that cannot be traversed to?
|
||||
**Q**: In a disconnected graph, are there points that cannot be traversed?
|
||||
|
||||
In a disconnected graph, starting from a certain vertex, there is at least one vertex that cannot be reached. Traversing a disconnected graph requires setting multiple starting points to traverse all connected components of the graph.
|
||||
In a disconnected graph, there is at least one vertex that cannot be reached from a specific point. To traverse a disconnected graph, you need to set multiple starting points to traverse all the connected components of the graph.
|
||||
|
||||
**Q**: In an adjacency list, does the order of "all vertices connected to that vertex" matter?
|
||||
|
||||
It can be in any order. However, in practical applications, it might be necessary to sort according to certain rules, such as the order in which vertices are added, or the order of vertex values, etc., to facilitate the quick search for vertices with certain extremal values.
|
||||
It can be in any order. However, in real-world applications, it might be necessary to sort them according to certain rules, such as the order in which vertices are added, or the order of vertex values. This can help find vertices quickly with certain extreme values.
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
# Summary
|
||||
|
||||
### Key review
|
||||
### Key Highlights
|
||||
|
||||
- A binary tree is a non-linear data structure that reflects the "divide and conquer" logic of splitting one into two. Each binary tree node contains a value and two pointers, which point to its left and right child nodes, respectively.
|
||||
- For a node in a binary tree, the tree formed by its left (right) child node and all nodes under it is called the node's left (right) subtree.
|
||||
- Related terminology of binary trees includes root node, leaf node, level, degree, edge, height, and depth, among others.
|
||||
- For a node in a binary tree, its left (right) child node and the tree formed below it is called the node's left (right) subtree.
|
||||
- Terms related to binary trees include root node, leaf node, level, degree, edge, height, and depth.
|
||||
- The operations of initializing a binary tree, inserting nodes, and removing nodes are similar to those of linked list operations.
|
||||
- Common types of binary trees include perfect binary trees, complete binary trees, full binary trees, and balanced binary trees. The perfect binary tree represents the ideal state, while the linked list is the worst state after degradation.
|
||||
- A binary tree can be represented using an array by arranging the node values and empty slots in a level-order traversal sequence and implementing pointers based on the index mapping relationship between parent nodes and child nodes.
|
||||
|
@ -12,7 +12,7 @@
|
|||
- Pre-order, in-order, and post-order traversals are all depth-first search methods, reflecting the traversal manner of "going to the end first, then backtracking to continue." They are usually implemented using recursion.
|
||||
- A binary search tree is an efficient data structure for element searching, with the time complexity of search, insert, and remove operations all being $O(\log n)$. When a binary search tree degrades into a linked list, these time complexities deteriorate to $O(n)$.
|
||||
- An AVL tree, also known as a balanced binary search tree, ensures that the tree remains balanced after continuous node insertions and removals through rotation operations.
|
||||
- Rotation operations in an AVL tree include right rotation, left rotation, right-then-left rotation, and left-then-right rotation. After inserting or removing nodes, an AVL tree performs rotation operations from bottom to top to rebalance the tree.
|
||||
- Rotation operations in an AVL tree include right rotation, left rotation, right-left rotation, and left-right rotation. After inserting or removing nodes, the AVL tree performs rotation operations from bottom to top to rebalance the tree.
|
||||
|
||||
### Q & A
|
||||
|
||||
|
@ -20,7 +20,7 @@
|
|||
|
||||
Yes, because height and depth are typically defined as "the number of edges passed."
|
||||
|
||||
**Q**: The insertion and removal in a binary tree are generally completed by a set of operations. What does "a set of operations" refer to here? Can it be understood as the release of resources of the child nodes?
|
||||
**Q**: The insertion and removal in a binary tree are generally accomplished by a set of operations. What does "a set of operations" refer to here? Can it be understood as the release of resources of the child nodes?
|
||||
|
||||
Taking the binary search tree as an example, the operation of removing a node needs to be handled in three different scenarios, each requiring multiple steps of node operations.
|
||||
|
||||
|
@ -34,7 +34,7 @@ We need to view this problem from a recursive perspective. The `right_rotate(roo
|
|||
|
||||
**Q**: In C++, functions are divided into `private` and `public` sections. What considerations are there for this? Why are the `height()` function and the `updateHeight()` function placed in `public` and `private`, respectively?
|
||||
|
||||
It depends on the scope of the method's use. If a method is only used within the class, then it is designed to be `private`. For example, it makes no sense for users to call `updateHeight()` on their own, as it is just a step in the insertion or removal operations. However, `height()` is for accessing node height, similar to `vector.size()`, thus it is set to `public` for use.
|
||||
It depends on the scope of the function's use. If a function is only used within the class, then it is designed to be `private`. For example, it makes no sense for users to call `updateHeight()` on their own, as it is just a step in the insertion or removal operations. However, `height()` is for accessing node height, similar to `vector.size()`, thus it is set to `public` for use.
|
||||
|
||||
**Q**: How do you build a binary search tree from a set of input data? Is the choice of root node very important?
|
||||
|
||||
|
|
Loading…
Reference in a new issue