前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >判断无向图是否是一颗树

判断无向图是否是一颗树

作者头像
DuncanZhou
发布2020-01-21 10:32:43
4790
发布2020-01-21 10:32:43
举报
文章被收录于专栏:Duncan's BlogDuncan's Blog

这是一个基本概念,且很重要,记录一下.

树的定义:用图的知识来表示即为,无环的连通图或者边数等于顶点数减1.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970

package questionCheckisTree;import java.util.Scanner;/** * Created by duncan on 17-7-30. *///本题为判断一个无向图是否是一颗树 //树为无环的连通图或者边数等于顶点数-1class Graph{ //邻接矩阵,从1开始存储 int[][] graph = new int[501][501]; //顶点数和边数 int nodes,edges;}public class Main { //判断是否是连通图,并且顶点数-1是否是边数 //判断是否是连通图深度遍历一遍,都遍历到了即为连通图 public void DFS(int[] visited,Graph graph,int start){ //如果该顶点没有访问过 if(visited[start] == 0){ visited[start] = 1; //继续访问与其相连的顶点 for(int i = 1; i <= graph.nodes; i++){ if(graph.graph[start][i] == 1) DFS(visited,graph,i); } } } public static void main(String[] args) { Main solution = new Main(); Scanner in = new Scanner(System.in); int num = in.nextInt(); for(int i = 0; i < num; i++){ //输入顶点数和边数 Graph graph = new Graph(); graph.nodes = in.nextInt(); graph.edges = in.nextInt(); //初始化邻接矩阵 for(int j = 0; j < graph.edges; j++){ int a = in.nextInt(); int b = in.nextInt(); graph.graph[a][b] = 1; graph.graph[b][a] = 1; } if(graph.edges != graph.nodes - 1) { System.out.println("NO"); continue; } //判断是否连通 //初始化一个顶点访问数组,从1开始存储 int[] visited = new int[graph.nodes+1]; solution.DFS(visited,graph,1); //对visited数组判断 boolean flag = true; for(int j = 1; j < graph.nodes; j++) { if (visited[j] == 0) { flag = false; break; } } if(flag) System.out.println("YES"); else System.out.println("NO"); } }}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-08-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档