爆款云主机2核4G限时秒杀,88元/年起!
查看详情

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 618智算钜惠季 爆款云主机2核4G限时秒杀,88元/年起!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 首保服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

      图【数据结构与算法java】

      首页 知识中心 数据库 文章详情页

      图【数据结构与算法java】

      2025-03-27 09:41:58 阅读次数:7

      bfs,dfs

      StackX

      package dfs;
      
      /**
       * @author CSDN@日星月云
       * @date 2022/10/26 19:03
       */
      public class StackX {
          private final int SIZE = 20;
          private int[] st;
          private int top;
      
          public StackX() {// constructor
              st = new int[SIZE];
              top = -1;// make array
          }
      
          public void push(int j) { // put item on stack
              st[++top] = j;
          }
      
          public int pop() {// take item off stack
              return st[top--];
          }
      
          public int peek() {// peek at top of stack
              return st[top];
          }
      
          public boolean isEmpty() { // true if nothing on stack
              return (top == -1);
          }
      
          //测试StackX
          public static void main(String[] args) {
              StackX stackX=new StackX();
              System.out.println(stackX.isEmpty());
              stackX.push(1);
              stackX.push(2);
              stackX.push(3);
              stackX.pop();
              int peek = stackX.peek();
              System.out.println(peek);
              stackX.pop();
              stackX.pop();
              System.out.println(stackX.isEmpty());
          }
      
      }  // end class Stackx
      
      
      

      QueueX

      package bfs;
      
      /**
       * @author CSDN@日星月云
       * @date 2022/10/26 19:10
       */
      public class QueueX {
          private final int SIZE = 20;
          private int[] queArray;
          private int front;
          private int rear;
      
          public QueueX() {// constructor
              queArray = new int[SIZE];
              front = 0;
              rear = -1;
          }
      
          public void insert(int j) {// put item at rear of queue
              if (rear == SIZE - 1)
                  rear = -1;
              queArray[++rear] = j;
          }
      
          public int remove() {// take item from front of queue
              int temp = queArray[front++];
              if (front == SIZE)
                  front = 0;
              return temp;
          }
      
          public boolean isEmpty() {// true if queue is empty
              return (rear + 1 == front || (front + SIZE - 1 == rear));
          }
      
          //测试Queue
          public static void main(String[] args) {
              QueueX queueX =new QueueX();
              System.out.println(queueX.isEmpty());
              queueX.insert(1);
              queueX.insert(2);
              queueX.insert(3);
              int remove = queueX.remove();
              System.out.println(remove);
              queueX.remove();
              queueX.remove();
              System.out.println(queueX.isEmpty());
      
          }
      
      
      
      } // end class QueueX
      
      

      dfs

      DFSApp

      package dfs;
      
      
      /**
       * @author CSDN@日星月云
       * @date 2022/10/26 18:37
       * bfs
       * 深度优先搜索
       * 规则1如果可能,访问一个邻接的未访问顶点,标记它,并把它放入栈中。
       * 规则2当不能执行规则1时,如果栈不空,就从栈中弹出一个顶点。
       * 规则3如果不能执行规则1和规则2,就完成了整个搜索过程。
       */
      
      
      class Vertex {
          public char label;// label (e.g. 'A')
          public boolean wasVisited;
      
          public Vertex(char lab){// constructor
              label = lab;
              wasVisited = false;
          }
      
      }
      
      class Graph {
          private final int MAX_VERTS = 20;
          private Vertex vertexList[]; // array of vertices
          private int adjMat[][];     // adjacency matrix
          private int nVerts;         // current number of vertices
          private StackX theStack;
      
      
          //------------------------------------------------------------
          public Graph() { // constructor
      
              vertexList = new Vertex[MAX_VERTS];// adjacency matrix
              adjMat = new int[MAX_VERTS][MAX_VERTS];
              nVerts = 0;
              for (int j = 0; j < MAX_VERTS; j++)// set adjacency
                  for (int k = 0; k < MAX_VERTS; k++)//matrix to 0
                      adjMat[j][k] = 0;
      
              theStack = new StackX();
      
          }// end constructor
      
          //------------------------------------------------------------
          public void addVertex(char lab) {// argument is label
              vertexList[nVerts++] = new Vertex(lab);
          }
      
          //------------------------------------------------------------
          public void addEdge(int start, int end) {
              adjMat[start][end] = 1;
              adjMat[end][start] = 1;
          }
      
          public void displayVertex(int v) {
              System.out.print(vertexList[v].label);
          }
          //------------------------------------------------------------
      
      
          /**
           * 深度优先搜索的关键在于能够找到与某一顶点邻接且没有访问过的顶点。如何做呢?邻接矩阵
           * 是关键。找到指定顶点所在的行,从第一列开始向后寻找值为1的列:列号是邻接顶点的号码。检
           * 查这个顶点是否未访问过,如果是这样,那么这就是要访问的下一个顶点。如果该行没有顶点既等
           * 于1 (邻接)且又是未访问的,那么与指定点相邻接的顶点就全部访问过了。这个过程在
           * getAdjUnvisitedVertex ()方法中实现:
      
           */
          // returns an unvisited vertex adjacent to v
          public int getAdjUnvisitedVertex(int v) {
              for (int j = 0; j < nVerts; j++)
                  if (adjMat[v][j] == 1 && vertexList[j].wasVisited == false)
                      return j;// return first such vertex
              return -1;// no such vertices
          } // end getAdjUnvisitedVertex()
      
          /**
           * 现在开始考察Graph类中的dfs()方法,这个方法实际执行了深度优先搜索。下面会看到这段代
           * 码如何包含了前面提出的三条规则。它循环执行,直到栈为空。每次循环中,它做四件书:
           * 1.用peek()方法检查栈顶的顶点。
           * 2.试图找到这个顶点还未访问的邻接点。
           * 3.如果没有找到,出栈。
           * 4.如果找到这样的顶点,访问这个顶点,并把它放入栈。
           * 5.下面是dfs()方法的代码:
           */
          public void dfs() { // depth-first search
      //        Stack<Integer> theStack = new Stack<>();//java.util.Stack
              // begin at vertex 0
              vertexList[0].wasVisited = true;// mark it
      
              displayVertex(0);//display it
              theStack.push(0);// push it
              while (!theStack.isEmpty()) {// until stack empty,
                  // get an unvisited vertex adjacent to stack top
                  int v = getAdjUnvisitedVertex(theStack.peek());
                  if (v == -1)// if no such vertex,
                      theStack.pop();//pop a new one
                  else {// if it exists,
                      vertexList[v].wasVisited = true;  // mark it
                      displayVertex(v); // display it
                      theStack.push(v);// push it
                  }
              } // end while
              // stack is empty, so we're done
              for (int j = 0; j < nVerts; j++)// reset flags
                  vertexList[j].wasVisited = false;
          }// end dfs
      
          /**
           * 在dfs()方法的最后,重置了所有wasVisited标记位,这样就可以在稍后继续使用dfs()方法。栈
           * 此时已为空,所以不需要重置。
           */
      
      
      }// end class Graph
      
      class DFSApp{
          /**
           * 现在Graph类已经有了需要的所有片段.下面是创建图对象的代码,并在图中加入一些顶点和
           * 边,然后执行深度优先搜索
           */
          public static void main(String[] args) {
              Graph theGraph = new Graph();
              theGraph.addVertex('A');//0 (start for dfs)
              theGraph.addVertex('B');//1
              theGraph.addVertex('C');//2
              theGraph.addVertex('D');//3
              theGraph.addVertex('E');//4
              theGraph.addEdge(0, 1);// AB
              theGraph.addEdge(1, 2);// BC
              theGraph.addEdge(0, 3);// AD
              theGraph.addEdge(3, 4);// DE
              /*
              A--B--C
              |
              D--E
               */
              System.out.print("Visits:");
              theGraph.dfs();       // depth-first search
              System.out.println();//Visits:ABCDE
      
          }
      
      }
      
      

      bfs

      BFSApp

      package bfs;
      
      
      /**
       * @author CSDN@日星月云
       * @date 2022/10/26 18:37
       * bfs
       * 广度优先搜索
       * 规则1访问下一个未来访问的邻接点(如果存在),这个顶点必须是当前顶点的邻接点,标记它,并把它插入到队列中。
       * 规则2如果因为已经没有未访问顶点而不能执行规则1,那么从队列头取一个顶点(如果存在),并使其成为当前顶点。
       * 规则3如果因为队列为空而不能执行规则2,则搜索结束。
       */
      
      class Vertex {
          public char label;// label (e.g. 'A')
          public boolean wasVisited;
      
          public Vertex(char lab){// constructor
              label = lab;
              wasVisited = false;
          }
      
      }
      class Graph {
          private final int MAX_VERTS = 20;
          private Vertex vertexList[]; // array of vertices
          private int adjMat[][];     // adjacency matrix
          private int nVerts;         // current number of vertices
          private QueueX theQueueX;
      
          //------------------------------------------------------------
          public Graph() { // constructor
      
              vertexList = new Vertex[MAX_VERTS];// adjacency matrix
              adjMat = new int[MAX_VERTS][MAX_VERTS];
              nVerts = 0;
              for (int j = 0; j < MAX_VERTS; j++)// set adjacency
                  for (int k = 0; k < MAX_VERTS; k++)//matrix to 0
                      adjMat[j][k] = 0;
      
              theQueueX = new QueueX();
          }// end constructor
      
          //------------------------------------------------------------
          public void addVertex(char lab) {// argument is label
              vertexList[nVerts++] = new Vertex(lab);
          }
      
          //------------------------------------------------------------
          public void addEdge(int start, int end) {
              adjMat[start][end] = 1;
              adjMat[end][start] = 1;
          }
      
          public void displayVertex(int v) {
              System.out.print(vertexList[v].label);
          }
          //------------------------------------------------------------
      
      
      
          // returns an unvisited vertex adjacent to v
          public int getAdjUnvisitedVertex(int v) {
              for (int j = 0; j < nVerts; j++)
                  if (adjMat[v][j] == 1 && vertexList[j].wasVisited == false)
                      return j;// return first such vertex
              return -1;// no such vertices
          } // end getAdjUnvisitedVertex()
      
      
      
          public void bfs() {// breadth-first search
      //        Queue<Integer> theQueue=new ArrayDeque<>();//java.util.Queue
              // begin at vertex 0
              vertexList[0].wasVisited = true; // mark it
              displayVertex(0);// display it
              theQueueX.insert(0); // insert at tail
              int v2;
              while (!theQueueX.isEmpty()) {// until queue empty,
                  int v1 = theQueueX.remove();// remove vertex at head
                  // until it has no unvisited neighbors
                  while ((v2 = getAdjUnvisitedVertex(v1)) != -1) {// get one,
                      vertexList[v2].wasVisited = true;// mark it
                      displayVertex(v2);// display it
                      theQueueX.insert(v2);// insert it
                  } // end while
              }// end while(queue not empty)
      
              // queue is empty, so we're done
              for (int j = 0; j < nVerts; j++) {
                  vertexList[j].wasVisited = false; // reset flags
              }
      
          }// end bfs()
      
      
      
      }// end class Graph
      
      public class BFSApp{
          //测试  bfs
          public static void main(String[] args) {
              Graph theGraph = new Graph();
              theGraph.addVertex('A');//0 (start for dfs)
              theGraph.addVertex('B');//1
              theGraph.addVertex('C');//2
              theGraph.addVertex('D');//3
              theGraph.addVertex('E');//4
              theGraph.addEdge(0, 1);// AB
              theGraph.addEdge(1, 2);// BC
              theGraph.addEdge(0, 3);// AD
              theGraph.addEdge(3, 4);// DE
              /*
              A--B--C
              |
              D--E
               */
              System.out.print("Visits:");
              theGraph.bfs();// breadth-first search
              System.out.println();//Visits:ABDCE
          }
      
      
      }
      
      

      mst

      MSTApp

      package mst;
      
      /**
       * @author CSDN@日星月云
       * @date 2022/10/26 18:37
       * mst
       * 最小生成树
       */
      
      class Vertex {
          public char label;// label (e.g. 'A')
          public boolean wasVisited;
      
          public Vertex(char lab){// constructor
              label = lab;
              wasVisited = false;
          }
      
      }
      
      class Graph {
          private final int MAX_VERTS = 20;
          private Vertex vertexList[]; // array of vertices
          private int adjMat[][];     // adjacency matrix
          private int nVerts;         // current number of vertices
          private StackX theStack;
      
          //------------------------------------------------------------
          public Graph() { // constructor
      
              vertexList = new Vertex[MAX_VERTS];// adjacency matrix
              adjMat = new int[MAX_VERTS][MAX_VERTS];
              nVerts = 0;
              for (int j = 0; j < MAX_VERTS; j++)// set adjacency
                  for (int k = 0; k < MAX_VERTS; k++)//matrix to 0
                      adjMat[j][k] = 0;
      
              theStack = new StackX();
          }// end constructor
      
          //------------------------------------------------------------
          public void addVertex(char lab) {// argument is label
              vertexList[nVerts++] = new Vertex(lab);
          }
      
          //------------------------------------------------------------
          public void addEdge(int start, int end) {
              adjMat[start][end] = 1;
              adjMat[end][start] = 1;
          }
      
          public void displayVertex(int v) {
              System.out.print(vertexList[v].label);
          }
          //------------------------------------------------------------
      
      
      
          // returns an unvisited vertex adjacent to v
          public int getAdjUnvisitedVertex(int v) {
              for (int j = 0; j < nVerts; j++)
                  if (adjMat[v][j] == 1 && vertexList[j].wasVisited == false)
                      return j;// return first such vertex
              return -1;// no such vertices
          } // end getAdjUnvisitedVertex()
      
      
          //最小生成树
          public void mst() {// minimum spanning tree (depth first)
              // start at 0
              vertexList[0].wasVisited = true;// mark it
              theStack.push(0);// push it
      
              while (!theStack.isEmpty()) {// until stack empty
                  // get stack top
                  int currentVertex = theStack.peek();
                  //get next unvisited neighbor
                  int v = getAdjUnvisitedVertex(currentVertex);
                  if (v == -1) {
                      // if no more neighbors
                      theStack.pop();// pop it away
                  } else { // got a neighbor
      
                      vertexList[v].wasVisited = true; // mark it
                      theStack.push(v);// push it
                      // display edge
                      displayVertex(currentVertex);// from currentV
                      displayVertex(v);// to v
                      System.out.print(" ");
                  }
              }// end while(stack not empty)
              // stack is empty, so we're done
              for (int j = 0; j < nVerts; j++) {// reset flags
                  vertexList[j].wasVisited = false;
              } // end tree
          }
      
      
      
      
      }// end class Graph
      
      public class MSTApp{
          //测试 mst
          public static void main(String[] args) {
              Graph theGraph = new Graph();
              theGraph.addVertex('A');//0 (start for dfs)
              theGraph.addVertex('B');//1
              theGraph.addVertex('C');//2
              theGraph.addVertex('D');//3
              theGraph.addVertex('E');//4
              theGraph.addEdge(0, 1);// AB
              theGraph.addEdge(0, 2);// AC
              theGraph.addEdge(0, 3);// AD
              theGraph.addEdge(0, 4);// AE
              theGraph.addEdge(1, 2);// BC
              theGraph.addEdge(1, 3);// BD
              theGraph.addEdge(1, 4);// BD
              theGraph.addEdge(2, 3);// CD
              theGraph.addEdge(2, 4);// CE
              theGraph.addEdge(3, 4);// DE
              /*
              A--B--C
              |
              D--E
               */
              System.out.print("Minimum spanning tree:");
              theGraph.mst();       // minimum spanning tree
              System.out.println(); // AB BC CD DE
      
          }
      
      }
      
      

      topo

      TopoApp

      package topo;
      
      /**
       * @author CSDN@日星月云
       * @date 2022/10/26 18:37
       * 有向图
       * topo
       * 拓扑排序
       * 步骤1找到一个没有后继的顶点。
       * 步骤2从图中删除这个顶点,在列表的前面插入顶点的标记。
       * 重复步骤1和步骤2,直到所有顶点都从图中删除。
       * 这时,列表显示的顶点顺序就是拓扑排序的结果。
       */
      class Vertex {
          public char label;// label (e.g. 'A')
          public boolean wasVisited;
      
          public Vertex(char lab) {// constructor
              label = lab;
              wasVisited = false;
          }
      
      }
      
      class Graph {
          private final int MAX_VERTS = 20;
          private Vertex vertexList[]; // array of vertices
          private int adjMat[][];     // adjacency matrix
          private int nVerts;         // current number of vertices
          private char[] sortedArray;
      
          //------------------------------------------------------------
          public Graph() { // constructor
      
              vertexList = new Vertex[MAX_VERTS];// adjacency matrix
              adjMat = new int[MAX_VERTS][MAX_VERTS];
              nVerts = 0;
              for (int j = 0; j < MAX_VERTS; j++)// set adjacency
                  for (int k = 0; k < MAX_VERTS; k++)//matrix to 0
                      adjMat[j][k] = 0;
      
              sortedArray = new char[MAX_VERTS];// sorted vert labels
          }// end constructor
      
          //------------------------------------------------------------
          public void addVertex(char lab) {// argument is label
              vertexList[nVerts++] = new Vertex(lab);
          }
      
          //------------------------------------------------------------
          public void addEdge(int start, int end) {
              adjMat[start][end] = 1;
          }
      
          public void displayVertex(int v) {
              System.out.print(vertexList[v].label);
          }
          //------------------------------------------------------------
      
      
          public void topo() {// topological sort
              int orig_nVerts = nVerts; // remember how many verts
              while (nVerts > 0) { // while vertices remain,
                  // get a vertex with no successors, or -1
                  int currentVertex = noSuccessors();
                  if (currentVertex == -1) {// must be a cycle
                      System.out.println("ERROR: Graph has cycles");
                      return;
                  }
                  // insert vertex label in sorted array (start at end)
                  sortedArray[nVerts - 1] = vertexList[currentVertex].label;
      
                  deleteVertex(currentVertex); // delete vertex}
              }// end while
      
              // vertices all gone;display sortedArray
              System.out.print("Topologically sorted order: ");
              for (int j = 0; j < orig_nVerts; j++) {
                  System.out.print(sortedArray[j]);
              }
              System.out.println("");
          }// end topo
      
          /**
           *主要工作在while循环中进行。这个循环直到顶点数目为0时才退出。下面是步骤:
           * 1.调用noSuccessors()找到任意一个没有后继的顶点。
           * 2.如果找到一个这样的顶点,把顶点放入数组sortedArray[],并且从图中删除顶点。
           * 3.如果没有这样的顶点,则图必然存在环。
           *
           * 最后一个被删除的顶点出现在列表的开头,所以,随着nVerts (图中顶点个数)逐渐变小,顶
           * 点从sortedArray数组的最后开始,依次向前排列。
           *
           * 如果有顶点还在图中,但它们都有后继,那么图必然存在环,算法会显示一条信息并退出。如
           * 果没有环,则while循环退出,显示sortedArray数组中的数据,这时顶点是按拓扑有序排列。
           *
           */
      
      
          /**
           * noSuccessorsO方法使用邻接矩阵找到没有后继的顶点。在外层for循环中,沿着每一行考察每
           * 个顶点。在每行中,用内层for循环扫描列,査找值为1的顶点。如果找到一个,就说明顶点有后
           * 继,因为从这个顶点到其他点有边存在。当找到一个1时,跳出内层循环,考察下一个顶点。
           * <p>
           * 只有整个一行都没有1存在,才说明有一个顶点没有后继;这时,就返回它的行号。如果没有
           * 这样的顶点,就返回-1。下面是noSuccessors()方法:
           */
          public int noSuccessors() {// returns vert with no successors
              // (or -1 if no such verts)
              boolean isEdge; // edge from row to column in adjMat
              for (int row = 0; row < nVerts; row++) { // for each vertex,
                  isEdge = false;
                  for (int col = 0; col < nVerts; col++) { // check edges
                      if (adjMat[row][col] > 0) { // if edge to
                          // another,
                          isEdge = true;             // this vertex
                          break;
                      }//has a successor
                  }// try another
                  if (!isEdge)// if no edges,
                      return row;  // has no successor
      
              }
              return -1;// no such vertex
          }// end noSuccessors()
      
          /**
           * 除了一些细节外,删除一个顶点很简单。顶点从vertexList[]数组删除,后面的顶点向前移动填
           * 补空位。同样的,顶点的行列从邻接矩阵中删除,下面的行和右面的列移动来填补空位。这些任务
           * 由delete Vertex。、moveRowUp()和moveColLeft ()方法来完成。这些方法将在topo.java程序的完整
           * 代码(清单13.4)中看到。对于这个算法,用邻接表表示图效率更高,但要使用更多空间。
           */
      
          public void deleteVertex(int delVert) {
              if (delVert != nVerts - 1) {// if not last vertex,
                  // delete from vertexList
                  for (int j = delVert; j < nVerts - 1; j++)
                      vertexList[j] = vertexList[j + 1];
                  // delete row from adjMat
                  for (int row = delVert; row < nVerts - 1; row++)
                      moveRowUp(row, nVerts);
                  // delete col from adjMat
                  for (int col = delVert; col < nVerts - 1; col++)
                      moveColLeft(col, nVerts - 1);
      
              }
              nVerts--;// one less vertex
          }// end deleteVertex
      
      
          private void moveRowUp(int row, int length) {
              for (int col = 8; col < length; col++) {
                  adjMat[row][col] = adjMat[row + 1][col];
              }
          }
      
          private void moveColLeft(int col, int length) {
              for (int row = 0; row < length; row++)
                  adjMat[row][col] = adjMat[row][col + 1];
          }
      
      
      }// end class Graph
      
      public class TopoApp {
          /**
           * main。例程调用一些方法创建如图13.10所示的图,这些方法与前面看到的类似。应该注意到,
           * addEdge()方法只在邻接矩阵中插入一个数,因为这是有向图。下面是main()方法的代码:
           */
          public static void main(String[] args) {
              Graph theGraph = new Graph();
              theGraph.addVertex('A');//0
              theGraph.addVertex('B');//1
              theGraph.addVertex('C');//2
              theGraph.addVertex('D');//3
              theGraph.addVertex('E');//4
              theGraph.addVertex('F');//5
              theGraph.addVertex('G');//6
              theGraph.addVertex('H');//7
              theGraph.addEdge(0, 3);// AD
              theGraph.addEdge(0, 4);// AE
              theGraph.addEdge(1, 4);// BE
              theGraph.addEdge(2, 5);// CF
              theGraph.addEdge(3, 6);// DG
              theGraph.addEdge(4, 6);// EG
              theGraph.addEdge(5, 7);// FH
              theGraph.addEdge(6, 7);// GH
      
              theGraph.topo();// do the sort
              /*
              Topologically sorted order: GBAEDCFH
               */
          }// end main()
      }// end class TopoApp
      
      

      pstw

      MSTWApp

      package pstw;
      
      /**
       * @author CSDN@日星月云
       * @date 2022/10/27 15:09
       *
       * 最小生成树
       * 无向带权图
       * 从一个顶点开始,把它放入树的集合中。然后重复做下面的事情:
       * 1.找到从最新的顶点到其他顶点的所有边,这些顶点不能在树的集合中。把这些边放入优先级队列。
       * 2.找岀权值最小的边,把它和它所到达的顶点放入树的集合中。
       * 重复这些步骤,直到所有顶点都在树的集合中。这时,工作完成。
       */
      
      /**
       * PriorityQ类用数组存储对象。正如前面提到的,在处理大量数据的程序中,用堆存储对象比数
       * 组更合适。PriorityQ类已经增加了不同的方法,如前所示,它用find()方法找到到达指定顶点的边;
       * 用peekN()方法得到任意一个数据项;用removeN()方法删除任意一个数据项。程序的其余部分前面
       * 已经看到。清单14.1显示了完整的mstw.java程序。
       */
      //mstw.java
      
      // demonstrates minimum spanning tree with weighted graphs
      // to run this program: C>java MSTWApp
      
      class Edge {
          public int srcVert;     //index of a vertex starting edge
          public int destVert;    //index of a vertex	ending edge
          public int distance;    //distance from src to dest
          //-------------------------------------------
      
          public Edge(int sv, int dv, int d) // constructor
          {
              srcVert = sv;
              destVert = dv;
              distance = d;
          }
      //
      }// end class Edge
      
      
      class PriorityQ {
      
          // array in sorted order, from max at 0 to min at size-1
          private final int SIZE = 20;
          private Edge[] queArray;
          private int size;
          //-------------------------------------------
      
          public PriorityQ() {    // constructor
              queArray = new Edge[SIZE];
              size = 0;
          }
      
          //
          public void insert(Edge item) {//If insert item in sorted order
              int j;
      
              for (j = 0; j < size; j++)    // find place to insert
                  if (item.distance >= queArray[j].distance)
                      break;
              for (int k = size - 1; k >= j; k--)    // move items up
                  queArray[k + 1] = queArray[k];
      
              queArray[j] = item;    // insert item
              size++;
          }
      
      
          //
          public Edge removeMin() {// remove minimum item
              return queArray[--size];
          }
      
      
          public void removeN(int n) { // remove item at n
              for (int j = n; j < size - 1; j++) // move items down
                  queArray[j] = queArray[j + 1];
              size--;
          }
      
          //
          public Edge peekMin() {    // peek at minimum item
              return queArray[size - 1];
          }
      
          //
          public int size() {// return number of items
              return size;
          }
      
          //
          public boolean isEmpty() {    // true if queue is empty
      
              return (size == 0);
          }
      
          //
          public Edge peekN(int n) { // peek at item n
              return queArray[n];
          }
      
      
          public int find(int findDex) { // find item with specified
              // destVert value
              for (int j = 0; j < size; j++)
                  if (queArray[j].destVert == findDex)
                      return j;
              return -1;
          }
      
      
      }//end class PriorityQ
      
      
      class Vertex {
          〃// class Vertex
          public char label; // label (e.g. 'A1)
          public boolean isInTree;
      
          //
          public Vertex(char lab) { // constructor
              label = lab;
              isInTree = false;
          }
      //
      } // end class Vertex
      
      
      class Graph {
          private final int MAX_VERTS = 20;
          private final int INFINITY = 1000000;
          private Vertex vertexList[];// list of vertices
          private int adjMat[][];// adjacency matrix
          private int nVerts; // current number of vertices
          private int currentVert;
          private PriorityQ thePQ;
          private int nTree; // number of verts in tree
      
          public Graph()    // constructor
          {
              vertexList = new Vertex[MAX_VERTS];
              // adjacency matrix
              adjMat = new int[MAX_VERTS][MAX_VERTS];
              nVerts = 0;
              for (int j = 0; j < MAX_VERTS; j++) // set adjacency
                  for (int k = 0; k < MAX_VERTS; k++) // matrix to 0
                      adjMat[j][k] = INFINITY;
              thePQ = new PriorityQ();
          }//end constructor
      
          //
          public void addVertex(char lab) {
              vertexList[nVerts++] = new Vertex(lab);
          }
      
          public void addEdge(int start, int end, int weight) {
              adjMat[start][end] = weight;
              adjMat[end][start] = weight;
          }
      
          //
          public void displayVertex(int v) {
              System.out.print(vertexList[v].label);
          }
      
          /**
           * 根据前面的算法要点,编制有向图最小生成树的方法mstw()。正如其他图的程序一样,假定在
           * vertexList[]数组中有一个顶点列表,并且从下标为0的顶点开始。currentVert代表最近加到树中的
           * 顶点。下面是mstw()方法的代码:
           */
          public void mstw()    //	minimum spanning tree
          {
              currentVert = 0;    //	start at 0
              while (nTree < nVerts - 1) {    //	while not all verts in tree
                  //	put currentVert in tree
                  vertexList[currentVert].isInTree = true;
                  nTree++;
      
                  // insert edges adjacent to currentVert into PQ
                  for (int j = 0; j < nVerts; j++) {// for each vertex,
                      if (j == currentVert)// skip if it 's us
                          continue;
                      if (vertexList[j].isInTree) // skip if in the tree
                          continue;
                      int distance = adjMat[currentVert][j];
                      if (distance == INFINITY)
                          continue;
                      putInPQ(j, distance);//put it in PQ (maybe)
                  }
                  if (thePQ.size() == 0) {//no vertices in PQ?
                      System.out.println("GRAPH NOT CONNECTED");
                      return;
                  }
      
                  // remove edge with minimum distance, from PQ
                  Edge theEdge = thePQ.removeMin();
                  int sourceVert = theEdge.srcVert;
                  currentVert = theEdge.destVert;
                  // display edge from source to current
                  System.out.print(vertexList[sourceVert].label);
                  System.out.print(vertexList[currentVert].label);
                  System.out.print(" ");
              }// end while( not all verts in tree)
              // mst is complete
              for (int j = 0; j < nVerts; j++)    // unmark vertices
                  vertexList[j].isInTree = false;
          } // end mstw
      
          /**
           * 算法在while循环中执行,循环结束条件是所有顶点都己在树中。循环中完成下面的操作:
           * 1.当前顶点放在树中。
           * 2.连接这个顶点的边放到优先级队列中(如果合适)。
           * 3.从优先级队列中删除权值最小的边。这条边的目的顶点变成当前顶点。
           * 再看看这些步骤的细节。在步骤1中,通过标记currentVert所指顶点的isInTree字段来表示该
           * 顶点放入树中。
           * 在步骤2中,连接这个顶点的边插入优先级队列。通过在邻接矩阵中扫描行号是currentVert的
           * 行寻找需要的边。只要下面任意一个条件为真,这条边就不能放入队列中:
           *  •源点和终点相同。
           *  •终点在树中。
           *  •源点和终点之间没有边(邻接矩阵中对应的值等于无限大)。
           * 如果没有一个条件为真,调用putlnPQO方法把这条边放入队列中。实际上,正如下面要看到
           * 的,这个例程并不总把边放到队列中。
           * 在步骤3中,将最小权值的边从优先级队列中删除。把这条边和该边的终点加入树,并显示源
           * 点(currentVert)和终点。
           *在mstw()方法最后,所有顶点的isInTree变量被重置,即从树中删除。在该程序中这样做,是
           * 因为根据这些数据只能创建一棵树。然而,在完成一项工作后,最好把数据恢复到原始的形态。
           */
      
          /**
           * 正如前面所强调的,在优先级队列中应该只含有一条到达某个特定目标顶点的边。putlnPQ()
           * 方法保证了这一点。它调用PriorityQ类的find()方法,这个方法经过修正,可以寻找到达指定点的
           * 边。如果不存在到达指定点的边,find。方法返回一1,这时putlnPQ()方法只要把新边插入优先级队
           * 列中即可。然而,如果有到达指定点的老边存在,putlnPQ()方法就要检査老边是否比新边有更小的
           * 权值。如果老边的权值小,就不需要作什么变化。如果新边有更小的权值,就要把老边从队列中删
           * 除,把新边放进去。下面是putlnPQO方法的代码:
           */
          public void putInPQ(int newVert, int newDist) {
              //is there another edge with the same destination vertex?
              int queueIndex = thePQ.find(newVert);
              if (queueIndex != -1) {    // got edge's index
      
                  Edge tempEdge = thePQ.peekN(queueIndex);// get edge
                  int oldDist = tempEdge.distance;
                  if (oldDist > newDist) {    // if new edge shorter,
                      thePQ.removeN(queueIndex); // remove old edge
                      Edge theEdge = new Edge(currentVert, newVert, newDist);
                      thePQ.insert(theEdge);    // insert new edge
                  }
                  // else no action; just leave the old vertex there
              } // end if
              else { //no edge with same destination vertex
      
                  //so insert new one
                  Edge theEdge = new Edge(currentVert, newVert, newDist);
                  thePQ.insert(theEdge);
              }
          } // end putInPQ()
      
      } // end class Graph
      
      
      class MSTWApp {
          public static void main(String[] args) {
              Graph theGraph = new Graph();
              theGraph.addVertex('A');//0 (start for mst)
              theGraph.addVertex('B');//1
              theGraph.addVertex('C');//2
              theGraph.addVertex('D');//3
              theGraph.addVertex('E');//4
              theGraph.addVertex('F');//5
      
              theGraph.addEdge(0, 1, 6);//AB	6
              theGraph.addEdge(0, 3, 4);//AD	4
              theGraph.addEdge(1, 2, 10);//BC 10
              theGraph.addEdge(1, 3, 7);//BD 7
              theGraph.addEdge(1, 4, 7);//BE 7
              theGraph.addEdge(2, 3, 8);//CD 8
              theGraph.addEdge(2, 4, 5);//CE 5
              theGraph.addEdge(2, 5, 6);//CF 6
              theGraph.addEdge(3, 4, 12);//DE 12
              theGraph.addEdge(4, 5, 7);//EF 7
      
              System.out.print("Minimum spanning tree: ");
              theGraph.mstw();    // minimum spanning tree
              System.out.println();//Minimum spanning tree: AD AB BE EC CF
          }// end main()
      }//end class MSTWApp
      ///
      
      
      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://jsss-1.blog.csdn.net/article/details/127559047,作者:日星月云,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:Vue 的 computed 和 watch

      下一篇:查找【数据结构与算法java】

      相关文章

      2025-05-16 09:15:17

      多源BFS问题(2)_飞地的数量

      多源BFS问题(2)_飞地的数量

      2025-05-16 09:15:17
      bfs , grid , 单元格 , 算法
      2025-03-31 08:49:58

      算法题:剑指 Offer 12. 矩阵中的路径(题目+思路+代码+注释)DFS+回溯

      算法题:剑指 Offer 12. 矩阵中的路径(题目+思路+代码+注释)DFS+回溯

      2025-03-31 08:49:58
      dfs , 回溯
      2025-03-31 08:49:38

      算法题:剑指 Offer 13. 机器人的运动范围(题目+思路+代码+注释)时空O(m*n) O(m*n) 0ms击败100%、35%用户

      算法题:剑指 Offer 13. 机器人的运动范围(题目+思路+代码+注释)时空O(m*n) O(m*n) 0ms击败100%、35%用户

      2025-03-31 08:49:38
      dfs
      2025-02-27 09:34:21

      C++算法:2876有向图计数优化版原理及实现

      C++算法:2876有向图计数优化版原理及实现

      2025-02-27 09:34:21
      cur , dfs , DFS , ret , return
      2025-02-13 08:41:23

      【算法】递归、搜索与回溯——简介

      【算法】递归、搜索与回溯——简介

      2025-02-13 08:41:23
      bfs , dfs , 函数 , 回溯 , 搜索 , 递归
      2025-01-17 09:05:56

      BFS:Floodfill算法

      BFS:Floodfill算法

      2025-01-17 09:05:56
      bfs , LeetCode , 力扣 , 数组 , 标记 , 矩阵 , 遍历
      2025-01-17 09:05:56

      BFS:边权相同的最短路问题

      BFS:边权相同的最短路问题

      2025-01-17 09:05:56
      bfs , lambda , LeetCode , map , sort , vector , 力扣
      2024-05-27 09:15:18

      UVA10305 给任务排序 Ordering Tasks

      John有n个任务要做,每个任务在做之前要先做特定的一些任务。

      2024-05-27 09:15:18
      dfs , 数据结构 , 算法
      2024-05-27 09:15:18

      P3916 图的遍历

      P3916 图的遍历

      2024-05-27 09:15:18
      dfs , 算法
      2023-08-09 06:40:53

      hdfs提示/tmp/.cloudera_health_monitoring_canary_file

      一、查看hdfs的tmp文件夹是否存在sudo su - hdfshdfs dfs -ls /发现hdfs的根目录下没有tmp文件夹新建tmp文件夹hdfs dfs  -mkdir /tmphdfs dfs -chmod -R 775  /

      2023-08-09 06:40:53
      dfs , hdfs , tmp
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      33561

      阅读量

      5239477

      查看更多

      最新文章

      UVA10305 给任务排序 Ordering Tasks

      2024-05-27 09:15:18

      查看更多

      热门文章

      UVA10305 给任务排序 Ordering Tasks

      2024-05-27 09:15:18

      查看更多

      热门标签

      数据库 mysql 字符串 数据结构 MySQL 算法 redis oracle java sql python 数据 索引 SQL 查询
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      UVA10305 给任务排序 Ordering Tasks

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 旗舰店
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 天翼云旗舰店
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号