Graph Theory¶
Resources¶
- Graph Editor: Create and visualize graphs.
- 【题单】图论算法(DFS/BFS/拓扑排序/最短路/最小生成树/二分图/基环树/欧拉路径)
Concepts¶
- Graph
- Vertex (Node)
- Edge
- Weight
Types¶
- Undirected graph: A graph in which edges have no direction.
- Directed graph: A graph in which edges have direction.
flowchart LR
A((1)) --> B((2)) & C((3))
C --> B & D((4))
D --> C
- Cyclic graph: A graph in which there is a cycle. A cycle is a path of edges that starts and ends at the same vertex, e.g.,
1 -> 3 -> 4 -> 1
.
flowchart LR
A((1)) --> B((2)) & C((3))
B --> C
C --> D((4))
D --> A
- Acyclic graph: A graph in which there is no cycle.
- Directed Acyclic Graph (DAG): A directed graph with no cycles.
- Topological sort
- Weighted graph: A graph in which edges have weights.
- Connected Graph: A graph in which there is a path between every pair of vertices.
- Disconnected Graph: A graph in which there is no path between some pairs of vertices.
- Eulerian path: A path that visits every edge exactly once, e.g.,
5 -> 4 -> 1 -> 2 -> 3
.
Representation¶
- Adjacency Matrix
- Adjacency List
flowchart LR
1((1))
2((2))
3((3))
4((4))
1 --> 3
1 --> 2
3 --> 4
2 --> 3
Adjacency Matrix
Node 1 | Node 2 | Node 3 | Node 4 | |
---|---|---|---|---|
Node 1 | 0 | 1 | 1 | 0 |
Node 2 | 0 | 0 | 1 | 0 |
Node 3 | 0 | 0 | 0 | 1 |
Node 4 | 0 | 0 | 0 | 0 |
Adjacency List
classDiagram
direction LR
class 1{2, 3}
class 2{3}
class 3{4}
class 4{-}
1 -- 2
2 -- 3
3 -- 4
Degree¶
- Degree: Number of edges connected to a node
- In-degree: Number of edges coming into a node
- Out-degree: Number of edges going out of a node
flowchart LR
1((1))
2((2))
3((3))
4((4))
1 --> 3
1 --> 2
3 --> 4
2 --> 3
- In-degree of Node 1: 0
- Out-degree of Node 1: 2
- In-degree of Node 2: 1
- Out-degree of Node 2: 1
# List
in_degree = [0, 1, 2, 1]
out_degree = [2, 1, 1, 0]
# Dict
in_degree = {1: 0, 2: 1, 3: 2, 4: 1}
out_degree = {1: 2, 2: 1, 3: 1, 4: 0}
Graph - Bellman-Ford Algorithm¶
- The Bellman-Ford algorithm is used to find the shortest path from a source vertex to all other vertices in a weighted graph.
- It is slower than Dijkstra's algorithm, but it is more versatile, as it is able to handle graphs with negative edge weights.
- Time Complexity: O(V x E), where V is the number of vertices and E is the number of edges in the graph.
- Space Complexity: O(V), where V is the number of vertices in the graph.