Chapter 6 Graph
Definition
- G(V, E)
- G ::= graph
- V ::= vertice
- E ::= edge
- Undirected graph:
- Directed graph:
- Restrictions:
- No self loop
- No multigraph
- Complete graph: has the maximum number of edges
- Undirected Graph:
- Directed Graph:
- Adjacent:
- Adjacent to
- Adjacent from
- Subgraph
- Path form to
- Length of path
- Simple path: No repeated
- Cycle: simple path with
- Connected
- Component of an undirected graph: the maximal connected subgraph
- Tree: connected & acyclic graph
- DAG: directed acyclic graph
- Strongly connected directed graph
- Weakly conncted directed graph (ignore direction)
- Strongly connected component
- Degree(v): number of edges incident to v
- in-degree
- out-degree
Representation
Adjacency Matrix
If G is undirected, adj_mat[][] is symmetric — space-wasting
replace by one-dimension array
即使如此,对于稀疏图 / 散点图,邻接矩阵依然 spece-wasting
Adjacency Lists
Replace each row by a linked list
[Example figure TBD]
The order of nodes in each list does not matter
To calculate the degree of undirected G:
-
Add inverse adjacency lists
同时维护出度表和入度表
-
Multilist representation for adj_mat[ i ][ j ]
Adjacency Multilists
graph[i] -> [node] <- graph[j]
each node represents a edge
- mark: the feature of edge (weight, direction, …)
Topological Sort
[Eg] Choosing Course Problem
- AOV Network: activity on vertice network
- Predecessor
- Immediate Predecessor
- Partial order: both transitive irreflexive ( is impossible)
If a project is feasible, it must be irreflexive
Topo. sort is not unique
Implementation
See Also: 小时候写的 Topo. Sort
void Toposort(Graph G){ |
queue or stack amplify
void Toposort(){ |
void toposort(Graph G){ |