Java 程序实现图形数据结构

javacampus interviewserver side programmingprogramming更新于 2024/8/8 18:32:00

在本文中,我们将了解如何实现图形数据结构。我们实现图形数据结构我们使用 HashMap 集合在 Java 中实现图形。HashMap 元素采用键值对的形式。我们可以在 HashMap 中表示图形邻接列表。

下面是相同的演示 −

假设我们的输入是

顶点数:5
边数:5

期望的输出将是

图形节点之间的连接是:
1 - 2
1 - 3
1 - 4
2 - 4
2 - 5
3 - 4
3 - 5
4 - 5

算法

步骤 1 - 开始
步骤 2 - 声明一个 Graph 类的对象,即 graph_object,类 ‘Edge’ 中的两个整数,即源和目标,以及 ‘main’ 函数中的两个整数,即 vertices_count 和 edges_count。
步骤 3 - 定义值。
步骤 4 - 初始化顶点和计数的值。
步骤 5 - 创建先前定义的类的新实例。
步骤 6 - 使用相关值初始化实例。
步骤 7 - 使用 ‘for’ 循环遍历实例,并在控制台上显示输出。
步骤 8 - 显示结果
步骤 9 - 停止

示例 1

在这里,我们将所有操作都绑定在‘main’函数下。

public class Graph {
   class Edge {
      int source, destination;
   }
   int vertices, edges;
   Edge[] edge;
   Graph(int vertices, int edges) {
      this.vertices = vertices;
      this.edges = edges;
      edge = new Edge[edges];
      for(int i = 0; i < edges; i++) {
         edge[i] = new Edge();
      }
   }
   public static void main(String[] args) {
      int vertices_count = 5;
      int edges_count = 8;
      Graph graph_object = new Graph(vertices_count, edge_count);
      System.out.println("定义一个图对象。");
      graph_object.edge[0].source = 1;
      graph_object.edge[0].destination = 2;
      graph_object.edge[1].source = 1;
      graph_object.edge[1].destination = 3;
      graph_object.edge[2].source = 1;
      graph_object.edge[2].destination = 4;
      graph_object.edge[3].source = 2;
      graph_object.edge[3].destination = 4;
      graph_object.edge[4].source = 2;
      graph_object.edge[4].destination = 5;
      graph_object.edge[5].source = 3;
      graph_object.edge[5].destination = 4;
      graph_object.edge[6].source = 3;
      graph_object.edge[6].destination = 5;
      graph_object.edge[7].source = 4;
      graph_object.edge[7].destination = 5;
      System.out.println("图的边之间的连接是:");
      for(int i = 0; i < edge_count; i++) {
         System.out.println(graph_object.edge[i].source + " - " + graph_object.edge[i].destination);
      }
   }
}

输出

定义一个图对象。
图的边之间的连接为:
1 - 2
1 - 3
1 - 4
2 - 4
2 - 5
3 - 4
3 - 5
4 - 5

示例 2

在这里,我们将操作封装成展现面向对象编程的函数。

public class Graph {
   class Edge {
      int source, destination;
   }
   int vertices, edges;
   Edge[] edge;
   Graph(int vertices, int edges) {
      this.vertices = vertices;
      this.edges = edges;
      edge = new Edge[edges];
      for(int i = 0; i < edges; i++) {
         edge[i] = new Edge();
      }
   }
   static void print(Graph graph_object,int edges_count){
      System.out.println("Graph 各边之间的连接为: ");
      for(int i = 0; i < edge_count; i++) {
         System.out.println(graph_object.edge[i].source + " - " + graph_object.edge[i].destination);
      }
   }
   static void connect_edges(Graph graph_object){
      graph_object.edge[0].source = 1;
      graph_object.edge[0].destination = 2;
      graph_object.edge[1].source = 1;
      graph_object.edge[1].destination = 3;
      graph_object.edge[2].source = 1;
      graph_object.edge[2].destination = 4;
      graph_object.edge[3].source = 2;
      graph_object.edge[3].destination = 4;
      graph_object.edge[4].source = 2;
      graph_object.edge[4].destination = 5;
      graph_object.edge[5].source = 3;
      graph_object.edge[5].destination = 4;
      graph_object.edge[6].source = 3;
      graph_object.edge[6].destination = 5;
      graph_object.edge[7].source = 4;
      graph_object.edge[7].destination = 5;
   }
    public static void main(String[] args) {
      int vertices_count = 5;
      int edge_count = 8;
      Graph graph_object = new Graph(vertices_count, edges_count);
      System.out.println("定义了一个图对象。");
      connect_edges(graph_object);
      print(graph_object, edges_count);
   }
}

输出

定义了一个图对象。
Graph 的边之间的连接为:1 - 2
1 - 3
1 - 4
2 - 4
2 - 5
3 - 4
3 - 5
4 - 5

相关文章