Figure Adapter

What is a graph A
graph is a data structure composed of a collection of nodes and a collection of edges.
Graphs are divided into directed graphs and undirected graphs. Among them, undirected graphs can also be understood as directed graphs, so all graphs can be considered as directed graphs.
For example, there is such a picture. Where a points to bc, b points to c, and c points to p. Edges are directed, so this is a directed graph.
insert image description here
What about undirected graphs? A connects to b, b connects to c, and c connects to a. It can be completely understood that ab points to each other, bc points to each other, and ca points to each other, so undirected graphs can also be understood as directed graphs.
insert image description here

There are many ways to express graphs, among which the adjacency list method, adjacency matrix method and so on are common. The algorithm of the graph is not difficult, but the difficulty is the expression of the data structure of the graph.
For example, given a two-dimensional array of n * 3, [1, 3, 5], [0, 5, 5], [2, 6, 5] where [i][0] represents the weight of the edge , [i][1], [i][2] represent the edge from where to where. What is given is the weight of each edge and from where to where, and the graph drawn according to each edge is like this. This is also a graph representation.
insert image description here
It is even more possible to use a 1-dimensional array to represent a picture, so different ways of expressing the picture require different solutions. Every kind of coding needs to be practiced. Is it possible to unify the structure? No matter what kind of graph, it is converted into its own structure, and the abstract graph becomes concrete through code, and the idea of ​​object-oriented programming is more.

Code
The point is converted through the code. The Node object encapsulates all the basic information of the point, the value of the point, how many edges are connected to this point, and how many edges go out from this point.

/*
* 点的描述
* */
public class Node {
    
    
    //点本身value
    public int value;
    //入度(有多少个点的边直接指向该点)
    public int in;
    //出度(从自己出发,直接指向别人的)
    public int out;
    //从自己出发能找到的边
    public ArrayList<Edge> edges;
    //从自己出发能找到的点
    public ArrayList<Node> nexts;

    public Node(int val){
    
    
        this.value = val;
        this.in = 0;
        this.out = 0;
        edges = new ArrayList<>();
        nexts = new ArrayList<>();
    }
}

The description information of the edge, including the weight of the edge, which point is connected to which point.

/*
* 边的描述信息
* */
public class Edge {
    
    
    //边的权重
    public int weight;
    //从哪个点出来
    public Node from;
    //连接到哪个点
    public Node to;

    public Edge(int weight,Node from,Node to){
    
    
        this.weight = weight;
        this.from = from;
        this.to = to;
    }
}

A graph is composed of multiple points and edges, so it contains all point information and edge information, where HashMap is a specific point value and an encapsulated point object.

/*
* 图的描述,包含了所有的点和所有的边
* */
public class Graph {
    
    

    public HashMap<Integer,Node> nodes;
    public HashSet<Edge> edges;

    public Graph(){
    
    
        nodes = new HashMap<>();
        edges = new HashSet<>();
    }
}

Generating graph code
Take the graph form as a two-dimensional array as an example, the position [i][0] is the weight of the edge, and [i][1],[i][2] are which point is connected to which point.

public class GraphGenerator {
    
    

    public static Graph createGraph(int[][] matrix) {
    
    
        Graph graph = new Graph();

        for (int i = 0; i < matrix.length; i++) {
    
    
            int weight = matrix[i][0];
            int from = matrix[i][1];
            int to = matrix[i][2];

            if (!graph.nodes.containsKey(from)){
    
    
                graph.nodes.put(from,new Node(from));
            }
            if (!graph.nodes.containsKey(to)){
    
    
                graph.nodes.put(to,new Node(to));
            }
            Node fromNode = graph.nodes.get(from);
            Node toNode = graph.nodes.get(to);
            Edge edge = new Edge(weight,fromNode,toNode);

            fromNode.nexts.add(toNode);
            fromNode.out++;
            toNode.in++;
            fromNode.edges.add(edge);
            graph.edges.add(edge);
        }
        return graph;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43936962/article/details/132157289