1. The concept of graph
Graph: It is composed of a finite non-empty set of vertices and a set of edges between vertices.
Vertex: A data element in a graph.
Edge (Edge): The logical relationship between vertices. Edges can be directed or undirected, and can also have weights (can represent distance, cost, etc.) Undirected
edges: If the edges between vertices have no direction, then Call this edge an undirected edge
and a directed edge: if the edge from the vertex vi to νj has a direction, this edge is called a directed edge (an undirected edge can be represented by two directed edges)
Undirected graph
The edge between any two vertices in the graph is an undirected edge, which means that from node 1 to node 2, or from node 2 to node 1
directed graph
The edges between any two vertices in the graph are directed. The directions in the graph are directed in one direction. Not all directed graphs are directed in one direction. It is just for convenience. A directed graph can be understood as a path with a direction. direction, only in the direction of the arrow
2. Image storage
Commonly used adjacency matrix and adjacency
list for graph storage are simple and convenient to store and query. The disadvantage is that when the graph encountered is a sparse graph, a lot of space will be wasted
. The adjacency list is more complicated than the adjacency matrix. Its advantages are not obvious
adjacency matrix
The array (i, j) indicates whether it is connected from i to j, 0 means it is not connected, and not 0 means it is connected
undirected graph storage
Convert a directed edge into two directed edges, such as the undirected edge of vertex 1 and vertex 2 into two directed edges from vertex 1 to vertex 2 and vertex 2 to vertex 1
directed graph storage
adjacency list
//顶点
class POS{
public POS(int head) {
this.head = head;
}
public int head;//这个值指向的是边
}
//边
class Edge{
public int v;
public int next;
}
//图的初始化
top =0;//用来记录边的位置
posList =new ArrayList<>();//顶点
edgeList =new ArrayList<>();//边的列表
for(int i = 0;i<=posSize;i++){
posList.add(new POS(-1));//初始化
hadVisted[i] =false;//初始化
}
//添加边邻接表,添加一条从u到v的边
public void Add_Edge(int u, int v) {
// 1 -> 4->3->2
Edge edge =new Edge();
edge.v =v;
edge.next =posList.get(u).head;
posList.get(u).head =top;
edgeList.add(edge);
top++;
}
3. Image search
deep search
Deep search is a recursion 1, traverse from vertex 1, traverse to vertex 2, then traverse 2 from vertex 2, traverse from vertex 2 to vertex 5, and traverse from vertex 5 to vertex 8
3. To vertex 8, there is no path backtracking to vertex 5, then backtracking to vertex 2, traversing vertex 6, traversing from vertex 6 to vertex 7 4, vertex 8 has been traversed, backtracking to 6, then backtracking to 2, then backtracking to 1 5 , traverse vertices 3, 4
//深搜
public void dfsVist(int u){
for(int i = posList.get(u).head;i!=-1;i=edgeList.get(i).next){
Edge edge = edgeList.get(i);
if(!hadVisted[edge.v]){
System.out.println("访问节点:"+edge.v);
hadVisted[edge.v] =true;
vist(edge.v);
}
}
}
wide search
Guangsou needs a queue to assist in adding 2, 3, and 4 connected to 1 to the queue from 1, and at the same time, 1 is dequeued to take out vertex 2 from the beginning of the queue, and 5 and 6 connected to 2 are added to the queue, and 2 is dequeued to take out 3 , all those connected to 3 have been visited, 4, 3, 4 are taken out, 7 is entered into the queue and 5 is taken, 8 connected to 5 is added to the queue, 5 is dequeued, and 6, 7, 8 are taken out, because they have all been visited , when the list is empty, the traversal ends
public void bfsVist(int u){
Queue queue = new LinkedList();
queue.add(u);//加入队列
System.out.println("访问节点="+u);
while (!queue.isEmpty()){//直至列表为空
Integer p = (Integer) queue.poll();//取出列表里元素
for(int i = posList.get(p).head;i!=-1;i=edgeList.get(i).next){
Edge edge = edgeList.get(i);
if(!hadVisted[edge.v]){
hadVisted[edge.v] =true;
System.out.println("访问节点="+edge.v);
queue.add((Integer)edge.v);
}
}
}
}
- END -
About Qi Wu Troupe
Qi Wu Troupe is the largest front-end team of 360 Group, and participates in the work of W3C and ECMA members (TC39) on behalf of the group. Qi Wu Troupe attaches great importance to talent training, and has various development directions such as engineers, lecturers, translators, business interface people, and team leaders for employees to choose from, and provides corresponding technical, professional, general, and leadership training course. Qi Dance Troupe welcomes all kinds of outstanding talents to pay attention to and join Qi Dance Troupe with an open and talent-seeking attitude.