您现在的位置是:热点 >>正文
图算法系列之无向图的数据结构
热点310286人已围观
简介...
前言
从本篇开始我们将会一起来学习图相关的算法,图算有很多相当实用算法,比如:垃圾回收器的标记清除算法、地图上求路径的最短距离、拓扑排序等。在开始学习这些算法之前我们需要先来了解下图的基本定义,以及使用哪种数据结构来表示一张图,本篇我们先从无向图开始学习。
图的定义
图:是有一组顶点和一组能够将两个订单相连组成的。连接两个顶点的边没有方向,这种图称之为无向图。
图的术语
通过同一条边相连的两个顶点我们称这两个顶点相邻;
某个顶点的度数即表示连接这个顶点的边的总数;如上图:顶点1的度数是3
一条边连接了一个顶点与其自身,我们称为自环
连接同一对顶点的边称为平行边
术语还有很多,暂时这里只列出本篇我们需要使用到的术语,后面有在使用到其他的术语再做解释,太多也不太容易记得住
如何表示出图
图用什么数据结构来表示主要参考两个要求:
在开发图的相关算法时,图的表示的数据结构是基础,所以这种数据结构效率的高 在实际的过程中图的大小不确定,可能会很大,所以需要预留出足够的空间
考虑了这两个要求之后大佬们提出以下三个方法来供选择:
邻接矩阵 键入有v个顶点的图,我们可以使用v乘以图算v的矩阵来表示,如果顶点v与w相连,那么把v行w列设置为true,这样就可以表示两个顶点相连,但是这个方式有个问题,如果遇到图很大,会造成空间的浪费。不满足第二点。其次这种方式没办法表示平行边 边的数组 可以定义一个表示的边对象,包含两个int属性表示顶点,但是如果需要找到某个顶点的相连顶点有哪些,我们就需要遍历一遍全部的边。这种数据结构的效率较差 邻接表数组 定义一个数组,数组的大小为顶点的个数,数据下标表示顶点,数组中每个元素都是一个链表对象(LinkedListQueue),链表中存放的值就是与该顶点相连的顶点。(LinkedListQueue我们已经在之前的文章中实现,可以参考文章《https://juejin.cn/post/6926685994347397127》)
无向图的API定义 public class Graph { public Graph(int V); //创建含有v个顶点不含边的图 &nb法系sp; public int V(); //返回顶点的个数 public int E(); //返回图中边的总数 public void addEdge(int v, int w); //向图中添加一条边 v-W public Iterable adj(int v); //返回与v相邻的所有顶点&n列之bsp; public String toString(); //使用字符串打印出图的关系 } 无向图API的实现
要实现上面定义的API,我们需要三个成员变量,v表示图中顶点的个数,e表示图总共边的数据,LinkedListQueue的数组用来存储顶点v的相邻节点;
构造函数会初始化空的邻接表数组
因为是无向图,所以addEdge方法在向图中添加边既要添加一条v->w的边,有需要添加一条w->v的边
public class Graph { private final int v; private int e; private 无向;LinkedListQueue[] adj; public Graph(int v) { this.v = v; this.adj = (LinkedListQueue[]) new LinkedListQueue[v]; for (int i = 0; i < v; i++) { &n图的bsp; this.adj[i] = new LinkedListQueue<>(); } } public int V() { return v; } public int E() {  数据;return e; } public void addEdge(int v, int w) { this.adj[v].enqueue(w); this.adj[w].enqueue(v); this.e++; } public Iterable adj(int v) &结构#123; return this.adj[v]; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(v).append(" 个顶点,").append(e).append(" 条边 "); 图算 for (int i = 0; i < v; i++) { sb.append(i).append(": "); for (int j : this.adj[i]) { sb.append(j).ap法系pend(" "); } sb.append(" "); } return sb.toString(); } } 图的常用工具方法
基于图数据结构的实现,我们可以提供一些工具方法
计算顶点v的度数 顶点的度数就等于与之相连接顶点的个数
public static int degree(Graph graph,列之 int v) { int degree = 0; for (int w : graph.adj(v)) { degree++; } return degree; }
计算所有顶点的最大度数
public static int maxDegree(Graph graph) { int maxDegree = 0;&nbs无向p; for (int v = 0; v < graph.V(); v++) { int degree = degree(graph, v); if (maxDegree < degree) { maxDegree = degree; &图的nbsp;} } return maxDegree; }
计算所有顶点的平均度数 每条边都有两个顶点,所以图所有顶点的总度数是边的2倍
public static double avgDegree(Graph graph) { return 2.0 * graph.E() / graph.V(); }
计算图中的自环个数 对于顶点v,如果v同时也出现了在v的邻接表中,那么表示v存在一个自环;由于是无向图,每条边都被记录了两次(如果不好理解可以把图的toString打印出来就可以理解了)
public static int numberOfSelfLoops(Graph graph) 数据;{ int count = 0; for (int v = 0; v < graph.V(); v++) { for (int w : graph.adj(v)) { if (v == w) {  结构; count++; } } } return count / 2; } 总结
本篇我们主要学习使用何种数据结构来表示一张图,以及基于这种数据结构实现了几个简单的工具方法,在下一篇我们将来学习图的第一个搜索算法 - 深度优先搜索
文中所有源码已放入到了github仓库:https://github.com/silently9527/JavaCore
Tags:
相关文章
标签: 高光制药 Biohaven 梁从新 高瓴
热点云梦级12月18日云梦级消息,据子弹财经报道,近日,一家成立8年的杭州创新药企——高光制药递表港交所,拟在港交所主板IPO上市,中金公司、招银国际为联席保荐人。 董事长兼CEO梁从新早年在辉瑞旗下Su...
【热点】
阅读更多电信发布新一代5G全网通云手机,1199元起售
热点1月26日讯,今天下午,中电信发布了电信自主品牌5G全网通云手机--天翼1号2021,拥有两个不同配置4G+64G和6G+128G,其中4G+64G售价1199元; 6G+128G售价1499元,首销...
【热点】
阅读更多用 Python 轻松实现机器学习
热点用朴素贝叶斯分类器解决现实世界里的机器学习问题。 朴素贝叶斯Naïve Bayes是一种分类技术,它是许多分类器建模算法的基础。基于朴素贝叶斯的分类器是简单、快速和易用的机器学习技术之一,而且在现实世...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.remit.co.jp/id/?s=谷歌maps群发程序|软件【tg:seogoogle987】,5Ka
- https://www.sonepar.ch/fr/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,xyC
- https://www.tcv.org.uk/search/谷歌maps群发程序|软件【tg:seogoogle987】,eFg
- https://www.smics.com/tc/site/search?token=谷歌maps群发程序|软件【tg:seogoogle987】,9Wm
- https://www.totolink.vn/search/谷歌maps群发程序|软件【tg:seogoogle987】,suF
- https://www.tiktok.com/tag/谷歌maps群发程序|软件【tg:seogoogle987】,a0b
- https://www.paulaschoice.hk/zh_hant/catalogsearch/result/?q=谷歌maps群发程序|软件【tg:seogoogle987】,55S
- https://www.selfsufficientish.com/main/?s=谷歌maps群发程序|软件【tg:seogoogle987】,dN2
- https://www.dba.dk/soeg/?soeg=谷歌maps群发程序|软件【tg:seogoogle987】tXC
- https://dict.naver.com/dict.search?dicQuery&query=谷歌maps群发程序|软件【tg:seogoogle987】bqB
- https://www.silentiumpc.com/en/search/谷歌maps群发程序|软件【tg:seogoogle987】,2vD
- https://www.tmtpost.com/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,ELG
- https://www.wellbees.com/index.php/catalogsearch/result/index/?q=谷歌maps群发程序|软件【tg:seogoogle987】,ByY
- https://m.shoppinghow.kakao.com/m/search/q/谷歌maps群发程序|软件【tg:seogoogle987】wK7
- https://www.udc.co.uk/index.php?route=product/search&sort=p.price&order=DESC&tag=谷歌maps群发程序|软件【tg:seogoogle987】,GgO
- https://www.teamlab.art/fr/?submit=谷歌maps群发程序|软件【tg:seogoogle987】,SZ6
- https://www.nissin.com/jp/search/?cat=105&q=谷歌maps群发程序|软件【tg:seogoogle987】,rIj
- https://www.nss.nhs.scot/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,6kw
- https://www.sbaad.no/search/谷歌maps群发程序|软件【tg:seogoogle987】,ITG
- https://www.stormberg.com/no/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,lSo
- https://www.maigoo.com/search/?block=&q=谷歌maps群发程序|软件【tg:seogoogle987】,AHi
- https://www.rea.govt.nz/search?searchterm=谷歌maps群发程序|软件【tg:seogoogle987】,IIZ
- https://www.rotherham.gov.uk/site-search/results/?q=谷歌maps群发程序|软件【tg:seogoogle987】,z8r
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌maps群发程序|软件【tg:seogoogle987】lz8
- https://www.synonyms.com/antonyms/谷歌maps群发程序|软件【tg:seogoogle987】,lnl
- https://www.tahiti-infos.com/search/谷歌maps群发程序|软件【tg:seogoogle987】,Tbs
- https://www.oikos-tecnics.com/?s=谷歌maps群发程序|软件【tg:seogoogle987】,Y4R
- https://www.seagrant.wisc.edu/?page=63&s=谷歌maps群发程序|软件【tg:seogoogle987】,I7m
- https://www.kaliastyle.com/catalogsearch/result/?q=GOOGLE谷歌maps群发程序|软件【tg:seogoogle987】,fSr
- https://www.pmindia.gov.in/en/?s=谷歌maps群发程序|软件【tg:seogoogle987】,Eh6
- https://www.universal-music.co.jp/search/?q=谷歌maps群发程序|软件【tg:seogoogle987】,0Yv
- https://www.xvideos.com/tags/谷歌maps群发程序|软件【tg:seogoogle987】,MnW
- https://www.olx.ba/pretraga?trazilica=谷歌maps群发程序|软件【tg:seogoogle987】,G7S
- https://www.mingwai.edu.hk/?langzh-hant&s=谷歌maps群发程序|软件【tg:seogoogle987】,KIl
- https://www.supersauna.de/catalogsearch/result/?q=谷歌maps群发程序|软件【tg:seogoogle987】,tHe
- https://www.abbreviations.com/谷歌maps群发程序|软件【tg:seogoogle987】92K
- https://www.ncetm.org.uk/search?action=CHECK&q=谷歌maps群发程序|软件【tg:seogoogle987】,hLk
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=谷歌maps群发程序|软件【tg:seogoogle987】PsW
- https://www2.ual.es/ProyectoLates/?s=谷歌maps群发程序|软件【tg:seogoogle987】,XBY
- https://www.youtube.com/hashtag/谷歌maps群发程序|软件【tg:seogoogle987】,aht
- https://blvdseafood.com/?s=谷歌maps群发程序|软件【tg:seogoogle987】cGX
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌maps群发程序|软件【tg:seogoogle987】tKe
- https://www.spoiledsweetboutique.com/search/谷歌maps群发程序|软件【tg:seogoogle987】,gP1
- https://www.nctsn.org/search?cat=191&query=谷歌maps群发程序|软件【tg:seogoogle987】,gNN
- https://www.hanbat.ac.kr/search/front/Search.jsp?qt=谷歌maps群发程序|软件【tg:seogoogle987】6BE
- https://www.kuaikanmanhua.com/sou/谷歌maps群发程序|软件【tg:seogoogle987】,ioL
- https://www.zutari.com/search/谷歌maps群发程序|软件【tg:seogoogle987】,vju
- https://www.umc.edu/search.aspx?ID=I4gnDKRS991MFcIDDzoYze4aWSAszAm7&q=谷歌maps群发程序|软件【tg:seogoogle987】,FRy
- https://www.resopal.de/catalogsearch/result/?action=SEARCH&q=谷歌maps群发程序|软件【tg:seogoogle987】,Lve
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=谷歌maps群发程序|软件【tg:seogoogle987】,r1e