This commit is contained in:
krahets
2025-12-31 19:38:45 +08:00
parent fdc032e300
commit f31cbc3f9f
474 changed files with 130810 additions and 103691 deletions
+147 -147
View File
@@ -58,8 +58,8 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i%7CJetBrains+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Lato";--md-code-font:"JetBrains Mono"}</style>
@@ -371,7 +371,7 @@
<span class="md-ellipsis">
Before starting
Before Starting
@@ -388,7 +388,7 @@
<span class="md-nav__icon md-icon"></span>
Before starting
Before Starting
</label>
@@ -487,7 +487,7 @@
<span class="md-ellipsis">
0.1 About this book
0.1 About This Book
@@ -515,7 +515,7 @@
<span class="md-ellipsis">
0.2 How to read
0.2 How to Use This Book
@@ -604,7 +604,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
@@ -626,7 +626,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
</label>
@@ -648,7 +648,7 @@
<span class="md-ellipsis">
1.1 Algorithms are everywhere
1.1 Algorithms Are Everywhere
@@ -676,7 +676,7 @@
<span class="md-ellipsis">
1.2 What is an algorithm
1.2 What Is an Algorithm
@@ -769,7 +769,7 @@
<span class="md-ellipsis">
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
@@ -791,7 +791,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
</label>
@@ -813,7 +813,7 @@
<span class="md-ellipsis">
2.1 Algorithm efficiency assessment
2.1 Algorithm Efficiency Evaluation
@@ -841,7 +841,7 @@
<span class="md-ellipsis">
2.2 Iteration and recursion
2.2 Iteration and Recursion
@@ -869,7 +869,7 @@
<span class="md-ellipsis">
2.3 Time complexity
2.3 Time Complexity
@@ -897,7 +897,7 @@
<span class="md-ellipsis">
2.4 Space complexity
2.4 Space Complexity
@@ -990,7 +990,7 @@
<span class="md-ellipsis">
Chapter 3. Data structures
Chapter 3. Data Structures
@@ -1012,7 +1012,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 3. Data structures
Chapter 3. Data Structures
</label>
@@ -1034,7 +1034,7 @@
<span class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
@@ -1062,7 +1062,7 @@
<span class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
@@ -1090,7 +1090,7 @@
<span class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
@@ -1118,7 +1118,7 @@
<span class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
@@ -1211,7 +1211,7 @@
<span class="md-ellipsis">
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
@@ -1233,7 +1233,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
</label>
@@ -1283,7 +1283,7 @@
<span class="md-ellipsis">
4.2 Linked list
4.2 Linked List
@@ -1339,7 +1339,7 @@
<span class="md-ellipsis">
4.4 Memory and cache *
4.4 Memory and Cache *
@@ -1430,7 +1430,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
@@ -1452,7 +1452,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
</label>
@@ -1530,7 +1530,7 @@
<span class="md-ellipsis">
5.3 Double-ended queue
5.3 Double-Ended Queue
@@ -1621,7 +1621,7 @@
<span class="md-ellipsis">
Chapter 6. Hash table
Chapter 6. Hashing
@@ -1643,7 +1643,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hash table
Chapter 6. Hashing
</label>
@@ -1665,7 +1665,7 @@
<span class="md-ellipsis">
6.1 Hash table
6.1 Hash Table
@@ -1693,7 +1693,7 @@
<span class="md-ellipsis">
6.2 Hash collision
6.2 Hash Collision
@@ -1721,7 +1721,7 @@
<span class="md-ellipsis">
6.3 Hash algorithm
6.3 Hash Algorithm
@@ -1860,7 +1860,7 @@
<span class="md-ellipsis">
7.1 Binary tree
7.1 Binary Tree
@@ -1888,7 +1888,7 @@
<span class="md-ellipsis">
7.2 Binary tree traversal
7.2 Binary Tree Traversal
@@ -1916,7 +1916,7 @@
<span class="md-ellipsis">
7.3 Array Representation of tree
7.3 Array Representation of Tree
@@ -1944,7 +1944,7 @@
<span class="md-ellipsis">
7.4 Binary Search tree
7.4 Binary Search Tree
@@ -1972,7 +1972,7 @@
<span class="md-ellipsis">
7.5 AVL tree *
7.5 AVL Tree *
@@ -2135,7 +2135,7 @@
<span class="md-ellipsis">
8.2 Building a heap
8.2 Building a Heap
@@ -2163,7 +2163,7 @@
<span class="md-ellipsis">
8.3 Top-k problem
8.3 Top-K Problem
@@ -2353,10 +2353,10 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#911-common-types-and-terminologies-of-graphs" class="md-nav__link">
<a href="#911-common-types-and-terminology-of-graphs" class="md-nav__link">
<span class="md-ellipsis">
9.1.1 &nbsp; Common types and terminologies of graphs
9.1.1 &nbsp; Common Types and Terminology of Graphs
</span>
</a>
@@ -2367,19 +2367,19 @@
<a href="#912-representation-of-graphs" class="md-nav__link">
<span class="md-ellipsis">
9.1.2 &nbsp; Representation of graphs
9.1.2 &nbsp; Representation of Graphs
</span>
</a>
<nav class="md-nav" aria-label="9.1.2   Representation of graphs">
<nav class="md-nav" aria-label="9.1.2   Representation of Graphs">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#1-adjacency-matrix" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Adjacency matrix
1. &nbsp; Adjacency Matrix
</span>
</a>
@@ -2390,7 +2390,7 @@
<a href="#2-adjacency-list" class="md-nav__link">
<span class="md-ellipsis">
2. &nbsp; Adjacency list
2. &nbsp; Adjacency List
</span>
</a>
@@ -2406,7 +2406,7 @@
<a href="#913-common-applications-of-graphs" class="md-nav__link">
<span class="md-ellipsis">
9.1.3 &nbsp; Common applications of graphs
9.1.3 &nbsp; Common Applications of Graphs
</span>
</a>
@@ -2436,7 +2436,7 @@
<span class="md-ellipsis">
9.2 Basic graph operations
9.2 Basic Operations on Graphs
@@ -2464,7 +2464,7 @@
<span class="md-ellipsis">
9.3 Graph traversal
9.3 Graph Traversal
@@ -2603,7 +2603,7 @@
<span class="md-ellipsis">
10.1 Binary search
10.1 Binary Search
@@ -2631,7 +2631,7 @@
<span class="md-ellipsis">
10.2 Binary search insertion
10.2 Binary Search Insertion
@@ -2659,7 +2659,7 @@
<span class="md-ellipsis">
10.3 Binary search boundaries
10.3 Binary Search Edge Cases
@@ -2687,7 +2687,7 @@
<span class="md-ellipsis">
10.4 Hashing optimization strategies
10.4 Hash Optimization Strategy
@@ -2715,7 +2715,7 @@
<span class="md-ellipsis">
10.5 Search algorithms revisited
10.5 Search Algorithms Revisited
@@ -2864,7 +2864,7 @@
<span class="md-ellipsis">
11.1 Sorting algorithms
11.1 Sorting Algorithms
@@ -2892,7 +2892,7 @@
<span class="md-ellipsis">
11.2 Selection sort
11.2 Selection Sort
@@ -2920,7 +2920,7 @@
<span class="md-ellipsis">
11.3 Bubble sort
11.3 Bubble Sort
@@ -2948,7 +2948,7 @@
<span class="md-ellipsis">
11.4 Insertion sort
11.4 Insertion Sort
@@ -2976,7 +2976,7 @@
<span class="md-ellipsis">
11.5 Quick sort
11.5 Quick Sort
@@ -3004,7 +3004,7 @@
<span class="md-ellipsis">
11.6 Merge sort
11.6 Merge Sort
@@ -3032,7 +3032,7 @@
<span class="md-ellipsis">
11.7 Heap sort
11.7 Heap Sort
@@ -3060,7 +3060,7 @@
<span class="md-ellipsis">
11.8 Bucket sort
11.8 Bucket Sort
@@ -3088,7 +3088,7 @@
<span class="md-ellipsis">
11.9 Counting sort
11.9 Counting Sort
@@ -3116,7 +3116,7 @@
<span class="md-ellipsis">
11.10 Radix sort
11.10 Radix Sort
@@ -3209,7 +3209,7 @@
<span class="md-ellipsis">
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
@@ -3231,7 +3231,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
</label>
@@ -3253,7 +3253,7 @@
<span class="md-ellipsis">
12.1 Divide and conquer algorithms
12.1 Divide and Conquer Algorithms
@@ -3281,7 +3281,7 @@
<span class="md-ellipsis">
12.2 Divide and conquer search strategy
12.2 Divide and Conquer Search Strategy
@@ -3309,7 +3309,7 @@
<span class="md-ellipsis">
12.3 Building binary tree problem
12.3 Building a Binary Tree Problem
@@ -3337,7 +3337,7 @@
<span class="md-ellipsis">
12.4 Tower of Hanoi Problem
12.4 Hanoi Tower Problem
@@ -3474,7 +3474,7 @@
<span class="md-ellipsis">
13.1 Backtracking algorithms
13.1 Backtracking Algorithm
@@ -3502,7 +3502,7 @@
<span class="md-ellipsis">
13.2 Permutation problem
13.2 Permutations Problem
@@ -3530,7 +3530,7 @@
<span class="md-ellipsis">
13.3 Subset sum problem
13.3 Subset-Sum Problem
@@ -3558,7 +3558,7 @@
<span class="md-ellipsis">
13.4 n queens problem
13.4 N-Queens Problem
@@ -3655,7 +3655,7 @@
<span class="md-ellipsis">
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
@@ -3677,7 +3677,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
</label>
@@ -3699,7 +3699,7 @@
<span class="md-ellipsis">
14.1 Introduction to dynamic programming
14.1 Introduction to Dynamic Programming
@@ -3727,7 +3727,7 @@
<span class="md-ellipsis">
14.2 Characteristics of DP problems
14.2 Characteristics of Dynamic Programming Problems
@@ -3755,7 +3755,7 @@
<span class="md-ellipsis">
14.3 DP problem-solving approach
14.3 Dynamic Programming Problem-Solving Approach
@@ -3783,7 +3783,7 @@
<span class="md-ellipsis">
14.4 0-1 Knapsack problem
14.4 0-1 Knapsack Problem
@@ -3811,7 +3811,7 @@
<span class="md-ellipsis">
14.5 Unbounded knapsack problem
14.5 Unbounded Knapsack Problem
@@ -3839,7 +3839,7 @@
<span class="md-ellipsis">
14.6 Edit distance problem
14.6 Edit Distance Problem
@@ -3976,7 +3976,7 @@
<span class="md-ellipsis">
15.1 Greedy algorithms
15.1 Greedy Algorithm
@@ -4004,7 +4004,7 @@
<span class="md-ellipsis">
15.2 Fractional knapsack problem
15.2 Fractional Knapsack Problem
@@ -4032,7 +4032,7 @@
<span class="md-ellipsis">
15.3 Maximum capacity problem
15.3 Maximum Capacity Problem
@@ -4060,7 +4060,7 @@
<span class="md-ellipsis">
15.4 Maximum product cutting problem
15.4 Maximum Product Cutting Problem
@@ -4193,7 +4193,7 @@
<span class="md-ellipsis">
16.1 Installation
16.1 Programming Environment Installation
@@ -4221,7 +4221,7 @@
<span class="md-ellipsis">
16.2 Contributing
16.2 Contributing Together
@@ -4249,7 +4249,7 @@
<span class="md-ellipsis">
16.3 Terminology
16.3 Terminology Table
@@ -4363,10 +4363,10 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#911-common-types-and-terminologies-of-graphs" class="md-nav__link">
<a href="#911-common-types-and-terminology-of-graphs" class="md-nav__link">
<span class="md-ellipsis">
9.1.1 &nbsp; Common types and terminologies of graphs
9.1.1 &nbsp; Common Types and Terminology of Graphs
</span>
</a>
@@ -4377,19 +4377,19 @@
<a href="#912-representation-of-graphs" class="md-nav__link">
<span class="md-ellipsis">
9.1.2 &nbsp; Representation of graphs
9.1.2 &nbsp; Representation of Graphs
</span>
</a>
<nav class="md-nav" aria-label="9.1.2   Representation of graphs">
<nav class="md-nav" aria-label="9.1.2   Representation of Graphs">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#1-adjacency-matrix" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Adjacency matrix
1. &nbsp; Adjacency Matrix
</span>
</a>
@@ -4400,7 +4400,7 @@
<a href="#2-adjacency-list" class="md-nav__link">
<span class="md-ellipsis">
2. &nbsp; Adjacency list
2. &nbsp; Adjacency List
</span>
</a>
@@ -4416,7 +4416,7 @@
<a href="#913-common-applications-of-graphs" class="md-nav__link">
<span class="md-ellipsis">
9.1.3 &nbsp; Common applications of graphs
9.1.3 &nbsp; Common Applications of Graphs
</span>
</a>
@@ -4461,7 +4461,7 @@
<!-- Page content -->
<h1 id="91-graph">9.1 &nbsp; Graph<a class="headerlink" href="#91-graph" title="Permanent link">&para;</a></h1>
<p>A <u>graph</u> is a type of nonlinear data structure, consisting of <u>vertices</u> and <u>edges</u>. A graph <span class="arithmatex">\(G\)</span> can be abstractly represented as a collection of a set of vertices <span class="arithmatex">\(V\)</span> and a set of edges <span class="arithmatex">\(E\)</span>. The following example shows a graph containing 5 vertices and 7 edges.</p>
<p>A <u>graph</u> is a nonlinear data structure consisting of <u>vertices</u> and <u>edges</u>. We can abstractly represent a graph <span class="arithmatex">\(G\)</span> as a set of vertices <span class="arithmatex">\(V\)</span> and a set of edges <span class="arithmatex">\(E\)</span>. The following example shows a graph containing 5 vertices and 7 edges.</p>
<div class="arithmatex">\[
\begin{aligned}
V &amp; = \{ 1, 2, 3, 4, 5 \} \newline
@@ -4469,61 +4469,61 @@ E &amp; = \{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \} \newline
G &amp; = \{ V, E \} \newline
\end{aligned}
\]</div>
<p>If vertices are viewed as nodes and edges as references (pointers) connecting the nodes, graphs can be seen as a data structure that extends from linked lists. As shown in Figure 9-1, <strong>compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) are more complex due to their higher degree of freedom</strong>.</p>
<p><a class="glightbox" href="../graph.assets/linkedlist_tree_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Relationship between linked lists, trees, and graphs" class="animation-figure" src="../graph.assets/linkedlist_tree_graph.png" /></a></p>
<p align="center"> Figure 9-1 &nbsp; Relationship between linked lists, trees, and graphs </p>
<p>If we view vertices as nodes and edges as references (pointers) connecting the nodes, we can see graphs as a data structure extended from linked lists. As shown in Figure 9-1, <strong>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</strong>.</p>
<p><a class="glightbox" href="../graph.assets/linkedlist_tree_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Relationships among linked lists, trees, and graphs" class="animation-figure" src="../graph.assets/linkedlist_tree_graph.png" /></a></p>
<p align="center"> Figure 9-1 &nbsp; Relationships among linked lists, trees, and graphs </p>
<h2 id="911-common-types-and-terminologies-of-graphs">9.1.1 &nbsp; Common types and terminologies of graphs<a class="headerlink" href="#911-common-types-and-terminologies-of-graphs" title="Permanent link">&para;</a></h2>
<p>Graphs can be divided into <u>undirected graphs</u> and <u>directed graphs</u> depending on whether edges have direction, as shown in Figure 9-2.</p>
<h2 id="911-common-types-and-terminology-of-graphs">9.1.1 &nbsp; Common Types and Terminology of Graphs<a class="headerlink" href="#911-common-types-and-terminology-of-graphs" title="Permanent link">&para;</a></h2>
<p>Graphs can be divided into <u>undirected graphs</u> and <u>directed graphs</u> based on whether edges have direction, as shown in Figure 9-2.</p>
<ul>
<li>In undirected graphs, edges represent a "bidirectional" connection between two vertices, for example, the "friends" in Facebook.</li>
<li>In directed graphs, edges have directionality, that is, the edges <span class="arithmatex">\(A \rightarrow B\)</span> and <span class="arithmatex">\(A \leftarrow B\)</span> are independent of each other. For example, the "follow" and "followed" relationship on Instagram or TikTok.</li>
<li>In undirected graphs, edges represent a "bidirectional" connection between two vertices, such as the "friend relationship" on WeChat or QQ.</li>
<li>In directed graphs, edges have directionality, meaning edges <span class="arithmatex">\(A \rightarrow B\)</span> and <span class="arithmatex">\(A \leftarrow B\)</span> are independent of each other, such as the "follow" and "be followed" relationships on Weibo or TikTok.</li>
</ul>
<p><a class="glightbox" href="../graph.assets/directed_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Directed and undirected graphs" class="animation-figure" src="../graph.assets/directed_graph.png" /></a></p>
<p align="center"> Figure 9-2 &nbsp; Directed and undirected graphs </p>
<p>Depending on whether all vertices are connected, graphs can be divided into <u>connected graphs</u> and <u>disconnected graphs</u>, as shown in Figure 9-3.</p>
<p>Graphs can be divided into <u>connected graphs</u> and <u>disconnected graphs</u> based on whether all vertices are connected, as shown in Figure 9-3.</p>
<ul>
<li>For connected graphs, it is possible to reach any other vertex starting from an arbitrary vertex.</li>
<li>For disconnected graphs, there is at least one vertex that cannot be reached from an arbitrary starting vertex.</li>
<li>For connected graphs, starting from any vertex, all other vertices can be reached.</li>
<li>For disconnected graphs, starting from a certain vertex, at least one vertex cannot be reached.</li>
</ul>
<p><a class="glightbox" href="../graph.assets/connected_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Connected and disconnected graphs" class="animation-figure" src="../graph.assets/connected_graph.png" /></a></p>
<p align="center"> Figure 9-3 &nbsp; Connected and disconnected graphs </p>
<p>We can also add a weight variable to edges, resulting in <u>weighted graphs</u> as shown in Figure 9-4. For example, in Instagram, the system sorts your follower and following list by the level of interaction between you and other users (likes, views, comments, etc.). Such an interaction network can be represented by a weighted graph.</p>
<p>We can also add a "weight" variable to edges, resulting in <u>weighted graphs</u> as shown in Figure 9-4. For example, in mobile games like "Honor of Kings", the system calculates the "intimacy" between players based on their shared game time, and such intimacy networks can be represented using weighted graphs.</p>
<p><a class="glightbox" href="../graph.assets/weighted_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Weighted and unweighted graphs" class="animation-figure" src="../graph.assets/weighted_graph.png" /></a></p>
<p align="center"> Figure 9-4 &nbsp; Weighted and unweighted graphs </p>
<p>Graph data structures include the following commonly used terms.</p>
<ul>
<li><u>Adjacency</u>: When there is an edge connecting two vertices, these two vertices are said to be "adjacent". In Figure 9-4, the adjacent vertices of vertex 1 are vertices 2, 3, and 5.</li>
<li><u>Path</u>: The sequence of edges passed from vertex A to vertex B is called a path from A to B. In Figure 9-4, the edge sequence 1-5-2-4 is a path from vertex 1 to vertex 4.</li>
<li><u>Degree</u>: The number of edges a vertex has. For directed graphs, <u>in-degree</u> refers to how many edges point to the vertex, and <u>out-degree</u> refers to how many edges point out from the vertex.</li>
<li><u>Adjacency</u>: When two vertices are connected by an edge, these two vertices are said to be "adjacent". In Figure 9-4, the adjacent vertices of vertex 1 are vertices 2, 3, and 5.</li>
<li><u>Path</u>: The sequence of edges from vertex A to vertex B is called a "path" from A to B. In Figure 9-4, the edge sequence 1-5-2-4 is a path from vertex 1 to vertex 4.</li>
<li><u>Degree</u>: The number of edges a vertex has. For directed graphs, <u>in-degree</u> indicates how many edges point to the vertex, and <u>out-degree</u> indicates how many edges point out from the vertex.</li>
</ul>
<h2 id="912-representation-of-graphs">9.1.2 &nbsp; Representation of graphs<a class="headerlink" href="#912-representation-of-graphs" title="Permanent link">&para;</a></h2>
<p>Common representations of graphs include "adjacency matrix" and "adjacency list". The following examples use undirected graphs.</p>
<h3 id="1-adjacency-matrix">1. &nbsp; Adjacency matrix<a class="headerlink" href="#1-adjacency-matrix" title="Permanent link">&para;</a></h3>
<p>Let the number of vertices in the graph be <span class="arithmatex">\(n\)</span>, the <u>adjacency matrix</u> uses an <span class="arithmatex">\(n \times n\)</span> matrix to represent the graph, where each row (column) represents a vertex, and the matrix elements represent edges, with <span class="arithmatex">\(1\)</span> or <span class="arithmatex">\(0\)</span> indicating whether there is an edge between two vertices.</p>
<p>As shown in Figure 9-5, let the adjacency matrix be <span class="arithmatex">\(M\)</span>, and the list of vertices be <span class="arithmatex">\(V\)</span>, then the matrix element <span class="arithmatex">\(M[i, j] = 1\)</span> indicates there is an edge between vertex <span class="arithmatex">\(V[i]\)</span> and vertex <span class="arithmatex">\(V[j]\)</span>, conversely <span class="arithmatex">\(M[i, j] = 0\)</span> indicates there is no edge between the two vertices.</p>
<p><a class="glightbox" href="../graph.assets/adjacency_matrix.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Representation of a graph with an adjacency matrix" class="animation-figure" src="../graph.assets/adjacency_matrix.png" /></a></p>
<p align="center"> Figure 9-5 &nbsp; Representation of a graph with an adjacency matrix </p>
<h2 id="912-representation-of-graphs">9.1.2 &nbsp; Representation of Graphs<a class="headerlink" href="#912-representation-of-graphs" title="Permanent link">&para;</a></h2>
<p>Common representations of graphs include "adjacency matrices" and "adjacency lists". The following uses undirected graphs as examples.</p>
<h3 id="1-adjacency-matrix">1. &nbsp; Adjacency Matrix<a class="headerlink" href="#1-adjacency-matrix" title="Permanent link">&para;</a></h3>
<p>Given a graph with <span class="arithmatex">\(n\)</span> vertices, an <u>adjacency matrix</u> uses an <span class="arithmatex">\(n \times n\)</span> matrix to represent the graph, where each row (column) represents a vertex, and matrix elements represent edges, using <span class="arithmatex">\(1\)</span> or <span class="arithmatex">\(0\)</span> to indicate whether an edge exists between two vertices.</p>
<p>As shown in Figure 9-5, let the adjacency matrix be <span class="arithmatex">\(M\)</span> and the vertex list be <span class="arithmatex">\(V\)</span>. Then matrix element <span class="arithmatex">\(M[i, j] = 1\)</span> indicates that an edge exists between vertex <span class="arithmatex">\(V[i]\)</span> and vertex <span class="arithmatex">\(V[j]\)</span>, whereas <span class="arithmatex">\(M[i, j] = 0\)</span> indicates no edge between the two vertices.</p>
<p><a class="glightbox" href="../graph.assets/adjacency_matrix.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Adjacency matrix representation of a graph" class="animation-figure" src="../graph.assets/adjacency_matrix.png" /></a></p>
<p align="center"> Figure 9-5 &nbsp; Adjacency matrix representation of a graph </p>
<p>Adjacency matrices have the following characteristics.</p>
<p>Adjacency matrices have the following properties.</p>
<ul>
<li>A vertex cannot be connected to itself, so the elements on the main diagonal of the adjacency matrix are meaningless.</li>
<li>For undirected graphs, edges in both directions are equivalent, thus the adjacency matrix is symmetric with regard to the main diagonal.</li>
<li>By replacing the elements of the adjacency matrix from <span class="arithmatex">\(1\)</span> and <span class="arithmatex">\(0\)</span> to weights, we can represent weighted graphs.</li>
<li>In simple graphs, vertices cannot connect to themselves, so the elements on the main diagonal of the adjacency matrix are meaningless.</li>
<li>For undirected graphs, edges in both directions are equivalent, so the adjacency matrix is symmetric about the main diagonal.</li>
<li>Replacing the elements of the adjacency matrix from <span class="arithmatex">\(1\)</span> and <span class="arithmatex">\(0\)</span> to weights allows representation of weighted graphs.</li>
</ul>
<p>When representing graphs with adjacency matrices, it is possible to directly access matrix elements to obtain edges, resulting in efficient operations of addition, deletion, lookup, and modification, all with a time complexity of <span class="arithmatex">\(O(1)\)</span>. However, the space complexity of the matrix is <span class="arithmatex">\(O(n^2)\)</span>, which consumes more memory.</p>
<h3 id="2-adjacency-list">2. &nbsp; Adjacency list<a class="headerlink" href="#2-adjacency-list" title="Permanent link">&para;</a></h3>
<p>The <u>adjacency list</u> uses <span class="arithmatex">\(n\)</span> linked lists to represent the graph, with each linked list node representing a vertex. The <span class="arithmatex">\(i\)</span>-th linked list corresponds to vertex <span class="arithmatex">\(i\)</span> and contains all adjacent vertices (vertices connected to that vertex). Figure 9-6 shows an example of a graph stored using an adjacency list.</p>
<p><a class="glightbox" href="../graph.assets/adjacency_list.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Representation of a graph with an adjacency list" class="animation-figure" src="../graph.assets/adjacency_list.png" /></a></p>
<p align="center"> Figure 9-6 &nbsp; Representation of a graph with an adjacency list </p>
<p>When using adjacency matrices to represent graphs, we can directly access matrix elements to obtain edges, resulting in highly efficient addition, deletion, lookup, and modification operations, all with a time complexity of <span class="arithmatex">\(O(1)\)</span>. However, the space complexity of the matrix is <span class="arithmatex">\(O(n^2)\)</span>, which consumes significant memory.</p>
<h3 id="2-adjacency-list">2. &nbsp; Adjacency List<a class="headerlink" href="#2-adjacency-list" title="Permanent link">&para;</a></h3>
<p>An <u>adjacency list</u> uses <span class="arithmatex">\(n\)</span> linked lists to represent a graph, with linked list nodes representing vertices. The <span class="arithmatex">\(i\)</span>-th linked list corresponds to vertex <span class="arithmatex">\(i\)</span> and stores all adjacent vertices of that vertex (vertices connected to that vertex). Figure 9-6 shows an example of a graph stored using an adjacency list.</p>
<p><a class="glightbox" href="../graph.assets/adjacency_list.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Adjacency list representation of a graph" class="animation-figure" src="../graph.assets/adjacency_list.png" /></a></p>
<p align="center"> Figure 9-6 &nbsp; Adjacency list representation of a graph </p>
<p>The adjacency list only stores actual edges, and the total number of edges is often much less than <span class="arithmatex">\(n^2\)</span>, making it more space-efficient. However, finding edges in the adjacency list requires traversing the linked list, so its time efficiency is not as good as that of the adjacency matrix.</p>
<p>Observing Figure 9-6, <strong>the structure of the adjacency list is very similar to the "chaining" in hash tables, hence we can use similar methods to optimize efficiency</strong>. For example, when the linked list is long, it can be transformed into an AVL tree or red-black tree, thus optimizing the time efficiency from <span class="arithmatex">\(O(n)\)</span> to <span class="arithmatex">\(O(\log n)\)</span>; the linked list can also be transformed into a hash table, thus reducing the time complexity to <span class="arithmatex">\(O(1)\)</span>.</p>
<h2 id="913-common-applications-of-graphs">9.1.3 &nbsp; Common applications of graphs<a class="headerlink" href="#913-common-applications-of-graphs" title="Permanent link">&para;</a></h2>
<p>As shown in Table 9-1, many real-world systems can be modeled with graphs, and corresponding problems can be reduced to graph computing problems.</p>
<p>Adjacency lists only store edges that actually exist, and the total number of edges is typically much less than <span class="arithmatex">\(n^2\)</span>, making them more space-efficient. However, finding edges in an adjacency list requires traversing the linked list, so its time efficiency is inferior to that of adjacency matrices.</p>
<p>Observing Figure 9-6, <strong>the structure of adjacency lists is very similar to "chaining" in hash tables, so we can adopt similar methods to optimize efficiency</strong>. For example, when linked lists are long, they can be converted to AVL trees or red-black trees, thereby optimizing time efficiency from <span class="arithmatex">\(O(n)\)</span> to <span class="arithmatex">\(O(\log n)\)</span>; linked lists can also be converted to hash tables, thereby reducing time complexity to <span class="arithmatex">\(O(1)\)</span>.</p>
<h2 id="913-common-applications-of-graphs">9.1.3 &nbsp; Common Applications of Graphs<a class="headerlink" href="#913-common-applications-of-graphs" title="Permanent link">&para;</a></h2>
<p>As shown in Table 9-1, many real-world systems can be modeled using graphs, and corresponding problems can be reduced to graph computation problems.</p>
<p align="center"> Table 9-1 &nbsp; Common graphs in real life </p>
<div class="center-table">
@@ -4533,27 +4533,27 @@ G &amp; = \{ V, E \} \newline
<th></th>
<th>Vertices</th>
<th>Edges</th>
<th>Graph Computing Problem</th>
<th>Graph Computation Problem</th>
</tr>
</thead>
<tbody>
<tr>
<td>Social Networks</td>
<td>Social network</td>
<td>Users</td>
<td>Follow / Followed</td>
<td>Potential Following Recommendations</td>
<td>Friend relationships</td>
<td>Potential friend recommendation</td>
</tr>
<tr>
<td>Subway Lines</td>
<td>Subway lines</td>
<td>Stations</td>
<td>Connectivity Between Stations</td>
<td>Shortest Route Recommendations</td>
<td>Connectivity between stations</td>
<td>Shortest route recommendation</td>
</tr>
<tr>
<td>Solar System</td>
<td>Celestial Bodies</td>
<td>Gravitational Forces Between Celestial Bodies</td>
<td>Planetary Orbit Calculations</td>
<td>Solar system</td>
<td>Celestial bodies</td>
<td>Gravitational forces between celestial bodies</td>
<td>Planetary orbit calculation</td>
</tr>
</tbody>
</table>
@@ -4604,7 +4604,7 @@ aria-label="Footer"
<a
href="../graph_operations/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 9.2 Basic graph operations"
aria-label="Next: 9.2 Basic Operations on Graphs"
rel="next"
>
<div class="md-footer__title">
@@ -4612,7 +4612,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
9.2 Basic graph operations
9.2 Basic Operations on Graphs
</div>
</div>
<div class="md-footer__button md-icon">
@@ -4722,13 +4722,13 @@ aria-label="Footer"
<a href="../graph_operations/" class="md-footer__link md-footer__link--next" aria-label="Next: 9.2 Basic graph operations">
<a href="../graph_operations/" class="md-footer__link md-footer__link--next" aria-label="Next: 9.2 Basic Operations on Graphs">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
9.2 Basic graph operations
9.2 Basic Operations on Graphs
</div>
</div>
<div class="md-footer__button md-icon">