图算是数据结构中比较难的问题,但是在实际中解决的问题也更多。

其中,在图结构中涉及的问题主要有:

图的存储:

  • 邻接表(Adjacency list):为每个节点建立一个链表存放与之连接的点.
  • 邻接矩阵(Adjacency matrix):n*n的矩阵,有边的是1,无边的是0.

最短路径:

  • Dijkstra:记录起点能够到达的所有节点的最短路径,这样,我们要找的终点一定在其中啊。
    • DIST(w) = min(DIST(w), DIST(u) + c(u, w))
    • 图实践经典问题一览
    • 图实践经典问题一览
    • 代码实现示例:
    • package com.realise;
      
      import java.util.Arrays;
      
      public class Dijkstra {
          static int max = 10000;
          
          public static int[] dijkstra(int[][] edge) {
              int n = edge.length;
              int[] res = new int[n];
              for(int i=0; i<n; i++)
                  res[i] = edge[0][i];
              for(int i=0; i<n; i++) {
                  for(int j=0; j<n; j++) {
                      if(res[j] + edge[j][i] < res[i])
                          res[i] = res[j] + edge[j][i];
                  }
              }
              return res;
          }
          
          
          public static void main(String[] args) {
              // TODO Auto-generated method stub
              int[][] edge = {
                      {0,20,50,30,max, max, max},
                      {max,0,25,max,max,70,max},
                      {max,max,0,40,25,50,max},
                      {max,max,max,0,55,max,max},
                      {max,max,max,max,0,10,70},
                      {max,max,max,max,max,0,50},
                      {max,max,max,max,max,max,0}
              };
              int[] res = dijkstra(edge);
              System.out.println(Arrays.toString(res));
          }
      
      }
      View Code

相关文章:

  • 2021-12-26
  • 2021-09-03
  • 2021-09-10
  • 2022-01-23
  • 2022-02-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-02
  • 2022-02-13
  • 2021-12-04
  • 2022-01-02
  • 2021-09-04
  • 2022-12-23
  • 2021-06-24
相关资源
相似解决方案