BFS与DFS
两种图,从外文摘抄的,后续有时间了反应一下
Representations of graphs
Graph consists of two components:node(vertice) & edge
Differing from the definition of edfe,Graph can be divided into directed graph,Undirected graph.And different connection also heve other different property.
Here are two standard ways to represent a graph $G=(V,E)$:
Adjacency-list
Using list to express edge between the vertex element and followed elements,the C++ codes as follows:
1 | // A simple representation of graph using STL |
This way is used to represent a directed graphs,and is also a compact way to represent spare graphs.
Adjacency-matrix
We using adj[i][j] to express the edge form i to j;
Directed graph need to assign adj[i][j]=1 and adj[j][i]=1
Undirected graph only need to assign adj[i][j]=;
here are the C++ codes:
1 |
|
when we need to be able to tell quickly if there is an edge connecting two given vertices.