Basic concepts of data structure diagram

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
203f4472814137b3a06686ed16217bad.png

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
a80c9ba9f1a09fbad0955fcb2085427b.png

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 108b830c3a1176dfae173d4012a2450a2.png

directed graph storage

50273bd040989184dd29d91876d1c6d3.png
Directed graph two-dimensional array.png

adjacency list

551bfc02c48a233613c5a61922c83235.png
Directed graph adjacency list.png
//顶点 
 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 a3c6378273932d92fab51789d0159ee2.png5, and traverse from vertex 5 to vertex 8

d687a0423c6a59e1e6fdc4d3e996d70d.png3. 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 f9278d164b2481689dfa80fc4fb7c5b3.pngto 2, then backtracking to 1 f756f4b7d850fa9d34e2bdea17fb3255.png5 , traverse vertices 3, 4a15e74c9ebecde8587622e9823480699.png

//深搜
 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, 9d43f06f3059ef532b81ec7787751b9c.png3, and 4 connected to 1 to the queue from 1, and at the same time, 1 is dequeued d5900009aacf7e64dc421e2dd6795db5.pngto 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 75f74f491dae23b1b85cc62ef131aedd.png3 , all those connected to 3 have been visited, 4, 3, 4 are taken out, 7 is entered into the queue 58e6b576af86764b895860869e7561af.pngand 5 is taken, 8 connected to 5 is added to the queue, 5 is dequeued, and 6451e8628e516af1c1cebaaf390d5b1f.png6, 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.

5f2aa06f84db051e174290ce3bc84545.png

Guess you like

Origin blog.csdn.net/qiwoo_weekly/article/details/130939351